Java Code Examples for javax.swing.Action#getValue()

The following examples show how to use javax.swing.Action#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicLookAndFeel.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * If necessary, invokes {@code actionPerformed} on
 * {@code audioAction} to play a sound.
 * The {@code actionPerformed} method is invoked if the value of
 * the {@code "AuditoryCues.playList"} default is a {@code
 * non-null} {@code Object[]} containing a {@code String} entry
 * equal to the name of the {@code audioAction}.
 *
 * @param audioAction an Action that knows how to render the audio
 *                    associated with the system or user activity
 *                    that is occurring; a value of {@code null}, is
 *                    ignored
 * @throws ClassCastException if {@code audioAction} is {@code non-null}
 *         and the value of the default {@code "AuditoryCues.playList"}
 *         is not an {@code Object[]}
 * @since 1.4
 */
protected void playSound(Action audioAction) {
    if (audioAction != null) {
        Object[] audioStrings = (Object[])
                                UIManager.get("AuditoryCues.playList");
        if (audioStrings != null) {
            // create a HashSet to help us decide to play or not
            HashSet<Object> audioCues = new HashSet<Object>();
            for (Object audioString : audioStrings) {
                audioCues.add(audioString);
            }
            // get the name of the Action
            String actionName = (String)audioAction.getValue(Action.NAME);
            // if the actionName is in the audioCues HashSet, play it.
            if (audioCues.contains(actionName)) {
                audioAction.actionPerformed(new
                    ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                                actionName));
            }
        }
    }
}
 
Example 2
Source File: SectionPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addAction (Action action) {
    String name = (String) action.getValue(Action.NAME);
    LinkButton btn = new LinkButton(name);
    btn.addActionListener(action);
    
    if (notEmpty) {
        JLabel separator = new javax.swing.JLabel();
        separator.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createEmptyBorder(2, 0, 2, 0),
                BorderFactory.createLineBorder(Color.BLACK, 1)
        ));
        horizontalSeqGroup
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(separator)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
        verticalParallelGroup
            .addComponent(separator, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);
    }
    
    horizontalSeqGroup
            .addComponent(btn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
    verticalParallelGroup
            .addComponent(btn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
    notEmpty = true;
}
 
Example 3
Source File: AbstractEditorAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public final Object getValue(String key) {
    Action dAction = delegateAction;
    // Delegate whole getValue() if delegateAction already exists
    if (dAction != null && dAction != UNITIALIZED_ACTION) {
        Object value = dAction.getValue(key);
        if (value == null) {
            value = getValueLocal(key);
            if (value != null) {
                if (LOG.isLoggable(Level.FINE)) {
                    LOG.fine("Transfer wrapper action property: key=" + key + ", value=" + value + '\n'); // NOI18N
                }
                dAction.putValue(key, value);
            }
        }
        return value;
    }

    return getValueLocal(key);
}
 
Example 4
Source File: LinkButton.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public LinkButton(String text, boolean handlePopupEvents, Action a, boolean underlined) {
    super(text);
    this.handlePopupEvents = handlePopupEvents;
    this.underlined = underlined;
    
    if (null != a) {
        Icon icon = (Icon) a.getValue(Action.SMALL_ICON);
        if (null != icon) {
            setIcon(icon);
            setPressedIcon(icon);
        }
        Object tooltip = a.getValue(Action.SHORT_DESCRIPTION);
        if (null != tooltip) {
            setToolTipText(tooltip.toString());
        }
    }
    init(a);
}
 
Example 5
Source File: PreferencesDialog.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public static Action decorateWithLogging(final SwingLogPanel log, final Pref< ? > pref, final Action action) {
    if (log == null)
        return action;
    return new AbstractAction((String) action.getValue(Action.NAME), (Icon) action.getValue(Action.SMALL_ICON)) {

        private static final long serialVersionUID = -2790668001235140089L;

        @Override
        public void actionPerformed(ActionEvent e) {
            Object oldVal = pref.get();
            action.actionPerformed(e);
            Object newVal = pref.get();
            if (!newVal.equals(oldVal))
                logPrefChanged(log, pref);
        }
    };
}
 
Example 6
Source File: JavaKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target){
    // Try to get the accelerator
    Keymap km = target.getKeymap();
    if (km != null) {

        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        if (keys != null && keys.length > 0) {
            item.setAccelerator(keys[0]);
        }else if (a!=null){
            KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
            if (ks!=null) {
                item.setAccelerator(ks);
            }
        }
    }
}
 
