Example #1
0
  /**
   * Set the button to have simplified UI.
   *
   * @param button button to be modified
   * @param <T> type of button
   * @return button
   */
  public static <T extends AbstractButton> T decoratedToSimpleButton(final T button) {

    button.setForeground(Color.BLACK);
    button.setBackground(Color.LIGHT_GRAY);
    button.setBorderPainted(true);
    button.setFocusPainted(true);
    button.setContentAreaFilled(false);
    button.setOpaque(true);

    if (button instanceof JToggleButton) {
      ((JToggleButton) button)
          .addActionListener(
              new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                  if (button.isSelected()) {
                    button.setBackground(Color.WHITE);
                  }
                }
              });
    }
    button.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseEntered(MouseEvent e) {
            super.mouseEntered(e);
            button.setBackground(Color.WHITE);
          }

          @Override
          public void mouseExited(MouseEvent e) {
            super.mouseExited(e);
            if (!button.isSelected()) {
              button.setBackground(Color.LIGHT_GRAY);
            }
          }
        });

    button.addFocusListener(
        new FocusAdapter() {
          @Override
          public void focusLost(FocusEvent e) {
            if (!button.isSelected()) {
              button.setBackground(Color.LIGHT_GRAY);
            }
          }
        });
    Border line = new LineBorder(Color.BLACK);
    Border margin = new EmptyBorder(5, 15, 5, 15);
    Border compound = new CompoundBorder(line, margin);
    button.setBorder(compound);
    return button;
  }
Example #2
0
  /**
   * Configure the given AbstractButton to fire the given action, load its data from the resources
   * beginning with <code>bundleName</code> and load the icon from <code>/resources/images/</code>,
   * if <code>iconName</code> is not <code>null</code>.
   *
   * <p>These bundle name suffixes are recognized:
   *
   * <dl>
   *   <dt>"" (empty suffix):
   *   <dd>the text of the button
   *   <dt>.hideText
   *   <dd>if set (to any other value than "false"), don't display the text in the toolbar
   *   <dt>.tooltip:
   *   <dd>the tooltip for the button
   *   <dt>.mnemonic:
   *   <dd>the mnemonic for the button (the letter that will appear underlined in the button's text)
   *   <dt>.accelerator:
   *   <dd>the accelerator to be used with this button; if mnemonic is not set, use the keyCode of
   *       this accelerator as mnemonic
   * </dl>
   *
   * @param <T> The type of the button.
   * @param button The button to configure.
   * @param action The action the button should invoke.
   * @param bundleName The base of the resource bundle strings used to configure this button.
   * @param iconName The name that will be appended to the path <code>/resources/images/</code> to
   *     find the icon.
   * @return The button given in <code>button</code> param, configured.
   */
  public <T extends AbstractButton> T createToolbarItem(
      final T button, final Action action, final String bundleName, final String iconName) {
    if (bundleName == null)
      throw new NullPointerException(
          "Tried to create toolbar item without giving the corresponding bundle name.");

    button.setOpaque(false);

    button.setAction(action);
    if (iconName != null) {
      URL url = getClass().getResource("/resources/images/" + iconName);
      if (url != null) button.setIcon(new ImageIcon(url));
    }

    final AbstractButton btn;
    if (button instanceof JDropDownButton) {
      btn = ((JDropDownButton) button).getMainButton();
    } else if (button instanceof JMenuItem) {
      btn = null;
    } else {
      btn = button;
    }

    final PropertyChangeListener listener =
        new PropertyChangeListener() {
          @Override
          public void propertyChange(PropertyChangeEvent evt) {
            boolean hideText = false;
            try {
              String hide = messages.getString(bundleName + ".hideText");
              if (!"false".equals(hide)) hideText = true;
            } catch (MissingResourceException e) {
            }

            try {
              if (!hideText) button.setText(messages.getString(bundleName));
              else button.setText("");
            } catch (MissingResourceException e) {
              button.setText("");
            }

            if (btn != null) {
              if (btn.getText() == null || btn.getText().length() == 0) {
                btn.setHorizontalAlignment(SwingConstants.CENTER);
              } else {
                btn.setHorizontalAlignment(SwingConstants.LEFT);
              }
            }

            String mnemonic = null;
            try {
              mnemonic = messages.getString(bundleName + ".mnemonic");
            } catch (MissingResourceException e) {
            }
            String accelerator = null;
            try {
              accelerator = messages.getString(bundleName + ".accelerator");
            } catch (MissingResourceException e) {
            }

            KeyStroke accStroke = KeyStroke.getKeyStroke(accelerator);
            KeyStroke mnemStroke = KeyStroke.getKeyStroke(mnemonic);
            if (mnemStroke == null) mnemStroke = accStroke;

            if (evt.getOldValue() != null && (evt.getOldValue() instanceof ResourceBundle)) {
              try {
                String oldAcc =
                    ((ResourceBundle) evt.getOldValue()).getString(bundleName + ".accelerator");
                KeyStroke oldStroke = KeyStroke.getKeyStroke(oldAcc);
                if (oldStroke != null) {
                  getInputMap(WHEN_IN_FOCUSED_WINDOW).remove(oldStroke);
                }
              } catch (MissingResourceException e) {
              }
            }

            if (accStroke != null) {
              if ((button instanceof JMenuItem) && !(button instanceof JMenu))
                ((JMenuItem) button).setAccelerator(accStroke);
              // TODO although this should enable the key shortcuts in the application-wide manner,
              // it doesn't do
              // that... instead, after a key shortcut is used, the user has to click in the
              // application in order
              // to be able to use another key shortcut
              getInputMap(WHEN_IN_FOCUSED_WINDOW).put(accStroke, action);
              getActionMap().put(action, action);
            }
            if (mnemStroke != null) {
              button.setMnemonic(mnemStroke.getKeyCode());
            }

            try {
              String title = null;
              try {
                title = messages.getString(bundleName);
              } catch (MissingResourceException e) {
              }
              String tooltip =
                  ServiceLocator.get(TooltipFactory.class)
                      .getDecorated(
                          messages.getString(bundleName + ".tooltip"), title, iconName, accStroke);
              button.setToolTipText(tooltip);
              button.getAccessibleContext().setAccessibleDescription(tooltip);
            } catch (MissingResourceException e) {
            }
          }
        };
    listener.propertyChange(new PropertyChangeEvent(this, "messages", null, messages));
    addPropertyChangeListener("messages", listener);

    if (btn != null && btn.getIcon() != null) {
      if (btn != button) {
        btn.setMinimumSize(
            new Dimension(btn.getIcon().getIconWidth() + 3, btn.getIcon().getIconHeight() + 3));
      } else {
        button.setMinimumSize(
            new Dimension(btn.getIcon().getIconWidth() + 2, btn.getIcon().getIconHeight() + 2));
      }
    }

    return button;
  }