Example #1
0
  @Override
  protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    // Hide Window on ESC
    Action escapeAction = new AbstractAction("ESCAPE") { // $NON-NLS-1$
          /** */
          private static final long serialVersionUID = -6543764044868772971L;

          @Override
          public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);
          }
        };
    // Do search on Enter
    Action enterAction = new AbstractAction("ENTER") { // $NON-NLS-1$

          private static final long serialVersionUID = -3661361497864527363L;

          @Override
          public void actionPerformed(final ActionEvent actionEvent) {
            checkDirtyAndLoad(actionEvent);
          }
        };
    ActionMap actionMap = rootPane.getActionMap();
    actionMap.put(escapeAction.getValue(Action.NAME), escapeAction);
    actionMap.put(enterAction.getValue(Action.NAME), enterAction);
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(KeyStrokes.ESC, escapeAction.getValue(Action.NAME));
    inputMap.put(KeyStrokes.ENTER, enterAction.getValue(Action.NAME));

    return rootPane;
  }
  /**
   * Add a section to a list of components.
   *
   * @param components List of components
   * @param sectionId The {@link URI} identifying the section
   * @param menuOptions {@link MenuOptions options} for creating the menu
   */
  private void addSection(List<Component> components, URI sectionId, MenuOptions menuOptions) {
    List<Component> childComponents = makeComponents(sectionId, menuOptions);

    MenuComponent sectionDef = uriToMenuElement.get(sectionId);
    addNullSeparator(components);
    if (childComponents.isEmpty()) {
      logger.warn("No sub components found for section " + sectionId);
      return;
    }
    Action sectionAction = sectionDef.getAction();
    if (sectionAction != null) {
      String sectionLabel = (String) sectionAction.getValue(NAME);
      if (sectionLabel != null) {
        // No separators before the label
        stripTrailingNullSeparator(components);
        Color labelColor = (Color) sectionAction.getValue(SECTION_COLOR);
        if (labelColor == null) labelColor = GREEN;
        ShadedLabel label = new ShadedLabel(sectionLabel, labelColor);
        components.add(label);
      }
    }
    for (Component childComponent : childComponents)
      if (childComponent == null) {
        logger.warn("Separator found within section " + sectionId);
        addNullSeparator(components);
      } else components.add(childComponent);
    addNullSeparator(components);
  }
  /**
   * Manages the selection of the default tool in a popup tool in the toolbar.
   *
   * <p>I.e. in a toolbar, you can have tools that can be opened, into a grid of tools. The last
   * used tool is remembered, and put at the top when the popup is closed, i.e. is the only tool
   * that remains visible. This remembering is persistent, hence stored in the configuration file,
   * under a certain key (i.e. name).
   *
   * @param actions the array of actions that make up the popup
   * @param key appendix for the key for the configuration file
   */
  public static void manageDefault(Object[] actions, String key) {
    Action defaultAction = null;
    ConfigurationKey k = Configuration.makeKey("default", "popupactions", key);
    String defaultName = Configuration.getString(k);
    PopupActionsListener listener = new PopupActionsListener(k);
    for (int i = 0; i < actions.length; ++i) {
      if (actions[i] instanceof Action) {
        Action a = (Action) actions[i];
        if (a.getValue(Action.NAME).equals(defaultName)) {
          defaultAction = a;
        }
        a.addPropertyChangeListener(listener);
      } else if (actions[i] instanceof Object[]) {
        Object[] actionRow = (Object[]) actions[i];
        for (int j = 0; j < actionRow.length; ++j) {
          Action a = (Action) actionRow[j];
          if (a.getValue(Action.NAME).equals(defaultName)) {
            defaultAction = a;
          }
          a.addPropertyChangeListener(listener);
        }
      }
    }

    if (defaultAction != null) {
      defaultAction.putValue("isDefault", Boolean.valueOf(true));
    }
  }
  private void setPointButtonAction() {
    JButton button = calculator.getButtonPoint();
    Action action =
        new AbstractAction("colonAction") {
          @Override
          public void actionPerformed(ActionEvent e) {
            appendTextInCalculatorDisplay(".");
          }
        };

    // Shortcut al punto normal
    action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0));
    button.getActionMap().put("colonAction", action);
    button
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "colonAction");

    // Shortcut al punto del numpad
    action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DECIMAL, 0));
    button.getActionMap().put("colonAction", action);
    button
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "colonAction");

    button.addActionListener(action);
  }
  private void setMinusButtonAction() {
    JButton button = calculator.getButtonMinus();
    Action action =
        new AbstractAction("buttonMinusAction") {
          @Override
          public void actionPerformed(ActionEvent e) {
            appendTextInCalculatorDisplay("-");
          }
        };

    // Shortcut al menos del numpad
    action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0));
    button.getActionMap().put("buttonMinusAction", action);
    button
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "buttonMinusAction");

    // Shortcut al guion alto
    action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0));
    button.getActionMap().put("buttonMinusAction", action);
    button
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "buttonMinusAction");
    button.addActionListener(action);
  }
  private void setButtonAction(JButton button, final String key) {

    Action action =
        new AbstractAction("button" + key + "Action") {
          @Override
          public void actionPerformed(ActionEvent e) {
            appendTextInCalculatorDisplay(key.toLowerCase());
          }
        };

    // Shortcuts a los numeros comunes
    action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(key));
    button.getActionMap().put("button" + key + "Action", action);
    button
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "button" + key + "Action");

    // Shortcuts a los del numpad
    if (getNumpadVk(key) != null) {
      action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(getNumpadVk(key), 0));
      button.getActionMap().put("button" + key + "Action", action);
      button
          .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
          .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "button" + key + "Action");
    }

    button.addActionListener(action);
  }
  /**
   * <Some description here>
   *
   * @param evt
   * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
   */
  public void propertyChange(PropertyChangeEvent evt) {

    if (evt.getPropertyName().equals("enabled")) {

      Action tmp = this.delegate;

      if (evt.getNewValue().equals(Boolean.FALSE)) {
        for (int i = 0; i < this.delegates.length; i++) {
          if (this.delegates[i].isEnabled()) {
            this.delegate = this.delegates[i];
            break;
          }
        }
      } else {
        this.delegate = (Action) evt.getSource();
      }

      if (tmp != this.delegate) {
        this.firePropertyChange(NAME, tmp.getValue(NAME), this.delegate.getValue(NAME));
        this.firePropertyChange(
            SMALL_ICON, tmp.getValue(SMALL_ICON), this.delegate.getValue(SMALL_ICON));
        this.firePropertyChange(
            SHORT_DESCRIPTION,
            tmp.getValue(SHORT_DESCRIPTION),
            this.delegate.getValue(SHORT_DESCRIPTION));
      }
    }

    this.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
  }
