Ticket #12421: SetNumberOfRecentlyAddedTags.patch

File SetNumberOfRecentlyAddedTags.patch, 7.5 KB (added by kolesar, 6 years ago)
  • src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java b/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java
    index b7ffcc6..38d625b 100644
    a b public class TagEditHelper { 
    573573    protected class AddTagsDialog extends AbstractTagsDialog {
    574574        private final List<JosmAction> recentTagsActions = new ArrayList<>();
    575575        protected final transient FocusAdapter focus;
     576        private JPanel recentTagsPanel;
    576577
    577578        // Counter of added commands for possible undo
    578579        private int commandCount;
    public class TagEditHelper { 
    583584            setCancelButton(2);
    584585            configureContextsensitiveHelp("/Dialog/AddValue", true /* show help button */);
    585586
    586             JPanel mainPanel = new JPanel(new GridBagLayout());
     587            final JPanel mainPanel = new JPanel(new GridBagLayout());
    587588            keys = new AutoCompletingComboBox();
    588589            values = new AutoCompletingComboBox();
    589590
    public class TagEditHelper { 
    644645                    }
    645646                });
    646647
    647             suggestRecentlyAddedTags(mainPanel, focus);
     648            suggestRecentlyAddedTags(mainPanel);
    648649
    649650            mainPanel.add(Box.createVerticalGlue(), GBC.eop().fill());
    650651            setContent(mainPanel, false);
    public class TagEditHelper { 
    655656                @Override
    656657                public void actionPerformed(ActionEvent e) {
    657658                    selectNumberOfTags();
     659                    suggestRecentlyAddedTags(mainPanel);
    658660                }
    659661            });
    660662            JCheckBoxMenuItem rememberLastTags = new JCheckBoxMenuItem(
    public class TagEditHelper { 
    696698        }
    697699
    698700        protected void selectNumberOfTags() {
    699             String s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display"));
    700             if (s == null) {
    701                 return;
    702             }
    703             try {
    704                 int v = Integer.parseInt(s);
    705                 if (v >= 0 && v <= MAX_LRU_TAGS_NUMBER) {
    706                     PROPERTY_RECENT_TAGS_NUMBER.put(v);
     701            String s = String.format("%d", PROPERTY_RECENT_TAGS_NUMBER.get());
     702            while (true) {
     703                s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display"), s);
     704                if (s == null) {
    707705                    return;
    708706                }
    709             } catch (NumberFormatException ex) {
    710                 Main.warn(ex);
     707                try {
     708                    int v = Integer.parseInt(s);
     709                    if (v >= 0 && v <= MAX_LRU_TAGS_NUMBER) {
     710                        PROPERTY_RECENT_TAGS_NUMBER.put(v);
     711                        return;
     712                    }
     713                } catch (NumberFormatException ex) {
     714                    Main.warn(ex);
     715                }
     716                JOptionPane.showMessageDialog(this, tr("Please enter integer number between 0 and {0}", MAX_LRU_TAGS_NUMBER));
    711717            }
    712             JOptionPane.showMessageDialog(this, tr("Please enter integer number between 0 and {0}", MAX_LRU_TAGS_NUMBER));
    713718        }
    714719
    715         protected void suggestRecentlyAddedTags(JPanel mainPanel, final FocusAdapter focus) {
     720        protected void suggestRecentlyAddedTags(JPanel mainPanel) {
     721
     722            if (recentTagsPanel == null) {
     723                recentTagsPanel = new JPanel(new GridBagLayout());
     724                suggestRecentlyAddedTags();
     725                mainPanel.add(recentTagsPanel, GBC.eol().fill(GBC.HORIZONTAL));
     726            } else {
     727                Dimension panelOldSize = panelOldSize = recentTagsPanel.getPreferredSize();
     728                recentTagsPanel.removeAll();
     729                suggestRecentlyAddedTags();
     730                Dimension panelNewSize = recentTagsPanel.getPreferredSize();
     731                Dimension dialogOldSize = getMinimumSize();
     732                Dimension dialogNewSize = new Dimension(dialogOldSize.width, dialogOldSize.height-panelOldSize.height+panelNewSize.height);
     733                setMinimumSize(dialogNewSize);
     734                setPreferredSize(dialogNewSize);
     735                setSize(dialogNewSize);
     736                revalidate();
     737                repaint();
     738            }
     739        }
     740
     741        protected void suggestRecentlyAddedTags() {
    716742            final int tagsToShow = Math.min(PROPERTY_RECENT_TAGS_NUMBER.get(), MAX_LRU_TAGS_NUMBER);
    717743            if (!(tagsToShow > 0 && !recentTags.isEmpty()))
    718744                return;
    719 
    720             mainPanel.add(new JLabel(tr("Recently added tags")), GBC.eol());
     745            recentTagsPanel.add(new JLabel(tr("Recently added tags")), GBC.eol());
    721746
    722747            int count = 1;
    723             // We store the maximum number (9) of recent tags to allow dynamic change of number of tags shown in the preferences.
     748            // We store the maximum number of recent tags to allow dynamic change of number of tags shown in the preferences.
    724749            // This implies to iterate in descending order, as the oldest elements will only be removed after we reach the maximum
    725750            // number and not the number of tags to show.
    726751            // However, as Set does not allow to iterate in descending order, we need to copy its elements into a List we can access
    public class TagEditHelper { 
    777802                }
    778803                GridBagConstraints gbc = new GridBagConstraints();
    779804                gbc.ipadx = 5;
    780                 mainPanel.add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
     805                recentTagsPanel.add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
    781806                // Create tag label
    782807                final String color = action.isEnabled() ? "" : "; color:gray";
    783808                final JLabel tagLabel = new JLabel("<html>"
    public class TagEditHelper { 
    789814                tagLabel.setFont(tagLabel.getFont().deriveFont(Font.PLAIN));
    790815                if (action.isEnabled() && sc != null && scShift != null) {
    791816                    // Register action
    792                     mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "choose"+count);
    793                     mainPanel.getActionMap().put("choose"+count, action);
    794                     mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scShift.getKeyStroke(), "apply"+count);
    795                     mainPanel.getActionMap().put("apply"+count, actionShift);
     817                    recentTagsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "choose"+count);
     818                    recentTagsPanel.getActionMap().put("choose"+count, action);
     819                    recentTagsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scShift.getKeyStroke(), "apply"+count);
     820                    recentTagsPanel.getActionMap().put("apply"+count, actionShift);
    796821                }
    797822                if (action.isEnabled()) {
    798823                    // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut)
    public class TagEditHelper { 
    822847                // Finally add label to the resulting panel
    823848                JPanel tagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    824849                tagPanel.add(tagLabel);
    825                 mainPanel.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
     850                recentTagsPanel.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
    826851            }
    827852        }
    828853