예제 #1
0
  /**
   * Sets the visibility of the popup menu.
   *
   * @param b true to make the popup visible, or false to hide it
   * @beaninfo bound: true description: Makes the popup visible
   */
  public void setVisible(boolean b) {
    if (DEBUG) {
      System.out.println("JPopupMenu.setVisible " + b);
    }

    // Is it a no-op?
    if (b == isVisible()) return;

    // if closing, first close all Submenus
    if (b == false) {

      // 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
      // a protected method and cannot be called from BasicPopupMenuUI directly
      // The real solution could be to make
      // firePopupMenuCanceled public and call it directly.
      Boolean doCanceled = (Boolean) getClientProperty("JPopupMenu.firePopupMenuCanceled");
      if (doCanceled != null && doCanceled == Boolean.TRUE) {
        putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
        firePopupMenuCanceled();
      }
      getSelectionModel().clearSelection();

    } else {
      // This is a popup menu with MenuElement children,
      // set selection path before popping up!
      if (isPopupMenu()) {
        MenuElement me[] = new MenuElement[1];
        me[0] = this;
        MenuSelectionManager.defaultManager().setSelectedPath(me);
      }
    }

    if (b) {
      firePopupMenuWillBecomeVisible();
      popup = getPopup();
      firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);

    } else if (popup != null) {
      firePopupMenuWillBecomeInvisible();
      popup.hide();
      popup = null;
      firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
      // 4694797: When popup menu is made invisible, selected path
      // should be cleared
      if (isPopupMenu()) {
        MenuSelectionManager.defaultManager().clearSelectedPath();
      }
    }
  }
 public void processMouseEvent(
     JMenuItem item, MouseEvent e, MenuElement path[], MenuSelectionManager manager) {
   Point p = e.getPoint();
   if (p.x >= 0 && p.x < item.getWidth() && p.y >= 0 && p.y < item.getHeight()) {
     if (e.getID() == MouseEvent.MOUSE_RELEASED) {
       manager.clearSelectedPath();
       item.doClick(0);
       item.setArmed(false);
     } else manager.setSelectedPath(path);
   } else if (item.getModel().isArmed()) {
     MenuElement newPath[] = new MenuElement[path.length - 1];
     int i, c;
     for (i = 0, c = path.length - 1; i < c; i++) newPath[i] = path[i];
     manager.setSelectedPath(newPath);
   }
 }
예제 #3
0
 /**
  * Processes key stroke events such as mnemonics and accelerators.
  *
  * @param evt the key event to be processed
  */
 protected void processKeyEvent(KeyEvent evt) {
   MenuSelectionManager.defaultManager().processKeyEvent(evt);
   if (evt.isConsumed()) {
     return;
   }
   super.processKeyEvent(evt);
 }
 public void propertyChange(final PropertyChangeEvent e) {
   if (myAlarm.getActiveRequestCount() == 0) {
     myInitialFocusedWindow = (Window) e.getOldValue();
     final MenuElement[] selectedPath = MenuSelectionManager.defaultManager().getSelectedPath();
     if (selectedPath.length == 0) { // there is no visible popup
       return;
     }
     Component firstComponent = null;
     for (final MenuElement menuElement : selectedPath) {
       final Component component = menuElement.getComponent();
       if (component instanceof JMenuBar) {
         firstComponent = component;
         break;
       } else if (component instanceof JPopupMenu) {
         firstComponent = ((JPopupMenu) component).getInvoker();
         break;
       }
     }
     if (firstComponent == null) {
       return;
     }
     final Window window = SwingUtilities.getWindowAncestor(firstComponent);
     if (window != myInitialFocusedWindow) { // focused window doesn't have popup
       return;
     }
   }
   myLastFocusedWindow = (Window) e.getNewValue();
   myAlarm.cancelAllRequests();
   myAlarm.addRequest(myClearSelectedPathRunnable, 150);
 }
예제 #5
0
  /** 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);
    }
  }
예제 #6
0
 public void mouseReleased(MouseEvent e) {
   setColor(col);
   MenuSelectionManager.defaultManager().clearSelectedPath();
   doSelection();
 }