Ticket #6953: toolbarMapModeShowHide.patch

File toolbarMapModeShowHide.patch, 5.7 KB (added by akks, 12 years ago)
  • src/org/openstreetmap/josm/gui/IconToggleButton.java

     
    66import java.beans.PropertyChangeEvent;
    77import java.beans.PropertyChangeListener;
    88
     9import javax.swing.AbstractAction;
    910import javax.swing.Action;
    1011import javax.swing.JToggleButton;
    1112
     13import org.openstreetmap.josm.Main;
    1214import org.openstreetmap.josm.tools.Destroyable;
    1315
    1416/**
     
    5860        }
    5961        action.removePropertyChangeListener(this);
    6062    }
     63   
     64    public void checkButtonHiddenPreferences() {
     65            String actionName = (String) getAction().getValue(AbstractAction.NAME);
     66            boolean hiddenFlag =
     67                    Main.pref.getBoolean(actionName + ".itbutton_hidden", false);
     68            setVisible(!hiddenFlag);   
     69    }
     70
     71    public void setButtonHidden(boolean b) {
     72            String actionName = (String) getAction().getValue(AbstractAction.NAME);
     73            setVisible(!b);
     74            Main.pref.put(actionName + ".itbutton_hidden", b);
     75    }
    6176}
  • src/org/openstreetmap/josm/gui/MapFrame.java

     
    1313import java.awt.event.MouseWheelEvent;
    1414import java.awt.event.MouseWheelListener;
    1515import java.util.ArrayList;
     16import java.util.Enumeration;
    1617import java.util.HashMap;
    1718import java.util.List;
    1819import java.util.Map;
     
    108109    public final ButtonGroup toolGroup = new ButtonGroup();
    109110
    110111    public final JButton otherButton = new JButton(new OtherButtonsAction());
     112    public final JButton listAllMapModesButton = new JButton(new ListAllMapModesAction());
    111113
    112114    /**
    113115     * Default width of the toggle dialog area.
     
    304306            mapModes.add((MapMode) b.getAction());
    305307        } else
    306308            throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
     309        final IconToggleButton bb = b; // ee do not want to change signature of the method
     310        //context menu
     311        b.addMouseListener(new PopupMenuLauncher(new JPopupMenu() {
     312            {
     313                add(new AbstractAction() {
     314                    {
     315                        putValue(NAME, tr("Hide this button"));
     316                        putValue(SHORT_DESCRIPTION, tr("Click the arrow at the bottom to show it again."));
     317                    }
     318                    @Override
     319                    public void actionPerformed(ActionEvent e) {
     320                        bb.setButtonHidden(true);
     321                        validateToolBarVisibility();
     322                    }
     323                });
     324            }
     325        }));
     326        b.checkButtonHiddenPreferences();
    307327    }
    308328
    309329    /**
     
    362382        jb.setFloatable(false);
    363383        toolBarActions.setAlignmentX(0.5f);
    364384        jb.add(toolBarActions);
    365 
     385        listAllMapModesButton.setAlignmentX(0.5f);
     386        listAllMapModesButton.setBorder(null);
     387        listAllMapModesButton.setFont(otherButton.getFont().deriveFont(Font.PLAIN));
     388        jb.add(listAllMapModesButton);
     389       
    366390        if(Main.pref.getBoolean("sidetoolbar.togglevisible", true)) {
    367391            jb.addSeparator(new Dimension(0,18));
    368392            toolBarToggle.setAlignmentX(0.5f);
     
    425449            menu.show(otherButton, bounds.x+bounds.width, 0);
    426450        }
    427451    }
     452   
     453        class ListAllMapModesAction extends AbstractAction {
    428454
     455        public ListAllMapModesAction() {
     456            putValue(NAME, ">>");
     457        }
     458
     459        @Override
     460        public void actionPerformed(ActionEvent e) {
     461            JPopupMenu menu = new JPopupMenu();
     462            AbstractButton b;
     463            Enumeration<AbstractButton> elements = toolGroup.getElements();
     464            while (elements.hasMoreElements()) {
     465                b = elements.nextElement();
     466                if (!(b instanceof IconToggleButton)) continue;
     467                final IconToggleButton t = (IconToggleButton) b;
     468                menu.add(new JCheckBoxMenuItem(new AbstractAction() {
     469
     470                    {
     471                        putValue(NAME, t.getAction().getValue(NAME));
     472                        putValue(SMALL_ICON, t.getAction().getValue(SMALL_ICON));
     473                        putValue(SELECTED_KEY, t.isVisible());
     474                        putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button"));
     475                    }
     476
     477                    @Override
     478                    public void actionPerformed(ActionEvent e) {
     479                        if ((Boolean) getValue(SELECTED_KEY)) {
     480                            t.setButtonHidden(false);
     481                        } else {
     482                            t.setButtonHidden(true);
     483                        }
     484                        validateToolBarVisibility();
     485                    }
     486                }));
     487            }
     488            Rectangle bounds = listAllMapModesButton.getBounds();
     489            menu.show(listAllMapModesButton, bounds.x + bounds.width, 0);
     490        }
     491    }
     492
     493    public void validateToolBarVisibility() {
     494        AbstractButton b;
     495        Enumeration<AbstractButton> elements = toolGroup.getElements();
     496        while (elements.hasMoreElements()) {
     497            b = elements.nextElement();
     498            if (!(b instanceof IconToggleButton)) continue;
     499            ((IconToggleButton) b).checkButtonHiddenPreferences();
     500        }
     501        toolBarActions.repaint();
     502    }
     503
     504
    429505    /**
    430506     * Replies the instance of a toggle dialog of type <code>type</code> managed by this
    431507     * map frame