public SearchPopup(String initialString) {
      final Color foregroundColor = UIUtil.getToolTipForeground();
      Color color1 = UIUtil.getToolTipBackground();
      mySearchField = new SearchField();
      final JLabel searchLabel =
          new JLabel(" " + UIBundle.message("search.popup.search.for.label") + " ");
      searchLabel.setFont(searchLabel.getFont().deriveFont(Font.BOLD));
      searchLabel.setForeground(foregroundColor);
      mySearchField.setBorder(null);
      mySearchField.setBackground(color1.brighter());
      mySearchField.setForeground(foregroundColor);

      mySearchField.setDocument(
          new PlainDocument() {
            public void insertString(int offs, String str, AttributeSet a)
                throws BadLocationException {
              String oldText;
              try {
                oldText = getText(0, getLength());
              } catch (BadLocationException e1) {
                oldText = "";
              }

              String newText = oldText.substring(0, offs) + str + oldText.substring(offs);
              super.insertString(offs, str, a);
              if (findElement(newText) == null) {
                mySearchField.setForeground(Color.RED);
              } else {
                mySearchField.setForeground(foregroundColor);
              }
            }
          });
      mySearchField.setText(initialString);

      setBorder(BorderFactory.createLineBorder(Color.gray, 1));
      setBackground(color1.brighter());
      setLayout(new BorderLayout());
      add(searchLabel, BorderLayout.WEST);
      add(mySearchField, BorderLayout.EAST);
      Object element = findElement(mySearchField.getText());
      updateSelection(element);
    }
 private static String getChooserTitle(final FileChooserDescriptor descriptor) {
   final String title = descriptor.getTitle();
   return title != null ? title : UIBundle.message("file.chooser.default.title");
 }
Exemplo n.º 3
0
  public static JPanel createButtonsTable(
      final JTable table, final RowEditableTableModel tableModel, boolean addMnemonics) {
    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

    buttonsPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbConstraints = new GridBagConstraints();
    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
    gbConstraints.insets = new Insets(2, 4, 2, 4);

    final JButton addButton = new JButton();
    addButton.setText(
        addMnemonics ? UIBundle.message("row.add") : UIBundle.message("row.add.without.mnemonic"));
    addButton.setDefaultCapable(false);
    buttonsPanel.add(addButton, gbConstraints);

    final JButton removeButton = new JButton();
    removeButton.setText(
        addMnemonics
            ? UIBundle.message("row.remove")
            : UIBundle.message("row.remove.without.mnemonic"));
    removeButton.setDefaultCapable(false);
    buttonsPanel.add(removeButton, gbConstraints);

    final JButton upButton = new JButton();
    upButton.setText(
        addMnemonics
            ? UIBundle.message("row.move.up")
            : UIBundle.message("row.move.up.without.mnemonic"));
    upButton.setDefaultCapable(false);
    buttonsPanel.add(upButton, gbConstraints);

    final JButton downButton = new JButton();
    downButton.setText(
        addMnemonics
            ? UIBundle.message("row.move.down")
            : UIBundle.message("row.move.down.without.mnemonic"));
    downButton.setDefaultCapable(false);
    buttonsPanel.add(downButton, gbConstraints);

    gbConstraints.weighty = 1;
    buttonsPanel.add(new JPanel(), gbConstraints);

    addButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            TableUtil.stopEditing(table);
            tableModel.addRow();
            final int index = tableModel.getRowCount() - 1;
            table.editCellAt(index, 0);
            table.setRowSelectionInterval(index, index);
            table.setColumnSelectionInterval(0, 0);
            table.getParent().repaint();
            final Component editorComponent = table.getEditorComponent();
            if (editorComponent != null) {
              final Rectangle bounds = editorComponent.getBounds();
              table.scrollRectToVisible(bounds);
              editorComponent.requestFocus();
            }
          }
        });

    removeButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            TableUtil.stopEditing(table);
            int index = table.getSelectedRow();
            if (0 <= index && index < tableModel.getRowCount()) {
              tableModel.removeRow(index);
              if (index < tableModel.getRowCount()) {
                table.setRowSelectionInterval(index, index);
              } else {
                if (index > 0) {
                  table.setRowSelectionInterval(index - 1, index - 1);
                }
              }
              updateButtons(table, tableModel, addButton, removeButton, upButton, downButton);
            }

            table.getParent().repaint();
            table.requestFocus();
          }
        });

    upButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            TableUtil.stopEditing(table);
            int index = table.getSelectedRow();
            if (0 < index && index < tableModel.getRowCount()) {
              tableModel.exchangeRows(index, index - 1);
              table.setRowSelectionInterval(index - 1, index - 1);
            }
            table.requestFocus();
          }
        });

    downButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            TableUtil.stopEditing(table);
            int index = table.getSelectedRow();
            if (0 <= index && index < tableModel.getRowCount() - 1) {
              tableModel.exchangeRows(index, index + 1);
              table.setRowSelectionInterval(index + 1, index + 1);
            }
            table.requestFocus();
          }
        });

    table
        .getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {
              @Override
              public void valueChanged(ListSelectionEvent e) {
                updateButtons(table, tableModel, addButton, removeButton, upButton, downButton);
              }
            });
    updateButtons(table, tableModel, addButton, removeButton, upButton, downButton);

    return buttonsPanel;
  }