Example usage for javax.swing JPopupMenu show

List of usage examples for javax.swing JPopupMenu show

Introduction

In this page you can find the example usage for javax.swing JPopupMenu show.

Prototype

public void show(Component invoker, int x, int y) 

Source Link

Document

Displays the popup menu at the position x,y in the coordinate space of the component invoker.

Usage

From source file:org.squidy.designer.zoom.impl.SourceCodeShape.java

private JEditorPane createCodePane(URL sourceCodeURL) {

    codePane = new JEditorPane() {
        @Override//from   w  ww  .  ja  va 2s. c  o m
        protected void processComponentKeyEvent(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE && e.isControlDown()) {
                System.out.println("Code completion");

                int caretPosition = codePane.getCaretPosition();

                String code = codePane.getText();
                switch (code.charAt(caretPosition - 1)) {
                case '.':
                    int pseudoCaret = caretPosition - 1;
                    StringBuilder word = new StringBuilder();
                    for (char c = code.charAt(--pseudoCaret); !isEndOfWord(c); c = code.charAt(--pseudoCaret)) {
                        word.append(c);
                    }

                    word = word.reverse();

                    System.out.println("WORD: " + word);

                    // Class<?> type =
                    // ReflectionUtil.loadClass(word.toString());
                    //                  
                    // System.out.println("TYPE: " + type);

                    JPopupMenu menu = new JPopupMenu("sdaf");
                    Point p = codePane.getCaret().getMagicCaretPosition();
                    System.out.println("CARET POS: " + p);
                    // Point p = codePane.get

                    // menu.setPreferredSize(new Dimension(200, 200));
                    menu.setLocation(30, 30);
                    menu.add("test");

                    codePane.add(menu);

                    // System.out.println(p);

                    // codePane.get

                    menu.show(codePane, p.x, p.y);

                    break;
                }
            }

            super.processComponentKeyEvent(e);
        }

        /**
         * @param c
         * @return
         */
        private boolean isEndOfWord(char c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t';
        }
    };

    EditorKit editorKit = new StyledEditorKit() {

        /**
         * 
         */
        private static final long serialVersionUID = 7024886168909204806L;

        public Document createDefaultDocument() {
            return new SyntaxDocument();
        }
    };

    codePane.setEditorKitForContentType("text/java", editorKit);
    codePane.setContentType("text/java");

    try {
        FileInputStream fis = new FileInputStream(sourceCodeURL.getPath());
        codePane.read(fis, null);
        originSourceCode = codePane.getText();

        computeHeightOfCodePane();
        codePane.setAutoscrolls(true);
    } catch (Exception e) {
        codePane.setText("File not found!");
    }

    codePane.requestFocus();
    codePane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    codePane.addKeyListener(new KeyAdapter() {

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.KeyAdapter#keyPressed(java.awt.event.KeyEvent)
         */
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);

            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_DELETE:
            case KeyEvent.VK_BACK_SPACE:
            case KeyEvent.VK_SPACE:
                computeHeightOfCodePane();
                break;
            }

            markDirty();
        }
    });

    // final JPopupMenu menu = new JPopupMenu();
    //
    // JMenuItem i = new JMenuItem("Option 1");
    // JMenuItem i2 = new JMenuItem("Option 2");
    // menu.add(i);
    // menu.add(i2);
    // edit.add(menu);
    // getComponent().addKeyListener(new KeyAdapter() {
    //      
    // public void keyTyped(KeyEvent e) {
    // if(e.getModifiers() == 2 && e.getKeyChar() == ' ') {
    // Point popupLoc = edit.getCaret().getMagicCaretPosition();
    // System.out.println(popupLoc);
    // menu.setLocation(new
    // Point((int)popupLoc.getX(),(int)popupLoc.getY()));
    // menu.setVisible(true);
    // }
    //      
    // }
    // });

    return codePane;
}

From source file:org.tellervo.desktop.tridasv2.ui.ComponentViewer.java

private void setupTree() {
    treeModel = new DefaultTreeModel(null);
    tree = new JTree(treeModel);

    tree.setCellRenderer(new ComponentTreeCellRenderer());

    // popup menu
    tree.addMouseListener(new PopupListener() {
        @Override//www  .  java 2s.  co m
        public void showPopup(MouseEvent e) {
            TreePath path = tree.getPathForLocation(e.getX(), e.getY());
            DefaultMutableTreeNode node = (path == null) ? null
                    : (DefaultMutableTreeNode) path.getLastPathComponent();

            if (node == null)
                return;

            // ensure we select the node...
            tree.setSelectionPath(path);

            // get the element
            Element element = (Element) node.getUserObject();

            // create and show the menu
            JPopupMenu popup = new ElementListPopupMenu(element, ComponentViewer.this);
            popup.show(tree, e.getX(), e.getY());
        }
    });
    ToolTipManager.sharedInstance().setInitialDelay(0);

    tree.setToolTipText("");

}

