20 package net.sf.gridarta.utils;
22 import javax.swing.Action;
23 import javax.swing.Icon;
24 import javax.swing.KeyStroke;
25 import net.
sf.japi.swing.action.ActionBuilder;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
39 private static final String
CATEGORY =
"Category";
74 if (name != null && !name.isEmpty()) {
78 final String shortDescription =
getStringValue(action, Action.SHORT_DESCRIPTION);
79 if (shortDescription != null) {
80 return shortDescription;
94 public static String
getActionId(@NotNull
final Action action) {
97 throw new IllegalArgumentException();
109 final String longDescription =
getStringValue(action, Action.LONG_DESCRIPTION);
110 if (longDescription != null) {
111 return longDescription;
114 final String shortDescription =
getStringValue(action, Action.SHORT_DESCRIPTION);
115 if (shortDescription != null) {
116 return shortDescription;
128 public static KeyStroke
getShortcut(@NotNull
final Action action) {
129 return getShortcut(action, Action.ACCELERATOR_KEY);
139 return getShortcut(action, ActionBuilder.ACCELERATOR_KEY_2);
149 private static KeyStroke
getShortcut(@NotNull
final Action action, @NotNull
final String key) {
150 final Object acceleratorKey = action.getValue(key);
151 return acceleratorKey instanceof KeyStroke ? (KeyStroke) acceleratorKey : null;
159 public static void setActionShortcut(@NotNull
final Action action, @Nullable
final KeyStroke shortcut) {
160 action.putValue(Action.ACCELERATOR_KEY, shortcut);
172 final KeyStroke shortcut =
getShortcut(action, key);
173 return shortcut == null ?
NO_SHORTCUT : shortcut.toString();
183 final Object icon = action.getValue(Action.SMALL_ICON);
184 return icon instanceof Icon ? (Icon) icon : null;
195 private static String
getStringValue(@NotNull
final Action action, @NotNull
final String key) {
196 final Object value = action.getValue(key);
197 return value != null && value instanceof String ? (String) value : null;
212 public static Action
newAction(@NotNull
final ActionBuilder actionBuilder, @NotNull
final String category, @NotNull
final EditorAction editorAction, @NotNull
final String key) {
213 final Action action = actionBuilder.createAction(
true, key, editorAction);
214 action.putValue(CATEGORY, category);
215 editorAction.setAction(action, key);
230 public static Action
newToggleAction(@NotNull
final ActionBuilder actionBuilder, @NotNull
final String category, @NotNull
final EditorAction editorAction, @NotNull
final String key) {
231 final Action action = actionBuilder.createToggle(
true, key, editorAction);
232 action.putValue(CATEGORY, category);
233 editorAction.setAction(action, key);
static KeyStroke getAlternativeShortcut(@NotNull final Action action)
Returns the alternative shortcut of an Action.
static String getShortcutDescription(@NotNull final Action action, @NotNull final String key)
Returns a description of the shortcut of an Action.
static String getActionCategory(@NotNull final Action action)
Returns an Action's category.
Utility class implementing Action related functions.
ActionUtils()
Private construct to prevent instantiation.
static final String CATEGORY
Action key for the action's category.
static Icon getActionIcon(@NotNull final Action action)
Returns an Icon associated with the action.
static String getActionId(@NotNull final Action action)
Returns an Action's ID string.
static final String UNDEFINED_CATEGORY
Category value for Actions not defining a CATEGORY.
static String getStringValue(@NotNull final Action action, @NotNull final String key)
Returns an Action's value as a string.
static KeyStroke getShortcut(@NotNull final Action action)
Returns the shortcut of an Action.
static final String DEFAULT_ACCELERATOR_KEY
Action key to store the default shortcut.
static void setActionShortcut(@NotNull final Action action, @Nullable final KeyStroke shortcut)
Sets the shortcut of an Action.
static KeyStroke getShortcut(@NotNull final Action action, @NotNull final String key)
Returns the shortcut of an Action.
static String getActionDescription(@NotNull final Action action)
Returns the description for an Action.
static String getActionName(@NotNull final Action action)
Returns the name of an Action.
static final String NO_SHORTCUT
The shortcut description for actions without shortcuts.
static Action newToggleAction(@NotNull final ActionBuilder actionBuilder, @NotNull final String category, @NotNull final EditorAction editorAction, @NotNull final String key)
Creates a new Action instance.
static Action newAction(@NotNull final ActionBuilder actionBuilder, @NotNull final String category, @NotNull final EditorAction editorAction, @NotNull final String key)
Creates a new Action instance.