Example #8
0
 private JButton addButton(Action a) {
   JButton button = new JButton((String) a.getValue(Action.NAME), null);
   button.setEnabled(a.isEnabled());
   button.setToolTipText((String) a.getValue(Action.SHORT_DESCRIPTION));
   button.setAction(a);
   add(button);
   return button;
 }
Example #9
0
 private JToggleButton addToggleButton(Action a) {
   JToggleButton tb = new JToggleButton((String) a.getValue(Action.NAME), null);
   tb.setEnabled(a.isEnabled());
   tb.setToolTipText((String) a.getValue(Action.SHORT_DESCRIPTION));
   tb.setAction(a);
   add(tb);
   return tb;
 }
Example #10
0
    public String getDisplayTooltip() {
      if (!name.isEmpty()) return name;

      Object tt = action.getValue(TaggingPreset.OPTIONAL_TOOLTIP_TEXT);
      if (tt != null) return (String) tt;

      return (String) action.getValue(Action.SHORT_DESCRIPTION);
    }
 private void addButton(JPanel container, Action action, Hashtable actions, Icon icon) {
   String actionName = (String) action.getValue(Action.NAME);
   String actionCommand = (String) action.getValue(Action.ACTION_COMMAND_KEY);
   JButton button = new JButton(icon);
   button.setActionCommand(actionCommand);
   button.addActionListener(DeepaMehtaClientUtils.getActionByName(actionName, actions));
   container.add(button);
   toolbarButtons[buttonIndex++] = button;
 }
