source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/ToolbarPreferences.java@ 3200

Last change on this file since 3200 was 3200, checked in by bastiK, 14 years ago

fixed #4930 - Relation editor "creeps"

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