Example 7
Source File: ToolBar.java    From Dayon with GNU General Public License v3.0 5 votes vote down vote up
public void addToggleAction(Action action) {
	final JToggleButton button = new JToggleButton();

	button.setMargin(zeroInsets);
	button.setHideActionText(true);
	button.setAction(action);

	if (action.getValue(Action.SMALL_ICON) == null) {
		button.setText((String) action.getValue("DISPLAY_NAME"));
	}

	button.setFocusable(false);

	add(button);
}
 
Example 8
Source File: XJMenuItem.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public XJMenuItem(Action a, StatusListener listener){
  super(a);
  this.description = (String) a.getValue(Action.SHORT_DESCRIPTION);
  this.listener = listener;
  // stop showing tooltip in the menu, status bar is enough
  setToolTipText(null);
  initListeners();
}
 
Example 9
Source File: CommonProjectActionsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Assure that two clients do not interact.
 */
public void testNewProjectAction() {
    Action client1 = CommonProjectActions.newProjectAction();
    Object existingSources = new Object();
    client1.putValue(CommonProjectActions.EXISTING_SOURCES_FOLDER, existingSources);
    Action client2 = CommonProjectActions.newProjectAction();
    Object o = client2.getValue(CommonProjectActions.EXISTING_SOURCES_FOLDER);
    assertTrue(o != existingSources);
}
 
Example 10
Source File: PresenterUpdater.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private PresenterUpdater(int type, Action action) {
    if (action == null) {
        throw new IllegalArgumentException("action must not be null"); // NOI18N
    }
    this.type = type;
    this.actionName = (String) action.getValue(Action.NAME);
    this.action = action;
    if (type == TOOLBAR) {
        presenter = new JButton();
        useActionSelectedProperty = false;
    } else { // MENU or POPUP
        useActionSelectedProperty = (action.getValue(AbstractEditorAction.PREFERENCES_KEY_KEY) != null);
        if (useActionSelectedProperty) {
            presenter = new LazyJCheckBoxMenuItem();
            presenter.setSelected(isActionSelected());
        } else {
            presenter = new LazyJMenuItem();
        }
    }

    action.addPropertyChangeListener(WeakListeners.propertyChange(this, action));
    if (type == MENU) {
        listenedContextActions = new WeakSet<Action>();
        EditorRegistryWatcher.get().registerPresenterUpdater(this); // Includes notification of active component
    } else {
        listenedContextActions = null;
    }

    presenter.addActionListener(this);
    updatePresenter(null); // Not active yet => mark updates pending
}
 
Example 11
Source File: TransparentToolBar.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private JButton createActionComponent(Action a) {
    JButton b = new JButton();
    if (a != null && (a.getValue(Action.SMALL_ICON) != null ||
                      a.getValue(Action.LARGE_ICON_KEY) != null)) {
        b.setHideActionText(true);
    }
    b.setHorizontalTextPosition(JButton.CENTER);
    b.setVerticalTextPosition(JButton.BOTTOM);
    b.setAction(a);
    return b;
}
 
Example 12
Source File: DefaultSyntaxKit.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
private void configActionProperties(Action action, String actionName, String configKey) {

		// if we have an icon, then load it:
		String iconLoc = getConfig().getString(configKey + ".SmallIcon", actionName + ".png");
		URL loc = this.getClass().getResource(DefaultSyntaxAction.SMALL_ICONS_LOC_PREFIX + iconLoc);
		if (loc != null) {
			ImageIcon i = new ImageIcon(loc);
			action.putValue(Action.SMALL_ICON, i);
		}
		// Set the menu text.  Use the Action.NAME property, unless it is
		// already set.
		// The NAME would be set for default actions, and we should not change those names.
		// so we will put another property and use it for the menu text
		String name = getProperty(configKey + ".MenuText");
		if (action.getValue(Action.NAME) == null) {
			action.putValue(Action.NAME, name);
		} else {
			action.putValue(ACTION_MENU_TEXT, name);
		}
		// Set the menu tooltips
		String shortDesc = getProperty(configKey + ".ToolTip");
		if (shortDesc != null) {
			action.putValue(Action.SHORT_DESCRIPTION, shortDesc);
		} else {
			action.putValue(Action.SHORT_DESCRIPTION, name);
		}
	}
 