Example #12
0
  /**
   * Print Action and Input Map for component
   *
   * @param comp Component with ActionMap
   */
  public static void printActionInputMap(JComponent comp) {
    //	Action Map
    ActionMap am = comp.getActionMap();
    Object[] amKeys = am.allKeys(); //  including Parents
    if (amKeys != null) {
      System.out.println("-------------------------");
      System.out.println("ActionMap for Component " + comp.toString());
      for (int i = 0; i < amKeys.length; i++) {
        Action a = am.get(amKeys[i]);

        StringBuffer sb = new StringBuffer("- ");
        sb.append(a.getValue(Action.NAME));
        if (a.getValue(Action.ACTION_COMMAND_KEY) != null)
          sb.append(", Cmd=").append(a.getValue(Action.ACTION_COMMAND_KEY));
        if (a.getValue(Action.SHORT_DESCRIPTION) != null)
          sb.append(" - ").append(a.getValue(Action.SHORT_DESCRIPTION));
        System.out.println(sb.toString() + " - " + a);
      }
    }
    /**
     * Same as below KeyStroke[] kStrokes = comp.getRegisteredKeyStrokes(); if (kStrokes != null) {
     * System.out.println("-------------------------"); System.out.println("Registered Key Strokes -
     * " + comp.toString()); for (int i = 0; i < kStrokes.length; i++) { System.out.println("- " +
     * kStrokes[i].toString()); } } /** Focused
     */
    InputMap im = comp.getInputMap(JComponent.WHEN_FOCUSED);
    KeyStroke[] kStrokes = im.allKeys();
    if (kStrokes != null) {
      System.out.println("-------------------------");
      System.out.println("InputMap for Component When Focused - " + comp.toString());
      for (int i = 0; i < kStrokes.length; i++) {
        System.out.println("- " + kStrokes[i].toString() + " - " + im.get(kStrokes[i]).toString());
      }
    }
    /** Focused in Window */
    im = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    kStrokes = im.allKeys();
    if (kStrokes != null) {
      System.out.println("-------------------------");
      System.out.println("InputMap for Component When Focused in Window - " + comp.toString());
      for (int i = 0; i < kStrokes.length; i++) {
        System.out.println("- " + kStrokes[i].toString() + " - " + im.get(kStrokes[i]).toString());
      }
    }
    /** Focused when Ancester */
    im = comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    kStrokes = im.allKeys();
    if (kStrokes != null) {
      System.out.println("-------------------------");
      System.out.println("InputMap for Component When Ancestor - " + comp.toString());
      for (int i = 0; i < kStrokes.length; i++) {
        System.out.println("- " + kStrokes[i].toString() + " - " + im.get(kStrokes[i]).toString());
      }
    }
    System.out.println("-------------------------");
  } //  printActionInputMap
Example #13
0
  /**
   * Add the action to the given toolbar. If the LARGE_ICON property is specified in the Action,
   * then use it for the button. We use this instead of SMALL_ICON, because SMALL_ICON shows up when
   * the action is added to a menu, and in most cases we don't actually want an icon there. If no
   * icon is specified, then the button will just have the name of the action. If the "tooltip"
   * property is specified in the action, then create a tooltip for the button with the string. The
   * new button is added to the action as the "toolButton" property. The button is enabled by
   * default.
   */
  public static JButton addToolBarButton(JToolBar toolbar, Action action) {
    Icon icon = (Icon) action.getValue(LARGE_ICON);
    String label = null;

    if (icon == null) {
      label = (String) action.getValue(Action.NAME);
    }

    return addToolBarButton(toolbar, action, null, icon, label, true);
  }
Example #14
0
 private void setButtonFromAction(Action action) {
   iconName = (String) action.getValue(LimeAction.ICON_NAME);
   rollOverIconName = (String) action.getValue(LimeAction.ICON_NAME_ROLLOVER);
   message = (String) action.getValue(LimeAction.SHORT_NAME);
   // fall back on Action.NAME
   if (message == null) {
     message = (String) action.getValue(Action.NAME);
   }
   updateButton();
 }
Example #15
0
  /**
   * Add an action to a menu and return the menu item created. If the tool tip is null, use the
   * "tooltip" property already in the action, otherwise add the property to the action. (The
   * mnemonic isn't added.) The new menu item is added to the action as the "menuItem" property. The
   * menu item's text is set using the action's name, concatenated with a description of a keyboard
   * accelerator, if one has been set previously on the action. The item will be enabled by default.
   */
  public static JMenuItem addMenuItem(JMenu menu, Action action) {
    String label = (String) action.getValue(Action.NAME);
    int mnemonic = 0;
    Integer i = (Integer) action.getValue(MNEMONIC_KEY);

    if (i != null) {
      mnemonic = i.intValue();
    }

    return addMenuItem(menu, label, action, mnemonic, null, true);
  }
