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

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

fix prefs display

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1package wmsplugin;
2
3import java.awt.FlowLayout;
4import javax.swing.JCheckBox;
5import javax.swing.JSpinner;
6import javax.swing.SpinnerNumberModel;
7import org.openstreetmap.josm.Main;
8import static org.openstreetmap.josm.tools.I18n.tr;
9
10import java.awt.Dimension;
11import java.awt.GridBagLayout;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.util.HashMap;
15import java.util.Map;
16
17import javax.swing.Box;
18import javax.swing.JButton;
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22import javax.swing.JScrollPane;
23import javax.swing.JTable;
24import javax.swing.JTextField;
25import javax.swing.table.DefaultTableModel;
26
27import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
28import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
29import org.openstreetmap.josm.tools.GBC;
30
31public class WMSPreferenceEditor implements PreferenceSetting {
32 private Map<String,String> orig;
33 private DefaultTableModel model;
34 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>();
35
36 JCheckBox overlapCheckBox;
37 JSpinner spinLat;
38 JSpinner spinLon;
39
40 public void addGui(final PreferenceDialog gui) {
41 JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu"));
42
43 model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0);
44 final JTable list = new JTable(model);
45 JScrollPane scroll = new JScrollPane(list);
46 p.add(scroll, GBC.eol().fill(GBC.BOTH));
47 scroll.setPreferredSize(new Dimension(200,200));
48
49 for (WMSInfo i : WMSPlugin.wmsList) {
50 oldValues.put(i.prefid, i);
51 model.addRow(new String[]{i.name, i.url});
52 }
53
54 final DefaultTableModel modeldef = new DefaultTableModel(
55 new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0);
56 final JTable listdef = new JTable(modeldef){
57 public boolean isCellEditable(int row,int column){return false;}
58 };;
59 JScrollPane scrolldef = new JScrollPane(listdef);
60 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GBC.BOTH));
61 scrolldef.setPreferredSize(new Dimension(200,200));
62
63 for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) {
64 modeldef.addRow(new String[]{i.getKey(), i.getValue()});
65 }
66
67 JPanel buttonPanel = new JPanel(new FlowLayout());
68
69 JButton add = new JButton(tr("Add"));
70 buttonPanel.add(add, GBC.std().insets(0,5,0,0));
71 add.addActionListener(new ActionListener(){
72 public void actionPerformed(ActionEvent e) {
73 JPanel p = new JPanel(new GridBagLayout());
74 p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0));
75 JTextField key = new JTextField(10);
76 JTextField value = new JTextField(10);
77 p.add(key, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
78 p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0));
79 p.add(value, GBC.eol().insets(5,0,0,0).fill(GBC.HORIZONTAL));
80 int answer = JOptionPane.showConfirmDialog(gui, p, tr("Enter a menu name and WMS URL"), JOptionPane.OK_CANCEL_OPTION);
81 if (answer == JOptionPane.OK_OPTION) {
82 model.addRow(new String[]{key.getText(), value.getText()});
83 }
84 }
85 });
86
87 JButton delete = new JButton(tr("Delete"));
88 buttonPanel.add(delete, GBC.std().insets(0,5,0,0));
89 delete.addActionListener(new ActionListener(){
90 public void actionPerformed(ActionEvent e) {
91 if (list.getSelectedRow() == -1)
92 JOptionPane.showMessageDialog(gui, tr("Please select the row to delete."));
93 else
94 {
95 Integer i;
96 while ((i = list.getSelectedRow()) != -1)
97 model.removeRow(i);
98 }
99 }
100 });
101
102 JButton copy = new JButton(tr("Copy Default"));
103 buttonPanel.add(copy, GBC.std().insets(0,5,0,0));
104 copy.addActionListener(new ActionListener(){
105 public void actionPerformed(ActionEvent e) {
106 Integer line = listdef.getSelectedRow();
107 if (line == -1)
108 JOptionPane.showMessageDialog(gui, tr("Please select the row to copy."));
109 else
110 {
111 model.addRow(new String[]{modeldef.getValueAt(line, 0).toString(),
112 modeldef.getValueAt(line, 1).toString()});
113 }
114 }
115 });
116
117 p.add(buttonPanel);
118 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
119
120 overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.doOverlap );
121 JLabel labelLat = new JLabel(tr("% of lat:"));
122 JLabel labelLon = new JLabel(tr("% of lon:"));
123 spinLat = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapLat, 1, 50, 1));
124 spinLon = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapLon, 1, 50, 1));
125
126 JPanel overlapPanel = new JPanel(new FlowLayout());
127 overlapPanel.add(overlapCheckBox);
128 overlapPanel.add(labelLat);
129 overlapPanel.add(spinLat);
130 overlapPanel.add(labelLon);
131 overlapPanel.add(spinLon);
132
133 p.add(overlapPanel);
134 }
135
136 public boolean ok() {
137 boolean change = false;
138 for (int i = 0; i < model.getRowCount(); ++i) {
139 String name = model.getValueAt(i,0).toString();
140 String url = model.getValueAt(i,1).toString();
141
142 WMSInfo origValue = oldValues.get(i);
143 if (origValue == null)
144 {
145 new WMSInfo(name, url, i).save();
146 change = true;
147 }
148 else
149 {
150 if (!origValue.name.equals(name) || !origValue.url.equals(url))
151 {
152 origValue.name = name;
153 origValue.url = url;
154 origValue.save();
155 change = true;
156 }
157 oldValues.remove(i);
158 }
159 }
160
161 // using null values instead of empty string really deletes
162 // the preferences entry
163 for (WMSInfo i : oldValues.values())
164 {
165 i.url = null;
166 i.name = null;
167 i.save();
168 change = true;
169 }
170
171 if (change) WMSPlugin.refreshMenu();
172
173 WMSPlugin.doOverlap = overlapCheckBox.getModel().isSelected();
174 WMSPlugin.overlapLat = (Integer) spinLat.getModel().getValue();
175 WMSPlugin.overlapLon = (Integer) spinLon.getModel().getValue();
176
177 Main.pref.put("wmsplugin.url.overlap", String.valueOf(WMSPlugin.doOverlap));
178 Main.pref.put("wmsplugin.url.overlapLat", String.valueOf(WMSPlugin.overlapLat));
179 Main.pref.put("wmsplugin.url.overlapLon", String.valueOf(WMSPlugin.overlapLon));
180
181 return false;
182 }
183
184 /**
185 * Updates a server URL in the preferences dialog. Used by other plugins.
186 *
187 * @param server The server name
188 * @param url The server URL
189 */
190 public void setServerUrl(String server, String url)
191 {
192 for (int i = 0; i < model.getRowCount(); i++)
193 {
194 if( server.equals(model.getValueAt(i,0).toString()) )
195 {
196 model.setValueAt(url, i, 1);
197 return;
198 }
199 }
200 model.addRow(new String[]{server, url});
201 }
202
203 /**
204 * Gets a server URL in the preferences dialog. Used by other plugins.
205 *
206 * @param server The server name
207 * @return The server URL
208 */
209 public String getServerUrl(String server)
210 {
211 for (int i = 0; i < model.getRowCount(); i++)
212 {
213 if( server.equals(model.getValueAt(i,0).toString()) )
214 {
215 return model.getValueAt(i,1).toString();
216 }
217 }
218 return null;
219 }
220}
221
Note: See TracBrowser for help on using the repository browser.