source: osm/applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPreferenceEditor.java@ 22794

Last change on this file since 22794 was 22794, checked in by jttt, 14 years ago

Fix #5378 Ghost images appear when wmsplugin.url.overlap=true

  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
1package wmsplugin;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.Dimension;
6import java.awt.FlowLayout;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.util.HashMap;
12import java.util.Map;
13
14import javax.swing.Box;
15import javax.swing.JButton;
16import javax.swing.JCheckBox;
17import javax.swing.JComboBox;
18import javax.swing.JLabel;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21import javax.swing.JScrollPane;
22import javax.swing.JSpinner;
23import javax.swing.JTable;
24import javax.swing.JTextField;
25import javax.swing.SpinnerNumberModel;
26import javax.swing.table.DefaultTableModel;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
30import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
31import org.openstreetmap.josm.tools.GBC;
32
33public 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 }
290}
291
Note: See TracBrowser for help on using the repository browser.