Example #16
0
 private void addKeyBindings() {
   for (Action a : ActionManager.getInstance().getActions()) {
     KeyStroke key = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
     if (key != null) {
       String name = (String) a.getValue(Action.NAME);
       getInputMap().put(key, name);
       getActionMap().put(name, a);
       // System.out.println("Action: " + name + ":" + key);
     } // else
     // System.out.println("Action: " + name + ":" + key);
   }
 }
 // XXX: this is actually wierd, because it changes the action's properties
 // perhaps we should just update the presenter, but should not touch the
 // action itself
 private static void updateIcon(Action a) {
   Object icon = a.getValue(Action.SMALL_ICON);
   if (icon == null) {
     String resourceId = (String) a.getValue(BaseAction.ICON_RESOURCE_PROPERTY);
     if (resourceId != null) {
       Image img = ImageUtilities.loadImage(resourceId);
       if (img != null) {
         a.putValue(Action.SMALL_ICON, new ImageIcon(img));
       }
     }
   }
 }
Example #18
0
  private JComponent addItem(
      final Component pComponent, final KToolbarImpl pToolbar, final Action pAction) {
    JComponent item = null;
    final String key = (String) pAction.getValue(Action.NAME);

    if (pAction instanceof KMenu) {
      final KMenu actionMenu = (KMenu) pAction;
      item = actionMenu.create(pComponent);
      pToolbar.add(item);
    } else if (pAction == KAction.SEPARATOR) {
      pToolbar.addSeparator();
    } else if (pAction instanceof KComponentAction) {
      item = ((KComponentAction) pAction).getComponent();
      pToolbar.add(item);
    } else {
      AbstractButton button;
      ActionGroup group = (ActionGroup) pAction.getValue(KAction.KEY_GROUP);
      if (group != null) {
        button = new JToggleButton(pAction);
        ((JToggleButton) button).setSelected(group.getSelected() == pAction);
        ButtonGroup bg = group.getButtonGroup(ResKey.TOOLBAR);
        bg.add(button);
      } else {
        button = new JButton(pAction);
      }
      item = button;

      final WidgetResources wr = ResourceAdapter.getInstance().getWidget(key, ResKey.TOOLBAR);
      Icon icon = wr.getIcon();
      if (icon == null) {
        icon = DEF_ICON;
      }
      button.setIcon(icon);
      button.setToolTipText(wr.getToolTip());
      button.setMargin(ZERO_INSETS);
      button.setRequestFocusEnabled(false);
      button.setFocusable(false);

      if (false) {
        button.setText(wr.getText());
        button.setMnemonic(wr.getMnenomnic());
        button.setDisplayedMnemonicIndex(wr.getMnenomnicIndex());
      } else {
        button.setText(null);
      }
    }

    if (item != null) {
      pToolbar.add(item);
    }
    return item;
  }
Example #19
0
 // TODO accelerators for buttons
 private static void installAccelerator(Action action, final JButton button) {
   if (action.getValue(Action.ACCELERATOR_KEY) instanceof KeyStroke) {
     KeyStroke keyStroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
     InputMap windowInputMap =
         SwingUtilities.getUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW);
     if (windowInputMap == null) {
       windowInputMap = new ComponentInputMapUIResource(button);
       SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap);
     }
     windowInputMap.put(keyStroke, keyStroke.toString());
     button.getActionMap().put(keyStroke.toString(), action);
   }
 }
