Java Code Examples for javax.swing.JPopupMenu#getComponentCount()

The following examples show how to use javax.swing.JPopupMenu#getComponentCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfilerTabbedPane.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private void showPopupMenu(int index, Component component, MouseEvent e) {
        JPopupMenu popup = new JPopupMenu() {
//            public void setVisible(boolean visible) {
//                if (visible) popupShowing();
//                super.setVisible(visible);
//                if (!visible) popupHidden();
//            }
        };
        
        populatePopup(popup, index, component);
        
        if (popup.getComponentCount() > 0) {
            if (e == null) {
                // TODO: invoked by keyboard? handle it? 
            } else {
                popup.show(this, e.getX(), e.getY());
            }
        }
    }
 
Example 2
Source File: PolarChartPanel.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 *
 * @param menu  the menu.
 * @param text  the label.
 *
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 3
Source File: PolarChartPanel.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 *
 * @param menu  the menu.
 * @param text  the label.
 *
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 4
Source File: UIUtils.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public static int findMenuItemPosition(JPopupMenu popupMenu, String name) {
    int n = popupMenu.getComponentCount();
    for (int i = 0; i < n; i++) {
        Component c = popupMenu.getComponent(i);
        if (c instanceof JMenuItem) {
            JMenuItem menuItem = (JMenuItem) c;
            if (name.equals(menuItem.getName())) {
                return i;
            }
        }
    }
    return -1;
}
 
Example 5
Source File: PolarChartPanel.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 *
 * @param menu  the menu.
 * @param text  the label.
 *
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 6
Source File: PolarChartPanel.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 * 
 * @param menu  the menu.
 * @param text  the label.
 * 
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 7
Source File: DropdownButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void displayPopup() {
    JPopupMenu menu = new JPopupMenu();
    populatePopup(menu);
    if (menu.getComponentCount() > 0) {
        Dimension size = menu.getPreferredSize();
        size.width = Math.max(size.width, getWidth());
        menu.setPreferredSize(size);
        menu.show(this, 0, getHeight());
    }
}
 
Example 8
Source File: LinkButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu();
    populatePopup(popup);
    
    if (popup.getComponentCount() > 0) {
        Dimension pref = popup.getPreferredSize();
        if (e == null) {
            popup.show(this, getWidth() / 2, -pref.height);
        } else {
            popup.show(this, e.getX(), e.getY() - pref.height);
        }
    }
}
 
Example 9
Source File: LinkButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu();
    populatePopup(popup);
    
    if (popup.getComponentCount() > 0) {
        Dimension pref = popup.getPreferredSize();
        if (e == null) {
            popup.show(this, getWidth() / 2, -pref.height);
        } else {
            popup.show(this, e.getX(), e.getY() - pref.height);
        }
    }
}
 
Example 10
Source File: PolarChartPanel.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 *
 * @param menu  the menu.
 * @param text  the label.
 *
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 11
Source File: AbstractMenuCreator.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Add a separator to a menu if useful.
 * 
 * @param menu Menu.
 * @return Number of items added.
 */
public int addSeparator(JPopupMenu menu) {
  if ((menu == null) || (menu.getComponentCount() == 0)) {
    return 0;
  }
  Component item = menu.getComponent(menu.getComponentCount() - 1);
  if (!(item instanceof JMenuItem)) {
    return 0;
  }
  menu.add(new JSeparator());
  return 1;
}
 
Example 12
Source File: PolarChartPanel.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 *
 * @param menu  the menu.
 * @param text  the label.
 *
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 13
Source File: PopupButton.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void displayPopup() {
    JPopupMenu menu = new JPopupMenu();
    populatePopup(menu);
    if (menu.getComponentCount() > 0) {
        Dimension size = menu.getPreferredSize();
        size.width = Math.max(size.width, getWidth());
        menu.setPreferredSize(size);
        
        int align = getPopupAlign();
        
        int x;
        switch (align) {
            case SwingConstants.EAST:
            case SwingConstants.NORTH_EAST:
            case SwingConstants.SOUTH_EAST:
                x = getWidth() - size.width;
                break;
            default:
                x = 0;
                break;
        }
        
        int y;
        switch (align) {
            case SwingConstants.NORTH:
            case SwingConstants.NORTH_EAST:
            case SwingConstants.NORTH_WEST:
                y = -size.height;
                break;
            default:
                y = getHeight();
                break;
        }
        
        menu.show(this, x, y);
    }
}
 
Example 14
Source File: ProfilerTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu() {
        public void setVisible(boolean visible) {
            if (visible) popupShowing();
            super.setVisible(visible);
            if (!visible) popupHidden();
        }
    };
    
    int row = getSelectedRow();
    Object value = getValueForRow(row);
    Object userValue = getUserValueForRow(row);
    populatePopup(popup, value, userValue);
    
    if (popup.getComponentCount() > 0) {
        if (e == null) {
            boolean b = row == -1;
            int c = b ? -1 : convertColumnIndexToView(mainColumn);
            Rectangle t = b ? getVisibleRect() : getCellRect(row, c, false);
            Dimension s = popup.getPreferredSize();
            int x = t.x + (t.width - s.width) / 2;
            int y = t.y + (b ? (t.height - s.height) / 2 : getRowHeight() - 4);
            popup.show(this, Math.max(x, 0), Math.max(y, 0));
        } else {
            popup.show(this, e.getX(), e.getY());
        }
    }
}
 