From source file:org.tellervo.desktop.tridasv2.ui.ComponentViewer.java

private void setupTable() {
    tableModel = new ElementListTableModel();
    table = new JXTable(tableModel);

    tableSorter = new ElementListTableSorter(tableModel, table);
    table.getTableHeader().addMouseListener(tableSorter); // add sorter & header renderer
    table.setColumnSelectionAllowed(false);
    table.setRowSelectionAllowed(true);//from  w  ww  .  j a  va 2s  .  c o m

    // set our column widths
    ElementListTableModel.setupColumnWidths(table);

    table.setDefaultRenderer(Object.class, new ElementListCellRenderer(this, false));
    table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer(this, false));

    // hide irrelevent columns
    TableColumnModelExt colmodel = (TableColumnModelExt) table.getColumnModel();
    table.setColumnControlVisible(true);
    colmodel.getColumnExt(I18n.getText("hidden.MostRecentVersion")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.n")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.rec")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.hash")).setVisible(false);

    // popup menu
    table.addMouseListener(new PopupListener() {
        @Override
        public void showPopup(MouseEvent e) {
            // only clicks on tables
            if (!(e.getSource() instanceof JTable))
                return;

            JTable table = (JTable) e.getSource();
            ElementListTableModel model = (ElementListTableModel) table.getModel();

            // get the row and sanity check
            int row = table.rowAtPoint(e.getPoint());
            if (row < 0 || row >= model.getRowCount())
                return;

            // select it?
            table.setRowSelectionInterval(row, row);

            // get the element
            Element element = model.getElementAt(row);

            // create and show the menu
            JPopupMenu popup = new ElementListPopupMenu(element, ComponentViewer.this);
            popup.show(table, e.getX(), e.getY());
        }
    });
}

From source file:org.tellervo.desktop.tridasv2.ui.ComponentViewerOld.java

private void setupTree() {
    treeModel = new DefaultTreeModel(null);
    tree = new JTree(treeModel);

    tree.setCellRenderer(new ComponentTreeCellRenderer());

    // popup menu
    tree.addMouseListener(new PopupListener() {
        @Override/*from  w w  w  . j a  v a2  s  . com*/
        public void showPopup(MouseEvent e) {
            TreePath path = tree.getPathForLocation(e.getX(), e.getY());
            DefaultMutableTreeNode node = (path == null) ? null
                    : (DefaultMutableTreeNode) path.getLastPathComponent();

            if (node == null)
                return;

            // ensure we select the node...
            tree.setSelectionPath(path);

            // get the element
            Element element = (Element) node.getUserObject();

            // create and show the menu
            JPopupMenu popup = new ElementListPopupMenu(element, ComponentViewerOld.this);
            popup.show(tree, e.getX(), e.getY());
        }
    });
    ToolTipManager.sharedInstance().setInitialDelay(0);

    tree.setToolTipText("");

}

From source file:org.tellervo.desktop.tridasv2.ui.ComponentViewerOld.java

private void setupTable() {
    tableModel = new ElementListTableModel();
    table = new JXTable(tableModel);

    tableSorter = new ElementListTableSorter(tableModel, table);
    table.getTableHeader().addMouseListener(tableSorter); // add sorter & header renderer
    table.setColumnSelectionAllowed(false);
    table.setRowSelectionAllowed(true);//  ww w .  ja v a2 s.  c  o m

    // set our column widths
    ElementListTableModel.setupColumnWidths(table);

    table.setDefaultRenderer(Object.class, new ElementListCellRenderer(this, false));
    table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer(this, false));

    // hide irrelevent columns
    TableColumnModelExt colmodel = (TableColumnModelExt) table.getColumnModel();
    table.setColumnControlVisible(true);
    colmodel.getColumnExt(I18n.getText("hidden.MostRecentVersion")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.n")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.rec")).setVisible(false);
    colmodel.getColumnExt(I18n.getText("dbbrowser.hash")).setVisible(false);

    // popup menu
    table.addMouseListener(new PopupListener() {
        @Override
        public void showPopup(MouseEvent e) {
            // only clicks on tables
            if (!(e.getSource() instanceof JTable))
                return;

            JTable table = (JTable) e.getSource();
            ElementListTableModel model = (ElementListTableModel) table.getModel();

            // get the row and sanity check
            int row = table.rowAtPoint(e.getPoint());
            if (row < 0 || row >= model.getRowCount())
                return;

            // select it?
            table.setRowSelectionInterval(row, row);

            // get the element
            Element element = model.getElementAt(row);

            // create and show the menu
            JPopupMenu popup = new ElementListPopupMenu(element, ComponentViewerOld.this);
            popup.show(table, e.getX(), e.getY());
        }
    });
}

