Gridarta Editor
ActionUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
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;
28 
33 public class ActionUtils {
34 
38  @NotNull
39  private static final String CATEGORY = "Category";
40 
45  @NotNull
46  private static final String UNDEFINED_CATEGORY = "Other";
47 
51  @NotNull
52  public static final String NO_SHORTCUT = "none";
53 
57  @NotNull
58  public static final String DEFAULT_ACCELERATOR_KEY = "DefaultAcceleratorKey";
59 
63  private ActionUtils() {
64  }
65 
71  @NotNull
72  public static String getActionName(@NotNull final Action action) {
73  final String name = getStringValue(action, Action.NAME);
74  if (name != null && !name.isEmpty()) {
75  return name;
76  }
77 
78  final String shortDescription = getStringValue(action, Action.SHORT_DESCRIPTION);
79  if (shortDescription != null) {
80  return shortDescription;
81  }
82 
83  return getActionId(action);
84  }
85 
93  @NotNull
94  public static String getActionId(@NotNull final Action action) {
95  final String id = getStringValue(action, ActionBuilder.ACTION_ID);
96  if (id == null) {
97  throw new IllegalArgumentException();
98  }
99  return id;
100  }
101 
107  @NotNull
108  public static String getActionDescription(@NotNull final Action action) {
109  final String longDescription = getStringValue(action, Action.LONG_DESCRIPTION);
110  if (longDescription != null) {
111  return longDescription;
112  }
113 
114  final String shortDescription = getStringValue(action, Action.SHORT_DESCRIPTION);
115  if (shortDescription != null) {
116  return shortDescription;
117  }
118 
119  return "";
120  }
121 
127  @Nullable
128  public static KeyStroke getShortcut(@NotNull final Action action) {
129  return getShortcut(action, Action.ACCELERATOR_KEY);
130  }
131 
137  @Nullable
138  public static KeyStroke getAlternativeShortcut(@NotNull final Action action) {
139  return getShortcut(action, ActionBuilder.ACCELERATOR_KEY_2);
140  }
141 
148  @Nullable
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;
152  }
153 
159  public static void setActionShortcut(@NotNull final Action action, @Nullable final KeyStroke shortcut) {
160  action.putValue(Action.ACCELERATOR_KEY, shortcut);
161  }
162 
170  @NotNull
171  public static String getShortcutDescription(@NotNull final Action action, @NotNull final String key) {
172  final KeyStroke shortcut = getShortcut(action, key);
173  return shortcut == null ? NO_SHORTCUT : shortcut.toString();
174  }
175 
181  @Nullable
182  public static Icon getActionIcon(@NotNull final Action action) {
183  final Object icon = action.getValue(Action.SMALL_ICON);
184  return icon instanceof Icon ? (Icon) icon : null;
185  }
186 
194  @Nullable
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;
198 
199  }
200 
211  @NotNull
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);
216  return action;
217  }
218 
229  @NotNull
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);
234  return action;
235  }
236 
242  @NotNull
243  public static String getActionCategory(@NotNull final Action action) {
244  final String category = getStringValue(action, CATEGORY);
245  return category == null ? UNDEFINED_CATEGORY : category;
246  }
247 
248 }
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.
A global editor action.
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.