Ignore:
Timestamp:
2010-08-17T23:25:20+02:00 (14 years ago)
Author:
bomm
Message:

register remote command handler with remotecontrol plug-in if available
allow loading new WMS layer using remote command

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPreferenceEditor.java

    r22008 r22677  
    3232
    3333public class WMSPreferenceEditor implements PreferenceSetting {
    34     private DefaultTableModel model;
    35     private JComboBox browser;
    36     private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>();
    37 
    38     JCheckBox overlapCheckBox;
    39     JSpinner spinEast;
    40     JSpinner spinNorth;
    41     JSpinner spinSimConn;
    42 
    43     public void addGui(final PreferenceTabbedPane gui) {
    44         JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu"));
    45 
    46         model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0);
    47         final JTable list = new JTable(model);
    48         JScrollPane scroll = new JScrollPane(list);
    49         p.add(scroll, GBC.eol().fill(GridBagConstraints.BOTH));
    50         scroll.setPreferredSize(new Dimension(200,200));
    51 
    52         for (WMSInfo i : WMSPlugin.wmsList) {
    53             oldValues.put(i.prefid, i);
    54             model.addRow(new String[]{i.name, i.url});
    55         }
    56 
    57         final DefaultTableModel modeldef = new DefaultTableModel(
    58         new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0);
    59         final JTable listdef = new JTable(modeldef){
    60             @Override
    61             public boolean isCellEditable(int row,int column){return false;}
    62         };
    63         JScrollPane scrolldef = new JScrollPane(listdef);
    64         // scrolldef is added after the buttons so it's clearer the buttons
    65         // control the top list and not the default one
    66         scrolldef.setPreferredSize(new Dimension(200,200));
    67 
    68         for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) {
    69             modeldef.addRow(new String[]{i.getKey(), i.getValue()});
    70         }
    71 
    72         JPanel buttonPanel = new JPanel(new FlowLayout());
    73 
    74         JButton add = new JButton(tr("Add"));
    75         buttonPanel.add(add, GBC.std().insets(0,5,0,0));
    76         add.addActionListener(new ActionListener(){
    77             public void actionPerformed(ActionEvent e) {
    78                 JPanel p = new JPanel(new GridBagLayout());
    79                 p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0));
    80                 JTextField key = new JTextField(40);
    81                 JTextField value = new JTextField(40);
    82                 p.add(key, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
    83                 p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0));
    84                 p.add(value, GBC.eol().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
    85                 int answer = JOptionPane.showConfirmDialog(
    86                         gui, p,
    87                         tr("Enter a menu name and WMS URL"),
    88                         JOptionPane.OK_CANCEL_OPTION,
    89                         JOptionPane.QUESTION_MESSAGE);
    90                 if (answer == JOptionPane.OK_OPTION) {
    91                     model.addRow(new String[]{key.getText(), value.getText()});
    92                 }
    93             }
    94         });
    95 
    96         JButton delete = new JButton(tr("Delete"));
    97         buttonPanel.add(delete, GBC.std().insets(0,5,0,0));
    98         delete.addActionListener(new ActionListener(){
    99             public void actionPerformed(ActionEvent e) {
    100                 if (list.getSelectedRow() == -1)
    101                     JOptionPane.showMessageDialog(gui, tr("Please select the row to delete."));
    102                 else
    103                 {
    104                     Integer i;
    105                     while ((i = list.getSelectedRow()) != -1)
    106                         model.removeRow(i);
    107                 }
    108             }
    109         });
    110 
    111         JButton copy = new JButton(tr("Copy Selected Default(s)"));
    112         buttonPanel.add(copy, GBC.std().insets(0,5,0,0));
    113         copy.addActionListener(new ActionListener(){
    114             public void actionPerformed(ActionEvent e) {
    115                 int[] lines = listdef.getSelectedRows();
    116                 if (lines.length == 0) {
    117                     JOptionPane.showMessageDialog(
    118                             gui,
    119                             tr("Please select at least one row to copy."),
    120                             tr("Information"),
    121                             JOptionPane.INFORMATION_MESSAGE
    122                             );
    123                     return;
    124                 }
    125 
    126                 outer: for(int i = 0; i < lines.length; i++) {
    127                     String c1 = modeldef.getValueAt(lines[i], 0).toString();
    128                     String c2 = modeldef.getValueAt(lines[i], 1).toString();
    129 
    130                     // Check if an entry with exactly the same values already
    131                     // exists
    132                     for(int j = 0; j < model.getRowCount(); j++) {
    133                         if(c1.equals(model.getValueAt(j, 0).toString())
    134                                 && c2.equals(model.getValueAt(j, 1).toString())) {
    135                             // Select the already existing row so the user has
    136                             // some feedback in case an entry exists
    137                             list.getSelectionModel().setSelectionInterval(j, j);
    138                             list.scrollRectToVisible(list.getCellRect(j, 0, true));
    139                             continue outer;
    140                         }
    141                     }
    142 
    143                     model.addRow(new String[] {c1, c2});
    144                     int lastLine = model.getRowCount() - 1;
    145                     list.getSelectionModel().setSelectionInterval(lastLine, lastLine);
    146                     list.scrollRectToVisible(list.getCellRect(lastLine, 0, true));
    147                 }
    148             }
    149         });
    150 
    151         p.add(buttonPanel);
    152         p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    153         // Add default item list
    154         p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GridBagConstraints.BOTH));
    155 
    156         browser = new JComboBox(new String[]{
    157         "webkit-image {0}",
    158         "gnome-web-photo --mode=photo --format=png {0} /dev/stdout",
    159         "gnome-web-photo-fixed {0}",
    160         "webkit-image-gtk {0}"});
    161         browser.setEditable(true);
    162         browser.setSelectedItem(Main.pref.get("wmsplugin.browser", "webkit-image {0}"));
    163         p.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    164         p.add(browser);
    165 
    166         //Overlap
    167         p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    168 
    169         overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.doOverlap );
    170         JLabel labelEast = new JLabel(tr("% of east:"));
    171         JLabel labelNorth = new JLabel(tr("% of north:"));
    172         spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapEast, 1, 50, 1));
    173         spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapNorth, 1, 50, 1));
    174 
    175         JPanel overlapPanel = new JPanel(new FlowLayout());
    176         overlapPanel.add(overlapCheckBox);
    177         overlapPanel.add(labelEast);
    178         overlapPanel.add(spinEast);
    179         overlapPanel.add(labelNorth);
    180         overlapPanel.add(spinNorth);
    181 
    182         p.add(overlapPanel);
    183                
    184         // Simultaneous connections
    185         p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    186         JLabel labelSimConn = new JLabel(tr("Simultaneous connections"));
    187         spinSimConn = new JSpinner(new SpinnerNumberModel(WMSPlugin.simultaneousConnections, 1, 30, 1));
    188         JPanel overlapPanelSimConn = new JPanel(new FlowLayout());
    189         overlapPanelSimConn.add(labelSimConn);
    190         overlapPanelSimConn.add(spinSimConn);
    191         p.add(overlapPanelSimConn);
    192     }
    193 
    194     public boolean ok() {
    195         boolean change = false;
    196         for (int i = 0; i < model.getRowCount(); ++i) {
    197             String name = model.getValueAt(i,0).toString();
    198             String url = model.getValueAt(i,1).toString();
    199 
    200             WMSInfo origValue = oldValues.get(i);
    201             if (origValue == null)
    202             {
    203                 new WMSInfo(name, url, i).save();
    204                 change = true;
    205             }
    206             else
    207             {
    208                 if (!origValue.name.equals(name) || !origValue.url.equals(url))
    209                 {
    210                     origValue.name = name;
    211                     origValue.url = url;
    212                     origValue.save();
    213                     change = true;
    214                 }
    215                 oldValues.remove(i);
    216             }
    217         }
    218 
    219         // using null values instead of empty string really deletes
    220         // the preferences entry
    221         for (WMSInfo i : oldValues.values())
    222         {
    223             i.url = null;
    224             i.name = null;
    225             i.save();
    226             change = true;
    227         }
    228 
    229         if (change) WMSPlugin.refreshMenu();
    230 
    231         WMSPlugin.doOverlap = overlapCheckBox.getModel().isSelected();
    232         WMSPlugin.overlapEast = (Integer) spinEast.getModel().getValue();
    233         WMSPlugin.overlapNorth = (Integer) spinNorth.getModel().getValue();
    234         WMSPlugin.simultaneousConnections = (Integer) spinSimConn.getModel().getValue();
    235 
    236         Main.pref.put("wmsplugin.url.overlap",    String.valueOf(WMSPlugin.doOverlap));
    237         Main.pref.put("wmsplugin.url.overlapEast", String.valueOf(WMSPlugin.overlapEast));
    238         Main.pref.put("wmsplugin.url.overlapNorth", String.valueOf(WMSPlugin.overlapNorth));
    239 
    240         Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString());
    241         Main.pref.put("wmsplugin.simultaneousConnections", String.valueOf(WMSPlugin.simultaneousConnections));
    242         return false;
    243     }
    244 
    245     /**
    246      * Updates a server URL in the preferences dialog. Used by other plugins.
    247      *
    248      * @param server The server name
    249      * @param url The server URL
    250      */
    251     public void setServerUrl(String server, String url)
    252     {
    253         for (int i = 0; i < model.getRowCount(); i++)
    254         {
    255             if( server.equals(model.getValueAt(i,0).toString()) )
    256             {
    257                 model.setValueAt(url, i, 1);
    258                 return;
    259             }
    260         }
    261         model.addRow(new String[]{server, url});
    262     }
    263 
    264     /**
    265      * Gets a server URL in the preferences dialog. Used by other plugins.
    266      *
    267      * @param server The server name
    268      * @return The server URL
    269      */
    270     public String getServerUrl(String server)
    271     {
    272         for (int i = 0; i < model.getRowCount(); i++)
    273         {
    274             if( server.equals(model.getValueAt(i,0).toString()) )
    275             {
    276                 return model.getValueAt(i,1).toString();
    277             }
    278         }
    279         return null;
    280     }
     34        private DefaultTableModel model;
     35        private JComboBox browser;
     36        private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>();
     37
     38        JCheckBox overlapCheckBox;
     39        JSpinner spinEast;
     40        JSpinner spinNorth;
     41        JSpinner spinSimConn;
     42        JCheckBox remoteCheckBox;
     43        boolean allowRemoteControl = true;
     44
     45        public void addGui(final PreferenceTabbedPane gui) {
     46                JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu"));
     47
     48                model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0);
     49                final JTable list = new JTable(model);
     50                JScrollPane scroll = new JScrollPane(list);
     51                p.add(scroll, GBC.eol().fill(GridBagConstraints.BOTH));
     52                scroll.setPreferredSize(new Dimension(200,200));
     53
     54                for (WMSInfo i : WMSPlugin.wmsList) {
     55                        oldValues.put(i.prefid, i);
     56                        model.addRow(new String[]{i.name, i.url});
     57                }
     58
     59                final DefaultTableModel modeldef = new DefaultTableModel(
     60                                new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0);
     61                final JTable listdef = new JTable(modeldef){
     62                        @Override
     63                        public boolean isCellEditable(int row,int column){return false;}
     64                };
     65                JScrollPane scrolldef = new JScrollPane(listdef);
     66                // scrolldef is added after the buttons so it's clearer the buttons
     67                // control the top list and not the default one
     68                scrolldef.setPreferredSize(new Dimension(200,200));
     69
     70                for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) {
     71                        modeldef.addRow(new String[]{i.getKey(), i.getValue()});
     72                }
     73
     74                JPanel buttonPanel = new JPanel(new FlowLayout());
     75
     76                JButton add = new JButton(tr("Add"));
     77                buttonPanel.add(add, GBC.std().insets(0,5,0,0));
     78                add.addActionListener(new ActionListener(){
     79                        public void actionPerformed(ActionEvent e) {
     80                                JPanel p = new JPanel(new GridBagLayout());
     81                                p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0));
     82                                JTextField key = new JTextField(40);
     83                                JTextField value = new JTextField(40);
     84                                p.add(key, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
     85                                p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0));
     86                                p.add(value, GBC.eol().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
     87                                int answer = JOptionPane.showConfirmDialog(
     88                                                gui, p,
     89                                                tr("Enter a menu name and WMS URL"),
     90                                                JOptionPane.OK_CANCEL_OPTION,
     91                                                JOptionPane.QUESTION_MESSAGE);
     92                                if (answer == JOptionPane.OK_OPTION) {
     93                                        model.addRow(new String[]{key.getText(), value.getText()});
     94                                }
     95                        }
     96                });
     97
     98                JButton delete = new JButton(tr("Delete"));
     99                buttonPanel.add(delete, GBC.std().insets(0,5,0,0));
     100                delete.addActionListener(new ActionListener(){
     101                        public void actionPerformed(ActionEvent e) {
     102                                if (list.getSelectedRow() == -1)
     103                                        JOptionPane.showMessageDialog(gui, tr("Please select the row to delete."));
     104                                else
     105                                {
     106                                        Integer i;
     107                                        while ((i = list.getSelectedRow()) != -1)
     108                                                model.removeRow(i);
     109                                }
     110                        }
     111                });
     112
     113                JButton copy = new JButton(tr("Copy Selected Default(s)"));
     114                buttonPanel.add(copy, GBC.std().insets(0,5,0,0));
     115                copy.addActionListener(new ActionListener(){
     116                        public void actionPerformed(ActionEvent e) {
     117                                int[] lines = listdef.getSelectedRows();
     118                                if (lines.length == 0) {
     119                                        JOptionPane.showMessageDialog(
     120                                                        gui,
     121                                                        tr("Please select at least one row to copy."),
     122                                                        tr("Information"),
     123                                                        JOptionPane.INFORMATION_MESSAGE
     124                                        );
     125                                        return;
     126                                }
     127
     128                                outer: for(int i = 0; i < lines.length; i++) {
     129                                        String c1 = modeldef.getValueAt(lines[i], 0).toString();
     130                                        String c2 = modeldef.getValueAt(lines[i], 1).toString();
     131
     132                                        // Check if an entry with exactly the same values already
     133                                        // exists
     134                                        for(int j = 0; j < model.getRowCount(); j++) {
     135                                                if(c1.equals(model.getValueAt(j, 0).toString())
     136                                                                && c2.equals(model.getValueAt(j, 1).toString())) {
     137                                                        // Select the already existing row so the user has
     138                                                        // some feedback in case an entry exists
     139                                                        list.getSelectionModel().setSelectionInterval(j, j);
     140                                                        list.scrollRectToVisible(list.getCellRect(j, 0, true));
     141                                                        continue outer;
     142                                                }
     143                                        }
     144
     145                                        model.addRow(new String[] {c1, c2});
     146                                        int lastLine = model.getRowCount() - 1;
     147                                        list.getSelectionModel().setSelectionInterval(lastLine, lastLine);
     148                                        list.scrollRectToVisible(list.getCellRect(lastLine, 0, true));
     149                                }
     150                        }
     151                });
     152
     153                p.add(buttonPanel);
     154                p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
     155                // Add default item list
     156                p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GridBagConstraints.BOTH));
     157
     158                browser = new JComboBox(new String[]{
     159                                "webkit-image {0}",
     160                                "gnome-web-photo --mode=photo --format=png {0} /dev/stdout",
     161                                "gnome-web-photo-fixed {0}",
     162                "webkit-image-gtk {0}"});
     163                browser.setEditable(true);
     164                browser.setSelectedItem(Main.pref.get("wmsplugin.browser", "webkit-image {0}"));
     165                p.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
     166                p.add(browser);
     167
     168                //Overlap
     169                p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
     170
     171                overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.doOverlap );
     172                JLabel labelEast = new JLabel(tr("% of east:"));
     173                JLabel labelNorth = new JLabel(tr("% of north:"));
     174                spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapEast, 1, 50, 1));
     175                spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapNorth, 1, 50, 1));
     176
     177                JPanel overlapPanel = new JPanel(new FlowLayout());
     178                overlapPanel.add(overlapCheckBox);
     179                overlapPanel.add(labelEast);
     180                overlapPanel.add(spinEast);
     181                overlapPanel.add(labelNorth);
     182                overlapPanel.add(spinNorth);
     183
     184                p.add(overlapPanel);
     185
     186                // Simultaneous connections
     187                p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
     188                JLabel labelSimConn = new JLabel(tr("Simultaneous connections"));
     189                spinSimConn = new JSpinner(new SpinnerNumberModel(WMSPlugin.simultaneousConnections, 1, 30, 1));
     190                JPanel overlapPanelSimConn = new JPanel(new FlowLayout());
     191                overlapPanelSimConn.add(labelSimConn);
     192                overlapPanelSimConn.add(spinSimConn);
     193                p.add(overlapPanelSimConn, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
     194
     195
     196                allowRemoteControl = Main.pref.getBoolean("wmsplugin.remotecontrol", true);
     197                remoteCheckBox = new JCheckBox(tr("Allow remote control (reqires remotecontrol plugin)"), allowRemoteControl );
     198                JPanel remotePanel = new JPanel(new FlowLayout());
     199                remotePanel.add(remoteCheckBox);
     200
     201                p.add(remotePanel);
     202
     203        }
     204
     205        public boolean ok() {
     206                boolean change = false;
     207                for (int i = 0; i < model.getRowCount(); ++i) {
     208                        String name = model.getValueAt(i,0).toString();
     209                        String url = model.getValueAt(i,1).toString();
     210
     211                        WMSInfo origValue = oldValues.get(i);
     212                        if (origValue == null)
     213                        {
     214                                new WMSInfo(name, url, i).save();
     215                                change = true;
     216                        }
     217                        else
     218                        {
     219                                if (!origValue.name.equals(name) || !origValue.url.equals(url))
     220                                {
     221                                        origValue.name = name;
     222                                        origValue.url = url;
     223                                        origValue.save();
     224                                        change = true;
     225                                }
     226                                oldValues.remove(i);
     227                        }
     228                }
     229
     230                // using null values instead of empty string really deletes
     231                // the preferences entry
     232                for (WMSInfo i : oldValues.values())
     233                {
     234                        i.url = null;
     235                        i.name = null;
     236                        i.save();
     237                        change = true;
     238                }
     239
     240                if (change) WMSPlugin.refreshMenu();
     241
     242                WMSPlugin.doOverlap = overlapCheckBox.getModel().isSelected();
     243                WMSPlugin.overlapEast = (Integer) spinEast.getModel().getValue();
     244                WMSPlugin.overlapNorth = (Integer) spinNorth.getModel().getValue();
     245                WMSPlugin.simultaneousConnections = (Integer) spinSimConn.getModel().getValue();
     246                allowRemoteControl = remoteCheckBox.getModel().isSelected();
     247
     248                Main.pref.put("wmsplugin.url.overlap",    String.valueOf(WMSPlugin.doOverlap));
     249                Main.pref.put("wmsplugin.url.overlapEast", String.valueOf(WMSPlugin.overlapEast));
     250                Main.pref.put("wmsplugin.url.overlapNorth", String.valueOf(WMSPlugin.overlapNorth));
     251
     252                Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString());
     253                Main.pref.put("wmsplugin.simultaneousConnections", String.valueOf(WMSPlugin.simultaneousConnections));
     254
     255                Main.pref.put("wmsplugin.remotecontrol",    String.valueOf(allowRemoteControl));
     256                return false;
     257        }
     258
     259        /**
     260         * Updates a server URL in the preferences dialog. Used by other plugins.
     261         *
     262         * @param server The server name
     263         * @param url The server URL
     264         */
     265        public void setServerUrl(String server, String url)
     266        {
     267                for (int i = 0; i < model.getRowCount(); i++)
     268                {
     269                        if( server.equals(model.getValueAt(i,0).toString()) )
     270                        {
     271                                model.setValueAt(url, i, 1);
     272                                return;
     273                        }
     274                }
     275                model.addRow(new String[]{server, url});
     276        }
     277
     278        /**
     279         * Gets a server URL in the preferences dialog. Used by other plugins.
     280         *
     281         * @param server The server name
     282         * @return The server URL
     283         */
     284        public String getServerUrl(String server)
     285        {
     286                for (int i = 0; i < model.getRowCount(); i++)
     287                {
     288                        if( server.equals(model.getValueAt(i,0).toString()) )
     289                        {
     290                                return model.getValueAt(i,1).toString();
     291                        }
     292                }
     293                return null;
     294        }
    281295}
    282296
Note: See TracChangeset for help on using the changeset viewer.