Example 13
Source File: DropdownButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void addAction(JPopupMenu popup, Action action) {
    if (action == null) {
        popup.addSeparator();
    } else {
        Class cls = (Class)action.getValue(KEY_CLASS);
        if (Boolean.class.equals(cls)) {
            Boolean boolvalue = (Boolean)action.getValue(KEY_BOOLVALUE);
            JCheckBoxMenuItem item = new JCheckBoxMenuItem(action);
            item.setSelected(boolvalue);
            popup.add(item);
        } else {
            popup.add(action);
        }
    }
}
 
Example 14
Source File: GeneralAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Object getValue(String key) {
    if (attrs != null && attrs.containsKey(key)) {
        return attrs.get(key);
    }
    Object ret = GeneralAction.extractCommonAttribute(map, this, key);
    if (ret != null) {
        return ret;
    }
    
    Action a = findAction();
    return a == null ? null : a.getValue(key);
}
 
Example 15
Source File: FindBar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String getName(Action a) {
    Object name = a.getValue(Action.NAME);
    if (name instanceof String) {
        return (String) name;
    } else {
        return "null"; // NOI18N
    }
}
 
Example 16
Source File: ExtKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected String getItemText(JTextComponent target, String actionName, Action a) {
    String itemText;
    if (a instanceof BaseAction) {
        itemText = ((BaseAction)a).getPopupMenuText(target);
    } else {
        itemText = (String) a.getValue("popupText");
        if (itemText == null) {
            itemText = (String) a.getValue("menuText");
            if (itemText == null) {
                itemText = actionName;
            }
        }
    }
    return itemText;
}
 
Example 17
Source File: LinkButton.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * C'tor
 *
 * @param text
 * @param icon
 * @param al Action to invoke when the button is pressed, can be null but
 * the button is disabled then.
 */
public LinkButton(String text, Icon icon, Action a, boolean underlined) {
    super(text);
    this.underlined = underlined;
    setIcon(icon);
    setPressedIcon(icon);
    Object tooltip = a.getValue(Action.SHORT_DESCRIPTION);
    if (null != tooltip) {
        setToolTipText(tooltip.toString());
    }
    this.handlePopupEvents = true;
    init(a);
}
 
Example 18
Source File: AlwaysEnabledActionTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testIconIsCorrect() throws Exception {
    myListenerCounter = 0;
    myIconResourceCounter = 0;
    Action a = readAction("testIconIsCorrect.instance");
    
    assertNotNull("Action created", a);
    assertEquals("No myListener called", 0, myListenerCounter);
    assertEquals("No myIconURL called", 0, myIconResourceCounter);
    
    Object name = a.getValue(a.NAME);
    Object mnem = a.getValue(a.MNEMONIC_KEY);
    Object smallIcon = a.getValue(a.SMALL_ICON);
    if (smallIcon instanceof Icon) {
        Icon icon = (Icon) smallIcon;
        assertEquals("Icon height", 32, icon.getIconHeight());
        assertEquals("Icon widht", 32, icon.getIconWidth());
    } else {
        fail("Icon shall be Icon: " + smallIcon);
    }
        
    assertEquals("Right localized name", "Icon &Name Action", name);
    assertEquals("Mnemonic is N", (int)'N', mnem);
    assertNotNull("small icon present", smallIcon);

    assertEquals("once icon called", 1, myIconResourceCounter);

    
    Object base = a.getValue("iconBase"); 
    assertEquals("iconBase attribute is delegated", 2, myIconResourceCounter);
 
    assertTrue("Always enabled", a.isEnabled());
    a.setEnabled(false);
    assertTrue("Still Always enabled", a.isEnabled());

    a.actionPerformed(new ActionEvent(this, 0, "kuk"));

    assertEquals("Listener invoked", 1, myListenerCounter);
    
    
    assertEquals("No icon in menu", Boolean.TRUE, a.getValue("noIconInMenu"));

    assertContextAware(a);
}
 
