public void setHook() {
   ComboBoxEditor anEditor = this.getEditor();
   if (anEditor.getEditorComponent() instanceof JTextField) {
     editor = (JTextField) anEditor.getEditorComponent();
     editor.setColumns(TXT_FILENAME_LENGTH);
     editor.addKeyListener(
         new KeyAdapter() {
           public void keyReleased(KeyEvent ev) {
             char key = ev.getKeyChar();
             if (!(Character.isLetterOrDigit(key) || Character.isSpaceChar(key))) {
               return;
             }
             caretPos = editor.getCaretPosition();
             String text = "";
             try {
               text = editor.getText(0, caretPos);
             } catch (Exception ex) {
               Debug.error(me + "setHook: Problem getting image file name\n%s", ex.getMessage());
             }
             int n = getItemCount();
             for (int i = 0; i < n; i++) {
               int ind = ((String) getItemAt(i)).indexOf(text);
               if (ind == 0) {
                 setSelectedIndex(i);
                 return;
               }
             }
           }
         });
   }
 }
  @Override
  public void setPopupVisible(boolean visible) {
    if (!isSwingPopup()) {
      if (visible && (myJBPopup == null || myJBPopup.isDisposed())) {
        final JBList list = createJBList(getModel());
        myJBPopup =
            JBPopupFactory.getInstance()
                .createListPopupBuilder(list)
                .setItemChoosenCallback(
                    new Runnable() {
                      @Override
                      public void run() {
                        final Object value = list.getSelectedValue();
                        if (value != null) {
                          configureEditor(getEditor(), value);
                          IdeFocusManager.getGlobalInstance().requestFocus(ComboBox.this, true);
                          assert myJBPopup != null;
                          ComboBox.this.getUI().setPopupVisible(ComboBox.this, false);
                          myJBPopup.cancel();
                        }
                      }
                    })
                .setFocusOwners(new Component[] {this})
                .setMinSize(new Dimension(getWidth(), -1))
                .createPopup();
        list.setBorder(IdeBorderFactory.createEmptyBorder());
        myJBPopup.showUnderneathOf(this);
        list.addFocusListener(
            new FocusAdapter() {
              @Override
              public void focusLost(FocusEvent e) {
                ComboBox.this.getUI().setPopupVisible(ComboBox.this, false);
                myJBPopup.cancel();
              }
            });
      }
      return;
    }

    if (getModel().getSize() == 0 && visible) return;
    if (visible && JBPopupFactory.getInstance().getChildFocusedPopup(this) != null) return;

    final boolean wasShown = isPopupVisible();
    super.setPopupVisible(visible);
    if (!wasShown
        && visible
        && isEditable()
        && !UIManager.getBoolean("ComboBox.isEnterSelectablePopup")) {

      final ComboBoxEditor editor = getEditor();
      final Object item = editor.getItem();
      final Object selectedItem = getSelectedItem();
      if (isSwingPopup() && (item == null || item != selectedItem)) {
        configureEditor(editor, selectedItem);
      }
    }
  }
 public Object getItem() {
   if (myDelegate != null) {
     return myDelegate.getItem();
   } else {
     return null;
   }
 }
 public Component getEditorComponent() {
   if (myDelegate != null) {
     return myDelegate.getEditorComponent();
   } else {
     return null;
   }
 }
 public MyEditor(final JComboBox comboBox, final ComboBoxEditor delegate) {
   myComboBox = comboBox;
   myDelegate = delegate;
   if (myDelegate != null) {
     myDelegate.addActionListener(
         new ActionListener() {
           public void actionPerformed(final ActionEvent e) {
             if (myComboBox.isPopupVisible()) {
               myComboBox.setPopupVisible(false);
             } else {
               final Object clientProperty =
                   myComboBox.getClientProperty(TABLE_CELL_EDITOR_PROPERTY);
               if (clientProperty instanceof CellEditor) {
                 // If combo box is inside editable table then we need to cancel editing
                 // and do not close heavy weight dialog container (if any)
                 ((CellEditor) clientProperty).stopCellEditing();
               } else {
                 myComboBox.setSelectedItem(getItem());
                 final JRootPane rootPane = myComboBox.getRootPane();
                 if (rootPane != null) {
                   final JButton button = rootPane.getDefaultButton();
                   if (button != null) {
                     button.doClick();
                   }
                 }
               }
             }
           }
         });
   }
 }
  public final void setEditor(final ComboBoxEditor editor) {
    ComboBoxEditor _editor = editor;
    if (SystemInfo.isMac && (UIUtil.isUnderAquaLookAndFeel() || UIUtil.isUnderIntelliJLaF())) {
      if ("AquaComboBoxEditor".equals(editor.getClass().getSimpleName())
          || UIUtil.isUnderIntelliJLaF()) {
        _editor = new FixedComboBoxEditor();
      }
    }

    super.setEditor(new MyEditor(this, _editor));
  }