From source file:org.ujmp.jung.JungVisualizationViewer.java

public final void mouseClicked(MouseEvent e) {
    switch (e.getButton()) {
    case MouseEvent.BUTTON1:
        break;/*from ww w  .java2 s  .  co m*/
    case MouseEvent.BUTTON2:
        break;
    case MouseEvent.BUTTON3:
        JPopupMenu popup = null;
        popup = new JungGraphActions(this);
        popup.show(e.getComponent(), e.getX(), e.getY());
        break;
    }
}

From source file:org.ut.biolab.medsavant.client.view.genetics.charts.SummaryChart.java

private synchronized Chart drawChart(ChartFrequencyMap[] chartMaps) {

    ChartFrequencyMap filteredChartMap = chartMaps[0];
    ChartFrequencyMap unfilteredChartMap = null;

    DefaultChartModel filteredChartModel = new DefaultChartModel();
    DefaultChartModel unfilteredChartModel = null;

    if (this.showComparedToOriginal) {
        unfilteredChartMap = ChartFrequencyMap.subtract(chartMaps[1], filteredChartMap, isLogScaleY());
        unfilteredChartModel = new DefaultChartModel();
    }/*from   w  ww.  j av  a 2s.c  om*/

    final Chart chart = new Chart();

    JPanel panel = new JPanel();
    Legend legend = new Legend(chart, 0);
    panel.add(legend);
    legend.addChart(chart);

    boolean multiColor = !mapGenerator.isNumeric() || isPie;

    chart.setRolloverEnabled(true);
    chart.setSelectionEnabled(true);
    chart.setSelectionShowsOutline(true);
    chart.setSelectionShowsExplodedSegments(true);
    chart.setAntiAliasing(true);
    chart.setBarGap(5);
    chart.setBorder(ViewUtil.getBigBorder());
    chart.setLabellingTraces(true);

    chart.setAnimateOnShow(false);

    chart.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
                JPopupMenu popup = createPopup(chart);
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    AbstractPieSegmentRenderer rpie = new DefaultPieSegmentRenderer();
    chart.setPieSegmentRenderer(rpie);

    //Makes a box with fill color 255,255,255,0 and put a label with a black
    //font in that box.  The box is positioned directly over the corresponding pie slice.
    /*
    SimplePieLabelRenderer plr = new SimplePieLabelRenderer();
    plr.setLabelColor(Color.BLACK);
    plr.setBackground(new Color(0,0,0,0));
    rpie.setPieLabelRenderer(plr);
    */

    //....alternatively, the below draws a line from the pie wedge to the label.
    //see http://www.jidesoft.com/javadoc/com/jidesoft/chart/render/LinePieLabelRenderer.html
    LinePieLabelRenderer plr = new LinePieLabelRenderer();
    plr.setLabelColor(Color.black);
    plr.setLineColor(Color.black); //see also plr.setLineStroke        
    rpie.setPieLabelRenderer(plr);

    DefaultBarRenderer rbar = new DefaultBarRenderer();
    chart.setBarRenderer(rbar);

    rpie.setSelectionColor(Color.gray);
    rbar.setSelectionColor(Color.gray);

    if (isSortedKaryotypically()) {
        filteredChartMap.sortKaryotypically();
        if (this.showComparedToOriginal) {
            unfilteredChartMap.sortKaryotypically();
        }
    }

    if (isSorted() && !mapGenerator.isNumeric() && !isSortedKaryotypically()) {
        filteredChartMap.sortNumerically();
        if (this.showComparedToOriginal) {
            chartMaps[1].sortNumerically();
        }
    } else {
        filteredChartMap.undoSortNumerically();
        if (this.showComparedToOriginal) {
            chartMaps[1].undoSortNumerically();
        }
    }

    long max = filteredChartMap.getMax();
    List<ChartCategory> chartCategories;
    if (this.showComparedToOriginal) {
        chartCategories = chartMaps[1].getCategories();
        max = chartMaps[1].getMax();
    } else {
        chartCategories = filteredChartMap.getCategories();
    }

    CategoryRange<String> range = new CategoryRange<String>();
    List<Highlight> highlights = new ArrayList<Highlight>();

    Color color = new Color(72, 181, 249);
    Highlight h;
    int catNum = 0;
    int totalCats = filteredChartMap.getEntries().size();

    for (ChartCategory category : chartCategories) {
        range.add(category);
        if (multiColor) {
            color = ViewUtil.getColor(catNum++, totalCats);
        }
        h = new Highlight(category.getName());
        highlights.add(h);
        chart.setHighlightStyle(h, barStyle(color));
    }

    final CategoryAxis xaxis = new CategoryAxis(range, "Category");
    chart.setXAxis(xaxis);
    if (this.isLogScaleY()) {
        chart.setYAxis(new Axis(new NumericRange(0, Math.log10(max) * 1.1), "log(Frequency)"));
    } else {
        chart.setYAxis(new Axis(new NumericRange(0, max * 1.1), "Frequency"));
    }

    addEntriesToChart(filteredChartModel, filteredChartMap, chartCategories, highlights);
    if (this.showComparedToOriginal) {
        addEntriesToChart(unfilteredChartModel, unfilteredChartMap, chartCategories, null);
    }

    chart.getXAxis().getLabel().setFont(ViewUtil.getMediumTitleFont());
    chart.getYAxis().getLabel().setFont(ViewUtil.getMediumTitleFont());

    // rotate 90 degrees (using radians)
    chart.getXAxis().setTickLabelRotation(1.57079633);

    if (isPie) {
        System.out.println("Setting chart type to pie");
        chart.setChartType(ChartType.PIE);
        chart.getXAxis().getLabel().setColor(Color.BLUE);
    }

    // This adds zooming cababilities to bar charts, not great though
    /*else {
     RubberBandZoomer rubberBand = new RubberBandZoomer(chart);
     chart.addDrawable(rubberBand);
     chart.addMouseListener(rubberBand);
     chart.addMouseMotionListener(rubberBand);
            
     rubberBand.addZoomListener(new ZoomListener() {
     public void zoomChanged(ChartSelectionEvent event) {
     if (event instanceof RectangleSelectionEvent) {
     Range<?> currentXRange = chart.getXAxis().getOutputRange();
     Range<?> currentYRange = chart.getYAxis().getOutputRange();
     ZoomFrame frame = new ZoomFrame(currentXRange, currentYRange);
     zoomStack.push(frame);
     Rectangle selection = (Rectangle) event.getLocation();
     Point topLeft = selection.getLocation();
     topLeft.x = (int) Math.floor(frame.getXRange().minimum());
     Point bottomRight = new Point(topLeft.x + selection.width, topLeft.y + selection.height);
     bottomRight.x = (int) Math.ceil(frame.getXRange().maximum());
     assert bottomRight.x >= topLeft.x;
     Point2D rp1 = chart.calculateUserPoint(topLeft);
     Point2D rp2 = chart.calculateUserPoint(bottomRight);
     if (rp1 != null && rp2 != null) {
     assert rp2.getX() >= rp1.getX();
     Range<?> xRange = new NumericRange(rp1.getX(), rp2.getX());
     assert rp1.getY() >= rp2.getY();
     Range<?> yRange = new NumericRange(rp2.getY(), rp1.getY());
     //chart.getXAxis().setRange(xRange);
     chart.getYAxis().setRange(yRange);
     }
     } else if (event instanceof PointSelectionEvent) {
     if (zoomStack.size() > 0) {
     ZoomFrame frame = zoomStack.pop();
     Range<?> xRange = frame.getXRange();
     Range<?> yRange = frame.getYRange();
     //chart.getXAxis().setRange(xRange);
     chart.getYAxis().setRange(yRange);
     }
     }
     }
     });
     }
     *
     */

    for (int i = 1; i < this.getComponentCount(); i++) {
        this.remove(i);
    }

    chart.addModel(filteredChartModel, new ChartStyle().withBars());
    if (this.showComparedToOriginal) {
        chart.addModel(unfilteredChartModel, new ChartStyle(new Color(10, 10, 10, 100)).withBars());
    }

    return chart;
}

