下面列出了javax.swing.JPopupMenu#getComponentCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
}
}
/**
* 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;
}
/**
* 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;
}
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());
}
}
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());
}
}
}
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);
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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);
}
}
}
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);
}
}
}
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());
}
}
/**
* 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;
}
/**
* 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;
}
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;
}
/**
* 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;
}
/**
* 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;
}
@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);
}
}
}
/**
* 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;
}
/**
* 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;
}