Ignore:
Timestamp:
2018-05-21T11:21:59+02:00 (6 years ago)
Author:
nzara
Message:

Prepare for user prefereneces, add some data entry checks/feedback

Location:
applications/editors/josm/plugins/pdfimport/src/pdfimport
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java

    r34201 r34211  
    1414import java.awt.event.ActionEvent;
    1515import java.awt.event.ActionListener;
     16import java.awt.event.FocusEvent;
     17import java.awt.event.FocusListener;
    1618import java.awt.event.KeyEvent;
    1719import java.awt.event.WindowAdapter;
     
    4244import javax.swing.KeyStroke;
    4345import javax.swing.SwingUtilities;
     46import javax.swing.border.Border;
    4447import javax.swing.filechooser.FileFilter;
    4548
     
    6164import org.openstreetmap.josm.gui.progress.swing.SwingRenderingProgressMonitor;
    6265import org.openstreetmap.josm.gui.util.WindowGeometry;
    63 import org.openstreetmap.josm.spi.preferences.Config;
    6466import org.openstreetmap.josm.tools.ImageProvider;
    6567import org.openstreetmap.josm.tools.Logging;
     
    161163    private JCheckBox splitOnSingleSegmentCheck;
    162164    private JCheckBox splitOnOrthogonalCheck;
     165    private Border  defaulfBorder = (new JTextField()).getBorder();
    163166
    164167    public LoadPdfDialog() {
     168        Logging.debug("PdfImport:LoadPdfDialog");
    165169        this.buildGUI();
    166170        FilePlacement pl = new FilePlacement();
     
    170174    }
    171175
    172      private void addListeners() {
    173 
    174         this.projectionCombo.addActionListener(new ActionListener() {
    175             @Override
    176             public void actionPerformed(ActionEvent e) {
    177                 updateProjectionPrefButton();
    178             }
    179 
    180         });
    181         this.projectionPreferencesButton.addActionListener(new ActionListener() {
    182             @Override
    183             public void actionPerformed(ActionEvent e) {
    184                 showProjectionPreferences();
    185             }
    186         });
    187 
    188         this.loadFileButton.addActionListener(new ActionListener() {
    189             @Override
    190             public void actionPerformed(ActionEvent e) {
    191                 loadFilePressed();
    192             }
    193         });
    194 
    195         this.okButton.addActionListener(new ActionListener() {
    196             @Override
    197             public void actionPerformed(ActionEvent e) {
    198                 okPressed();
    199             }
    200         });
    201 
    202         this.saveButton.addActionListener(new ActionListener() {
    203             @Override
    204             public void actionPerformed(ActionEvent e) {
    205                 savePressed();
    206             }
    207         });
    208 
    209         this.showButton.addActionListener(new ActionListener() {
    210             @Override
    211             public void actionPerformed(ActionEvent e) {
    212                 showPressed();
    213             }
    214         });
    215 
    216         this.cancelButton.addActionListener(new ActionListener() {
    217             @Override
    218             public void actionPerformed(ActionEvent e) {
    219                 cancelPressed();
    220             }
    221         });
    222 
    223         this.addWindowListener(new WindowAdapter() {
    224             @Override
    225             public void windowClosing(WindowEvent e) {
    226                 cancelPressed();
    227             }
    228         });
    229 
    230         this.getMinButton.addActionListener(new ActionListener() {
    231             @Override
    232             public void actionPerformed(ActionEvent e) {
    233                 getMinPressed();
    234             }
    235         });
    236 
    237         this.getMaxButton.addActionListener(new ActionListener() {
    238             @Override
    239             public void actionPerformed(ActionEvent e) {
    240                 getMaxPressed();
    241             }
    242         });
    243     }
     176        private class CheckDouble implements FocusListener {
     177
     178                @Override
     179                public void focusGained(FocusEvent e) {
     180                }
     181
     182                @Override
     183                public void focusLost(FocusEvent event) {
     184                        check((JTextField) event.getSource());
     185                }
     186
     187                public void check(JTextField t) {
     188                        try {
     189                                Double.valueOf(t.getText());
     190                                t.setBorder((LoadPdfDialog.this.defaulfBorder));
     191                        } catch (NumberFormatException e) {
     192                                t.setBorder(BorderFactory.createLineBorder(Color.red));
     193                        }
     194                }
     195        }
     196
     197private CheckDouble doublelistener = new CheckDouble();
     198
     199        private void addListeners() {
     200
     201                this.maxEastField.addFocusListener(doublelistener);
     202                this.maxEastField.addFocusListener(new FocusListener() {
     203                        @Override
     204                        public void focusLost(FocusEvent e) {
     205                                checkCoords(LoadPdfDialog.this.maxEastField, LoadPdfDialog.this.maxNorthField);
     206                        }
     207
     208                        @Override
     209                        public void focusGained(FocusEvent e) {
     210                        }
     211                });
     212                this.maxNorthField.addFocusListener(doublelistener);
     213
     214                this.minEastField.addFocusListener(doublelistener);
     215                this.minEastField.addFocusListener(new FocusListener() {
     216                        @Override
     217                        public void focusLost(FocusEvent e) {
     218                                checkCoords(LoadPdfDialog.this.minEastField, LoadPdfDialog.this.minNorthField);
     219                        }
     220
     221                        @Override
     222                        public void focusGained(FocusEvent e) {
     223                        }
     224                });
     225
     226                this.minNorthField.addFocusListener(doublelistener);
     227
     228                this.minXField.addFocusListener(doublelistener);
     229                this.minXField.addFocusListener(doublelistener);
     230                this.minXField.addFocusListener(new FocusListener() {
     231                        @Override
     232                        public void focusLost(FocusEvent e) {
     233                                checkCoords(LoadPdfDialog.this.minXField, LoadPdfDialog.this.minYField);
     234                        }
     235
     236                        @Override
     237                        public void focusGained(FocusEvent e) {
     238                        }
     239                });
     240
     241                this.minYField.addFocusListener(doublelistener);
     242
     243                this.maxXField.addFocusListener(doublelistener);
     244                this.maxXField.addFocusListener(new FocusListener() {
     245                        @Override
     246                        public void focusLost(FocusEvent e) {
     247                                checkCoords(LoadPdfDialog.this.maxXField, LoadPdfDialog.this.maxYField);
     248                        }
     249
     250                        @Override
     251                        public void focusGained(FocusEvent e) {
     252                        }
     253                });
     254
     255                this.maxYField.addFocusListener(doublelistener);
     256
     257                this.projectionCombo.addActionListener(new ActionListener() {
     258                        @Override
     259                        public void actionPerformed(ActionEvent e) {
     260                                updateProjectionPrefButton();
     261                        }
     262
     263                });
     264                this.projectionPreferencesButton.addActionListener(new ActionListener() {
     265                        @Override
     266                        public void actionPerformed(ActionEvent e) {
     267                                showProjectionPreferences();
     268                        }
     269                });
     270
     271                this.loadFileButton.addActionListener(new ActionListener() {
     272                        @Override
     273                        public void actionPerformed(ActionEvent e) {
     274                                loadFilePressed();
     275                        }
     276                });
     277
     278                this.okButton.addActionListener(new ActionListener() {
     279                        @Override
     280                        public void actionPerformed(ActionEvent e) {
     281                                okPressed();
     282                        }
     283                });
     284
     285                this.saveButton.addActionListener(new ActionListener() {
     286                        @Override
     287                        public void actionPerformed(ActionEvent e) {
     288                                savePressed();
     289                        }
     290                });
     291
     292                this.showButton.addActionListener(new ActionListener() {
     293                        @Override
     294                        public void actionPerformed(ActionEvent e) {
     295                                showPressed();
     296                        }
     297                });
     298
     299                this.cancelButton.addActionListener(new ActionListener() {
     300                        @Override
     301                        public void actionPerformed(ActionEvent e) {
     302                                cancelPressed();
     303                        }
     304                });
     305
     306                this.addWindowListener(new WindowAdapter() {
     307                        @Override
     308                        public void windowClosing(WindowEvent e) {
     309                                cancelPressed();
     310                        }
     311                });
     312
     313                this.getMinButton.addActionListener(new ActionListener() {
     314                        @Override
     315                        public void actionPerformed(ActionEvent e) {
     316                                getMinPressed();
     317                        }
     318                });
     319
     320                this.getMaxButton.addActionListener(new ActionListener() {
     321                        @Override
     322                        public void actionPerformed(ActionEvent e) {
     323                                getMaxPressed();
     324                        }
     325                });
     326        }
    244327
    245328    JPanel projectionPanel = null;
     
    260343        updateProjectionPrefButton();
    261344
    262         this.loadFileButton = new JButton(tr("Load file..."));
    263         this.okButton = new JButton(tr("Place"));
     345        this.loadFileButton = new JButton(tr("Load preview ..."));
     346        this.okButton = new JButton(tr("Import"));
    264347        this.saveButton = new JButton(tr("Save"));
    265348        this.showButton = new JButton(tr("Show target"));
    266         this.cancelButton = new JButton(tr("Discard"));
     349        this.cancelButton = new JButton(tr("Cancel"));
    267350        this.loadProgress = new JProgressBar();
    268351        this.progressRenderer = new LoadProgressRenderer(this.loadProgress);
     
    280363        this.getMaxButton = new JButton(tr("Take X and Y from selected node"));
    281364
    282         this.debugModeCheck = new JCheckBox(tr("Debug info"));
    283         this.mergeCloseNodesCheck = new JCheckBox(tr("Merge close nodes"));
    284         this.mergeCloseNodesTolerance = new JTextField("1e-3");
    285 
    286         this.removeSmallObjectsCheck = new JCheckBox(tr("Remove objects smaller than"));
    287         this.removeSmallObjectsSize = new JTextField("1");
    288 
    289         this.removeLargeObjectsCheck = new JCheckBox(tr("Remove objects larger than"));
    290         this.removeLargeObjectsSize = new JTextField("10");
    291 
    292 
    293         this.colorFilterCheck = new JCheckBox(tr("Only this color"));
    294         this.colorFilterColor = new JTextField("#000000");
    295 
    296         this.removeParallelSegmentsCheck = new JCheckBox(tr("Remove parallel lines"));
    297         this.removeParallelSegmentsTolerance = new JTextField("3");
    298 
    299         this.limitPathCountCheck = new JCheckBox(tr("Take only first X paths"));
    300         this.limitPathCount = new JTextField("10000");
    301 
    302         this.splitOnColorChangeCheck = new JCheckBox(tr("Color/width change"));
    303         this.splitOnShapeClosedCheck = new JCheckBox(tr("Shape closed"));
    304         this.splitOnSingleSegmentCheck = new JCheckBox(tr("Single segments"));
    305         this.splitOnOrthogonalCheck = new JCheckBox(tr("Orthogonal shapes"));
     365        this.debugModeCheck = new JCheckBox(tr("Debug info"),PdfImportPlugin.Preferences.DebugTags);
     366        this.mergeCloseNodesCheck = new JCheckBox(tr("Merge close nodes"),PdfImportPlugin.Preferences.MergeNodes);
     367        this.mergeCloseNodesTolerance = new JTextField(Double.toString(PdfImportPlugin.Preferences.MergeNodesValue));
     368
     369        this.removeSmallObjectsCheck = new JCheckBox(tr("Remove objects smaller than"),PdfImportPlugin.Preferences.RemoveSmall);
     370        this.removeSmallObjectsSize = new JTextField(Double.toString(PdfImportPlugin.Preferences.RemoveSmallValue));
     371
     372        this.removeLargeObjectsCheck = new JCheckBox(tr("Remove objects larger than"),PdfImportPlugin.Preferences.RemoveLarge);
     373        this.removeLargeObjectsSize = new JTextField(Double.toString(PdfImportPlugin.Preferences.RemoveLargeValue));
     374
     375
     376        this.colorFilterCheck = new JCheckBox(tr("Only this color"),PdfImportPlugin.Preferences.LimitColor);
     377        this.colorFilterColor = new JTextField(PdfImportPlugin.Preferences.LimitColorValue);
     378
     379        this.removeParallelSegmentsCheck = new JCheckBox(tr("Remove parallel lines"),PdfImportPlugin.Preferences.RemoveParallel);
     380        this.removeParallelSegmentsTolerance = new JTextField(Double.toString(PdfImportPlugin.Preferences.RemoveParallelValue));
     381
     382        this.limitPathCountCheck = new JCheckBox(tr("Take only first X paths"),PdfImportPlugin.Preferences.LimitPath);
     383        this.limitPathCount = new JTextField(Integer.toString(PdfImportPlugin.Preferences.LimitPathValue));
     384
     385        this.splitOnColorChangeCheck = new JCheckBox(tr("Color/width change"),PdfImportPlugin.Preferences.LayerAttribChange);
     386        this.splitOnShapeClosedCheck = new JCheckBox(tr("Shape closed"),PdfImportPlugin.Preferences.LayerClosed);
     387        this.splitOnSingleSegmentCheck = new JCheckBox(tr("Single segments",PdfImportPlugin.Preferences.LayerSegment));
     388        this.splitOnOrthogonalCheck = new JCheckBox(tr("Orthogonal shapes",PdfImportPlugin.Preferences.LayerOrtho));
    306389
    307390        JPanel configPanel = new JPanel(new GridBagLayout());
     
    541624    }
    542625
     626        private void checkCoords(JTextField x, JTextField y) {
     627                int splitpos = 0;
     628                String eastVal = x.getText().trim();
     629                if ((splitpos = eastVal.indexOf(';')) >= 0) {
     630                        // Split a coordinate into its parts for easy of data entry
     631                        y.setText(eastVal.substring(splitpos + 1).trim());
     632                        x.setText(eastVal.substring(0, splitpos).trim());
     633                }
     634                doublelistener.check(x);
     635                doublelistener.check(y);
     636        }
     637
    543638    private void showProjectionPreferences() {
    544639        ProjectionChoice proj = (ProjectionChoice) projectionCombo.getSelectedItem();
     
    555650            return;
    556651        }
    557 
     652        Logging.debug("PdfImport: Load Preview");
    558653        this.removeLayer();
    559654
     
    591686                            pdfFile = newFileName;
    592687                            newLayer = null;
    593                             LoadPdfDialog.this.loadFileButton.setText(tr("Loaded") + ": " + LoadPdfDialog.this.pdfFile.getName());
     688                            LoadPdfDialog.this.loadFileButton.setText(tr("Preview") + ": " + LoadPdfDialog.this.pdfFile.getName());
    594689                            LoadPdfDialog.this.loadFileButton.setEnabled(true);
    595                             FilePlacement placement = LoadPdfDialog.this.loadPlacement();
     690                            FilePlacement placement = LoadPdfDialog.this.loadPlacement(pdfFile);
    596691                            LoadPdfDialog.this.setPlacement(placement);
    597692                        }
     
    623718            return;
    624719        }
    625 
     720        Logging.debug("PdfImport: Import");
    626721        this.removeLayer();
    627722
     
    740835        //get PDF file to load
    741836        if (loadChooser == null) {
    742                 loadChooser = new JFileChooser(Config.getPref().get("pdfimport.loadDir"));
     837                loadChooser = new JFileChooser(PdfImportPlugin.Preferences.LoadDir);
    743838                loadChooser.setAcceptAllFileFilterUsed(false);
    744839                loadChooser.setMultiSelectionEnabled(false);
     
    757852                loadChooser.rescanCurrentDirectory();
    758853        }
    759         int result = loadChooser.showOpenDialog(this);
    760 
     854        int result = loadChooser.showDialog(this, tr("Preview"));
    761855        if (result != JFileChooser.APPROVE_OPTION) {
    762856            return null;
     
    10201114    }
    10211115
    1022     private FilePlacement loadPlacement() {
     1116    private FilePlacement loadPlacement(File BaseFile) {
    10231117        FilePlacement pl = null;
    10241118        //load saved transformation
    1025         File propertiesFile = new File(pdfFile.getAbsoluteFile() + ".placement");
     1119        File propertiesFile = new File(BaseFile.getAbsoluteFile() + ".placement");
    10261120        try {
    10271121
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfImportPlugin.java

    r34201 r34211  
    66import org.openstreetmap.josm.plugins.Plugin;
    77import org.openstreetmap.josm.plugins.PluginInformation;
     8import org.openstreetmap.josm.spi.preferences.Config;
    89
    910/**
    1011 * A plugin to import a PDF file.
    1112 */
     13
    1214public class PdfImportPlugin extends Plugin {
    1315
    14     public PdfImportPlugin(PluginInformation info) {
    15         super(info);
    16         MainMenu.add(MainApplication.getMenu().imagerySubMenu, new PdfImportAction());
    17     }
     16        public enum GuiMode {
     17                Auto, Expert, Simple
     18        };
     19
     20        public static class Preferences {
     21
     22                public static String LoadDir;
     23                public static GuiMode guiMode;
     24                public static boolean MergeNodes;
     25                public static double MergeNodesValue;
     26                public static boolean RemoveSmall;
     27                public static double RemoveSmallValue;
     28                public static boolean RemoveLarge;
     29                public static double RemoveLargeValue;
     30                public static boolean RemoveParallel;
     31                public static double RemoveParallelValue;
     32                public static boolean LimitPath;
     33                public static int LimitPathValue;
     34                public static boolean LimitColor;
     35                public static String LimitColorValue;
     36                public static boolean DebugTags;
     37                public static boolean LayerClosed;
     38                public static boolean LayerSegment;
     39                public static boolean LayerAttribChange;
     40                public static boolean LayerOrtho;
     41
     42                protected static int GuiCode;
     43
     44                private static String PreFix;
     45
     46                private Preferences() {
     47                        return;
     48                }
     49
     50                public static void reload(String Name) {
     51                        Preferences.PreFix = Name + ".";
     52                        Preferences.LoadDir = Config.getPref().get(Preferences.PreFix + "loadDir");
     53                        Preferences.GuiCode = Config.getPref().getInt(Preferences.PreFix + "GuiCode",0 );
     54                        switch (Preferences.GuiCode) {
     55                        case 1:
     56                                Preferences.guiMode = GuiMode.Expert;
     57                        case 2:
     58                                Preferences.guiMode = GuiMode.Simple;
     59                        default:
     60                                Preferences.guiMode = GuiMode.Expert;
     61                        }
     62                        Preferences.MergeNodes = Config.getPref().getBoolean(Preferences.PreFix + "MergeNodes");
     63                        Preferences.MergeNodesValue = Config.getPref().getDouble(Preferences.PreFix + "MergeNodes.Value", 1e-3);
     64                        Preferences.RemoveSmall = Config.getPref().getBoolean(Preferences.PreFix + "RemoveSmall");
     65                        Preferences.RemoveSmallValue = Config.getPref().getDouble(Preferences.PreFix + "RemoveSmall.Value", 1);
     66                        Preferences.RemoveLarge = Config.getPref().getBoolean(Preferences.PreFix + "RemoveLarge");
     67                        Preferences.RemoveLargeValue = Config.getPref().getDouble(Preferences.PreFix + "RemoveLarge.Value", 10);
     68                        Preferences.RemoveParallel = Config.getPref().getBoolean(Preferences.PreFix + "RemoveParallel");
     69                        Preferences.RemoveParallelValue = Config.getPref().getDouble(Preferences.PreFix + "RemoveParalle.Value", 3);
     70                        Preferences.LimitPath = Config.getPref().getBoolean(Preferences.PreFix + "LimitPath");
     71                        Preferences.LimitPathValue = Config.getPref().getInt(Preferences.PreFix + "LimitPath.Value", 10000);
     72                        Preferences.LimitColor = Config.getPref().getBoolean(Preferences.PreFix + "LimitColor");
     73                        Preferences.LimitColorValue = Config.getPref().get(Preferences.PreFix + "LimitColor.Value","#000000");
     74                        Preferences.DebugTags = Config.getPref().getBoolean(Preferences.PreFix + "DebugTags");
     75                        Preferences.LayerClosed = Config.getPref().getBoolean(Preferences.PreFix + "LayerClosed");
     76                        Preferences.LayerSegment = Config.getPref().getBoolean(Preferences.PreFix + "LayerSegment");
     77                        Preferences.LayerAttribChange = Config.getPref().getBoolean(Preferences.PreFix + "LayerAttribChanges");
     78                        Preferences.LayerOrtho = Config.getPref().getBoolean(Preferences.PreFix + "LayerOrtho");
     79                }
     80        }
     81
     82        public PdfImportPlugin(PluginInformation info) {
     83                super(info);
     84                MainMenu.add(MainApplication.getMenu().imagerySubMenu, new PdfImportAction());
     85                Preferences.reload(this.getPluginInformation().name);
     86        }
     87
    1888}
Note: See TracChangeset for help on using the changeset viewer.