1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Container;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.GridLayout;
|
---|
11 | import java.awt.LayoutManager;
|
---|
12 | import java.awt.Rectangle;
|
---|
13 | import java.awt.datatransfer.DataFlavor;
|
---|
14 | import java.awt.datatransfer.Transferable;
|
---|
15 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
16 | import java.awt.event.ActionEvent;
|
---|
17 | import java.awt.event.ActionListener;
|
---|
18 | import java.awt.event.InputEvent;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.Arrays;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.Collections;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.LinkedList;
|
---|
26 | import java.util.List;
|
---|
27 | import java.util.Map;
|
---|
28 |
|
---|
29 | import javax.swing.AbstractAction;
|
---|
30 | import javax.swing.Action;
|
---|
31 | import javax.swing.DefaultListCellRenderer;
|
---|
32 | import javax.swing.DefaultListModel;
|
---|
33 | import javax.swing.Icon;
|
---|
34 | import javax.swing.ImageIcon;
|
---|
35 | import javax.swing.JButton;
|
---|
36 | import javax.swing.JComponent;
|
---|
37 | import javax.swing.JLabel;
|
---|
38 | import javax.swing.JList;
|
---|
39 | import javax.swing.JMenuItem;
|
---|
40 | import javax.swing.JPanel;
|
---|
41 | import javax.swing.JPopupMenu;
|
---|
42 | import javax.swing.JScrollPane;
|
---|
43 | import javax.swing.JTable;
|
---|
44 | import javax.swing.JToolBar;
|
---|
45 | import javax.swing.JTree;
|
---|
46 | import javax.swing.ListCellRenderer;
|
---|
47 | import javax.swing.MenuElement;
|
---|
48 | import javax.swing.TransferHandler;
|
---|
49 | import javax.swing.event.ListSelectionEvent;
|
---|
50 | import javax.swing.event.ListSelectionListener;
|
---|
51 | import javax.swing.event.TreeSelectionEvent;
|
---|
52 | import javax.swing.event.TreeSelectionListener;
|
---|
53 | import javax.swing.table.AbstractTableModel;
|
---|
54 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
55 | import javax.swing.tree.DefaultTreeCellRenderer;
|
---|
56 | import javax.swing.tree.DefaultTreeModel;
|
---|
57 | import javax.swing.tree.TreePath;
|
---|
58 |
|
---|
59 | import org.openstreetmap.josm.Main;
|
---|
60 | import org.openstreetmap.josm.actions.ActionParameter;
|
---|
61 | import org.openstreetmap.josm.actions.AdaptableAction;
|
---|
62 | import org.openstreetmap.josm.actions.ParameterizedAction;
|
---|
63 | import org.openstreetmap.josm.actions.ParameterizedActionDecorator;
|
---|
64 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
65 | import org.openstreetmap.josm.tools.GBC;
|
---|
66 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
67 |
|
---|
68 | public class ToolbarPreferences implements PreferenceSettingFactory {
|
---|
69 |
|
---|
70 |
|
---|
71 | private static final String EMPTY_TOOLBAR_MARKER = "<!-empty-!>";
|
---|
72 |
|
---|
73 | public static class ActionDefinition {
|
---|
74 | private final Action action;
|
---|
75 | private String name = "";
|
---|
76 | private String icon = "";
|
---|
77 | private final Map<String, Object> parameters = new HashMap<String, Object>();
|
---|
78 |
|
---|
79 | public ActionDefinition(Action action) {
|
---|
80 | this.action = action;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Map<String, Object> getParameters() {
|
---|
84 | return parameters;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public Action getParametrizedAction() {
|
---|
88 | if (getAction() instanceof ParameterizedAction)
|
---|
89 | return new ParameterizedActionDecorator((ParameterizedAction) getAction(), parameters);
|
---|
90 | else
|
---|
91 | return getAction();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public Action getAction() {
|
---|
95 | return action;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public String getName() {
|
---|
99 | return name;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void setName(String name) {
|
---|
103 | this.name = name;
|
---|
104 | action.putValue(AbstractAction.SHORT_DESCRIPTION, Main.platform.makeTooltip(name, null));
|
---|
105 | action.putValue(AbstractAction.NAME, name);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public String getIcon() {
|
---|
109 | return icon;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void setIcon(String icon) {
|
---|
113 | this.icon = icon;
|
---|
114 | ImageIcon ico = ImageProvider.getIfAvailable("", icon);
|
---|
115 | if(ico != null)
|
---|
116 | action.putValue(AbstractAction.SMALL_ICON, ico);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public boolean isSeparator() {
|
---|
120 | return action == null;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public static ActionDefinition getSeparator() {
|
---|
124 | return new ActionDefinition(null);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public static class ActionParser {
|
---|
129 | private final Map<String, Action> actions;
|
---|
130 | private final StringBuilder result = new StringBuilder();
|
---|
131 | private int index;
|
---|
132 | private char[] s;
|
---|
133 |
|
---|
134 | public ActionParser(Map<String, Action> actions) {
|
---|
135 | this.actions = actions;
|
---|
136 | }
|
---|
137 |
|
---|
138 | private String readTillChar(char ch1, char ch2) {
|
---|
139 | result.setLength(0);
|
---|
140 | while (index < s.length && s[index] != ch1 && s[index] != ch2) {
|
---|
141 | if (s[index] == '\\') {
|
---|
142 | index++;
|
---|
143 | if (index >= s.length) {
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | result.append(s[index]);
|
---|
148 | index++;
|
---|
149 | }
|
---|
150 | return result.toString();
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void skip(char ch) {
|
---|
154 | if (index < s.length && s[index] == ch) {
|
---|
155 | index++;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public ActionDefinition loadAction(String actionName) {
|
---|
160 | index = 0;
|
---|
161 | this.s = actionName.toCharArray();
|
---|
162 |
|
---|
163 | String name = readTillChar('(', '{');
|
---|
164 | Action action = actions.get(name);
|
---|
165 |
|
---|
166 | if (action == null)
|
---|
167 | return null;
|
---|
168 |
|
---|
169 | ActionDefinition result = new ActionDefinition(action);
|
---|
170 |
|
---|
171 | if (action instanceof ParameterizedAction) {
|
---|
172 | skip('(');
|
---|
173 |
|
---|
174 | ParameterizedAction parametrizedAction = (ParameterizedAction)action;
|
---|
175 | Map<String, ActionParameter<?>> actionParams = new HashMap<String, ActionParameter<?>>();
|
---|
176 | for (ActionParameter<?> param: parametrizedAction.getActionParameters()) {
|
---|
177 | actionParams.put(param.getName(), param);
|
---|
178 | }
|
---|
179 |
|
---|
180 | while (index < s.length && s[index] != ')') {
|
---|
181 | String paramName = readTillChar('=', '=');
|
---|
182 | skip('=');
|
---|
183 | String paramValue = readTillChar(',',')');
|
---|
184 | if (paramName.length() > 0) {
|
---|
185 | ActionParameter<?> actionParam = actionParams.get(paramName);
|
---|
186 | if (actionParam != null) {
|
---|
187 | result.getParameters().put(paramName, actionParam.readFromString(paramValue));
|
---|
188 | }
|
---|
189 | }
|
---|
190 | skip(',');
|
---|
191 | }
|
---|
192 | skip(')');
|
---|
193 | }
|
---|
194 | if (action instanceof AdaptableAction) {
|
---|
195 | skip('{');
|
---|
196 |
|
---|
197 | while (index < s.length && s[index] != '}') {
|
---|
198 | String paramName = readTillChar('=', '=');
|
---|
199 | skip('=');
|
---|
200 | String paramValue = readTillChar(',','}');
|
---|
201 | if ("icon".equals(paramName) && paramValue.length() > 0)
|
---|
202 | result.setIcon(paramValue);
|
---|
203 | else if("name".equals(paramName) && paramValue.length() > 0)
|
---|
204 | result.setName(paramValue);
|
---|
205 | skip(',');
|
---|
206 | }
|
---|
207 | skip('}');
|
---|
208 | }
|
---|
209 |
|
---|
210 | return result;
|
---|
211 | }
|
---|
212 |
|
---|
213 | private void escape(String s) {
|
---|
214 | for (int i=0; i<s.length(); i++) {
|
---|
215 | char ch = s.charAt(i);
|
---|
216 | if (ch == '\\' || ch == '(' || ch == '{' || ch == ',' || ch == ')' || ch == '}' || ch == '=') {
|
---|
217 | result.append('\\');
|
---|
218 | result.append(ch);
|
---|
219 | } else {
|
---|
220 | result.append(ch);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | @SuppressWarnings("unchecked")
|
---|
226 | public String saveAction(ActionDefinition action) {
|
---|
227 | result.setLength(0);
|
---|
228 |
|
---|
229 | escape((String) action.getAction().getValue("toolbar"));
|
---|
230 | if (action.getAction() instanceof ParameterizedAction) {
|
---|
231 | result.append('(');
|
---|
232 | List<ActionParameter<?>> params = ((ParameterizedAction)action.getAction()).getActionParameters();
|
---|
233 | for (int i=0; i<params.size(); i++) {
|
---|
234 | ActionParameter<Object> param = (ActionParameter<Object>)params.get(i);
|
---|
235 | escape(param.getName());
|
---|
236 | result.append('=');
|
---|
237 | Object value = action.getParameters().get(param.getName());
|
---|
238 | if (value != null) {
|
---|
239 | escape(param.writeToString(value));
|
---|
240 | }
|
---|
241 | if (i < params.size() - 1) {
|
---|
242 | result.append(',');
|
---|
243 | } else {
|
---|
244 | result.append(')');
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 | if (action.getAction() instanceof AdaptableAction) {
|
---|
249 | boolean first = true;
|
---|
250 | String tmp = action.getName();
|
---|
251 | if(tmp.length() != 0) {
|
---|
252 | result.append(first ? "{" : ",");
|
---|
253 | result.append("name=");
|
---|
254 | escape(tmp);
|
---|
255 | first = false;
|
---|
256 | }
|
---|
257 | tmp = action.getIcon();
|
---|
258 | if(tmp.length() != 0) {
|
---|
259 | result.append(first ? "{" : ",");
|
---|
260 | result.append("icon=");
|
---|
261 | escape(tmp);
|
---|
262 | first = false;
|
---|
263 | }
|
---|
264 | if(!first)
|
---|
265 | result.append('}');
|
---|
266 | }
|
---|
267 |
|
---|
268 | return result.toString();
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | private static class ActionParametersTableModel extends AbstractTableModel {
|
---|
273 |
|
---|
274 | private ActionDefinition currentAction = ActionDefinition.getSeparator();
|
---|
275 |
|
---|
276 | public int getColumnCount() {
|
---|
277 | return 2;
|
---|
278 | }
|
---|
279 |
|
---|
280 | public int getRowCount() {
|
---|
281 | if (currentAction.isSeparator() || !(currentAction.getAction() instanceof ParameterizedAction))
|
---|
282 | return 0;
|
---|
283 | ParameterizedAction pa = (ParameterizedAction)currentAction.getAction();
|
---|
284 | return pa.getActionParameters().size()
|
---|
285 | + ((currentAction.getAction() instanceof ParameterizedAction) ? 2 : 0);
|
---|
286 | }
|
---|
287 |
|
---|
288 | @SuppressWarnings("unchecked")
|
---|
289 | private ActionParameter<Object> getParam(int index) {
|
---|
290 | ParameterizedAction pa = (ParameterizedAction)currentAction.getAction();
|
---|
291 | return (ActionParameter<Object>) pa.getActionParameters().get(index);
|
---|
292 | }
|
---|
293 |
|
---|
294 | public Object getValueAt(int rowIndex, int columnIndex) {
|
---|
295 | if(currentAction.getAction() instanceof ParameterizedAction)
|
---|
296 | {
|
---|
297 | if (rowIndex < 2) {
|
---|
298 | switch (columnIndex) {
|
---|
299 | case 0:
|
---|
300 | return rowIndex == 0 ? tr("Tooltip") : tr("Icon");
|
---|
301 | case 1:
|
---|
302 | return rowIndex == 0 ? currentAction.getName() : currentAction.getIcon();
|
---|
303 | default:
|
---|
304 | return null;
|
---|
305 | }
|
---|
306 | } else
|
---|
307 | rowIndex -= 2;
|
---|
308 | }
|
---|
309 | ActionParameter<Object> param = getParam(rowIndex);
|
---|
310 | switch (columnIndex) {
|
---|
311 | case 0:
|
---|
312 | return param.getName();
|
---|
313 | case 1:
|
---|
314 | return param.writeToString(currentAction.getParameters().get(param.getName()));
|
---|
315 | default:
|
---|
316 | return null;
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | @Override
|
---|
321 | public boolean isCellEditable(int row, int column) {
|
---|
322 | return column == 1;
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Override
|
---|
326 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
327 | if(currentAction.getAction() instanceof ParameterizedAction)
|
---|
328 | {
|
---|
329 | if (rowIndex == 0) {
|
---|
330 | currentAction.setName((String)aValue);
|
---|
331 | return;
|
---|
332 | } else if (rowIndex == 1) {
|
---|
333 | currentAction.setIcon((String)aValue);
|
---|
334 | return;
|
---|
335 | } else
|
---|
336 | rowIndex -= 2;
|
---|
337 | }
|
---|
338 | ActionParameter<Object> param = getParam(rowIndex);
|
---|
339 | currentAction.getParameters().put(param.getName(), param.readFromString((String)aValue));
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | public void setCurrentAction(ActionDefinition currentAction) {
|
---|
344 | this.currentAction = currentAction;
|
---|
345 | fireTableDataChanged();
|
---|
346 | }
|
---|
347 |
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Key: Registered name (property "toolbar" of action).
|
---|
352 | * Value: The action to execute.
|
---|
353 | */
|
---|
354 | private Map<String, Action> actions = new HashMap<String, Action>();
|
---|
355 | private Map<String, Action> regactions = new HashMap<String, Action>();
|
---|
356 |
|
---|
357 | private DefaultMutableTreeNode rootActionsNode = new DefaultMutableTreeNode(tr("Actions"));
|
---|
358 |
|
---|
359 | public JToolBar control = new JToolBar();
|
---|
360 |
|
---|
361 | public PreferenceSetting createPreferenceSetting() {
|
---|
362 | return new Settings(rootActionsNode);
|
---|
363 | }
|
---|
364 |
|
---|
365 | public class Settings implements PreferenceSetting {
|
---|
366 |
|
---|
367 | private final class Move implements ActionListener {
|
---|
368 | public void actionPerformed(ActionEvent e) {
|
---|
369 | if (e.getActionCommand().equals("<") && actionsTree.getSelectionCount() > 0) {
|
---|
370 |
|
---|
371 | int leadItem = selected.getSize();
|
---|
372 | if (selectedList.getSelectedIndex() != -1) {
|
---|
373 | int[] indices = selectedList.getSelectedIndices();
|
---|
374 | leadItem = indices[indices.length - 1];
|
---|
375 | }
|
---|
376 | for (TreePath selectedAction : actionsTree.getSelectionPaths()) {
|
---|
377 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedAction.getLastPathComponent();
|
---|
378 | if (node.getUserObject() == null) {
|
---|
379 | selected.add(leadItem++, ActionDefinition.getSeparator());
|
---|
380 | } else if (node.getUserObject() instanceof Action) {
|
---|
381 | selected.add(leadItem++, new ActionDefinition((Action)node.getUserObject()));
|
---|
382 |
|
---|
383 | }
|
---|
384 | }
|
---|
385 | } else if (e.getActionCommand().equals(">") && selectedList.getSelectedIndex() != -1) {
|
---|
386 | while (selectedList.getSelectedIndex() != -1) {
|
---|
387 | selected.remove(selectedList.getSelectedIndex());
|
---|
388 | }
|
---|
389 | } else if (e.getActionCommand().equals("up")) {
|
---|
390 | int i = selectedList.getSelectedIndex();
|
---|
391 | Object o = selected.get(i);
|
---|
392 | if (i != 0) {
|
---|
393 | selected.remove(i);
|
---|
394 | selected.add(i-1, o);
|
---|
395 | selectedList.setSelectedIndex(i-1);
|
---|
396 | }
|
---|
397 | } else if (e.getActionCommand().equals("down")) {
|
---|
398 | int i = selectedList.getSelectedIndex();
|
---|
399 | Object o = selected.get(i);
|
---|
400 | if (i != selected.size()-1) {
|
---|
401 | selected.remove(i);
|
---|
402 | selected.add(i+1, o);
|
---|
403 | selectedList.setSelectedIndex(i+1);
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | private class ActionTransferable implements Transferable {
|
---|
410 |
|
---|
411 | private DataFlavor[] flavors = new DataFlavor[] { ACTION_FLAVOR };
|
---|
412 |
|
---|
413 | private final List<ActionDefinition> actions;
|
---|
414 |
|
---|
415 | public ActionTransferable(List<ActionDefinition> actions) {
|
---|
416 | this.actions = actions;
|
---|
417 | }
|
---|
418 |
|
---|
419 | public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
---|
420 | return actions;
|
---|
421 | }
|
---|
422 |
|
---|
423 | public DataFlavor[] getTransferDataFlavors() {
|
---|
424 | return flavors;
|
---|
425 | }
|
---|
426 |
|
---|
427 | public boolean isDataFlavorSupported(DataFlavor flavor) {
|
---|
428 | return flavors[0] == flavor;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | private final Move moveAction = new Move();
|
---|
433 |
|
---|
434 | private final DefaultListModel selected = new DefaultListModel();
|
---|
435 | private final JList selectedList = new JList(selected);
|
---|
436 |
|
---|
437 | private final DefaultTreeModel actionsTreeModel;
|
---|
438 | private final JTree actionsTree;
|
---|
439 |
|
---|
440 | private final ActionParametersTableModel actionParametersModel = new ActionParametersTableModel();
|
---|
441 | private final JTable actionParametersTable = new JTable(actionParametersModel);
|
---|
442 | private JPanel actionParametersPanel;
|
---|
443 |
|
---|
444 | private JButton upButton;
|
---|
445 | private JButton downButton;
|
---|
446 | private JButton removeButton;
|
---|
447 | private JButton addButton;
|
---|
448 |
|
---|
449 | private String movingComponent;
|
---|
450 |
|
---|
451 | public Settings(DefaultMutableTreeNode rootActionsNode) {
|
---|
452 | actionsTreeModel = new DefaultTreeModel(rootActionsNode);
|
---|
453 | actionsTree = new JTree(actionsTreeModel);
|
---|
454 | }
|
---|
455 |
|
---|
456 | private JButton createButton(String name) {
|
---|
457 | JButton b = new JButton();
|
---|
458 | if (name.equals("up")) {
|
---|
459 | b.setIcon(ImageProvider.get("dialogs", "up"));
|
---|
460 | } else if (name.equals("down")) {
|
---|
461 | b.setIcon(ImageProvider.get("dialogs", "down"));
|
---|
462 | } else {
|
---|
463 | b.setText(name);
|
---|
464 | }
|
---|
465 | b.addActionListener(moveAction);
|
---|
466 | b.setActionCommand(name);
|
---|
467 | return b;
|
---|
468 | }
|
---|
469 |
|
---|
470 | private void updateEnabledState() {
|
---|
471 | int index = selectedList.getSelectedIndex();
|
---|
472 | upButton.setEnabled(index > 0);
|
---|
473 | downButton.setEnabled(index < selectedList.getModel().getSize() - 1);
|
---|
474 | removeButton.setEnabled(index != -1);
|
---|
475 | addButton.setEnabled(actionsTree.getSelectionCount() > 0);
|
---|
476 | }
|
---|
477 |
|
---|
478 | public void addGui(PreferenceTabbedPane gui) {
|
---|
479 | actionsTree.setCellRenderer(new DefaultTreeCellRenderer() {
|
---|
480 | @Override
|
---|
481 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
|
---|
482 | boolean leaf, int row, boolean hasFocus) {
|
---|
483 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
|
---|
484 | JLabel comp = (JLabel) super.getTreeCellRendererComponent(
|
---|
485 | tree, value, sel, expanded, leaf, row, hasFocus);
|
---|
486 | if (node.getUserObject() == null) {
|
---|
487 | comp.setText(tr("Separator"));
|
---|
488 | comp.setIcon(ImageProvider.get("preferences/separator"));
|
---|
489 | }
|
---|
490 | else if (node.getUserObject() instanceof Action) {
|
---|
491 | Action action = (Action) node.getUserObject();
|
---|
492 | comp.setText((String) action.getValue(Action.NAME));
|
---|
493 | comp.setIcon((Icon) action.getValue(Action.SMALL_ICON));
|
---|
494 | }
|
---|
495 | return comp;
|
---|
496 | }
|
---|
497 | });
|
---|
498 |
|
---|
499 | ListCellRenderer renderer = new DefaultListCellRenderer(){
|
---|
500 | @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
501 | String s;
|
---|
502 | Icon i;
|
---|
503 | ActionDefinition action = (ActionDefinition)value;
|
---|
504 | if (!action.isSeparator()) {
|
---|
505 | s = (String) action.getAction().getValue(Action.NAME);
|
---|
506 | i = (Icon) action.getAction().getValue(Action.SMALL_ICON);
|
---|
507 | } else {
|
---|
508 | i = ImageProvider.get("preferences/separator");
|
---|
509 | s = tr("Separator");
|
---|
510 | }
|
---|
511 | JLabel l = (JLabel)super.getListCellRendererComponent(list, s, index, isSelected, cellHasFocus);
|
---|
512 | l.setIcon(i);
|
---|
513 | return l;
|
---|
514 | }
|
---|
515 | };
|
---|
516 | selectedList.setCellRenderer(renderer);
|
---|
517 | selectedList.addListSelectionListener(new ListSelectionListener(){
|
---|
518 | public void valueChanged(ListSelectionEvent e) {
|
---|
519 | boolean sel = selectedList.getSelectedIndex() != -1;
|
---|
520 | if (sel) {
|
---|
521 | actionsTree.clearSelection();
|
---|
522 | ActionDefinition action = (ActionDefinition) selected.get(selectedList.getSelectedIndex());
|
---|
523 | actionParametersModel.setCurrentAction(action);
|
---|
524 | actionParametersPanel.setVisible(actionParametersModel.getRowCount() > 0);
|
---|
525 | }
|
---|
526 | updateEnabledState();
|
---|
527 | }
|
---|
528 | });
|
---|
529 |
|
---|
530 | selectedList.setDragEnabled(true);
|
---|
531 | selectedList.setTransferHandler(new TransferHandler() {
|
---|
532 | @Override
|
---|
533 | protected Transferable createTransferable(JComponent c) {
|
---|
534 | List<ActionDefinition> actions = new ArrayList<ActionDefinition>();
|
---|
535 | for (Object o: ((JList)c).getSelectedValues()) {
|
---|
536 | actions.add((ActionDefinition)o);
|
---|
537 | }
|
---|
538 | return new ActionTransferable(actions);
|
---|
539 | }
|
---|
540 |
|
---|
541 | @Override
|
---|
542 | public int getSourceActions(JComponent c) {
|
---|
543 | return TransferHandler.MOVE;
|
---|
544 | }
|
---|
545 |
|
---|
546 | @Override
|
---|
547 | public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
|
---|
548 | for (DataFlavor f : transferFlavors) {
|
---|
549 | if (ACTION_FLAVOR.equals(f))
|
---|
550 | return true;
|
---|
551 | }
|
---|
552 | return false;
|
---|
553 | }
|
---|
554 |
|
---|
555 | @Override
|
---|
556 | public void exportAsDrag(JComponent comp, InputEvent e, int action) {
|
---|
557 | super.exportAsDrag(comp, e, action);
|
---|
558 | movingComponent = "list";
|
---|
559 | }
|
---|
560 |
|
---|
561 | @Override
|
---|
562 | public boolean importData(JComponent comp, Transferable t) {
|
---|
563 | try {
|
---|
564 | int dropIndex = selectedList.locationToIndex(selectedList.getMousePosition(true));
|
---|
565 | List<?> draggedData = (List<?>) t.getTransferData(ACTION_FLAVOR);
|
---|
566 |
|
---|
567 | Object leadItem = dropIndex >= 0 ? selected.elementAt(dropIndex) : null;
|
---|
568 | int dataLength = draggedData.size();
|
---|
569 |
|
---|
570 |
|
---|
571 | if (leadItem != null) {
|
---|
572 | for (Object o: draggedData) {
|
---|
573 | if (leadItem.equals(o))
|
---|
574 | return false;
|
---|
575 |
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | int dragLeadIndex = -1;
|
---|
580 | boolean localDrop = "list".equals(movingComponent);
|
---|
581 |
|
---|
582 | if (localDrop) {
|
---|
583 | dragLeadIndex = selected.indexOf(draggedData.get(0));
|
---|
584 | for (Object o: draggedData) {
|
---|
585 | selected.removeElement(o);
|
---|
586 | }
|
---|
587 | }
|
---|
588 | int[] indices = new int[dataLength];
|
---|
589 |
|
---|
590 | if (localDrop) {
|
---|
591 | int adjustedLeadIndex = selected.indexOf(leadItem);
|
---|
592 | int insertionAdjustment = dragLeadIndex <= adjustedLeadIndex ? 1 : 0;
|
---|
593 | for (int i = 0; i < dataLength; i++) {
|
---|
594 | selected.insertElementAt(draggedData.get(i), adjustedLeadIndex + insertionAdjustment + i);
|
---|
595 | indices[i] = adjustedLeadIndex + insertionAdjustment + i;
|
---|
596 | }
|
---|
597 | } else {
|
---|
598 | for (int i = 0; i < dataLength; i++) {
|
---|
599 | selected.add(dropIndex, draggedData.get(i));
|
---|
600 | indices[i] = dropIndex + i;
|
---|
601 | }
|
---|
602 | }
|
---|
603 | selectedList.clearSelection();
|
---|
604 | selectedList.setSelectedIndices(indices);
|
---|
605 | movingComponent = "";
|
---|
606 | return true;
|
---|
607 | } catch (Exception e) {
|
---|
608 | e.printStackTrace();
|
---|
609 | }
|
---|
610 | return false;
|
---|
611 | }
|
---|
612 |
|
---|
613 | @Override
|
---|
614 | protected void exportDone(JComponent source, Transferable data, int action) {
|
---|
615 | if (movingComponent.equals("list")) {
|
---|
616 | try {
|
---|
617 | List<?> draggedData = (List<?>) data.getTransferData(ACTION_FLAVOR);
|
---|
618 | boolean localDrop = selected.contains(draggedData.get(0));
|
---|
619 | if (localDrop) {
|
---|
620 | int[] indices = selectedList.getSelectedIndices();
|
---|
621 | Arrays.sort(indices);
|
---|
622 | for (int i = indices.length - 1; i >= 0; i--) {
|
---|
623 | selected.remove(indices[i]);
|
---|
624 | }
|
---|
625 | }
|
---|
626 | } catch (Exception e) {
|
---|
627 | e.printStackTrace();
|
---|
628 | }
|
---|
629 | movingComponent = "";
|
---|
630 | }
|
---|
631 | }
|
---|
632 | });
|
---|
633 |
|
---|
634 | actionsTree.setTransferHandler(new TransferHandler() {
|
---|
635 | private static final long serialVersionUID = 1L;
|
---|
636 |
|
---|
637 | @Override
|
---|
638 | public int getSourceActions( JComponent c ){
|
---|
639 | return TransferHandler.MOVE;
|
---|
640 | }
|
---|
641 |
|
---|
642 | @Override
|
---|
643 | protected void exportDone(JComponent source, Transferable data, int action) {
|
---|
644 | }
|
---|
645 |
|
---|
646 | @Override
|
---|
647 | protected Transferable createTransferable(JComponent c) {
|
---|
648 | TreePath[] paths = actionsTree.getSelectionPaths();
|
---|
649 | List<ActionDefinition> dragActions = new ArrayList<ActionDefinition>();
|
---|
650 | for (TreePath path : paths) {
|
---|
651 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
|
---|
652 | Object obj = node.getUserObject();
|
---|
653 | if (obj == null) {
|
---|
654 | dragActions.add(ActionDefinition.getSeparator());
|
---|
655 | }
|
---|
656 | else if (obj instanceof Action) {
|
---|
657 | dragActions.add(new ActionDefinition((Action) obj));
|
---|
658 | }
|
---|
659 | }
|
---|
660 | return new ActionTransferable(dragActions);
|
---|
661 | }
|
---|
662 | });
|
---|
663 | actionsTree.setDragEnabled(true);
|
---|
664 | actionsTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
|
---|
665 | public void valueChanged(TreeSelectionEvent e) {
|
---|
666 | updateEnabledState();
|
---|
667 | }
|
---|
668 | });
|
---|
669 |
|
---|
670 | final JPanel left = new JPanel(new GridBagLayout());
|
---|
671 | left.add(new JLabel(tr("Toolbar")), GBC.eol());
|
---|
672 | left.add(new JScrollPane(selectedList), GBC.std().fill(GBC.BOTH));
|
---|
673 |
|
---|
674 | final JPanel right = new JPanel(new GridBagLayout());
|
---|
675 | right.add(new JLabel(tr("Available")), GBC.eol());
|
---|
676 | right.add(new JScrollPane(actionsTree), GBC.eol().fill(GBC.BOTH));
|
---|
677 |
|
---|
678 | final JPanel buttons = new JPanel(new GridLayout(6,1));
|
---|
679 | buttons.add(upButton = createButton("up"));
|
---|
680 | buttons.add(addButton = createButton("<"));
|
---|
681 | buttons.add(removeButton = createButton(">"));
|
---|
682 | buttons.add(downButton = createButton("down"));
|
---|
683 | updateEnabledState();
|
---|
684 |
|
---|
685 | final JPanel p = new JPanel();
|
---|
686 | p.setLayout(new LayoutManager(){
|
---|
687 | public void addLayoutComponent(String name, Component comp) {}
|
---|
688 | public void removeLayoutComponent(Component comp) {}
|
---|
689 | public Dimension minimumLayoutSize(Container parent) {
|
---|
690 | Dimension l = left.getMinimumSize();
|
---|
691 | Dimension r = right.getMinimumSize();
|
---|
692 | Dimension b = buttons.getMinimumSize();
|
---|
693 | return new Dimension(l.width+b.width+10+r.width,l.height+b.height+10+r.height);
|
---|
694 | }
|
---|
695 | public Dimension preferredLayoutSize(Container parent) {
|
---|
696 | Dimension l = new Dimension(200, 200); //left.getPreferredSize();
|
---|
697 | Dimension r = new Dimension(200, 200); //right.getPreferredSize();
|
---|
698 | return new Dimension(l.width+r.width+10+buttons.getPreferredSize().width,Math.max(l.height, r.height));
|
---|
699 | }
|
---|
700 | public void layoutContainer(Container parent) {
|
---|
701 | Dimension d = p.getSize();
|
---|
702 | Dimension b = buttons.getPreferredSize();
|
---|
703 | int width = (d.width-10-b.width)/2;
|
---|
704 | left.setBounds(new Rectangle(0,0,width,d.height));
|
---|
705 | right.setBounds(new Rectangle(width+10+b.width,0,width,d.height));
|
---|
706 | buttons.setBounds(new Rectangle(width+5, d.height/2-b.height/2, b.width, b.height));
|
---|
707 | }
|
---|
708 | });
|
---|
709 | p.add(left);
|
---|
710 | p.add(buttons);
|
---|
711 | p.add(right);
|
---|
712 |
|
---|
713 | actionParametersPanel = new JPanel(new GridBagLayout());
|
---|
714 | actionParametersPanel.add(new JLabel(tr("Action parameters")), GBC.eol().insets(0, 10, 0, 20));
|
---|
715 | actionParametersTable.getColumnModel().getColumn(0).setHeaderValue(tr("Parameter name"));
|
---|
716 | actionParametersTable.getColumnModel().getColumn(1).setHeaderValue(tr("Parameter value"));
|
---|
717 | actionParametersPanel.add(actionParametersTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
718 | actionParametersPanel.add(actionParametersTable, GBC.eol().fill(GBC.BOTH).insets(0, 0, 0, 10));
|
---|
719 | actionParametersPanel.setVisible(false);
|
---|
720 |
|
---|
721 | JPanel panel = gui.createPreferenceTab("toolbar", tr("Toolbar customization"),
|
---|
722 | tr("Customize the elements on the toolbar."), false);
|
---|
723 | panel.add(p, GBC.eol().fill(GBC.BOTH));
|
---|
724 | panel.add(actionParametersPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
725 | selected.removeAllElements();
|
---|
726 | for (ActionDefinition actionDefinition: getDefinedActions()) {
|
---|
727 | selected.addElement(actionDefinition);
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | public boolean ok() {
|
---|
732 | Collection<String> t = new LinkedList<String>();
|
---|
733 | ActionParser parser = new ActionParser(null);
|
---|
734 | for (int i = 0; i < selected.size(); ++i) {
|
---|
735 | ActionDefinition action = (ActionDefinition)selected.get(i);
|
---|
736 | if (action.isSeparator()) {
|
---|
737 | t.add("|");
|
---|
738 | } else {
|
---|
739 | t.add(parser.saveAction(action));
|
---|
740 | }
|
---|
741 | }
|
---|
742 | if (t.isEmpty()) {
|
---|
743 | t = Collections.singletonList(EMPTY_TOOLBAR_MARKER);
|
---|
744 | }
|
---|
745 | Main.pref.putCollection("toolbar", t);
|
---|
746 | Main.toolbar.refreshToolbarControl();
|
---|
747 | return false;
|
---|
748 | }
|
---|
749 |
|
---|
750 | }
|
---|
751 |
|
---|
752 | public ToolbarPreferences() {
|
---|
753 | control.setFloatable(false);
|
---|
754 | }
|
---|
755 |
|
---|
756 | private void loadAction(DefaultMutableTreeNode node, MenuElement menu) {
|
---|
757 | Object userObject = null;
|
---|
758 | MenuElement menuElement = menu;
|
---|
759 | if (menu.getSubElements().length > 0 &&
|
---|
760 | menu.getSubElements()[0] instanceof JPopupMenu) {
|
---|
761 | menuElement = menu.getSubElements()[0];
|
---|
762 | }
|
---|
763 | for (MenuElement item : menuElement.getSubElements()) {
|
---|
764 | if (item instanceof JMenuItem) {
|
---|
765 | JMenuItem menuItem = ((JMenuItem)item);
|
---|
766 | if (menuItem.getAction() != null) {
|
---|
767 | Action action = menuItem.getAction();
|
---|
768 | userObject = action;
|
---|
769 | actions.put((String) action.getValue("toolbar"), action);
|
---|
770 | } else {
|
---|
771 | userObject = menuItem.getText();
|
---|
772 | }
|
---|
773 | }
|
---|
774 | DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(userObject);
|
---|
775 | node.add(newNode);
|
---|
776 | loadAction(newNode, item);
|
---|
777 | }
|
---|
778 | }
|
---|
779 |
|
---|
780 | public Action getAction(String s)
|
---|
781 | {
|
---|
782 | Action e = actions.get(s);
|
---|
783 | if(e == null) {
|
---|
784 | e = regactions.get(s);
|
---|
785 | }
|
---|
786 | return e;
|
---|
787 | }
|
---|
788 |
|
---|
789 | private void loadActions() {
|
---|
790 | rootActionsNode.removeAllChildren();
|
---|
791 | loadAction(rootActionsNode, Main.main.menu);
|
---|
792 | for(Map.Entry<String, Action> a : regactions.entrySet())
|
---|
793 | {
|
---|
794 | if(actions.get(a.getKey()) == null) {
|
---|
795 | rootActionsNode.add(new DefaultMutableTreeNode(a.getValue()));
|
---|
796 | }
|
---|
797 | }
|
---|
798 | rootActionsNode.add(new DefaultMutableTreeNode(null));
|
---|
799 | }
|
---|
800 |
|
---|
801 | private static final String[] deftoolbar = {"open", "save", "download", "upload", "|", "undo", "redo", "|", "dialogs/search", "preference", "|", "splitway", "combineway", "wayflip", "|", "imagery-offset", "|", "tagginggroup_Highways/Streets", "tagginggroup_Highways/Ways", "tagginggroup_Highways/Waypoints", "tagginggroup_Highways/Barriers", "|", "tagginggroup_Transport/Car", "tagginggroup_Transport/Public Transport", "|", "tagginggroup_Travel/Tourism", "tagginggroup_Travel/Food+Drinks", "|", "tagginggroup_Travel/Historic Places", "|", "tagginggroup_Man-Made/Man Made"};
|
---|
802 |
|
---|
803 | private static Collection<String> getToolString() {
|
---|
804 |
|
---|
805 | Collection<String> toolStr = Main.pref.getCollection("toolbar", Arrays.asList(deftoolbar));
|
---|
806 | if (toolStr == null || toolStr.size() == 0) {
|
---|
807 | toolStr = Arrays.asList(deftoolbar);
|
---|
808 | }
|
---|
809 | return toolStr;
|
---|
810 | }
|
---|
811 |
|
---|
812 | private Collection<ActionDefinition> getDefinedActions() {
|
---|
813 | loadActions();
|
---|
814 |
|
---|
815 | Map<String, Action> allActions = new HashMap<String, Action>(regactions);
|
---|
816 | allActions.putAll(actions);
|
---|
817 | ActionParser actionParser = new ActionParser(allActions);
|
---|
818 |
|
---|
819 | Collection<ActionDefinition> result = new ArrayList<ActionDefinition>();
|
---|
820 |
|
---|
821 | for (String s : getToolString()) {
|
---|
822 | if (s.equals("|")) {
|
---|
823 | result.add(ActionDefinition.getSeparator());
|
---|
824 | } else {
|
---|
825 | ActionDefinition a = actionParser.loadAction(s);
|
---|
826 | if(a != null) {
|
---|
827 | result.add(a);
|
---|
828 | }
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | return result;
|
---|
833 | }
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * @return The parameter (for better chaining)
|
---|
837 | */
|
---|
838 | public Action register(Action action) {
|
---|
839 | regactions.put((String) action.getValue("toolbar"), action);
|
---|
840 | return action;
|
---|
841 | }
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * Parse the toolbar preference setting and construct the toolbar GUI control.
|
---|
845 | *
|
---|
846 | * Call this, if anything has changed in the toolbar settings and you want to refresh
|
---|
847 | * the toolbar content (e.g. after registering actions in a plugin)
|
---|
848 | */
|
---|
849 | public void refreshToolbarControl() {
|
---|
850 | control.removeAll();
|
---|
851 |
|
---|
852 | for (ActionDefinition action : getDefinedActions()) {
|
---|
853 | if (action.isSeparator()) {
|
---|
854 | control.addSeparator();
|
---|
855 | } else {
|
---|
856 | Action a = action.getParametrizedAction();
|
---|
857 | JButton b = control.add(a);
|
---|
858 | Object tt = a.getValue(TaggingPreset.OPTIONAL_TOOLTIP_TEXT);
|
---|
859 | if (tt != null) {
|
---|
860 | b.setToolTipText((String)tt);
|
---|
861 | }
|
---|
862 | }
|
---|
863 | }
|
---|
864 | control.setVisible(control.getComponentCount() != 0);
|
---|
865 | }
|
---|
866 |
|
---|
867 | private static DataFlavor ACTION_FLAVOR = new DataFlavor(
|
---|
868 | ActionDefinition.class, "ActionItem");
|
---|
869 |
|
---|
870 | }
|
---|