Ignore:
Timestamp:
2013-05-17T20:45:17+02:00 (11 years ago)
Author:
akks
Message:

see #8652 (patch by cmuelle8, part 1, modified): add "toggle panels" action (soon will be on Tab)
rework all toolbar context menus (add "do not hide" item and remove PopupMenuLauncher usage)
fixing minor warnings (@Override, private final)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/ToolbarPreferences.java

    r5954 r5965  
    3030import java.util.Map;
    3131
     32import javax.swing.AbstractAction;
    3233import javax.swing.Action;
    3334import javax.swing.DefaultListCellRenderer;
     
    3637import javax.swing.ImageIcon;
    3738import javax.swing.JButton;
     39import javax.swing.JCheckBoxMenuItem;
    3840import javax.swing.JComponent;
    3941import javax.swing.JLabel;
     
    5153import javax.swing.event.ListSelectionEvent;
    5254import javax.swing.event.ListSelectionListener;
     55import javax.swing.event.PopupMenuEvent;
     56import javax.swing.event.PopupMenuListener;
    5357import javax.swing.event.TreeSelectionEvent;
    5458import javax.swing.event.TreeSelectionListener;
     
    6670import org.openstreetmap.josm.actions.ParameterizedActionDecorator;
    6771import org.openstreetmap.josm.gui.tagging.TaggingPreset;
    68 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    6972import org.openstreetmap.josm.tools.GBC;
    7073import org.openstreetmap.josm.tools.ImageProvider;
     
    7275
    7376public class ToolbarPreferences implements PreferenceSettingFactory {
    74 
    7577
    7678    private static final String EMPTY_TOOLBAR_MARKER = "<!-empty-!>";
     
    228230                    skip('=');
    229231                    String paramValue = readTillChar(',','}');
    230                     if ("icon".equals(paramName) && paramValue.length() > 0)
     232                    if ("icon".equals(paramName) && paramValue.length() > 0) {
    231233                        result.setIcon(paramValue);
    232                     else if("name".equals(paramName) && paramValue.length() > 0)
     234                    } else if("name".equals(paramName) && paramValue.length() > 0) {
    233235                        result.setName(paramValue);
     236                    }
    234237                    skip(',');
    235238                }
     
    294297                    first = false;
    295298                }
    296                 if(!first)
     299                if(!first) {
    297300                    result.append('}');
     301            }
    298302            }
    299303
     
    339343                        return null;
    340344                    }
    341                 } else
     345                } else {
    342346                    rowIndex -= 2;
     347            }
    343348            }
    344349            ActionParameter<Object> param = getParam(rowIndex);
     
    368373                     currentAction.setIcon((String)aValue);
    369374                     return;
    370                 } else
     375                } else {
    371376                    rowIndex -= 2;
     377            }
    372378            }
    373379            ActionParameter<Object> param = getParam(rowIndex);
     
    383389    }
    384390
    385     private static class ToolbarPopupMenu extends JPopupMenu {
    386         public ToolbarPopupMenu(final ActionDefinition action) {
    387 
    388             if(action != null) {
    389                 add(tr("Remove from toolbar",action.getDisplayName()))
    390                         .addActionListener(new ActionListener() {
    391                             @Override public void actionPerformed(ActionEvent e) {
     391    private class ToolbarPopupMenu extends JPopupMenu  {
     392        ActionDefinition act;
     393
     394        private void setActionAndAdapt(ActionDefinition action) {
     395            this.act = action;
     396            doNotHide.setSelected(Main.pref.getBoolean("toolbar.always-visible", true));
     397            remove.setVisible(act!=null);
     398            shortcutEdit.setVisible(act!=null);
     399        }
     400
     401        JMenuItem remove = new JMenuItem(new AbstractAction(tr("Remove from toolbar")) {
     402            @Override
     403            public void actionPerformed(ActionEvent e) {
    392404                                Collection<String> t = new LinkedList<String>(getToolString());
    393405                                ActionParser parser = new ActionParser(null);
    394406                                // get text definition of current action
    395                                 String res = parser.saveAction(action);
     407                String res = parser.saveAction(act);
    396408                                // remove the button from toolbar preferences
    397409                                t.remove( res );
     
    400412                            }
    401413                });
    402             }
    403414           
    404             add(tr("Configure toolbar")).addActionListener(new ActionListener() {
    405                 @Override public void actionPerformed(ActionEvent e) {
     415        JMenuItem configure = new JMenuItem(new AbstractAction(tr("Configure toolbar")) {
     416            @Override
     417            public void actionPerformed(ActionEvent e) {
    406418                    final PreferenceDialog p =new PreferenceDialog(Main.parent);
    407419                    p.selectPreferencesTabByName("toolbar");
     
    410422            });
    411423
    412             add(tr("Edit shortcut")).addActionListener(new ActionListener() {
    413                 @Override public void actionPerformed(ActionEvent e) {
     424        JMenuItem shortcutEdit = new JMenuItem(new AbstractAction(tr("Edit shortcut")) {
     425            @Override
     426            public void actionPerformed(ActionEvent e) {
    414427                    final PreferenceDialog p =new PreferenceDialog(Main.parent);
    415                     p.getTabbedPane().getShortcutPreference().setDefaultFilter(action.getDisplayName());
     428                p.getTabbedPane().getShortcutPreference().setDefaultFilter(act.getDisplayName());
    416429                    p.selectPreferencesTabByName("shortcuts");
    417430                    p.setVisible(true);
    418                     // refresh toolbar to accept changes of shortcuts without restart
     431                // refresh toolbar to try using changed shortcuts without restart
    419432                    Main.toolbar.refreshToolbarControl();
    420433                }
    421434            });
    422         }
    423     }
     435
     436        JCheckBoxMenuItem doNotHide = new JCheckBoxMenuItem(new AbstractAction(tr("getSt hide toolbar and menu")) {
     437            @Override
     438            public void actionPerformed(ActionEvent e) {
     439                boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState();
     440                Main.pref.put("toolbar.always-visible", sel);
     441                Main.pref.put("menu.always-visible", sel);
     442        }
     443        });
     444        {
     445            addPopupMenuListener(new PopupMenuListener() {
     446                @Override
     447                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
     448                    setActionAndAdapt(buttonActions.get(
     449                            ((JPopupMenu)e.getSource()).getInvoker()
     450                    ));
     451    }
     452                @Override
     453                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
     454
     455                @Override
     456                public void popupMenuCanceled(PopupMenuEvent e) {}
     457            });
     458            add(remove);
     459            add(configure);
     460            add(shortcutEdit);
     461            add(doNotHide);
     462        }
     463    }
     464
     465    private ToolbarPopupMenu popupMenu = new ToolbarPopupMenu();
    424466
    425467    /**
     
    433475
    434476    public JToolBar control = new JToolBar();
     477    private HashMap<Object, ActionDefinition> buttonActions = new HashMap<Object, ActionDefinition>(30);
    435478
    436479    @Override
     
    826869                } else {
    827870                    String res = parser.saveAction(action);
    828                     if(res != null)
     871                    if(res != null) {
    829872                        t.add(res);
    830873                }
     874            }
    831875            }
    832876            if (t.isEmpty()) {
     
    842886    public ToolbarPreferences() {
    843887        control.setFloatable(false);
    844         control.addMouseListener(new PopupMenuLauncher(new ToolbarPopupMenu(null)));
     888        control.setComponentPopupMenu(popupMenu);
    845889    }
    846890
     
    9791023    public void refreshToolbarControl() {
    9801024        control.removeAll();
     1025        buttonActions.clear();
    9811026
    9821027        for (ActionDefinition action : getDefinedActions()) {
     
    9851030            } else {
    9861031                final JButton b = addButtonAndShortcut(action);
     1032                buttonActions.put(b, action);
    9871033               
    9881034                Icon i = action.getDisplayIcon();
     
    10011047                    });
    10021048                }
    1003                 b.addMouseListener(new PopupMenuLauncher( new ToolbarPopupMenu(action)));
     1049                b.setInheritsPopupMenu(true);
    10041050            }
    10051051        }
     
    10141060        if (action.getAction() instanceof JosmAction) {
    10151061            sc = ((JosmAction) action.getAction()).getShortcut();
    1016             if (sc.getAssignedKey() == KeyEvent.CHAR_UNDEFINED) sc = null;
     1062            if (sc.getAssignedKey() == KeyEvent.CHAR_UNDEFINED) {
     1063                sc = null;
     1064        }
    10171065        }
    10181066
     
    10231071       
    10241072        String tt = action.getDisplayTooltip();
    1025         if (tt==null) tt="";
     1073        if (tt==null) {
     1074            tt="";
     1075        }
    10261076
    10271077        if (sc == null || paramCode != 0) {
    10281078            String name = (String) action.getAction().getValue("toolbar");
    1029             if (name==null) name=action.getDisplayName();
    1030             if (paramCode!=0) name = name+paramCode;
     1079            if (name==null) {
     1080                name=action.getDisplayName();
     1081            }
     1082            if (paramCode!=0) {
     1083                name = name+paramCode;
     1084            }
    10311085            String desc = action.getDisplayName() + ((paramCode==0)?"":action.parameters.toString());
    10321086            sc = Shortcut.registerShortcut("toolbar:"+name, tr("Toolbar: {0}", desc),
     
    10441098        }
    10451099       
    1046         if (!tt.isEmpty()) b.setToolTipText(tt);
     1100        if (!tt.isEmpty()) {
     1101            b.setToolTipText(tt);
     1102        }
    10471103        return b;
    10481104    }
Note: See TracChangeset for help on using the changeset viewer.