Changeset 22936 in osm for applications
- Timestamp:
- 2010-09-02T04:13:48+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/wmsplugin/src/wmsplugin
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPreferenceEditor.java
r22794 r22936 6 6 import java.awt.FlowLayout; 7 7 import java.awt.GridBagConstraints; 8 import java.awt.GridBagLayout;9 8 import java.awt.event.ActionEvent; 10 9 import java.awt.event.ActionListener; … … 22 21 import javax.swing.JSpinner; 23 22 import javax.swing.JTable; 24 import javax.swing.JTextField;25 23 import javax.swing.SpinnerNumberModel; 26 24 import javax.swing.table.DefaultTableModel; … … 32 30 33 31 public 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 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.PROP_OVERLAP.get() ); 172 JLabel labelEast = new JLabel(tr("% of east:")); 173 JLabel labelNorth = new JLabel(tr("% of north:")); 174 spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.PROP_OVERLAP_EAST.get(), 1, 50, 1)); 175 spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.PROP_OVERLAP_NORTH.get(), 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.PROP_SIMULTANEOUS_CONNECTIONS.get(), 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.PROP_OVERLAP.put(overlapCheckBox.getModel().isSelected()); 243 WMSPlugin.PROP_OVERLAP_EAST.put((Integer) spinEast.getModel().getValue()); 244 WMSPlugin.PROP_OVERLAP_NORTH.put((Integer) spinNorth.getModel().getValue()); 245 WMSPlugin.PROP_SIMULTANEOUS_CONNECTIONS.put((Integer) spinSimConn.getModel().getValue()); 246 allowRemoteControl = remoteCheckBox.getModel().isSelected(); 247 248 Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString()); 249 250 Main.pref.put("wmsplugin.remotecontrol", String.valueOf(allowRemoteControl)); 251 return false; 252 } 253 254 /** 255 * Updates a server URL in the preferences dialog. Used by other plugins. 256 * 257 * @param server The server name 258 * @param url The server URL 259 */ 260 public void setServerUrl(String server, String url) 261 { 262 for (int i = 0; i < model.getRowCount(); i++) 263 { 264 if( server.equals(model.getValueAt(i,0).toString()) ) 265 { 266 model.setValueAt(url, i, 1); 267 return; 268 } 269 } 270 model.addRow(new String[]{server, url}); 271 } 272 273 /** 274 * Gets a server URL in the preferences dialog. Used by other plugins. 275 * 276 * @param server The server name 277 * @return The server URL 278 */ 279 public String getServerUrl(String server) 280 { 281 for (int i = 0; i < model.getRowCount(); i++) 282 { 283 if( server.equals(model.getValueAt(i,0).toString()) ) 284 { 285 return model.getValueAt(i,1).toString(); 286 } 287 } 288 return null; 289 } 32 private DefaultTableModel model; 33 private JComboBox browser; 34 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>(); 35 36 JCheckBox overlapCheckBox; 37 JSpinner spinEast; 38 JSpinner spinNorth; 39 JSpinner spinSimConn; 40 JCheckBox remoteCheckBox; 41 boolean allowRemoteControl = true; 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 AddWMSLayerPanel p = new AddWMSLayerPanel(); 79 int answer = JOptionPane.showConfirmDialog( 80 gui, p, 81 tr("Add WMS URL"), 82 JOptionPane.OK_CANCEL_OPTION); 83 if (answer == JOptionPane.OK_OPTION) { 84 model.addRow(new String[]{p.getUrlName(), p.getUrl()}); 85 } 86 } 87 }); 88 89 JButton delete = new JButton(tr("Delete")); 90 buttonPanel.add(delete, GBC.std().insets(0,5,0,0)); 91 delete.addActionListener(new ActionListener(){ 92 public void actionPerformed(ActionEvent e) { 93 if (list.getSelectedRow() == -1) 94 JOptionPane.showMessageDialog(gui, tr("Please select the row to delete.")); 95 else 96 { 97 Integer i; 98 while ((i = list.getSelectedRow()) != -1) 99 model.removeRow(i); 100 } 101 } 102 }); 103 104 JButton copy = new JButton(tr("Copy Selected Default(s)")); 105 buttonPanel.add(copy, GBC.std().insets(0,5,0,0)); 106 copy.addActionListener(new ActionListener(){ 107 public void actionPerformed(ActionEvent e) { 108 int[] lines = listdef.getSelectedRows(); 109 if (lines.length == 0) { 110 JOptionPane.showMessageDialog( 111 gui, 112 tr("Please select at least one row to copy."), 113 tr("Information"), 114 JOptionPane.INFORMATION_MESSAGE 115 ); 116 return; 117 } 118 119 outer: for(int i = 0; i < lines.length; i++) { 120 String c1 = modeldef.getValueAt(lines[i], 0).toString(); 121 String c2 = modeldef.getValueAt(lines[i], 1).toString(); 122 123 // Check if an entry with exactly the same values already 124 // exists 125 for(int j = 0; j < model.getRowCount(); j++) { 126 if(c1.equals(model.getValueAt(j, 0).toString()) 127 && c2.equals(model.getValueAt(j, 1).toString())) { 128 // Select the already existing row so the user has 129 // some feedback in case an entry exists 130 list.getSelectionModel().setSelectionInterval(j, j); 131 list.scrollRectToVisible(list.getCellRect(j, 0, true)); 132 continue outer; 133 } 134 } 135 136 model.addRow(new String[] {c1, c2}); 137 int lastLine = model.getRowCount() - 1; 138 list.getSelectionModel().setSelectionInterval(lastLine, lastLine); 139 list.scrollRectToVisible(list.getCellRect(lastLine, 0, true)); 140 } 141 } 142 }); 143 144 p.add(buttonPanel); 145 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 146 // Add default item list 147 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GridBagConstraints.BOTH)); 148 149 browser = new JComboBox(new String[]{ 150 "webkit-image {0}", 151 "gnome-web-photo --mode=photo --format=png {0} /dev/stdout", 152 "gnome-web-photo-fixed {0}", 153 "webkit-image-gtk {0}"}); 154 browser.setEditable(true); 155 browser.setSelectedItem(Main.pref.get("wmsplugin.browser", "webkit-image {0}")); 156 p.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 157 p.add(browser); 158 159 //Overlap 160 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 161 162 overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.PROP_OVERLAP.get() ); 163 JLabel labelEast = new JLabel(tr("% of east:")); 164 JLabel labelNorth = new JLabel(tr("% of north:")); 165 spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.PROP_OVERLAP_EAST.get(), 1, 50, 1)); 166 spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.PROP_OVERLAP_NORTH.get(), 1, 50, 1)); 167 168 JPanel overlapPanel = new JPanel(new FlowLayout()); 169 overlapPanel.add(overlapCheckBox); 170 overlapPanel.add(labelEast); 171 overlapPanel.add(spinEast); 172 overlapPanel.add(labelNorth); 173 overlapPanel.add(spinNorth); 174 175 p.add(overlapPanel); 176 177 // Simultaneous connections 178 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 179 JLabel labelSimConn = new JLabel(tr("Simultaneous connections")); 180 spinSimConn = new JSpinner(new SpinnerNumberModel(WMSPlugin.PROP_SIMULTANEOUS_CONNECTIONS.get(), 1, 30, 1)); 181 JPanel overlapPanelSimConn = new JPanel(new FlowLayout()); 182 overlapPanelSimConn.add(labelSimConn); 183 overlapPanelSimConn.add(spinSimConn); 184 p.add(overlapPanelSimConn, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 185 186 187 allowRemoteControl = Main.pref.getBoolean("wmsplugin.remotecontrol", true); 188 remoteCheckBox = new JCheckBox(tr("Allow remote control (reqires remotecontrol plugin)"), allowRemoteControl ); 189 JPanel remotePanel = new JPanel(new FlowLayout()); 190 remotePanel.add(remoteCheckBox); 191 192 p.add(remotePanel); 193 194 } 195 196 public boolean ok() { 197 boolean change = false; 198 for (int i = 0; i < model.getRowCount(); ++i) { 199 String name = model.getValueAt(i,0).toString(); 200 String url = model.getValueAt(i,1).toString(); 201 202 WMSInfo origValue = oldValues.get(i); 203 if (origValue == null) 204 { 205 new WMSInfo(name, url, i).save(); 206 change = true; 207 } 208 else 209 { 210 if (!origValue.name.equals(name) || !origValue.url.equals(url)) 211 { 212 origValue.name = name; 213 origValue.url = url; 214 origValue.save(); 215 change = true; 216 } 217 oldValues.remove(i); 218 } 219 } 220 221 // using null values instead of empty string really deletes 222 // the preferences entry 223 for (WMSInfo i : oldValues.values()) 224 { 225 i.url = null; 226 i.name = null; 227 i.save(); 228 change = true; 229 } 230 231 if (change) WMSPlugin.refreshMenu(); 232 233 WMSPlugin.PROP_OVERLAP.put(overlapCheckBox.getModel().isSelected()); 234 WMSPlugin.PROP_OVERLAP_EAST.put((Integer) spinEast.getModel().getValue()); 235 WMSPlugin.PROP_OVERLAP_NORTH.put((Integer) spinNorth.getModel().getValue()); 236 WMSPlugin.PROP_SIMULTANEOUS_CONNECTIONS.put((Integer) spinSimConn.getModel().getValue()); 237 allowRemoteControl = remoteCheckBox.getModel().isSelected(); 238 239 Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString()); 240 241 Main.pref.put("wmsplugin.remotecontrol", String.valueOf(allowRemoteControl)); 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 } 290 281 } 291 282
Note:
See TracChangeset
for help on using the changeset viewer.