Example 15
Source File: LobbyGamePanel.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private void mouseOnGamesList(final MouseEvent mouseEvent) {
  if (!mouseEvent.isPopupTrigger()) {
    return;
  }
  if (!SwingUtilities.isRightMouseButton(mouseEvent)) {
    return;
  }
  final int selectedIndex = gameTable.getSelectedRow();
  if (selectedIndex == -1) {
    return;
  }

  final JPopupMenu menu = new JPopupMenu();

  List.of(
          SwingAction.of("Join Game", this::joinGame),
          SwingAction.of("Host Game", () -> hostGame(lobbyUri)),
          ShowPlayersAction.builder()
              .parentWindow(parent)
              .gameIdSelection(
                  () ->
                      gameTableModel.getGameListingForRow(
                          gameTable.convertRowIndexToModel(gameTable.getSelectedRow())))
              .playerToLobbyConnection(lobbyClient.getPlayerToLobbyConnection())
              .build()
              .buildSwingAction())
      .forEach(menu::add);

  if (lobbyClient.isModerator()) {
    final Collection<Action> generalAdminActions = getGeneralAdminGamesListContextActions();
    if (!generalAdminActions.isEmpty()) {
      menu.addSeparator();
      generalAdminActions.forEach(menu::add);
    }
  }

  if (menu.getComponentCount() > 0) {
    menu.show(gameTable, mouseEvent.getX(), mouseEvent.getY());
  }
}
 
Example 16
Source File: PolarChartPanel.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the index of an item in a popup menu.
 * 
 * @param menu  the menu.
 * @param text  the label.
 * 
 * @return The item index.
 */
private int getPopupMenuItem(JPopupMenu menu, String text) {
    int index = -1;
    for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
        Component comp = menu.getComponent(i);
        if (comp instanceof JMenuItem) {
            JMenuItem item = (JMenuItem) comp;
            if (text.equals(item.getText())) {
                index = i;
            }
        }
   }
   return index;
}
 
Example 17
Source File: PolarChartPanel.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a popup menu for the panel.
 *
 * @param properties  include a menu item for the chart property editor.
 * @param save  include a menu item for saving the chart.
 * @param print  include a menu item for printing the chart.
 * @param zoom  include menu items for zooming.
 *
 * @return The popup menu.
 */
protected JPopupMenu createPopupMenu(boolean properties,
                                     boolean save, 
                                     boolean print, 
                                     boolean zoom) {
    
   JPopupMenu result = super.createPopupMenu(properties, save, print, zoom);
   int zoomInIndex  = getPopupMenuItem(result, "Zoom In");
   int zoomOutIndex = getPopupMenuItem(result, "Zoom Out");
   int autoIndex     = getPopupMenuItem(result, "Auto Range");
   if (zoom) {
       JMenuItem zoomIn = new JMenuItem("Zoom In");
       zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
       zoomIn.addActionListener(this);
      
       JMenuItem zoomOut = new JMenuItem("Zoom Out");
       zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
       zoomOut.addActionListener(this);
      
       JMenuItem auto = new JMenuItem("Auto Range");
       auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
       auto.addActionListener(this);
      
       if (zoomInIndex != -1) {
           result.remove(zoomInIndex);
       }
       else {
           zoomInIndex = result.getComponentCount() - 1;
       }
       result.add(zoomIn, zoomInIndex);
       if (zoomOutIndex != -1) {
           result.remove(zoomOutIndex);
       }
       else {
           zoomOutIndex = zoomInIndex + 1;
       }
       result.add(zoomOut, zoomOutIndex);
       if (autoIndex != -1) {
           result.remove(autoIndex);
       }
       else {
           autoIndex = zoomOutIndex + 1;
       }
       result.add(auto, autoIndex);
   }
   return result;
}
 