From source file:org.ut.biolab.medsavant.client.view.list.ListView.java

private void updateShowCard() {
    showCard.removeAll();//www.j a  v  a 2s .c  om

    showCard.setLayout(new BorderLayout());

    String[] columnNames = detailedModel.getColumnNames();
    Class[] columnClasses = detailedModel.getColumnClasses();
    int[] columnVisibility = detailedModel.getHiddenColumns();

    int firstVisibleColumn = 0;
    while (columnVisibility.length > 0 && firstVisibleColumn == columnVisibility[firstVisibleColumn]) {
        firstVisibleColumn++;
    }

    Set<NiceListItem> selectedItems;
    if (list != null) {
        selectedItems = new HashSet<NiceListItem>(list.getSelectedItems());
    } else {
        selectedItems = new HashSet<NiceListItem>(); // empty set, for simplicity of not having to null check later on
    }

    list = new NiceList();
    if (listColorScheme != null) {
        list.setColorScheme(listColorScheme);
    }
    list.startTransaction();

    List<Integer> selectedIndicies = new ArrayList<Integer>();

    int counter = 0;
    for (Object[] row : data) {
        NiceListItem nli = new NiceListItem(row[firstVisibleColumn].toString(), row);
        list.addItem(nli);

        if (selectedItems.contains(nli)) {
            selectedIndicies.add(counter);
        }
        counter++;
    }

    /*
     int[] selectedIndiciesArray = new int[selectedIndicies.size()];
            
     System.out.println("Reselecting "  + selectedIndicies.size() + " items");
     for (int i = 0; i < selectedIndicies.size();i++) {
     System.out.println("Reselecting "  + list.getItem(selectedIndicies.get(i)).toString() + " at index " + selectedIndicies.get(i));
     selectedIndiciesArray[i] = selectedIndicies.get(i);
     }*/
    list.endTransaction();

    wp.setBackground(list.getColorScheme().getBackgroundColor());

    if (detailedView != null) {
        list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                if (!e.getValueIsAdjusting()) {
                    List<Object[]> selectedItems = getSelectedRows();
                    if (selectedItems.size() == 1) {
                        detailedView.setSelectedItem(selectedItems.get(0));
                    } else {
                        detailedView.setMultipleSelections(selectedItems);
                    }
                }
            }
        });

        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    JPopupMenu popup = detailedView.createPopup();
                    if (popup != null) {
                        popup.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
            }
        });
    }

    if (selectedIndicies.isEmpty()) {
        list.selectItemAtIndex(0);
    } else {
        list.selectItemsAtIndicies(selectedIndicies);
    }

    JScrollPane jsp = ViewUtil.getClearBorderlessScrollPane(list);
    jsp.setHorizontalScrollBar(null);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new MigLayout("wrap, fillx"));

    topPanel.add(ViewUtil.getEmphasizedLabel(pageName.toUpperCase()));
    topPanel.setBackground(list.getColorScheme().getBackgroundColor());

    if (searchBarEnabled) {
        topPanel.add(list.getSearchBar(), "growx 1.0");
    }
    showCard.add(topPanel, BorderLayout.NORTH);

    showCard.add(jsp, BorderLayout.CENTER);

    showCard.add(controlBar.getComponent(), BorderLayout.SOUTH);

}