Exemple #7
0
 private static int getComboBoxBaseline(JComboBox combobox, int height) {
   Insets insets = combobox.getInsets();
   int y = insets.top;
   height -= (insets.top + insets.bottom);
   if (combobox.isEditable()) {
     ComboBoxEditor editor = combobox.getEditor();
     if (editor != null && (editor.getEditorComponent() instanceof JTextField)) {
       JTextField tf = (JTextField) editor.getEditorComponent();
       return y + getSingleLineTextBaseline(tf, height);
     }
   }
   // Use the renderer to calculate baseline
   if (isMetal()) {
     if (isOceanTheme()) {
       y += 2;
       height -= 4;
     }
   } else if (isWindows()) {
     // This doesn't guarantee an XP style will be active,
     // but we don't offer public API to detect if XP is active.
     String osVersion = System.getProperty("os.version");
     if (osVersion != null) {
       Float version = Float.valueOf(osVersion);
       if (version.floatValue() > 4.0) {
         y += 2;
         height -= 4;
       }
     }
   }
   ListCellRenderer renderer = combobox.getRenderer();
   if (renderer instanceof JLabel) {
     int baseline = y + getLabelBaseline((JLabel) renderer, height);
     if (isAqua()) {
       return baseline - 1;
     }
     return baseline;
   }
   // Renderer isn't a label, use metrics directly.
   FontMetrics fm = combobox.getFontMetrics(combobox.getFont());
   return y + fm.getAscent();
 }
 public void setItem(final Object obj) {
   if (myDelegate != null) {
     myDelegate.setItem(obj);
   }
 }
 public void selectAll() {
   if (myDelegate != null) {
     myDelegate.selectAll();
   }
 }
Exemple #10
0
  public ServerDialog(OptionsDialog options_, Options opts_, CConn cc_) {

    super(true);
    cc = cc_;
    opts = opts_;

    options = options_;

    JLabel serverLabel = new JLabel("VNC server:", JLabel.RIGHT);
    String valueStr = null;
    if (opts.serverName != null) {
      String[] s = new String[1];
      s[0] = opts.serverName;
      server = new JComboBox(s);
    } else if ((valueStr = UserPreferences.get("ServerDialog", "history")) != null) {
      server = new JComboBox(valueStr.split(","));
    } else {
      server = new JComboBox();
    }

    // Hack to set the left inset on editable JComboBox
    if (UIManager.getLookAndFeel().getID() == "Windows") {
      server.setBorder(
          BorderFactory.createCompoundBorder(
              server.getBorder(), BorderFactory.createEmptyBorder(0, 2, 0, 0)));
    } else if (UIManager.getLookAndFeel().getID() == "Metal") {
      ComboBoxEditor editor = server.getEditor();
      JTextField jtf = (JTextField) editor.getEditorComponent();
      jtf.setBorder(new CompoundBorder(jtf.getBorder(), new EmptyBorder(0, 2, 0, 0)));
    }

    server.setEditable(true);
    editor = server.getEditor();
    editor
        .getEditorComponent()
        .addKeyListener(
            new KeyListener() {
              public void keyTyped(KeyEvent e) {}

              public void keyReleased(KeyEvent e) {}

              public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                  server.insertItemAt(editor.getItem(), 0);
                  server.setSelectedIndex(0);
                  commit();
                  endDialog();
                }
              }
            });

    topPanel = new JPanel(new GridBagLayout());

    Dialog.addGBComponent(
        new JLabel(VncViewer.logoIcon),
        topPanel,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.LINE_START,
        new Insets(5, 5, 5, 15));
    Dialog.addGBComponent(
        serverLabel,
        topPanel,
        1,
        0,
        1,
        1,
        0,
        0,
        0,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.LINE_END,
        new Insets(10, 0, 5, 5));
    Dialog.addGBComponent(
        server,
        topPanel,
        2,
        0,
        1,
        1,
        0,
        0,
        1,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(10, 0, 5, 20));

    optionsButton = new JButton("Options...");
    aboutButton = new JButton("About...");
    okButton = new JButton("Connect");
    cancelButton = new JButton("Cancel");
    buttonPanel = new JPanel(new GridBagLayout());
    buttonPanel.setPreferredSize(new Dimension(350, 40));
    Dialog.addGBComponent(
        aboutButton,
        buttonPanel,
        0,
        3,
        1,
        1,
        0,
        0,
        0.8,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));
    Dialog.addGBComponent(
        optionsButton,
        buttonPanel,
        1,
        3,
        1,
        1,
        0,
        0,
        1,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));
    Dialog.addGBComponent(
        okButton,
        buttonPanel,
        2,
        3,
        1,
        1,
        0,
        0,
        0.7,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));
    Dialog.addGBComponent(
        cancelButton,
        buttonPanel,
        3,
        3,
        1,
        1,
        0,
        0,
        0.6,
        1,
        GridBagConstraints.HORIZONTAL,
        GridBagConstraints.CENTER,
        new Insets(0, 2, 0, 5));

    server.addActionListener(this);
    optionsButton.addActionListener(this);
    aboutButton.addActionListener(this);
    okButton.addActionListener(this);
    cancelButton.addActionListener(this);
  }