/**
   * Sets initial answer (i.e. button that will be preselected by default).
   *
   * <p>Please note that this is not the default answer that will be returned by {@link
   * #getReturnCode()} if user does nothing (i.e. closes the window). It is just the preselectated
   * button.
   *
   * @param initialAnswer {@link #A_OK}, {@link #A_CANCEL}.
   */
  public void setInitialAnswer(final int initialAnswer) {
    // If the inial answer did not actual changed, do nothing
    if (this._initialAnswer == initialAnswer) {
      return;
    }

    //
    // Configure buttons accelerator (KeyStroke) and RootPane's default button
    final JRootPane rootPane = getRootPane();
    final CButton okButton = confirmPanel.getOKButton();
    final AppsAction okAction = (AppsAction) okButton.getAction();
    final CButton cancelButton = confirmPanel.getCancelButton();
    final AppsAction cancelAction = (AppsAction) cancelButton.getAction();
    if (initialAnswer == A_OK) {
      okAction.setDefaultAccelerator();
      cancelAction.setDefaultAccelerator();
      rootPane.setDefaultButton(okButton);
    } else if (initialAnswer == A_CANCEL) {
      // NOTE: we need to set the OK's Accelerator keystroke to null because in most of the cases it
      // is "Enter"
      // and we want to prevent user for hiting ENTER by mistake
      okAction.setAccelerator(null);
      cancelAction.setDefaultAccelerator();
      rootPane.setDefaultButton(cancelButton);
    } else {
      throw new IllegalArgumentException("Unknown inital answer: " + initialAnswer);
    }

    //
    // Finally, set the new inial answer
    this._initialAnswer = initialAnswer;
  }
Exemplo n.º 2
0
  private AppsAction(final Builder builder) {
    super();
    _actionCommand = builder.getAction();
    _acceleratorDefault = builder.getAccelerator();
    toggleButton = builder.isToggleButton();

    final String displayName;
    if (builder.isIsRetrieveAppsActionMessage()) {
      final IMsgBL msgBL = Services.get(IMsgBL.class);
      displayName = msgBL.getMsg(Env.getCtx(), _actionCommand);
    } else {
      displayName = _actionCommand;
    }

    //
    // Button insets
    if (builder.getButtonInsets() != null) {
      buttonInsets = builder.getButtonInsets();
    } else {
      buttonInsets = BUTTON_INSETS;
    }
    //
    buttonDefaultCapable = builder.getButtonDefaultCapable();

    //
    // Tooltip and mnemonic
    String toolTipText = builder.getToolTipText();
    {
      if (toolTipText == null) {
        toolTipText = displayName;
      }
      final int pos = toolTipText.indexOf('&');
      if (pos != -1 && toolTipText.length() > pos) // We have a mnemonic - creates ALT-_
      {
        final Character ch = toolTipText.toUpperCase().charAt(pos + 1);
        if (ch != ' ') {
          toolTipText = toolTipText.substring(0, pos) + toolTipText.substring(pos + 1);
          putValue(Action.MNEMONIC_KEY, new Integer(ch.hashCode()));
        }
      }
    }

    //
    // Load icons
    final Icon iconSmall = getIcon(_actionCommand, true);
    final Icon iconLarge = getIcon(_actionCommand, false);
    Icon iconSmallPressed = null;
    Icon iconLargePressed = null;
    if (toggleButton) {
      final String iconNamePressed =
          _actionCommand + "X"; // NOTE: ToggleIcons have the pressed name with X
      iconSmallPressed = getIcon(iconNamePressed, true);
      if (iconSmallPressed == null) {
        iconSmallPressed = iconSmall;
      }

      iconLargePressed = getIcon(iconNamePressed, false);
      if (iconLargePressed == null) {
        iconLargePressed = iconLarge;
      }
    }
    // Set icons
    this.iconSmallPressed = iconSmallPressed;
    if (builder.isSmallSize()) {
      icon = iconSmall;
      iconPressed = iconSmallPressed;
    } else {
      icon = iconLarge;
      iconPressed = iconLargePressed;
    }

    //
    // Button text
    String buttonText;
    if (builder.isUseTextFromActionName()) {
      buttonText = displayName;
    } else {
      buttonText = builder.getText();
    }
    // If there is no icon and no button text, then use the text from action,
    // because else we will get an empty button.
    // NOTE: we are checking the text without stripping the spaces because we want to handle the
    // case when,
    // the developer programatically sets " " as text.
    if (icon == null && Check.isEmpty(buttonText, false)) {
      buttonText = displayName;
    }
    this.buttonText = buttonText;

    // Attributes
    putValue(Action.NAME, displayName); // Display
    putValue(Action.SMALL_ICON, iconSmall); // Icon
    putValue(Action.SHORT_DESCRIPTION, toolTipText); // Tooltip
    putValue(Action.ACTION_COMMAND_KEY, _actionCommand); // ActionCommand

    //
    // Accelerator
    setAccelerator(_acceleratorDefault);
  } // Action
Exemplo n.º 3
0
 public void setDefaultAccelerator() {
   setAccelerator(_acceleratorDefault);
 }