Changeset 9502 in josm


Ignore:
Timestamp:
2016-01-17T15:27:57+01:00 (8 years ago)
Author:
Don-vip
Message:

see #12281 - core changes to clean up massive code duplication from OSMRecPlugin plugin (patch by simon04 + javadoc/sonar fixes)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r9484 r9502  
    8787 * @since 5633
    8888 */
    89 class TagEditHelper {
     89public class TagEditHelper {
     90
    9091    private final JTable tagTable;
    9192    private final DefaultTableModel tagData;
     
    9394
    9495    // Selection that we are editing by using both dialogs
    95     private Collection<OsmPrimitive> sel;
     96    protected Collection<OsmPrimitive> sel;
    9697
    9798    private String changedKey;
     
    108109    private String lastAddValue;
    109110
     111    /** Default number of recent tags */
    110112    public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
     113    /** Maximum number of recent tags */
    111114    public static final int MAX_LRU_TAGS_NUMBER = 30;
     115
     116    /** Use English language for tag by default */
     117    public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = new BooleanProperty("properties.fix-tag-combobox-locale", false);
     118    /** Whether recent tags must be remembered */
     119    public static final BooleanProperty PROPERTY_REMEMBER_TAGS = new BooleanProperty("properties.remember-recently-added-tags", true);
     120    /** Number of recent tags */
     121    public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags",
     122            DEFAULT_LRU_TAGS_NUMBER);
    112123
    113124    // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
     
    119130    };
    120131
    121     TagEditHelper(JTable tagTable, DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) {
     132    public TagEditHelper(JTable tagTable, DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) {
    122133        this.tagTable = tagTable;
    123134        this.tagData = propertyData;
     
    141152        changedKey = null;
    142153        sel = Main.main.getInProgressSelection();
    143         if (sel == null || sel.isEmpty()) return;
    144 
    145         final AddTagsDialog addDialog = new AddTagsDialog();
     154        if (sel == null || sel.isEmpty())
     155            return;
     156
     157        final AddTagsDialog addDialog = getAddTagsDialog();
    146158
    147159        addDialog.showDialog();
     
    152164        else
    153165            addDialog.undoAllTagsAdding();
     166    }
     167
     168    protected AddTagsDialog getAddTagsDialog() {
     169        return new AddTagsDialog();
    154170    }
    155171
     
    163179        changedKey = null;
    164180        sel = Main.main.getInProgressSelection();
    165         if (sel == null || sel.isEmpty()) return;
     181        if (sel == null || sel.isEmpty())
     182            return;
    166183
    167184        String key = getDataKey(row);
    168185        objKey = key;
    169186
    170         final EditTagDialog editDialog = new EditTagDialog(key, getDataValues(row), focusOnKey);
     187        final IEditTagDialog editDialog = getEditTagDialog(row, focusOnKey, key);
    171188        editDialog.showDialog();
    172         if (editDialog.getValue() != 1) return;
     189        if (editDialog.getValue() != 1)
     190            return;
    173191        editDialog.performTagEdit();
     192    }
     193
     194    protected interface IEditTagDialog {
     195        ExtendedDialog showDialog();
     196
     197        int getValue();
     198
     199        void performTagEdit();
     200    }
     201
     202    protected IEditTagDialog getEditTagDialog(int row, boolean focusOnKey, String key) {
     203        return new EditTagDialog(key, getDataValues(row), focusOnKey);
    174204    }
    175205
     
    251281    }
    252282
    253     public final class EditTagDialog extends AbstractTagsDialog {
     283    protected class EditTagDialog extends AbstractTagsDialog implements IEditTagDialog {
    254284        private final String key;
    255285        private final transient Map<String, Integer> m;
     
    278308                    String str = value.getValue();
    279309                    if (valueCount.containsKey(objKey)) {
    280                         Map<String, Integer> m = valueCount.get(objKey);
    281                         if (m.containsKey(str)) {
    282                             str = tr("{0} ({1})", str, m.get(str));
     310                        Map<String, Integer> map = valueCount.get(objKey);
     311                        if (map.containsKey(str)) {
     312                            str = tr("{0} ({1})", str, map.get(str));
    283313                            c.setFont(c.getFont().deriveFont(Font.ITALIC + Font.BOLD));
    284314                        }
     
    290320        };
    291321
    292         private EditTagDialog(String key, Map<String, Integer> map, final boolean initialFocusOnKey) {
     322        protected EditTagDialog(String key, Map<String, Integer> map, final boolean initialFocusOnKey) {
    293323            super(Main.parent, trn("Change value?", "Change values?", map.size()), new String[] {tr("OK"), tr("Cancel")});
    294324            setButtonIcons(new String[] {"ok", "cancel"});
     
    366396         * Confirmations may be needed.
    367397         */
    368         private void performTagEdit() {
     398        @Override
     399        public void performTagEdit() {
    369400            String value = Tag.removeWhiteSpaces(values.getEditor().getItem().toString());
    370401            value = Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
     
    425456    }
    426457
    427     public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = new BooleanProperty("properties.fix-tag-combobox-locale", false);
    428     public static final BooleanProperty PROPERTY_REMEMBER_TAGS = new BooleanProperty("properties.remember-recently-added-tags", true);
    429     public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags",
    430             DEFAULT_LRU_TAGS_NUMBER);
    431 
    432     abstract class AbstractTagsDialog extends ExtendedDialog {
     458    protected abstract class AbstractTagsDialog extends ExtendedDialog {
    433459        protected AutoCompletingComboBox keys;
    434460        protected AutoCompletingComboBox values;
     
    546572    }
    547573
    548     class AddTagsDialog extends AbstractTagsDialog {
     574    protected class AddTagsDialog extends AbstractTagsDialog {
    549575        private final List<JosmAction> recentTagsActions = new ArrayList<>();
     576        protected final transient FocusAdapter focus;
    550577
    551578        // Counter of added commands for possible undo
    552579        private int commandCount;
    553580
    554         AddTagsDialog() {
     581        protected AddTagsDialog() {
    555582            super(Main.parent, tr("Add value?"), new String[] {tr("OK"), tr("Cancel")});
    556583            setButtonIcons(new String[] {"ok", "cancel"});
     
    604631            }
    605632
    606             FocusAdapter focus = addFocusAdapter(autocomplete, defaultACItemComparator);
     633            focus = addFocusAdapter(autocomplete, defaultACItemComparator);
    607634            // fire focus event in advance or otherwise the popup list will be too small at first
    608635            focus.focusGained(null);
    609 
    610             int recentTagsToShow = PROPERTY_RECENT_TAGS_NUMBER.get();
    611             if (recentTagsToShow > MAX_LRU_TAGS_NUMBER) {
    612                 recentTagsToShow = MAX_LRU_TAGS_NUMBER;
    613             }
    614636
    615637            // Add tag on Shift-Enter
     
    624646                });
    625647
    626             suggestRecentlyAddedTags(mainPanel, recentTagsToShow, focus);
     648            suggestRecentlyAddedTags(mainPanel, focus);
    627649
    628650            mainPanel.add(Box.createVerticalGlue(), GBC.eop().fill());
     
    641663                @Override
    642664                public void actionPerformed(ActionEvent e) {
    643                     boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState();
    644                     PROPERTY_REMEMBER_TAGS.put(sel);
    645                     if (sel) saveTagsIfNeeded();
     665                    boolean state = ((JCheckBoxMenuItem) e.getSource()).getState();
     666                    PROPERTY_REMEMBER_TAGS.put(state);
     667                    if (state)
     668                        saveTagsIfNeeded();
    646669                }
    647670            });
     
    674697        }
    675698
    676         private void selectNumberOfTags() {
     699        protected void selectNumberOfTags() {
    677700            String s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display"));
    678701            if (s == null) {
     
    691714        }
    692715
    693         private void suggestRecentlyAddedTags(JPanel mainPanel, int tagsToShow, final FocusAdapter focus) {
     716        protected void suggestRecentlyAddedTags(JPanel mainPanel, final FocusAdapter focus) {
     717            final int tagsToShow = Math.max(PROPERTY_RECENT_TAGS_NUMBER.get(), MAX_LRU_TAGS_NUMBER);
    694718            if (!(tagsToShow > 0 && !recentTags.isEmpty()))
    695719                return;
     
    816840            String key = Tag.removeWhiteSpaces(keys.getEditor().getItem().toString());
    817841            String value = Tag.removeWhiteSpaces(values.getEditor().getItem().toString());
    818             if (key.isEmpty() || value.isEmpty()) return;
    819             for (OsmPrimitive osm: sel) {
     842            if (key.isEmpty() || value.isEmpty())
     843                return;
     844            for (OsmPrimitive osm : sel) {
    820845                String val = osm.get(key);
    821846                if (val != null && !val.equals(value)) {
     
    833858            Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
    834859            changedKey = key;
     860            clearEntries();
     861        }
     862
     863        protected void clearEntries() {
    835864            keys.getEditor().setItem("");
    836865            values.getEditor().setItem("");
Note: See TracChangeset for help on using the changeset viewer.