Example 18
Source File: AnnotationSetsView.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt){
  if(annotationEditor == null) return;
  //this action either creates a new annotation or starts editing an 
  //existing one. In either case we need first to make sure that the current
  //annotation is finished editing.
  if(!annotationEditor.editingFinished()) return;
  if(textLocation == -1) return;
  JPopupMenu popup = new JPopupMenu();

  //check for selection hovering
  if(textPane.getSelectedText() != null
      && textPane.getSelectionStart() <= textLocation
      && textPane.getSelectionEnd() >= textLocation){
    //add 'New annotation' to the popup menu
    popup.add(new NewAnnotationAction(textPane.getSelectedText()));
    popup.addSeparator();
  }

  //check for annotations at location
  for(SetHandler setHandler : setHandlers) {
    for(Annotation ann : setHandler.set.get(
          Math.max(0l, textLocation-1),
          Math.min(document.getContent().size(), textLocation+1))) {
      if(setHandler.getTypeHandler(ann.getType()).isSelected()) {
        AnnotationDataImpl annotAtPoint =
          new AnnotationDataImpl(setHandler.set, ann);
        //add annotations to edit to the popup menu
        popup.add(new HighlightMenuItem(
          new EditAnnotationAction(annotAtPoint),
          annotAtPoint.getAnnotation().getStartNode().getOffset().intValue(),
          annotAtPoint.getAnnotation().getEndNode().getOffset().intValue(),
          popup));
      }
    }
  }

  if (popup.getComponentCount() == 0) {
    // nothing to do
  } else if(popup.getComponentCount() == 1
    || (popup.getComponentCount() == 2
     && popup.getComponent(1) instanceof JSeparator)) {
    //only one annotation, start the editing directly
    //or only one selection, add new annotation
    ((JMenuItem)popup.getComponent(0)).getAction().actionPerformed(evt);
  } else { //mouse hover a selection AND annotation(s)
    try{
      Rectangle rect =  textPane.modelToView(textLocation);
      //display the popup
      popup.show(textPane, rect.x + 10, rect.y);
    }catch(BadLocationException ble){
      throw new GateRuntimeException(ble);
    }
  }
}
 
Example 19
Source File: PolarChartPanel.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a popup menu for the panel.
 *
 * @param properties  include a menu item for the chart property editor.
 * @param save  include a menu item for saving the chart.
 * @param print  include a menu item for printing the chart.
 * @param zoom  include menu items for zooming.
 *
 * @return The popup menu.
 */
protected JPopupMenu createPopupMenu(boolean properties,
                                     boolean save,
                                     boolean print,
                                     boolean zoom) {

   JPopupMenu result = super.createPopupMenu(properties, save, print, zoom);
   int zoomInIndex  = getPopupMenuItem(result, "Zoom In");
   int zoomOutIndex = getPopupMenuItem(result, "Zoom Out");
   int autoIndex     = getPopupMenuItem(result, "Auto Range");
   if (zoom) {
       JMenuItem zoomIn = new JMenuItem("Zoom In");
       zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
       zoomIn.addActionListener(this);

       JMenuItem zoomOut = new JMenuItem("Zoom Out");
       zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
       zoomOut.addActionListener(this);

       JMenuItem auto = new JMenuItem("Auto Range");
       auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
       auto.addActionListener(this);

       if (zoomInIndex != -1) {
           result.remove(zoomInIndex);
       }
       else {
           zoomInIndex = result.getComponentCount() - 1;
       }
       result.add(zoomIn, zoomInIndex);
       if (zoomOutIndex != -1) {
           result.remove(zoomOutIndex);
       }
       else {
           zoomOutIndex = zoomInIndex + 1;
       }
       result.add(zoomOut, zoomOutIndex);
       if (autoIndex != -1) {
           result.remove(autoIndex);
       }
       else {
           autoIndex = zoomOutIndex + 1;
       }
       result.add(auto, autoIndex);
   }
   return result;
}
 
Example 20
Source File: PolarChartPanel.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a popup menu for the panel.
 *
 * @param properties  include a menu item for the chart property editor.
 * @param save  include a menu item for saving the chart.
 * @param print  include a menu item for printing the chart.
 * @param zoom  include menu items for zooming.
 *
 * @return The popup menu.
 */
@Override
protected JPopupMenu createPopupMenu(boolean properties, boolean save,
        boolean print, boolean zoom) {

   JPopupMenu result = super.createPopupMenu(properties, save, print, zoom);
   int zoomInIndex = getPopupMenuItem(result,
           localizationResources.getString("Zoom_In"));
   int zoomOutIndex = getPopupMenuItem(result,
           localizationResources.getString("Zoom_Out"));
   int autoIndex = getPopupMenuItem(result,
           localizationResources.getString("Auto_Range"));
   if (zoom) {
       JMenuItem zoomIn = new JMenuItem(
               localizationResources.getString("Zoom_In"));
       zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
       zoomIn.addActionListener(this);

       JMenuItem zoomOut = new JMenuItem(
               localizationResources.getString("Zoom_Out"));
       zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
       zoomOut.addActionListener(this);

       JMenuItem auto = new JMenuItem(
               localizationResources.getString("Auto_Range"));
       auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
       auto.addActionListener(this);

       if (zoomInIndex != -1) {
           result.remove(zoomInIndex);
       }
       else {
           zoomInIndex = result.getComponentCount() - 1;
       }
       result.add(zoomIn, zoomInIndex);
       if (zoomOutIndex != -1) {
           result.remove(zoomOutIndex);
       }
       else {
           zoomOutIndex = zoomInIndex + 1;
       }
       result.add(zoomOut, zoomOutIndex);
       if (autoIndex != -1) {
           result.remove(autoIndex);
       }
       else {
           autoIndex = zoomOutIndex + 1;
       }
       result.add(auto, autoIndex);
   }
   return result;
}