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");
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);
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);
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);
226 actionsTree.setModel(
new DefaultTreeModel(top,
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);
241 final TreeCellRenderer treeCellRenderer =
new DefaultTreeCellRenderer() {
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) {
262 icon = tmpIcon ==
null ? emptyIcon : tmpIcon;
268 return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
272 public Icon getLeafIcon() {
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();
286 actionsScrollPane.setBackground(
actionsTree.getBackground());
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);
315 final Container actionDescriptionPanel =
new JPanel(
new GridBagLayout());
318 gbc.fill = GridBagConstraints.HORIZONTAL;
323 gbc.fill = GridBagConstraints.BOTH;
327 final Container actionShortcutPanel =
new JPanel(
new GridBagLayout());
330 gbc.fill = GridBagConstraints.HORIZONTAL;
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);
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) {
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;
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(
"invalid child node: " + childNode);
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 instanceof DefaultMutableTreeNode)) {
516 final DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) node;
517 final Object userObject = defaultMutableTreeNode.getUserObject();
518 if (!(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) {
546 if (userObject instanceof Action) {
592 if (action ==
null) {
610 if (action !=
null) {