Example 19
Source File: Actions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Attaches menu item to an action.
 * You can supply an alternative implementation
 * for this method by implementing method
 * {@link ButtonActionConnector#connect(JMenuItem, Action, boolean)} and
 * registering an instance of {@link ButtonActionConnector} in the
 * default lookup. 
 * <p>
 * Since version 7.1 the action can also provide properties
 * "menuText" and "popupText" if one wants to use other text on the JMenuItem
 * than the name
 * of the action taken from Action.NAME. The popupText is checked only if the
 * popup parameter is true and takes the biggest precedence. The menuText is
 * tested everytime and takes precedence over standard <code>Action.NAME</code>
 * <p>
 * By default icons are not visible in popup menus. This can be configured
 * via <a href="@[email protected]#branding-USE_MNEMONICS">branding</a>.
 * 
 * @param item menu item
 * @param action action
 * @param popup create popup or menu item
 * @since 3.29
 */
public static void connect(JMenuItem item, Action action, boolean popup) {
    for (ButtonActionConnector bac : buttonActionConnectors()) {
        if (bac.connect(item, action, popup)) {
            return;
        }
    }
    Bridge b;
    if ((item instanceof JCheckBoxMenuItem) && (action.getValue(Actions.ACTION_VALUE_TOGGLE) != null)) {
        b = new CheckMenuBridge((JCheckBoxMenuItem)item, action, popup);
    } else {
        b = new MenuBridge(item, action, popup);
    }
    b.prepare();

    if (item instanceof Actions.MenuItem) {
        ((Actions.MenuItem)item).setBridge(b);
    }
    item.putClientProperty(DynamicMenuContent.HIDE_WHEN_DISABLED, action.getValue(DynamicMenuContent.HIDE_WHEN_DISABLED));
}
 
Example 20
Source File: AbstractLinkButton.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates the HTML text which will represent the link button.
 *
 * @param action
 * @return
 */
private static String makeHTML(final Action action) {
	if (action == null) {
		throw new IllegalArgumentException("action must not be null!");
	}

	String name = (String) action.getValue(Action.NAME);
	if (name == null || name.trim().isEmpty()) {
		return "";
	}
	// if only part of the text should appear as link, <a href> tag is defined in i18n
	if (name.contains("<") || name.contains(">")) {
		return name;
	}

	URL iconUrl = null;
	if (action instanceof ResourceAction) {
		String iconName = ((ResourceAction) action).getIconName();
		if (iconName != null) {
			String regularIconPath = "icons/16/" + iconName;
			String retinaIconPath = "icons/16/@2x/" + iconName;
			boolean isRetina = SwingTools.getGUIScaling() == SwingTools.Scaling.RETINA;
			String iconLookup = isRetina ? retinaIconPath : regularIconPath;
			try {
				iconUrl = Tools.getResource(iconLookup);
				if (iconUrl == null && isRetina) {
					// fallback if @2x icon is missing on retina displays
					iconUrl = Tools.getResource(regularIconPath);
				}
			} catch (NullPointerException e) {
				// this can occur if no @2x icon exists on OS X and the security manager throws an NPE
				iconUrl = Tools.getResource(regularIconPath);
			}
		}
	}

	if (iconUrl != null) {
		return String.format(TEMPLATE_ICON_HTML, iconUrl.toString(), name);
	} else {
		if (Boolean.parseBoolean(String.valueOf(action.getValue(PROPERTY_BOLD)))) {
			return String.format(TEMPLATE_HTML_BOLD, name);
		} else {
			return String.format(TEMPLATE_HTML, name);
		}
	}
}