20 package net.sf.gridarta.gui.dialog.shortcuts;
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.awt.Dimension;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.Insets;
29 import java.util.regex.Pattern;
30 import javax.swing.Action;
31 import javax.swing.Icon;
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JDialog;
35 import javax.swing.JOptionPane;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTextArea;
39 import javax.swing.JTree;
40 import javax.swing.JViewport;
41 import javax.swing.ScrollPaneConstants;
42 import javax.swing.ToolTipManager;
43 import javax.swing.WindowConstants;
44 import javax.swing.border.LineBorder;
45 import javax.swing.border.TitledBorder;
46 import javax.swing.event.TreeSelectionEvent;
47 import javax.swing.event.TreeSelectionListener;
48 import javax.swing.tree.DefaultMutableTreeNode;
49 import javax.swing.tree.DefaultTreeCellRenderer;
50 import javax.swing.tree.DefaultTreeModel;
51 import javax.swing.tree.MutableTreeNode;
52 import javax.swing.tree.TreeCellRenderer;
53 import javax.swing.tree.TreeNode;
54 import javax.swing.tree.TreeSelectionModel;
59 import net.
sf.japi.swing.action.ActionBuilder;
60 import net.
sf.japi.swing.action.ActionBuilderFactory;
61 import net.
sf.japi.swing.action.ActionMethod;
62 import net.
sf.japi.swing.action.IconManager;
63 import org.jetbrains.annotations.NotNull;
64 import org.jetbrains.annotations.Nullable;
95 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
114 private final JButton
okButton =
new JButton(ACTION_BUILDER.createAction(
false,
"shortcutsOkay",
this));
121 private final Action
aSetShortcut = ACTION_BUILDER.createAction(
false,
"shortcutsSetShortcut",
this);
128 private final Action
aUnsetShortcut = ACTION_BUILDER.createAction(
false,
"shortcutsUnsetShortcut",
this);
147 private static final long serialVersionUID = 1L;
150 public String convertValueToText(
final Object value,
final boolean selected,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus) {
152 if (action != null) {
156 return super.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
188 okButton.setDefaultCapable(
true);
189 final JButton defaultsButton =
new JButton(ACTION_BUILDER.createAction(
false,
"shortcutsDefaults",
this));
190 setOptions(
new Object[] {
okButton, defaultsButton });
195 dialog.setResizable(
true);
196 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
197 dialog.getRootPane().setDefaultButton(okButton);
198 dialog.setModal(
true);
200 dialog.setMinimumSize(
new Dimension(400, 300));
201 dialog.setPreferredSize(
new Dimension(800, 600));
209 public void showDialog(@NotNull
final Component parentComponent) {
210 dialog.setLocationRelativeTo(parentComponent);
211 dialog.setVisible(
true);
212 setInitialValue(actionsTree);
213 actionsTree.setSelectionRow(0);
223 final DefaultMutableTreeNode top =
new DefaultMutableTreeNode(
ActionBuilderUtils.
getString(ACTION_BUILDER,
"shortcuts.allActions"));
226 actionsTree.setModel(
new DefaultTreeModel(top,
false));
227 actionsTree.setRootVisible(
false);
228 actionsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
229 final TreeSelectionListener treeSelectionListener =
new TreeSelectionListener() {
232 public void valueChanged(@NotNull
final TreeSelectionEvent e) {
237 actionsTree.addTreeSelectionListener(treeSelectionListener);
239 final Icon emptyIcon = IconManager.getDefaultIconManager().getIcon(
ActionBuilderUtils.
getString(ACTION_BUILDER,
"shortcuts.defaultIcon"));
241 final TreeCellRenderer treeCellRenderer =
new DefaultTreeCellRenderer() {
246 private static final long serialVersionUID = 1L;
255 public Component getTreeCellRendererComponent(
final JTree tree,
final Object value,
final boolean sel,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus) {
258 if (action != null) {
260 icon = tmpIcon == null ? emptyIcon : tmpIcon;
268 return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
272 public Icon getLeafIcon() {
277 actionsTree.setCellRenderer(treeCellRenderer);
279 ToolTipManager.sharedInstance().registerComponent(actionsTree);
281 final GridBagConstraints gbc =
new GridBagConstraints();
282 gbc.insets =
new Insets(2, 2, 2, 2);
284 final JScrollPane actionsScrollPane =
new JScrollPane();
285 actionsScrollPane.setViewportView(actionsTree);
286 actionsScrollPane.setBackground(actionsTree.getBackground());
287 actionsScrollPane.getViewport().add(actionsTree);
288 actionsScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
289 actionsScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
290 actionsScrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
291 final JComponent actionsPanel =
new JPanel(
new GridBagLayout());
295 gbc.fill = GridBagConstraints.BOTH;
298 actionsPanel.add(actionsScrollPane, gbc);
300 actionDescription.setEditable(
false);
301 actionDescription.setColumns(20);
302 actionDescription.setLineWrap(
true);
303 actionDescription.setWrapStyleWord(
true);
304 actionDescription.setBorder(
new LineBorder(Color.black));
305 actionDescription.setFocusable(
false);
307 actionShortcut.setEditable(
false);
308 actionShortcut.setColumns(20);
309 actionShortcut.setRows(3);
310 actionShortcut.setLineWrap(
true);
311 actionShortcut.setWrapStyleWord(
true);
312 actionShortcut.setBorder(
new LineBorder(Color.black));
313 actionShortcut.setFocusable(
false);
315 final Container actionDescriptionPanel =
new JPanel(
new GridBagLayout());
318 gbc.fill = GridBagConstraints.HORIZONTAL;
323 gbc.fill = GridBagConstraints.BOTH;
325 actionDescriptionPanel.add(actionDescription, gbc);
327 final Container actionShortcutPanel =
new JPanel(
new GridBagLayout());
330 gbc.fill = GridBagConstraints.HORIZONTAL;
335 actionShortcutPanel.add(actionShortcut, gbc);
337 final Component addShortcutButton =
new JButton(aSetShortcut);
338 final Component removeShortcutButton =
new JButton(aUnsetShortcut);
339 final Container actionButtonsPanel =
new JPanel();
340 actionButtonsPanel.setLayout(
new GridBagLayout());
343 gbc.fill = GridBagConstraints.HORIZONTAL;
346 actionButtonsPanel.add(addShortcutButton, gbc);
348 actionButtonsPanel.add(removeShortcutButton, gbc);
350 final JComponent selectedActionPanel =
new JPanel();
351 selectedActionPanel.setLayout(
new GridBagLayout());
355 gbc.fill = GridBagConstraints.BOTH;
358 selectedActionPanel.add(actionDescriptionPanel, gbc);
359 gbc.fill = GridBagConstraints.HORIZONTAL;
362 selectedActionPanel.add(actionShortcutPanel, gbc);
364 selectedActionPanel.add(actionButtonsPanel, gbc);
366 final JPanel panel =
new JPanel(
new GridBagLayout());
369 gbc.fill = GridBagConstraints.BOTH;
372 panel.add(actionsPanel, gbc);
375 panel.add(selectedActionPanel, gbc);
394 if (ACTION_BUILDER.showQuestionDialog(dialog,
"shortcutsRestoreDefaults")) {
402 public void setValue(@Nullable
final Object newValue) {
403 super.setValue(newValue);
404 if (newValue != UNINITIALIZED_VALUE) {
413 private void createNodes(@NotNull
final DefaultMutableTreeNode root) {
414 for (
final Action action : shortcutsManager) {
416 for (
final String category : PATTERN_CATEGORIES.split(categories, -1)) {
417 addNode(root, category, action);
428 private static void addNode(@NotNull
final DefaultMutableTreeNode root, @NotNull
final CharSequence category, @NotNull
final Action action) {
430 final MutableTreeNode treeNode =
new DefaultMutableTreeNode(action,
false);
441 private static DefaultMutableTreeNode
getOrCreateNodeForCategory(@NotNull
final DefaultMutableTreeNode root, @NotNull
final CharSequence category) {
442 DefaultMutableTreeNode node = root;
443 for (
final String subCategory : PATTERN_SUB_CATEGORIES.split(category, -1)) {
456 private static DefaultMutableTreeNode
getOrCreateChildNode(@NotNull
final MutableTreeNode root, @NotNull
final String subCategory) {
457 final int childCount = root.getChildCount();
458 for (
int i = 0; i < childCount; i++) {
459 final TreeNode treeNode = root.getChildAt(i);
460 final String nodeTitle =
getTitle(treeNode);
461 if (nodeTitle != null && nodeTitle.equals(CATEGORY_PREFIX + subCategory)) {
462 assert treeNode instanceof DefaultMutableTreeNode;
463 return (DefaultMutableTreeNode) treeNode;
467 final DefaultMutableTreeNode treeNode =
new DefaultMutableTreeNode(subCategory);
477 private static void insertChildNode(@NotNull
final MutableTreeNode branchNode, @NotNull
final MutableTreeNode childNode) {
487 private static int getInsertionIndex(@NotNull
final TreeNode parentNode, @NotNull
final TreeNode childNode) {
488 final String childTitle =
getTitle(childNode);
489 if (childTitle == null) {
490 throw new IllegalArgumentException();
493 final int childCount = parentNode.getChildCount();
494 for (
int i = 0; i < childCount; i++) {
495 final TreeNode treeNode = parentNode.getChildAt(i);
496 final String nodeTitle =
getTitle(treeNode);
497 if (nodeTitle != null && nodeTitle.compareToIgnoreCase(childTitle) > 0) {
511 private static Action
getAction(@Nullable
final Object node) {
512 if (node == null || !(node instanceof DefaultMutableTreeNode)) {
516 final DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) node;
517 final Object userObject = defaultMutableTreeNode.getUserObject();
518 if (userObject == null || !(userObject instanceof Action)) {
522 return (Action) userObject;
531 private static String
getTitle(@NotNull
final TreeNode node) {
532 if (!(node instanceof DefaultMutableTreeNode)) {
536 final DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) node;
537 final Object userObject = defaultMutableTreeNode.getUserObject();
538 if (userObject == null) {
542 if (userObject instanceof String) {
543 return CATEGORY_PREFIX + userObject;
546 if (userObject instanceof Action) {
558 if (this.selectedAction == selectedAction) {
571 if (selectedAction == null) {
572 actionShortcut.setText(
"");
576 aSetShortcut.setEnabled(selectedAction != null);
596 if (action == null) {
614 if (action != null) {
KeyStroke getKeyStroke()
Returns the currently shown key stroke.
Utility class for string manipulation.
Graphical User Interface of Gridarta.
static String getShortcutDescription(@NotNull final Action action, @NotNull final String key)
Returns a description of the shortcut of an Action.
Action selectedAction
The selected Action or.
void revertAll()
Reverts all shortcuts to their default values.
void shortcutsOkay()
Action method for okay.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
boolean showDialog(@NotNull final Component parentComponent)
Opens the dialog.
void shortcutsDefaults()
Action method for restore to defaults.
static String getActionCategory(@NotNull final Action action)
Returns an Action's category.
static String getTitle(@NotNull final TreeNode node)
Returns the category for a node in the action tree.
final Action aUnsetShortcut
The Action for the "unset shortcut" button.
Utility class implementing Action related functions.
ShortcutsDialog(@NotNull final Component parentComponent, @NotNull final ShortcutsManager shortcutsManager)
Creates a new instance.
void saveShortcuts()
Saves all shortcuts to the preferences.
final JTextArea actionShortcut
The shortcut of the selected action.
static void addNode(@NotNull final DefaultMutableTreeNode root, @NotNull final CharSequence category, @NotNull final Action action)
Adds an Action to a branch node.
static final String ACTION_PREFIX
Prefix for internal action names.
static int getInsertionIndex(@NotNull final TreeNode parentNode, @NotNull final TreeNode childNode)
Returns the index to insert a new child node into a parent node.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
void shortcutsUnsetShortcut()
The action method for the "set shortcut" button.
Base package of all Gridarta classes.
final JTextArea actionDescription
The description of the selected action.
A dialog that asks for a KeyStroke.
final JButton okButton
The JButton for ok.
void createNodes(@NotNull final DefaultMutableTreeNode root)
Creates nodes for all actions.
static DefaultMutableTreeNode getOrCreateChildNode(@NotNull final MutableTreeNode root, @NotNull final String subCategory)
Returns a child node by category name.
JPanel createPanel()
Creates the GUI.
static Icon getActionIcon(@NotNull final Action action)
Returns an Icon associated with the action.
static final Pattern PATTERN_CATEGORIES
The Pattern to split a list of action categories.
Manager for shortcuts of all Actions in an ActionBuilder instance.
final JDialog dialog
The JDialog instance.
Action getUnsetShortcutEnabled()
Returns whether "unset shortcut" is enabled.
static DefaultMutableTreeNode getOrCreateNodeForCategory(@NotNull final DefaultMutableTreeNode root, @NotNull final CharSequence category)
Returns the branch DefaultMutableTreeNode for a given category.
static final Pattern PATTERN_COMMA
The pattern that matches a single comma (",").
static KeyStroke getShortcut(@NotNull final Action action)
Returns the shortcut of an Action.
static final String CATEGORY_PREFIX
Prefix for internal category names.
Utility class for ActionBuilder related functions.
static void setActionShortcut(@NotNull final Action action, @Nullable final KeyStroke shortcut)
Sets the shortcut of an Action.
void setValue(@Nullable final Object newValue)
final Action aSetShortcut
The Action for the "set shortcut" button.
static String getActionDescription(@NotNull final Action action)
Returns the description for an Action.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
final ShortcutsManager shortcutsManager
The ShortcutsManager to affect.
final JTree actionsTree
The JTree showing all actions.
static final long serialVersionUID
The serial Version UID.
static String getActionName(@NotNull final Action action)
Returns the name of an Action.
void updateSelectedAction()
Updates the information shown for the selected action.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
static final Pattern PATTERN_SUB_CATEGORIES
The Pattern to split a category into sub-categories.
static Action getAction(@Nullable final Object node)
Returns the Action for a node in the action tree.
void setSelectedAction(@Nullable final Action selectedAction)
Updates the selected action.
Defines common UI constants used in different dialogs.
void shortcutsSetShortcut()
The action method for the "set shortcut" button.
void showDialog(@NotNull final Component parentComponent)
Opens the dialog.
static void insertChildNode(@NotNull final MutableTreeNode branchNode, @NotNull final MutableTreeNode childNode)
Inserts a new child node into a branch node.
static final Pattern PATTERN_SLASH
The pattern that matches a single slash ("/").