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

Last change on this file since 19306 was 19306, checked in by guggis, 14 years ago

'Update to JOSM 2748'

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1package wmsplugin;
2
3import java.awt.BorderLayout;
4import java.awt.FlowLayout;
5import javax.swing.JCheckBox;
6import javax.swing.JSpinner;
7import javax.swing.SpinnerNumberModel;
8import org.openstreetmap.josm.Main;
9import static org.openstreetmap.josm.tools.I18n.tr;
10
11import java.awt.Dimension;
12import java.awt.FlowLayout;
13import java.awt.GridBagLayout;
14import java.awt.event.ActionEvent;
15import java.awt.event.ActionListener;
16import java.util.HashMap;
17import java.util.Map;
18
19import javax.swing.Box;
20import javax.swing.JButton;
21import javax.swing.JComboBox;
22import javax.swing.JLabel;
23import javax.swing.JOptionPane;
24import javax.swing.JPanel;
25import javax.swing.JScrollPane;
26import javax.swing.JTable;
27import javax.swing.JTextField;
28import javax.swing.table.DefaultTableModel;
29
30import org.openstreetmap.josm.Main;
31import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
32import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
33import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
34import org.openstreetmap.josm.tools.GBC;
35
36public class WMSPreferenceEditor implements PreferenceSetting {
37 private DefaultTableModel model;
38 private JComboBox browser;
39 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>();
40
41 JCheckBox overlapCheckBox;
42 JSpinner spinEast;
43 JSpinner spinNorth;
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(GBC.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(GBC.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(GBC.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(GBC.HORIZONTAL));
155 // Add default item list
156 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GBC.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(GBC.HORIZONTAL));
166 p.add(browser);
167
168
169 //Overlap
170 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
171
172 overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.doOverlap );
173 JLabel labelEast = new JLabel(tr("% of east:"));
174 JLabel labelNorth = new JLabel(tr("% of north:"));
175 spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapEast, 1, 50, 1));
176 spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapNorth, 1, 50, 1));
177
178 JPanel overlapPanel = new JPanel(new FlowLayout());
179 overlapPanel.add(overlapCheckBox);
180 overlapPanel.add(labelEast);
181 overlapPanel.add(spinEast);
182 overlapPanel.add(labelNorth);
183 overlapPanel.add(spinNorth);
184
185 p.add(overlapPanel);
186 }
187
188 public boolean ok() {
189 boolean change = false;
190 for (int i = 0; i < model.getRowCount(); ++i) {
191 String name = model.getValueAt(i,0).toString();
192 String url = model.getValueAt(i,1).toString();
193
194 WMSInfo origValue = oldValues.get(i);
195 if (origValue == null)
196 {
197 new WMSInfo(name, url, i).save();
198 change = true;
199 }
200 else
201 {
202 if (!origValue.name.equals(name) || !origValue.url.equals(url))
203 {
204 origValue.name = name;
205 origValue.url = url;
206 origValue.save();
207 change = true;
208 }
209 oldValues.remove(i);
210 }
211 }
212
213 // using null values instead of empty string really deletes
214 // the preferences entry
215 for (WMSInfo i : oldValues.values())
216 {
217 i.url = null;
218 i.name = null;
219 i.save();
220 change = true;
221 }
222
223 if (change) WMSPlugin.refreshMenu();
224
225 WMSPlugin.doOverlap = overlapCheckBox.getModel().isSelected();
226 WMSPlugin.overlapEast = (Integer) spinEast.getModel().getValue();
227 WMSPlugin.overlapNorth = (Integer) spinNorth.getModel().getValue();
228
229 Main.pref.put("wmsplugin.url.overlap", String.valueOf(WMSPlugin.doOverlap));
230 Main.pref.put("wmsplugin.url.overlapEast", String.valueOf(WMSPlugin.overlapEast));
231 Main.pref.put("wmsplugin.url.overlapNorth", String.valueOf(WMSPlugin.overlapNorth));
232
233 Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString());
234 return false;
235 }
236
237 /**
238 * Updates a server URL in the preferences dialog. Used by other plugins.
239 *
240 * @param server The server name
241 * @param url The server URL
242 */
243 public void setServerUrl(String server, String url)
244 {
245 for (int i = 0; i < model.getRowCount(); i++)
246 {
247 if( server.equals(model.getValueAt(i,0).toString()) )
248 {
249 model.setValueAt(url, i, 1);
250 return;
251 }
252 }
253 model.addRow(new String[]{server, url});
254 }
255
256 /**
257 * Gets a server URL in the preferences dialog. Used by other plugins.
258 *
259 * @param server The server name
260 * @return The server URL
261 */
262 public String getServerUrl(String server)
263 {
264 for (int i = 0; i < model.getRowCount(); i++)
265 {
266 if( server.equals(model.getValueAt(i,0).toString()) )
267 {
268 return model.getValueAt(i,1).toString();
269 }
270 }
271 return null;
272 }
273}
274
Note: See TracBrowser for help on using the repository browser.