Ignore:
Timestamp:
2007-05-06T19:50:55+02:00 (17 years ago)
Author:
frsantos
Message:

Read spellcheck data from user file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/SpellCheck.java

    r2611 r2790  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.awt.GridBagLayout;
     6import java.awt.event.ActionEvent;
     7import java.awt.event.ActionListener;
    58import java.io.*;
    69import java.net.URL;
     
    811import java.util.Map.Entry;
    912
    10 import javax.swing.JCheckBox;
    11 import javax.swing.JPanel;
     13import javax.swing.*;
    1214
    1315import org.openstreetmap.josm.Main;
     
    1921import org.openstreetmap.josm.plugins.validator.*;
    2022import org.openstreetmap.josm.plugins.validator.util.Bag;
     23import org.openstreetmap.josm.plugins.validator.util.Util;
    2124import org.openstreetmap.josm.tools.GBC;
    2225import org.openstreetmap.josm.tools.XmlObjectParser;
     
    3033public class SpellCheck extends Test
    3134{
    32         /** The spell check key substitutions: the key should be substituted by the value */
     35        /** The default spellcheck data file */
     36    public static final String SPELLCHECK_DATA_FILE = "http://svn.openstreetmap.org/applications/utils/planet.osm/java/speller/words.cfg";
     37
     38    /** The spell check key substitutions: the key should be substituted by the value */
    3339        protected static Map<String, String> spellCheckKeyData;
    3440
     
    3642        protected static Bag<String, String> spellCheckValueData;
    3743       
    38         /** Preference name for checking values */
    39         public static final String PREF_CHECK_VALUES = "tests." + SpellCheck.class.getSimpleName() + ".checkValues";
    40        
    41         /** Whether to check values too */
    42         protected boolean checkValues = false;
    43 
    44         /** Preferences checkbox */
    45         protected JCheckBox prefCheckValues;
     44    /** Preference name for checking values */
     45    public static final String PREF_CHECK_VALUES = "tests." + SpellCheck.class.getSimpleName() + ".checkValues";
     46    /** Preference name for checking values */
     47    public static final String PREF_CHECK_KEYS = "tests." + SpellCheck.class.getSimpleName() + ".checkKeys";
     48    /** Preference name for sources */
     49    public static final String PREF_SOURCES = "tests." + SpellCheck.class.getSimpleName() + ".sources";
     50    /** Preference name for global upload check */
     51    public static final String PREF_CHECK_BEFORE_UPLOAD = "tests." + SpellCheck.class.getSimpleName() + ".checkBeforeUpload";
     52    /** Preference name for keys upload check */
     53    public static final String PREF_CHECK_KEYS_BEFORE_UPLOAD = "tests." + SpellCheck.class.getSimpleName() + ".checkKeysBeforeUpload";
     54    /** Preference name for values upload check */
     55    public static final String PREF_CHECK_VALUES_BEFORE_UPLOAD = "tests." + SpellCheck.class.getSimpleName() + ".checkValuesBeforeUpload";
     56       
     57    /** Whether to check keys */
     58    protected boolean checkKeys = false;
     59    /** Whether to check values */
     60    protected boolean checkValues = false;
     61
     62    /** Preferences checkbox for keys */
     63    protected JCheckBox prefCheckKeys;
     64    /** Preferences checkbox for values */
     65    protected JCheckBox prefCheckValues;
     66    /** The preferences checkbox for validation of keys on upload */
     67    protected JCheckBox prefCheckKeysBeforeUpload;
     68    /** The preferences checkbox for validation of values on upload */
     69    protected JCheckBox prefCheckValuesBeforeUpload;
     70    /** The add button */
     71    protected JButton addSrcButton;
     72    /** The edit button */
     73    protected JButton editSrcButton;
     74    /** The delete button */
     75    protected JButton deleteSrcButton;
    4676
    4777        /** Empty values error */
     
    5282        protected static int INVALID_VALUE      = 2;
    5383       
     84    /** List of sources for spellcheck data */
     85    protected JList spellcheckSources;
     86
     87    /** Whether this test must check the keys before upload. Used by peferences */
     88    protected boolean testKeysBeforeUpload;
     89    /** Whether this test must check the values before upload. Used by peferences */
     90    protected boolean testValuesBeforeUpload;
     91   
    5492        /**
    5593         * Constructor
     
    5795        public SpellCheck()
    5896        {
    59                 super(tr("Spell checker."),
     97                super(tr("Properties spell checker."),
    6098                          tr("This plugin checks misspelled property keys and values."));
    6199        }
     
    80118        private static void initializeSpellCheck(OSMValidatorPlugin plugin) throws FileNotFoundException, IOException
    81119        {
    82                 plugin.copy("/resources/spellCheck.data", "spellCheck.data");
    83                 BufferedReader reader = new BufferedReader( new FileReader(plugin.getPluginDir() + "/spellCheck.data") );
    84                
    85120                spellCheckKeyData = new HashMap<String, String>();
    86                 String okValue = null;
    87                 do
    88                 {
    89                         String line = reader.readLine();
    90                         if( line == null || line.length() == 0 )
    91                                 break;
    92 
    93                         if( line.charAt(0) == '+' )
    94                         {
    95                                 okValue = line.substring(1);
    96                         }
    97                         else if( line.charAt(0) == '-' && okValue != null )
    98                         {
    99                                 spellCheckKeyData.put(line.substring(1), okValue);
    100                         }
    101                         else
    102                         {
    103                                 System.err.println("Invalid spellcheck line:" + line);
    104                         }
    105                 }
    106                 while( true );
     121        String sources = Main.pref.get( PREF_SOURCES );
     122        if( sources == null || sources.length() == 0)
     123            sources = SPELLCHECK_DATA_FILE;
     124       
     125        StringTokenizer st = new StringTokenizer(sources, ";");
     126        while (st.hasMoreTokens())
     127        {
     128            String source = st.nextToken();
     129            File sourceFile = Util.mirror(new URL(source), plugin.getPluginDir());
     130            BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
     131
     132                String okValue = null;
     133                do
     134                {
     135                        String line = reader.readLine();
     136                if( line == null || line.length() == 0 )
     137                    break;
     138                if( line.startsWith("#") )
     139                    continue;
     140   
     141                        if( line.charAt(0) == '+' )
     142                        {
     143                                okValue = line.substring(1);
     144                        }
     145                        else if( line.charAt(0) == '-' && okValue != null )
     146                        {
     147                                spellCheckKeyData.put(line.substring(1), okValue);
     148                        }
     149                        else
     150                        {
     151                                System.err.println("Invalid spellcheck line:" + line);
     152                        }
     153                }
     154                while( true );
     155        }
    107156        }
    108157       
     
    127176                spellCheckValueData = new Bag<String, String>();
    128177                readPresetFromPreferences();
    129                
    130                 // TODO: allow per user word definitions
    131178        }
    132179       
     
    166213                        String key = prop.getKey();
    167214                        String value = prop.getValue();
    168                         if( (value==null || value.trim().length() == 0) && !withErrors.contains(p, "EV"))
     215                        if( checkValues && (value==null || value.trim().length() == 0) && !withErrors.contains(p, "EV"))
    169216                        {
    170217                                errors.add( new TestError(this, Severity.WARNING, tr("Tags with empty values"), p, EMPTY_VALUES) );
    171218                                withErrors.add(p, "EV");
    172219                        }
    173                         if( spellCheckKeyData.containsKey(key) && !withErrors.contains(p, "IPK"))
     220                        if( checkKeys && spellCheckKeyData.containsKey(key) && !withErrors.contains(p, "IPK"))
    174221                        {
    175222                                errors.add( new TestError(this, Severity.WARNING, tr("Invalid property keys"), p, INVALID_KEY) );
     
    263310        public void startTest()
    264311        {
    265                 checkValues = Main.pref.getBoolean(PREF_CHECK_VALUES);
    266         }
    267 
     312        checkKeys = Main.pref.getBoolean(PREF_CHECK_KEYS);
     313        if( isBeforeUpload )
     314            checkKeys = checkKeys && Main.pref.getBoolean(PREF_CHECK_KEYS_BEFORE_UPLOAD);
     315
     316        checkValues = Main.pref.getBoolean(PREF_CHECK_VALUES);
     317        if( isBeforeUpload )
     318            checkValues = checkValues && Main.pref.getBoolean(PREF_CHECK_VALUES_BEFORE_UPLOAD);
     319        }
     320
     321    @Override
     322    public void visit(Collection<OsmPrimitive> selection)
     323    {
     324        if( checkKeys || checkValues)
     325            super.visit(selection);
     326    }
     327   
    268328        @Override
    269329        public void addGui(JPanel testPanel)
    270330        {
    271                 boolean checkValues = Main.pref.getBoolean(PREF_CHECK_VALUES);
    272                
    273                 String text = tr("Check also property values from presets");
    274                 prefCheckValues = new JCheckBox(text, checkValues);
    275                 prefCheckValues.setToolTipText(text);
    276                 testPanel.add(prefCheckValues, GBC.eop().insets(40,0,0,0));
    277         }
    278 
     331        testPanel.add( new JLabel(), GBC.eol());
     332       
     333        boolean checkKeys = Main.pref.getBoolean(PREF_CHECK_KEYS);
     334        prefCheckKeys = new JCheckBox(tr("Check property keys."), checkKeys);
     335        prefCheckKeys .setToolTipText(tr("Validate that property keys are valid checking against list of words."));
     336        testPanel.add(prefCheckKeys, GBC.std().insets(40,0,0,0));
     337
     338        prefCheckKeysBeforeUpload = new JCheckBox();
     339        prefCheckKeysBeforeUpload.setSelected(Main.pref.getBoolean(PREF_CHECK_KEYS_BEFORE_UPLOAD));
     340        testPanel.add(prefCheckKeysBeforeUpload, GBC.eop().insets(20,0,0,0));
     341       
     342        spellcheckSources = new JList(new DefaultListModel());
     343        if( !Main.pref.hasKey(PREF_SOURCES))
     344            Main.pref.put(PREF_SOURCES, SPELLCHECK_DATA_FILE);
     345       
     346        String sources = Main.pref.get( PREF_SOURCES );
     347        StringTokenizer st = new StringTokenizer(sources, ";");
     348        while (st.hasMoreTokens())
     349            ((DefaultListModel)spellcheckSources.getModel()).addElement(st.nextToken());
     350       
     351        addSrcButton = new JButton(tr("Add"));
     352        addSrcButton.addActionListener(new ActionListener(){
     353            public void actionPerformed(ActionEvent e) {
     354                String source = JOptionPane.showInputDialog(Main.parent, tr("Spellcheck source"));
     355                if (source == null)
     356                    return;
     357                ((DefaultListModel)spellcheckSources.getModel()).addElement(source);
     358            }
     359        });
     360
     361        editSrcButton = new JButton(tr("Edit"));
     362        editSrcButton.addActionListener(new ActionListener(){
     363            public void actionPerformed(ActionEvent e) {
     364                if (spellcheckSources.getSelectedIndex() == -1)
     365                    JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to edit."));
     366                else {
     367                    String source = JOptionPane.showInputDialog(Main.parent, tr("Spellcheck source"), spellcheckSources.getSelectedValue());
     368                    if (source == null)
     369                        return;
     370                    ((DefaultListModel)spellcheckSources.getModel()).setElementAt(source, spellcheckSources.getSelectedIndex());
     371                }
     372            }
     373        });
     374
     375        deleteSrcButton = new JButton(tr("Delete"));
     376        deleteSrcButton.addActionListener(new ActionListener(){
     377            public void actionPerformed(ActionEvent e) {
     378                if (spellcheckSources.getSelectedIndex() == -1)
     379                    JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."));
     380                else {
     381                    ((DefaultListModel)spellcheckSources.getModel()).remove(spellcheckSources.getSelectedIndex());
     382                }
     383            }
     384        });
     385        spellcheckSources.setVisibleRowCount(3);
     386
     387        spellcheckSources.setToolTipText(tr("The sources (url or filename) of spell check data files. See http://wiki.openstreetmap.org/index.php/User:JLS/speller for help."));
     388        addSrcButton.setToolTipText(tr("Add a new spellcheck source to the list."));
     389        editSrcButton.setToolTipText(tr("Edit the selected source."));
     390        deleteSrcButton.setToolTipText(tr("Delete the selected source from the list."));
     391
     392        testPanel.add(new JLabel(tr("Spellcheck data sources")), GBC.eol().insets(40,0,0,0));
     393        testPanel.add(new JScrollPane(spellcheckSources), GBC.eol().insets(40,0,0,0));
     394        final JPanel buttonPanel = new JPanel(new GridBagLayout());
     395        testPanel.add(buttonPanel, GBC.eol().fill(GBC.HORIZONTAL));
     396        buttonPanel.add(addSrcButton, GBC.std().insets(0,5,0,0));
     397        buttonPanel.add(editSrcButton, GBC.std().insets(5,5,5,0));
     398        buttonPanel.add(deleteSrcButton, GBC.std().insets(0,5,0,0));
     399       
     400        prefCheckKeys.addActionListener(new ActionListener(){
     401            public void actionPerformed(ActionEvent e) {
     402                boolean selected = prefCheckKeys.isSelected();
     403                spellcheckSources.setEnabled( selected );
     404                addSrcButton.setEnabled(selected);
     405                editSrcButton.setEnabled(selected);
     406                deleteSrcButton.setEnabled(selected);
     407            }
     408        });
     409       
     410        spellcheckSources.setEnabled( checkKeys );
     411        buttonPanel.setEnabled( checkKeys );
     412       
     413        boolean checkValues = Main.pref.getBoolean(PREF_CHECK_VALUES);
     414        prefCheckValues = new JCheckBox(tr("Check property values."), checkValues);
     415        prefCheckValues .setToolTipText(tr("Validate that property values are valid checking against presets."));
     416                testPanel.add(prefCheckValues, GBC.std().insets(40,0,0,0));
     417
     418        prefCheckValuesBeforeUpload = new JCheckBox();
     419        prefCheckValuesBeforeUpload.setSelected(Main.pref.getBoolean(PREF_CHECK_VALUES_BEFORE_UPLOAD));
     420        testPanel.add(prefCheckValuesBeforeUpload, GBC.eop().insets(20,0,0,0));
     421       
     422        }
     423
     424    public void setGuiEnabled(boolean enabled)
     425    {
     426        prefCheckKeys.setEnabled(enabled);
     427        prefCheckKeysBeforeUpload.setEnabled(enabled);
     428        spellcheckSources.setEnabled( enabled );
     429        addSrcButton.setEnabled(enabled);
     430        editSrcButton.setEnabled(enabled);
     431        deleteSrcButton.setEnabled(enabled);
     432        prefCheckValues.setEnabled(enabled);
     433        prefCheckValuesBeforeUpload.setEnabled(enabled);
     434    }
     435   
    279436        @Override
    280437        public void ok()
    281438        {
    282                 Main.pref.put(PREF_CHECK_VALUES, prefCheckValues.isSelected());
     439        Main.pref.put(PREF_CHECK_VALUES, prefCheckValues.isSelected());
     440        Main.pref.put(PREF_CHECK_KEYS, prefCheckKeys.isSelected());
     441        Main.pref.put(PREF_CHECK_VALUES_BEFORE_UPLOAD, prefCheckValuesBeforeUpload.isSelected());
     442        Main.pref.put(PREF_CHECK_KEYS_BEFORE_UPLOAD, prefCheckKeysBeforeUpload.isSelected());
     443        Main.pref.put(PREF_CHECK_BEFORE_UPLOAD, prefCheckKeysBeforeUpload.isSelected() || prefCheckValuesBeforeUpload.isSelected());           
     444        String sources = "";
     445        if( spellcheckSources.getModel().getSize() > 0 )
     446        {
     447            StringBuilder sb = new StringBuilder();
     448            for (int i = 0; i < spellcheckSources.getModel().getSize(); ++i)
     449                sb.append(";"+spellcheckSources.getModel().getElementAt(i));
     450            sources = sb.substring(1);
     451        }
     452        Main.pref.put(PREF_SOURCES, sources );
     453
    283454        }
    284455       
Note: See TracChangeset for help on using the changeset viewer.