Changeset 6844 in josm for trunk/src


Ignore:
Timestamp:
2014-02-12T15:58:32+01:00 (11 years ago)
Author:
akks
Message:

see #9709: "Add to toolbar" in preset list context menu (F3-right click)

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r6744 r6844  
    333333            String res = actionParser.saveAction(aDef);
    334334
    335             Collection<String> t = new LinkedList<String>(ToolbarPreferences.getToolString());
    336335            // add custom search button to toolbar preferences
    337             if (!t.contains(res)) t.add(res);
    338             Main.pref.putCollection("toolbar", t);
    339             Main.toolbar.refreshToolbarControl();
     336            Main.toolbar.addCustomButton(res, -1, false);
    340337        }
    341338        return initialValues;
  • trunk/src/org/openstreetmap/josm/gui/preferences/ToolbarPreferences.java

    r6838 r6844  
    469469     * Value: The action to execute.
    470470     */
    471     private Map<String, Action> actions = new HashMap<String, Action>();
    472     private Map<String, Action> regactions = new HashMap<String, Action>();
    473 
    474     private DefaultMutableTreeNode rootActionsNode = new DefaultMutableTreeNode(tr("Actions"));
     471    private final Map<String, Action> actions = new HashMap<String, Action>();
     472    private final Map<String, Action> regactions = new HashMap<String, Action>();
     473
     474    private final DefaultMutableTreeNode rootActionsNode = new DefaultMutableTreeNode(tr("Actions"));
    475475
    476476    public JToolBar control = new JToolBar();
    477     private Map<Object, ActionDefinition> buttonActions = new HashMap<Object, ActionDefinition>(30);
     477    private final Map<Object, ActionDefinition> buttonActions = new HashMap<Object, ActionDefinition>(30);
    478478
    479479    @Override
     
    529529        private class ActionTransferable implements Transferable {
    530530
    531             private DataFlavor[] flavors = new DataFlavor[] { ACTION_FLAVOR };
     531            private final DataFlavor[] flavors = new DataFlavor[] { ACTION_FLAVOR };
    532532
    533533            private final List<ActionDefinition> actions;
     
    10561056        control.repaint();
    10571057    }
     1058   
     1059    /**
     1060     * The method to add custom button on toolbar like search or preset buttons
     1061     * @param definitionText toolbar definition text to describe the new button,
     1062     * must be carefully generated by using {@link ActionParser}
     1063     * @param preferredIndex place to put the new button, give -1 for the end of toolbar
     1064     * @param removeIfExists if true and the button already exists, remove it
     1065     */
     1066    public void addCustomButton(String definitionText, int preferredIndex, boolean removeIfExists) {
     1067        LinkedList<String> t = new LinkedList<String>(getToolString());
     1068        if (t.contains(definitionText)) {
     1069            if (!removeIfExists) return; // do nothing
     1070            t.remove(definitionText);
     1071        } else {
     1072            if (preferredIndex>=0 && preferredIndex < t.size()) {
     1073                t.add(preferredIndex, definitionText); // add to specified place
     1074            } else {
     1075                t.add(definitionText); // add to the end
     1076            }
     1077        }
     1078        Main.pref.putCollection("toolbar", t);
     1079        Main.toolbar.refreshToolbarControl();
     1080    }
    10581081
    10591082    private JButton addButtonAndShortcut(ActionDefinition action) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r6842 r6844  
    519519        public void actionPerformed(ActionEvent ae) {
    520520            String res = getToolbarString();
    521             LinkedList<String> t = new LinkedList<String>(ToolbarPreferences.getToolString());
    522             if (t.contains(res)) {
    523                 t.remove(res);
    524             } else {
    525                 if (toolbarIndex>=0) {
    526                     t.add(toolbarIndex, res); // add to the old place
    527                 } else {
    528                     t.add(res); // add to the end
    529                 }
    530             }
    531             Main.pref.putCollection("toolbar", t);
    532             Main.toolbar.refreshToolbarControl();
    533             }
     521            Main.toolbar.addCustomButton(res, toolbarIndex, true);
     522        }
    534523    }
    535524   
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetSelector.java

    r6738 r6844  
    55import java.awt.Component;
    66import java.awt.Dimension;
     7import java.awt.event.ActionEvent;
    78import java.awt.event.ActionListener;
    89import java.awt.event.ItemEvent;
     
    1718import java.util.EnumSet;
    1819import java.util.HashSet;
     20import java.util.LinkedList;
    1921import java.util.List;
     22import javax.swing.AbstractAction;
    2023import javax.swing.AbstractListModel;
    2124import javax.swing.Action;
     
    2730import javax.swing.JList;
    2831import javax.swing.JPanel;
     32import javax.swing.JPopupMenu;
    2933import javax.swing.JScrollPane;
    3034import javax.swing.event.DocumentEvent;
     
    3943import org.openstreetmap.josm.data.osm.Way;
    4044import org.openstreetmap.josm.data.preferences.BooleanProperty;
     45import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
    4146import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
    4247import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.Key;
     
    4550import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.Roles;
    4651import org.openstreetmap.josm.gui.widgets.JosmTextField;
     52import org.openstreetmap.josm.gui.widgets.ListPopupMenu;
     53import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    4754import static org.openstreetmap.josm.tools.I18n.tr;
    4855
     
    7077    private final List<PresetClassification> classifications = new ArrayList<PresetClassification>();
    7178    private ResultListModel lsResultModel = new ResultListModel();
    72    
     79    private JPopupMenu popupMenu;
     80
    7381    private ActionListener dblClickListener;
    7482    private ActionListener clickListener;
     
    288296
    289297        setPreferredSize(new Dimension(400, 300));
    290        
    291298        filterPresets();
     299        popupMenu = new JPopupMenu();
     300        popupMenu.add(new AbstractAction(tr("Add toolbar button")) {
     301            @Override
     302            public void actionPerformed(ActionEvent ae) {
     303                String res = getSelectedPreset().getToolbarString();
     304                Main.toolbar.addCustomButton(res, -1, false);
     305            }
     306        });
     307        lsResult.addMouseListener(new PopupMenuLauncher(popupMenu));
    292308    }
    293309   
Note: See TracChangeset for help on using the changeset viewer.