1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.shortcut;
21
22 import java.awt.BorderLayout;
23 import java.util.List;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.KeyStroke;
27 import javax.swing.ListSelectionModel;
28 import javax.swing.event.ChangeEvent;
29 import javax.swing.event.ChangeListener;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
32 import org.xnap.commons.gui.ColoredTable;
33 import org.xnap.commons.gui.table.AbstractSimpleTableModel;
34 import org.xnap.commons.gui.table.TableLayoutManager;
35 import org.xnap.commons.gui.table.TableSorter;
36 import org.xnap.commons.gui.util.GUIHelper;
37 import org.xnap.commons.i18n.I18n;
38 import org.xnap.commons.i18n.I18nFactory;
39
40 /***
41 * Provides controls to manage {@link org.xnap.commons.gui.shortcut.Shortcut}
42 * objects.
43 *
44 * @author Steffen Pingel
45 */
46 public class ShortcutPanel extends JPanel {
47
48 private static I18n i18n = I18nFactory.getI18n(ShortcutPanel.class);
49
50 private KeyStrokePanel keyStrokePanel;
51 private ColoredTable shortcutTable;
52 private TableLayoutManager shortcutTableLayoutManager;
53 private ShortcutTableModel shortcutTableModel;
54 private ShortcutProxy selectedProxy;
55
56 public ShortcutPanel()
57 {
58 setLayout(new BorderLayout());
59
60 shortcutTableModel = new ShortcutTableModel();
61 TableSorter sorter = new TableSorter(shortcutTableModel);
62 shortcutTable = new ColoredTable(sorter);
63 shortcutTableLayoutManager = new TableLayoutManager(shortcutTable);
64 shortcutTableLayoutManager.addColumnProperties("description", i18n.tr("Action"), 80, true);
65 shortcutTableLayoutManager.addColumnProperties("category", i18n.tr("Category"), 60, true);
66 shortcutTableLayoutManager.addColumnProperties("shortcut", i18n.tr("Shortcut"), 40, true);
67 shortcutTableLayoutManager.getTableLayout().setMaintainSortOrder(true);
68 shortcutTableLayoutManager.initializeTableLayout();
69 shortcutTable.setIntercellSpacing(new java.awt.Dimension(2, 1));
70 shortcutTable.setShowVerticalLines(true);
71 shortcutTable.setShowHorizontalLines(false);
72 shortcutTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
73 shortcutTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
74 public void valueChanged(ListSelectionEvent event)
75 {
76 applySelectedKeyStroke();
77
78 selectedProxy = getSelectedShortcut();
79 if (selectedProxy != null) {
80 keyStrokePanel.setKeyStroke(selectedProxy.getKeyStroke());
81 keyStrokePanel.setDefaultKeyStroke(selectedProxy.getDefaultKeyStroke());
82 keyStrokePanel.setEnabled(true);
83 }
84 else {
85 keyStrokePanel.setKeyStroke(null);
86 keyStrokePanel.setDefaultKeyStroke(null);
87 keyStrokePanel.setEnabled(false);
88 }
89 }
90 });
91
92 add(new JScrollPane(shortcutTable), BorderLayout.CENTER);
93
94 keyStrokePanel = new KeyStrokePanel();
95 keyStrokePanel.setBorder(GUIHelper.createTitledBorder(i18n.tr("Shortcut for Selected Action"), 5));
96 keyStrokePanel.addChangeListener(new ChangeListener() {
97 public void stateChanged(ChangeEvent e)
98 {
99 applySelectedKeyStroke();
100 }
101 });
102
103 keyStrokePanel.setEnabled(false);
104 add(keyStrokePanel, BorderLayout.SOUTH);
105 }
106
107 public void add(Shortcut shortcut, KeyStroke defaultKeyStroke)
108 {
109 shortcutTableModel.add(new ShortcutProxy(shortcut, defaultKeyStroke));
110 }
111
112 public void apply()
113 {
114 applySelectedKeyStroke();
115
116 List<ShortcutProxy> data = shortcutTableModel.getData();
117 for (ShortcutProxy proxy : data) {
118 proxy.getShortcut().setKeyStroke(proxy.getKeyStroke());
119 }
120 }
121
122 public void applySelectedKeyStroke()
123 {
124 if (selectedProxy != null) {
125 selectedProxy.setKeyStroke(keyStrokePanel.getKeyStroke());
126 shortcutTableModel.changed(selectedProxy);
127 }
128 }
129
130 protected int getSelectedRow()
131 {
132 int row = shortcutTable.getSelectedRow();
133 return (row == -1) ? -1 : ((TableSorter)shortcutTable.getModel()).mapToIndex(row);
134 }
135
136 protected ShortcutProxy getSelectedShortcut()
137 {
138 int row = getSelectedRow();
139 return (row == -1) ? null : shortcutTableModel.getItem(row);
140 }
141
142 public String getTitle()
143 {
144 return i18n.tr("Shortcuts");
145 }
146
147 private class ShortcutProxy {
148
149 private KeyStroke defaultKeyStroke;
150 private KeyStroke keyStroke;
151 private Shortcut shortcut;
152
153 public ShortcutProxy(Shortcut shortcut, KeyStroke defaultKeyStroke)
154 {
155 this.shortcut = shortcut;
156 this.keyStroke = shortcut.getKeyStroke();
157 this.defaultKeyStroke = defaultKeyStroke;
158 }
159
160 public KeyStroke getDefaultKeyStroke()
161 {
162 return defaultKeyStroke;
163 }
164
165 public KeyStroke getKeyStroke()
166 {
167 return keyStroke;
168 }
169
170 public Shortcut getShortcut()
171 {
172 return shortcut;
173 }
174
175 public void setKeyStroke(KeyStroke keyStroke)
176 {
177 this.keyStroke = keyStroke;
178 }
179
180 }
181
182 private class ShortcutTableModel extends AbstractSimpleTableModel<ShortcutProxy> {
183
184 public ShortcutTableModel()
185 {
186 super(new Class[] {
187 String.class,
188 String.class,
189 String.class,
190 });
191 }
192
193 public void changed(ShortcutProxy selectedProxy)
194 {
195 int i = getData().indexOf(selectedProxy);
196 if (i != -1) {
197 fireTableRowsUpdated(i, i);
198 }
199 }
200
201 @Override
202 public Object getValueAt(int row, int column)
203 {
204 ShortcutProxy proxy = getItem(row);
205 switch (column) {
206 case 0:
207 return proxy.getShortcut().getValue(Shortcut.DESCRIPTION);
208 case 1:
209 return proxy.getShortcut().getValue(Shortcut.CATEGORY);
210 case 2:
211 return (proxy.getKeyStroke() != null)
212 ? KeyStrokePanel.toString(proxy.getKeyStroke())
213 : null;
214 default:
215 return null;
216 }
217 }
218
219 }
220
221 }