Changeset 6553 in josm


Ignore:
Timestamp:
2013-12-28T01:03:35+01:00 (10 years ago)
Author:
simon04
Message:

see #9414 - MapCSS-based tagchecker: allow to add custom files in preferences (resumes r6551)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r6552 r6553  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.BufferedReader;
     7import java.io.File;
     8import java.io.FileInputStream;
    69import java.io.InputStreamReader;
    710import java.io.Reader;
    811import java.util.ArrayList;
    912import java.util.Collection;
     13import java.util.Collections;
    1014import java.util.HashMap;
    1115import java.util.LinkedHashMap;
     
    1620import java.util.regex.Pattern;
    1721
     22import org.openstreetmap.josm.Main;
    1823import org.openstreetmap.josm.command.ChangePropertyCommand;
    1924import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
     
    2530import org.openstreetmap.josm.data.osm.Tag;
    2631import org.openstreetmap.josm.data.osm.Way;
     32import org.openstreetmap.josm.data.preferences.CollectionProperty;
    2733import org.openstreetmap.josm.data.validation.FixableTestError;
    2834import org.openstreetmap.josm.data.validation.Severity;
     
    3844import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
    3945import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     46import org.openstreetmap.josm.gui.widgets.EditableList;
    4047import org.openstreetmap.josm.tools.CheckParameterUtil;
     48import org.openstreetmap.josm.tools.GBC;
    4149import org.openstreetmap.josm.tools.Predicate;
    4250import org.openstreetmap.josm.tools.Utils;
    4351
     52import javax.swing.JLabel;
    4453import javax.swing.JPanel;
    4554
     
    384393        addMapCSS("power");
    385394        addMapCSS("geometry");
    386     }
     395        for (final String i : sourcesProperty.get()) {
     396            final String file = new File(i).getAbsolutePath();
     397            try {
     398                Main.info(tr("Adding {0} to tag checker", file));
     399                addMapCSS(new BufferedReader(new InputStreamReader(new FileInputStream(i), Utils.UTF_8)));
     400            } catch (Exception ex) {
     401                Main.warn(new RuntimeException(tr("Failed to add {0} to tag checker", file), ex));
     402            }
     403        }
     404    }
     405
     406    protected EditableList sourcesList;
     407    protected final CollectionProperty sourcesProperty = new CollectionProperty(
     408            "validator." + this.getClass().getName() + ".sources", Collections.<String>emptyList());
    387409
    388410    @Override
    389411    public void addGui(JPanel testPanel) {
    390412        super.addGui(testPanel);
     413        sourcesList = new EditableList(tr("TagChecker source"));
     414        sourcesList.setItems(sourcesProperty.get());
     415        testPanel.add(new JLabel(tr("Data sources ({0})", "*.mapcss")), GBC.eol().insets(23, 0, 0, 0));
     416        testPanel.add(sourcesList, GBC.eol().fill(GBC.HORIZONTAL).insets(23, 0, 0, 0));
    391417    }
    392418
    393419    @Override
    394420    public boolean ok() {
     421        sourcesProperty.put(sourcesList.getItems());
    395422        return super.ok();
    396423    }
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r6552 r6553  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.awt.BorderLayout;
    78import java.awt.Dimension;
    89import java.awt.GridBagConstraints;
    9 import java.awt.GridBagLayout;
    1010import java.awt.event.ActionEvent;
    1111import java.awt.event.ActionListener;
     
    1818import java.util.Arrays;
    1919import java.util.Collection;
     20import java.util.Collections;
    2021import java.util.HashMap;
    2122import java.util.List;
     
    2728import java.util.regex.PatternSyntaxException;
    2829
    29 import javax.swing.DefaultListModel;
    30 import javax.swing.JButton;
    3130import javax.swing.JCheckBox;
    3231import javax.swing.JLabel;
    33 import javax.swing.JList;
    34 import javax.swing.JOptionPane;
    3532import javax.swing.JPanel;
    36 import javax.swing.JScrollPane;
    3733
    3834import org.openstreetmap.josm.Main;
     
    5955import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.CheckGroup;
    6056import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.KeyedItem;
     57import org.openstreetmap.josm.gui.widgets.EditableList;
    6158import org.openstreetmap.josm.io.MirroredInputStream;
    6259import org.openstreetmap.josm.tools.GBC;
     
    128125    protected JCheckBox prefUseSpellFile;
    129126
    130     protected JButton addSrcButton;
    131     protected JButton editSrcButton;
    132     protected JButton deleteSrcButton;
    133 
    134127    protected static final int EMPTY_VALUES      = 1200;
    135128    protected static final int INVALID_KEY       = 1201;
     
    145138    /** 1250 and up is used by tagcheck */
    146139
    147     /** List of sources for spellcheck data */
    148     protected JList sourcesList;
     140    protected EditableList sourcesList;
    149141
    150142    protected static final Entities entities = new Entities();
     
    533525        testPanel.add(prefCheckComplexBeforeUpload, a);
    534526
    535         sourcesList = new JList(new DefaultListModel());
    536 
    537         String sources = Main.pref.get( PREF_SOURCES );
    538         if (sources != null && sources.length() > 0) {
    539             for (String source : sources.split(";")) {
    540                 ((DefaultListModel)sourcesList.getModel()).addElement(source);
    541             }
    542         }
    543 
    544         addSrcButton = new JButton(tr("Add"));
    545         addSrcButton.addActionListener(new ActionListener() {
    546             @Override
    547             public void actionPerformed(ActionEvent e) {
    548                 String source = JOptionPane.showInputDialog(
    549                         Main.parent,
    550                         tr("TagChecker source"),
    551                         tr("TagChecker source"),
    552                         JOptionPane.QUESTION_MESSAGE);
    553                 if (source != null) {
    554                     ((DefaultListModel)sourcesList.getModel()).addElement(source);
    555                 }
    556                 sourcesList.clearSelection();
    557             }
    558         });
    559 
    560         editSrcButton = new JButton(tr("Edit"));
    561         editSrcButton.addActionListener(new ActionListener() {
    562             @Override
    563             public void actionPerformed(ActionEvent e) {
    564                 int row = sourcesList.getSelectedIndex();
    565                 if (row == -1 && sourcesList.getModel().getSize() == 1) {
    566                     sourcesList.setSelectedIndex(0);
    567                     row = 0;
    568                 }
    569                 if (row == -1) {
    570                     if (sourcesList.getModel().getSize() == 0) {
    571                         String source = JOptionPane.showInputDialog(Main.parent, tr("TagChecker source"), tr("TagChecker source"), JOptionPane.QUESTION_MESSAGE);
    572                         if (source != null) {
    573                             ((DefaultListModel)sourcesList.getModel()).addElement(source);
    574                         }
    575                     } else {
    576                         JOptionPane.showMessageDialog(
    577                                 Main.parent,
    578                                 tr("Please select the row to edit."),
    579                                 tr("Information"),
    580                                 JOptionPane.INFORMATION_MESSAGE
    581                                 );
    582                     }
    583                 } else {
    584                     String source = (String)JOptionPane.showInputDialog(Main.parent,
    585                             tr("TagChecker source"),
    586                             tr("TagChecker source"),
    587                             JOptionPane.QUESTION_MESSAGE, null, null,
    588                             sourcesList.getSelectedValue());
    589                     if (source != null) {
    590                         ((DefaultListModel)sourcesList.getModel()).setElementAt(source, row);
    591                     }
    592                 }
    593                 sourcesList.clearSelection();
    594             }
    595         });
    596 
    597         deleteSrcButton = new JButton(tr("Delete"));
    598         deleteSrcButton.addActionListener(new ActionListener() {
    599             @Override
    600             public void actionPerformed(ActionEvent e) {
    601                 if (sourcesList.getSelectedIndex() == -1) {
    602                     JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."), tr("Information"), JOptionPane.QUESTION_MESSAGE);
    603                 } else {
    604                     ((DefaultListModel)sourcesList.getModel()).remove(sourcesList.getSelectedIndex());
    605                 }
    606             }
    607         });
    608         sourcesList.setMinimumSize(new Dimension(300,50));
    609         sourcesList.setVisibleRowCount(3);
    610 
    611         sourcesList.setToolTipText(tr("The sources (URL or filename) of spell check (see http://wiki.openstreetmap.org/index.php/User:JLS/speller) or tag checking data files."));
    612         addSrcButton.setToolTipText(tr("Add a new source to the list."));
    613         editSrcButton.setToolTipText(tr("Edit the selected source."));
    614         deleteSrcButton.setToolTipText(tr("Delete the selected source from the list."));
    615 
    616         testPanel.add(new JLabel(tr("Data sources")), GBC.eol().insets(23,0,0,0));
    617         testPanel.add(new JScrollPane(sourcesList), GBC.eol().insets(23,0,0,0).fill(GridBagConstraints.HORIZONTAL));
    618         final JPanel buttonPanel = new JPanel(new GridBagLayout());
    619         testPanel.add(buttonPanel, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    620         buttonPanel.add(addSrcButton, GBC.std().insets(0,5,0,0));
    621         buttonPanel.add(editSrcButton, GBC.std().insets(5,5,5,0));
    622         buttonPanel.add(deleteSrcButton, GBC.std().insets(0,5,0,0));
     527        final String sources = Main.pref.get(PREF_SOURCES);
     528        sourcesList = new EditableList(tr("TagChecker source"));
     529        sourcesList.setItems(sources != null ? Arrays.asList(sources.split(";")) : Collections.<String>emptyList());
     530        testPanel.add(new JLabel(tr("Data sources ({0})", "*.cfg")), GBC.eol().insets(23, 0, 0, 0));
     531        testPanel.add(sourcesList, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(23, 0, 0, 0));
    623532
    624533        ActionListener disableCheckActionListener = new ActionListener() {
     
    667576        boolean selected = prefCheckKeys.isSelected() || prefCheckKeysBeforeUpload.isSelected()
    668577                || prefCheckComplex.isSelected() || prefCheckComplexBeforeUpload.isSelected();
    669         sourcesList.setEnabled( selected );
    670         addSrcButton.setEnabled(selected);
    671         editSrcButton.setEnabled(selected);
    672         deleteSrcButton.setEnabled(selected);
     578        sourcesList.setEnabled(selected);
    673579    }
    674580
     
    690596        Main.pref.put(PREF_USE_IGNORE_FILE, prefUseIgnoreFile.isSelected());
    691597        Main.pref.put(PREF_USE_SPELL_FILE, prefUseSpellFile.isSelected());
    692         StringBuilder sources = new StringBuilder();
    693         if (sourcesList.getModel().getSize() > 0) {
    694             for (int i = 0; i < sourcesList.getModel().getSize(); ++i) {
    695                 if (sources.length() > 0) {
    696                     sources.append(";");
    697                 }
    698                 sources.append(sourcesList.getModel().getElementAt(i));
    699             }
    700         }
    701         return Main.pref.put(PREF_SOURCES, sources.length() > 0 ? sources.toString() : null);
     598        final List<String> sources = sourcesList.getItems();
     599        return Main.pref.put(PREF_SOURCES, sources.isEmpty() ? null : Utils.join(";", sources));
    702600    }
    703601
Note: See TracChangeset for help on using the changeset viewer.