/** * Inserts the given <code>command</command> at the end of the <code>popupMenu</code>. * * @param popupMenu The popup menu to add the given <code>command</code> to. * @param command The command to insert. * @param manager The command manager. */ public static void insertCommandMenuItem( final JPopupMenu popupMenu, final Command command, final CommandManager manager) { Command[] commands = getCommands(popupMenu, manager, command); commands = sort(commands); final Map<String, Component> popupMenues = new HashMap<String, Component>(); int count = popupMenu.getComponentCount(); for (int i = 0; i < count; i++) { final Component component = popupMenu.getComponent(i); if (component instanceof JMenu) { popupMenues.put(component.getName(), component); } } popupMenu.removeAll(); for (Command command1 : commands) { insertCommandMenuItem(popupMenu, command1, popupMenu.getComponentCount()); } count = popupMenu.getComponentCount(); for (int i = 0; i < count; i++) { final Component component = popupMenu.getComponent(i); if (component instanceof JMenu) { final String name = component.getName(); final Object o = popupMenues.get(name); if (o != null) { popupMenu.remove(i); popupMenu.insert((Component) o, i); } } } }
/** Sets the visibility of the column with the specified name. */ public void setColumnVisible(String name, boolean visible) { // Get column and set visibility. TableColumnExt column = getColumnExt(name); column.setVisible(visible); // Get column title. String columnTitle = column.getTitle(); // Find checkbox menu item with matching title. JCheckBoxMenuItem matchingItem = null; int itemCount = headerPopup.getComponentCount(); for (int i = 0; i < itemCount; i++) { Component menuComponent = headerPopup.getComponent(i); if (menuComponent instanceof JCheckBoxMenuItem) { JCheckBoxMenuItem item = (JCheckBoxMenuItem) menuComponent; String itemTitle = item.getText(); if (itemTitle.equals(columnTitle)) { matchingItem = item; break; } } } // Select matching menu item. if (matchingItem != null) { matchingItem.setSelected(visible); } }
/** * Inserts the given <code>command</command> to the <code>popupMenu</code> at the given <code>position</code>. * * @param popupMenu The popup menu to add the given <code>command</code> to. * @param command The command to insert. * @param pos the position where to insert the <code>command</code>. */ public static void insertCommandMenuItem( final JPopupMenu popupMenu, final Command command, final int pos) { final JMenuItem menuItem = command.createMenuItem(); if (menuItem == null) { return; } int insertPos = pos; if (command.isSeparatorBefore() && insertPos > 0) { if (insertPos == popupMenu.getComponentCount()) { final Component c = popupMenu.getComponent(insertPos - 1); if (!(c instanceof JSeparator)) { popupMenu.addSeparator(); insertPos++; } } else { final Component c = popupMenu.getComponent(insertPos); if (!(c instanceof JSeparator)) { popupMenu.insert(new JPopupMenu.Separator(), insertPos); insertPos++; } } } if (insertPos >= popupMenu.getComponentCount()) { popupMenu.add(menuItem); } else { popupMenu.insert(menuItem, insertPos); } insertPos++; if (command.isSeparatorAfter()) { if (insertPos == popupMenu.getComponentCount()) { popupMenu.addSeparator(); } else { final Component c = popupMenu.getComponent(insertPos); if (!(c instanceof JSeparator)) { popupMenu.insert(new JPopupMenu.Separator(), insertPos); } } } }
public String[] getNames() { MenuElement[] jme = mymenu.getSubElements(); String[] sa = new String[jme.length]; for (int i = 0; i < jme.length; i++) { Object ob = jme[i]; if (ob instanceof DMenuItem) { sa[i] = ((DMenuItem) (mymenu.getComponent(i))).getText(); } } return sa; }
public void createPopupMenu() { menu = new JPopupMenu(); for (int i = filenames.size() - 2, j = 0; i >= 0; i--, j++) { menu.add((String) filenames.elementAt(i)); JMenuItem mi = (JMenuItem) menu.getComponent(j); mi.setFont(new Font("Arial", Font.PLAIN, 11)); mi.addActionListener(this); } menu.pack(); // setPopupLocation(200, 200); }
public static void comboBoxScroll(JComboBox box) { if (box == null) { return; } Object comp = box.getUI().getAccessibleChild(box, 0); if (!(comp instanceof JPopupMenu)) { return; } JPopupMenu popup = (JPopupMenu) comp; final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0); scrollPane.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL)); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); }
private void menuForViews() { CPopupMenu m1 = new CPopupMenu(this); CPopupMenu m2 = new CPopupMenu(this); JPopupMenu m3 = xmlView.getPopupMenu(); m3.addSeparator(); m3.add(m1.getViews()); this.setComponentPopupMenu(m2); xmlView.addPopMenu(m3); textView.addPopMenu(m2); treeView.addPopMenu(m2); manageMenu.setView((JMenu) m3.getComponent(12)); manageMenu.setMenu(m2); }
/** Code completion. */ private void complete() { if (selected()) return; // find first character final int caret = editor.pos(), startPos = editor.completionStart(); final String prefix = string(substring(editor.text(), startPos, caret)); if (prefix.isEmpty()) return; // find insertion candidates final TreeMap<String, String> tmp = new TreeMap<>(); for (final Entry<String, String> entry : REPLACE.entrySet()) { final String key = entry.getKey(); if (key.startsWith(prefix)) tmp.put(key, entry.getValue()); } if (tmp.size() == 1) { // insert single candidate complete(tmp.values().iterator().next(), startPos); } else if (!tmp.isEmpty()) { // show popup menu final JPopupMenu pm = new JPopupMenu(); final ActionListener al = new ActionListener() { @Override public void actionPerformed(final ActionEvent ae) { complete(ae.getActionCommand().replaceAll("^.*?\\] ", ""), startPos); } }; for (final Entry<String, String> entry : tmp.entrySet()) { final JMenuItem mi = new JMenuItem("[" + entry.getKey() + "] " + entry.getValue()); pm.add(mi); mi.addActionListener(al); } pm.addSeparator(); final JMenuItem mi = new JMenuItem(Text.INPUT + Text.COLS + prefix); mi.setEnabled(false); pm.add(mi); final int[] cursor = rend.cursor(); pm.show(this, cursor[0], cursor[1]); // highlight first entry final MenuElement[] me = {pm, (JMenuItem) pm.getComponent(0)}; MenuSelectionManager.defaultManager().setSelectedPath(me); } }
/** * Finds the insert position of the given <code>command</command> within the <code>popupMenu</code>. * * @param popupMenu The popup menu. * @param command The command to insert. * @param manager The command manager. */ public static int findMenuInsertPosition( final JPopupMenu popupMenu, final Command command, final CommandManager manager) { int pbi = popupMenu.getComponentCount(); String placeBefore = command.getPlaceBefore(); if (placeBefore != null) { pbi = UIUtils.findMenuItemPosition(popupMenu, placeBefore); } int pai = -1; String placeAfter = command.getPlaceAfter(); if (placeAfter != null) { pai = UIUtils.findMenuItemPosition(popupMenu, placeAfter) + 1; } final int componentCount = popupMenu.getComponentCount(); for (int i = 0; i < componentCount; i++) { final Component component = popupMenu.getComponent(i); if (!(component instanceof JMenuItem)) { continue; } final JMenuItem item = (JMenuItem) component; final String name = item.getName(); final Command menuCommand = manager.getCommand(name); if (menuCommand == null) { continue; } placeBefore = menuCommand.getPlaceBefore(); if (command.getCommandID().equals(placeBefore)) { if (pbi > i) { pbi = i + 1; } } placeAfter = menuCommand.getPlaceAfter(); if (command.getCommandID().equals(placeAfter)) { if (pai < i) { pai = i; } } } int insertPos = -1; if (pbi >= pai) { insertPos = pai; } if (insertPos == -1) { insertPos = popupMenu.getComponentCount(); } return insertPos; }
private static Command[] getCommands( final JPopupMenu popupMenu, final CommandManager manager, final Command command) { final List<Command> commands = new ArrayList<Command>(); final int count = popupMenu.getComponentCount(); for (int i = 0; i < count; i++) { final Component component = popupMenu.getComponent(i); if (!(component instanceof JMenuItem)) { continue; } final JMenuItem item = (JMenuItem) component; final String name = item.getName(); final Command menuCommand = manager.getCommand(name); if (menuCommand != null) { commands.add(menuCommand); } } commands.add(command); return commands.toArray(new Command[commands.size()]); }
/** * set text to the specified item index * * @param index index */ public void setText(int index) { JMenuItem item = (JMenuItem) popup.getComponent(index); setText(item.getText()); } // setText()
public void setEnabled(int index, boolean bEnabled) { JMenuItem item = (JMenuItem) popup.getComponent(index); if (item != null) item.setEnabled(bEnabled); }
/** set default text */ public void setDefaultText() { JMenuItem item = (JMenuItem) popup.getComponent(0); if (item != null) setText(item.getText()); } // setDefaultText()
@Override public Component getMenuComponent(int n) { return (popupMenu == null) ? null : popupMenu.getComponent(n); }