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

Last change on this file since 18761 was 18761, checked in by stoecker, 15 years ago

fixed code indentation

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