From source file:org.ut.biolab.medsavant.client.view.list.MasterView.java

private void updateShowCard() {
    showCard.removeAll();/*from  w  ww. ja va 2s. c o m*/

    showCard.setLayout(new BorderLayout());
    showCard.setBackground(ViewUtil.getTertiaryMenuColor());
    showCard.setBorder(ViewUtil.getBigBorder());

    String[] columnNames = detailedModel.getColumnNames();
    Class[] columnClasses = detailedModel.getColumnClasses();
    int[] columnVisibility = detailedModel.getHiddenColumns();

    stp = new ListViewTablePanel(data, columnNames, columnClasses, columnVisibility) {
        @Override
        public void forceRefreshData() {
            refreshList();
        }
    };

    selectionGrabber = new RowSelectionGrabber(stp.getTable(), data);

    if (detailedView != null) {
        stp.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                if (!e.getValueIsAdjusting()) {
                    List<Object[]> selectedItems = selectionGrabber.getSelectedItems();
                    if (selectedItems.size() == 1) {
                        detailedView.setSelectedItem(selectedItems.get(0));
                    } else {
                        detailedView.setMultipleSelections(selectedItems);
                    }
                }
            }
        });

        stp.getTable().addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    JPopupMenu popup = detailedView.createPopup();
                    if (popup != null) {
                        popup.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
            }
        });
    }

    stp.getTable().getSelectionModel().setSelectionInterval(0, 0);

    showCard.add(stp, BorderLayout.CENTER);

    showCard.add(buttonPanel, BorderLayout.SOUTH);

}

From source file:org.wandora.application.gui.topicpanels.webview.WebViewPanel.java

private void menuButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuButtonMousePressed
    Object[] menuItems = getBrowserMenuStruct();
    if (menuItems != null && menuItems.length > 0) {
        JPopupMenu popupMenu = UIBox.makePopupMenu(menuItems, this);
        popupMenu.show(this, menuButton.getX() + evt.getX(), menuButton.getY() + evt.getY());
    }//ww  w  . j a  va  2  s . c om
}