Example #20
0
  /**
   * Add a quick keystroke on the given pane for the given action. If the given keystroke is null,
   * then use the ACCELERATOR_KEY property that has been set in the action. If the given keystroke
   * is null, Otherwise, set the ACCELERATOR_KEY property to the given key stroke.
   */
  public static void addHotKey(JComponent pane, Action action, KeyStroke key) {
    String name = (String) action.getValue(Action.NAME);

    if (key == null) {
      key = (KeyStroke) action.getValue(ACCELERATOR_KEY);
    } else {
      action.putValue(ACCELERATOR_KEY, key);
    }

    if (key != null) {
      pane.registerKeyboardAction(action, name, key, JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
  }
 /**
  * Make an {@link AbstractButton} be configured in a "toolbar-like" way, for instance showing only
  * the icon.
  *
  * @param actionButton Button to toolbarise
  */
 protected void toolbarizeButton(AbstractButton actionButton) {
   Action action = actionButton.getAction();
   if (action.getValue(SHORT_DESCRIPTION) == null)
     action.putValue(SHORT_DESCRIPTION, action.getValue(NAME));
   actionButton.setBorder(new EmptyBorder(0, 2, 0, 2));
   // actionButton.setHorizontalTextPosition(JButton.CENTER);
   // actionButton.setVerticalTextPosition(JButton.BOTTOM);
   if (action.getValue(Action.SMALL_ICON) != null) {
     // Don't show the text
     actionButton.putClientProperty("hideActionText", true);
     // Since hideActionText seems to be broken in Java 5 and/or OS X
     actionButton.setText(null);
   }
 }
Example #22
0
 /**
  * Visible property of control linked with this action. A visible at True does not mean that the
  * user see the control, it can be hidden by other components.
  *
  * @param action Action to read
  * @return True if visible
  */
 public static boolean isVisible(Action action) {
   Object val = action.getValue(VISIBLE);
   if (!(val instanceof Boolean)) {
     return true;
   }
   return (Boolean) val;
 }
Example #23
0
 /**
  * Return the menu id.
  *
  * @param action Action to use
  * @return menu Name or empty if not set.
  */
 public static String getMenuId(Action action) {
   Object val = action.getValue(MENU_ID);
   if (val == null) {
     return "";
   }
   return (String) val;
 }
Example #24
0
 /** @return The other Menu id that should be after this action, empty if none. */
 public static String getInsertBeforeMenuId(Action action) {
   Object val = action.getValue(INSERT_BEFORE_MENUID);
   if (val == null) {
     return "";
   }
   return (String) val;
 }
Example #25
0
 /** @return The other Menu id that should be before this action, empty if none. */
 public static String getInsertAfterMenuId(Action action) {
   Object val = action.getValue(INSERT_AFTER_MENUID);
   if (val == null) {
     return "";
   }
   return (String) val;
 }
Example #26
0
 /** @return The ButtonGroup name to create */
 public static String getToggleGroup(Action action) {
   Object val = action.getValue(TOGGLE_GROUP);
   if (val == null) {
     return "";
   }
   return (String) val;
 }
Example #27
0
 /**
  * Using logical group on actions will automatically create JSeparator between such groups
  *
  * @return logical group name
  */
 public static String getLogicalGroup(Action action) {
   Object val = action.getValue(LOGICAL_GROUP);
   if (val == null) {
     return "";
   }
   return (String) val;
 }
Example #28
0
 /**
  * Return the icon.
  *
  * @param action Action to use
  * @return Icon instance or null
  */
 public static Icon getIcon(Action action) {
   Object val = action.getValue(Action.SMALL_ICON);
   if (val == null) {
     return null;
   }
   return (Icon) val;
 }
 /**
  * Returns whether the given action is selected
  *
  * @param action The action
  * @return Whether the given action is selected
  */
 private boolean isSelected(Action action) {
   Object value = action.getValue(AbstractAction.SELECTED_KEY);
   if (value instanceof Boolean) {
     return (Boolean) value;
   }
   return false;
 }
 public boolean askDiscardUnsaved(final javax.swing.Action action) {
   if (!isModified()) return true;
   final String title, msg;
   if (true) {
     final ResourceMap r = getContext().getResourceMap();
     title =
         r.getString("discard" + ".Dialog" + ".title", action.getValue(javax.swing.Action.NAME));
     msg = r.getString("discard" + ".Dialog" + ".message");
   } else if (action instanceof ApplicationAction) {
     final ApplicationAction aa = (ApplicationAction) action;
     final ResourceMap r = getContext().getResourceMap();
     title =
         action == null
             ? null
             : r.getString(
                 aa.getName() + ".Dialog" + ".title", aa.getValue(javax.swing.Action.NAME));
     msg = r.getString(aa.getName() + ".Dialog" + ".message");
   } else {
     title = null;
     msg = "Discard unsaved changes?";
   }
   return JOptionPane.YES_OPTION
       == JOptionPane.showConfirmDialog(
           getMainFrame(), msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
 }