1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 |
|
---|
12 | import java.io.BufferedReader;
|
---|
13 | import java.io.InputStreamReader;
|
---|
14 | import java.io.UnsupportedEncodingException;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.regex.Matcher;
|
---|
18 | import java.util.regex.Pattern;
|
---|
19 |
|
---|
20 | import javax.swing.BorderFactory;
|
---|
21 | import javax.swing.Box;
|
---|
22 | import javax.swing.DefaultListModel;
|
---|
23 | import javax.swing.JButton;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JList;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JScrollPane;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.ListCellRenderer;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
33 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
34 | import org.openstreetmap.josm.tools.GBC;
|
---|
35 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
36 |
|
---|
37 | public class StyleSources extends JPanel {
|
---|
38 | private JList sourcesList;
|
---|
39 | private JList sourcesDefaults;
|
---|
40 | private JList iconsList = null;
|
---|
41 | private String pref;
|
---|
42 | private String iconpref;
|
---|
43 |
|
---|
44 | public class SourceInfo {
|
---|
45 | String version;
|
---|
46 | String name;
|
---|
47 | String url;
|
---|
48 | String author;
|
---|
49 | String link;
|
---|
50 | String description;
|
---|
51 | String shortdescription;
|
---|
52 | public SourceInfo(String name, String url)
|
---|
53 | {
|
---|
54 | this.name = name;
|
---|
55 | this.url = url;
|
---|
56 | version = author = link = description = shortdescription = null;
|
---|
57 | }
|
---|
58 | public String getName()
|
---|
59 | {
|
---|
60 | return shortdescription == null ? name : shortdescription;
|
---|
61 | }
|
---|
62 | public String getTooltip()
|
---|
63 | {
|
---|
64 | String s = tr("Short Description: {0}", getName()) + "<br>" + tr("URL: {0}", url);
|
---|
65 | if(author != null) {
|
---|
66 | s += "<br>" + tr("Author: {0}", author);
|
---|
67 | }
|
---|
68 | if(link != null) {
|
---|
69 | s += "<br>" + tr("Webpage: {0}", link);
|
---|
70 | }
|
---|
71 | if(description != null) {
|
---|
72 | s += "<br>" + tr("Description: {0}", description);
|
---|
73 | }
|
---|
74 | if(version != null) {
|
---|
75 | s += "<br>" + tr("Version: {0}", version);
|
---|
76 | }
|
---|
77 | return "<html>" + s + "</html>";
|
---|
78 | }
|
---|
79 | @Override
|
---|
80 | public String toString()
|
---|
81 | {
|
---|
82 | return getName() + " (" + url + ")";
|
---|
83 | }
|
---|
84 | };
|
---|
85 |
|
---|
86 | class MyCellRenderer extends JLabel implements ListCellRenderer {
|
---|
87 | public Component getListCellRendererComponent(JList list, Object value,
|
---|
88 | int index, boolean isSelected, boolean cellHasFocus)
|
---|
89 | {
|
---|
90 | String s = value.toString();
|
---|
91 | setText(s);
|
---|
92 | if (isSelected) {
|
---|
93 | setBackground(list.getSelectionBackground());
|
---|
94 | setForeground(list.getSelectionForeground());
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | setBackground(list.getBackground());
|
---|
98 | setForeground(list.getForeground());
|
---|
99 | }
|
---|
100 | setEnabled(list.isEnabled());
|
---|
101 | setFont(list.getFont());
|
---|
102 | setOpaque(true);
|
---|
103 | setToolTipText(((SourceInfo)value).getTooltip());
|
---|
104 | return this;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public StyleSources(String pref, String iconpref, final String url, boolean named, final String name)
|
---|
109 | {
|
---|
110 | sourcesList = new JList(new DefaultListModel());
|
---|
111 | sourcesDefaults = new JList(new DefaultListModel());
|
---|
112 | sourcesDefaults.setCellRenderer(new MyCellRenderer());
|
---|
113 | getDefaults(url);
|
---|
114 |
|
---|
115 | this.pref = pref;
|
---|
116 | this.iconpref = iconpref;
|
---|
117 |
|
---|
118 | Collection<String> sources = Main.pref.getCollection(pref, null);
|
---|
119 | if(sources != null) {
|
---|
120 | for(String s : sources) {
|
---|
121 | ((DefaultListModel)sourcesList.getModel()).addElement(s);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | JButton iconadd = null;
|
---|
126 | JButton iconedit = null;
|
---|
127 | JButton icondelete = null;
|
---|
128 |
|
---|
129 | if(iconpref != null)
|
---|
130 | {
|
---|
131 | iconsList = new JList(new DefaultListModel());
|
---|
132 | sources = Main.pref.getCollection(iconpref, null);
|
---|
133 | if(sources != null) {
|
---|
134 | for(String s : sources) {
|
---|
135 | ((DefaultListModel)iconsList.getModel()).addElement(s);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | iconadd = new JButton(tr("Add"));
|
---|
140 | iconadd.addActionListener(new ActionListener(){
|
---|
141 | public void actionPerformed(ActionEvent e) {
|
---|
142 | String source = OptionPaneUtil.showInputDialog(
|
---|
143 | Main.parent,
|
---|
144 | tr("Icon paths"),
|
---|
145 | tr("Icon paths"),
|
---|
146 | JOptionPane.QUESTION_MESSAGE
|
---|
147 | );
|
---|
148 | if (source != null) {
|
---|
149 | ((DefaultListModel)iconsList.getModel()).addElement(source);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | });
|
---|
153 |
|
---|
154 | iconedit = new JButton(tr("Edit"));
|
---|
155 | iconedit.addActionListener(new ActionListener(){
|
---|
156 | public void actionPerformed(ActionEvent e) {
|
---|
157 | if (sourcesList.getSelectedIndex() == -1) {
|
---|
158 | OptionPaneUtil.showMessageDialog(
|
---|
159 | Main.parent,
|
---|
160 | tr("Please select the row to edit."),
|
---|
161 | tr("Warning"),
|
---|
162 | JOptionPane.WARNING_MESSAGE
|
---|
163 | );
|
---|
164 | } else {
|
---|
165 | String source = (String)OptionPaneUtil.showInputDialog(
|
---|
166 | Main.parent,
|
---|
167 | tr("Icon paths"),
|
---|
168 | tr("Icon paths"),
|
---|
169 | JOptionPane.QUESTION_MESSAGE,
|
---|
170 | null,
|
---|
171 | null,
|
---|
172 | iconsList.getSelectedValue()
|
---|
173 | );
|
---|
174 | if (source != null) {
|
---|
175 | ((DefaultListModel)iconsList.getModel()).setElementAt(source, iconsList.getSelectedIndex());
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | });
|
---|
180 |
|
---|
181 | icondelete = new JButton(tr("Delete"));
|
---|
182 | icondelete.addActionListener(new ActionListener(){
|
---|
183 | public void actionPerformed(ActionEvent e) {
|
---|
184 | if (iconsList.getSelectedIndex() == -1) {
|
---|
185 | OptionPaneUtil.showMessageDialog(
|
---|
186 | Main.parent, tr("Please select the row to delete."),
|
---|
187 | tr("Warning"),
|
---|
188 | JOptionPane.WARNING_MESSAGE);
|
---|
189 | } else {
|
---|
190 | ((DefaultListModel)iconsList.getModel()).remove(iconsList.getSelectedIndex());
|
---|
191 | }
|
---|
192 | }
|
---|
193 | });
|
---|
194 | }
|
---|
195 |
|
---|
196 | JButton add = new JButton(tr("Add"));
|
---|
197 | add.addActionListener(new ActionListener(){
|
---|
198 | public void actionPerformed(ActionEvent e) {
|
---|
199 | String source = OptionPaneUtil.showInputDialog(
|
---|
200 | Main.parent,
|
---|
201 | name,
|
---|
202 | name,
|
---|
203 | JOptionPane.QUESTION_MESSAGE);
|
---|
204 | if (source != null) {
|
---|
205 | ((DefaultListModel)sourcesList.getModel()).addElement(source);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | });
|
---|
209 |
|
---|
210 | JButton edit = new JButton(tr("Edit"));
|
---|
211 | edit.addActionListener(new ActionListener(){
|
---|
212 | public void actionPerformed(ActionEvent e) {
|
---|
213 | if (sourcesList.getSelectedIndex() == -1) {
|
---|
214 | OptionPaneUtil.showMessageDialog(
|
---|
215 | Main.parent, tr("Please select the row to edit."),
|
---|
216 | tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
217 | } else {
|
---|
218 | String source = (String)OptionPaneUtil.showInputDialog(
|
---|
219 | Main.parent,
|
---|
220 | name,
|
---|
221 | name,
|
---|
222 | JOptionPane.QUESTION_MESSAGE,
|
---|
223 | null,
|
---|
224 | null,
|
---|
225 | sourcesList.getSelectedValue()
|
---|
226 | );
|
---|
227 | if (source != null) {
|
---|
228 | ((DefaultListModel)sourcesList.getModel()).setElementAt(source, sourcesList.getSelectedIndex());
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | });
|
---|
233 |
|
---|
234 | JButton delete = new JButton(tr("Delete"));
|
---|
235 | delete.addActionListener(new ActionListener(){
|
---|
236 | public void actionPerformed(ActionEvent e) {
|
---|
237 | if (sourcesList.getSelectedIndex() == -1) {
|
---|
238 | OptionPaneUtil.showMessageDialog(Main.parent, tr("Please select the row to delete."),
|
---|
239 | tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
240 | } else {
|
---|
241 | ((DefaultListModel)sourcesList.getModel()).remove(sourcesList.getSelectedIndex());
|
---|
242 | }
|
---|
243 | }
|
---|
244 | });
|
---|
245 |
|
---|
246 | JButton copy = new JButton(tr("Copy defaults"));
|
---|
247 | copy.addActionListener(new ActionListener(){
|
---|
248 | public void actionPerformed(ActionEvent e) {
|
---|
249 | if (sourcesDefaults.getSelectedIndex() == -1) {
|
---|
250 | OptionPaneUtil.showMessageDialog(
|
---|
251 | Main.parent, tr("Please select the row to copy."),
|
---|
252 | tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
253 | } else {
|
---|
254 | ((DefaultListModel)sourcesList.getModel()).addElement(
|
---|
255 | ((SourceInfo)sourcesDefaults.getSelectedValue()).url);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | });
|
---|
259 |
|
---|
260 | JButton update = new JButton(tr("Update"));
|
---|
261 | update.addActionListener(new ActionListener(){
|
---|
262 | public void actionPerformed(ActionEvent e) {
|
---|
263 | MirroredInputStream.cleanup(url);
|
---|
264 | getDefaults(url);
|
---|
265 | int num = sourcesList.getModel().getSize();
|
---|
266 | if (num > 0)
|
---|
267 | {
|
---|
268 | ArrayList<String> l = new ArrayList<String>();
|
---|
269 | for (int i = 0; i < num; ++i) {
|
---|
270 | MirroredInputStream.cleanup((String)sourcesList.getModel().getElementAt(i));
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 | });
|
---|
275 |
|
---|
276 | sourcesList.setToolTipText(tr("The XML source (URL or filename) for {0} definition files.", name));
|
---|
277 | add.setToolTipText(tr("Add a new XML source to the list."));
|
---|
278 | delete.setToolTipText(tr("Delete the selected source from the list."));
|
---|
279 |
|
---|
280 | setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
---|
281 | setLayout(new GridBagLayout());
|
---|
282 | add(new JLabel(name), GBC.eol().insets(5,5,5,0));
|
---|
283 | add(new JScrollPane(sourcesList), GBC.eol().insets(5,0,5,0).fill(GBC.BOTH));
|
---|
284 | add(new JLabel(tr("Defaults (See tooltip for detailed information)")), GBC.eol().insets(5,5,5,0));
|
---|
285 | add(new JScrollPane(sourcesDefaults), GBC.eol().insets(5,0,5,0).fill(GBC.BOTH));
|
---|
286 | JPanel buttonPanel = new JPanel(new GridBagLayout());
|
---|
287 | add(buttonPanel, GBC.eol().insets(5,0,5,5).fill(GBC.HORIZONTAL));
|
---|
288 | buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
289 | buttonPanel.add(add, GBC.std().insets(0,5,0,0));
|
---|
290 | buttonPanel.add(edit, GBC.std().insets(5,5,5,0));
|
---|
291 | buttonPanel.add(delete, GBC.std().insets(0,5,5,0));
|
---|
292 | buttonPanel.add(copy, GBC.std().insets(0,5,5,0));
|
---|
293 | buttonPanel.add(update, GBC.std().insets(0,5,0,0));
|
---|
294 | if(iconsList != null)
|
---|
295 | {
|
---|
296 | add(new JLabel(tr("Icon paths")), GBC.eol().insets(5,-5,5,0));
|
---|
297 | add(new JScrollPane(iconsList), GBC.eol().insets(5,0,5,0).fill(GBC.BOTH));
|
---|
298 | buttonPanel = new JPanel(new GridBagLayout());
|
---|
299 | add(buttonPanel, GBC.eol().insets(5,0,5,5).fill(GBC.HORIZONTAL));
|
---|
300 | buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
301 | buttonPanel.add(iconadd, GBC.std().insets(0,5,0,0));
|
---|
302 | buttonPanel.add(iconedit, GBC.std().insets(5,5,5,0));
|
---|
303 | buttonPanel.add(icondelete, GBC.std().insets(0,5,0,0));
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | public boolean finish() {
|
---|
308 | boolean changed = false;
|
---|
309 | int num = sourcesList.getModel().getSize();
|
---|
310 | if (num > 0)
|
---|
311 | {
|
---|
312 | ArrayList<String> l = new ArrayList<String>();
|
---|
313 | for (int i = 0; i < num; ++i) {
|
---|
314 | l.add((String)sourcesList.getModel().getElementAt(i));
|
---|
315 | }
|
---|
316 | if(Main.pref.putCollection(pref, l)) {
|
---|
317 | changed = true;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | else if(Main.pref.putCollection(pref, null)) {
|
---|
321 | changed = true;
|
---|
322 | }
|
---|
323 | if(iconsList != null)
|
---|
324 | {
|
---|
325 | num = iconsList.getModel().getSize();
|
---|
326 | if (num > 0)
|
---|
327 | {
|
---|
328 | ArrayList<String> l = new ArrayList<String>();
|
---|
329 | for (int i = 0; i < num; ++i) {
|
---|
330 | l.add((String)iconsList.getModel().getElementAt(i));
|
---|
331 | }
|
---|
332 | if(Main.pref.putCollection(iconpref, l)) {
|
---|
333 | changed = true;
|
---|
334 | }
|
---|
335 | }
|
---|
336 | else if(Main.pref.putCollection(iconpref, null)) {
|
---|
337 | changed = true;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | return changed;
|
---|
341 | }
|
---|
342 |
|
---|
343 | public void getDefaults(String name)
|
---|
344 | {
|
---|
345 | ((DefaultListModel)sourcesDefaults.getModel()).removeAllElements();
|
---|
346 | String lang = LanguageInfo.getLanguageCodeXML();
|
---|
347 | try
|
---|
348 | {
|
---|
349 | MirroredInputStream stream = new MirroredInputStream(name);
|
---|
350 | InputStreamReader r;
|
---|
351 | try
|
---|
352 | {
|
---|
353 | r = new InputStreamReader(stream, "UTF-8");
|
---|
354 | }
|
---|
355 | catch (UnsupportedEncodingException e)
|
---|
356 | {
|
---|
357 | r = new InputStreamReader(stream);
|
---|
358 | }
|
---|
359 | BufferedReader reader = new BufferedReader(r);
|
---|
360 |
|
---|
361 | String line;
|
---|
362 | SourceInfo last = null;
|
---|
363 |
|
---|
364 | while((line = reader.readLine()) != null)
|
---|
365 | {
|
---|
366 | if(line.startsWith("\t"))
|
---|
367 | {
|
---|
368 | Matcher m = Pattern.compile("^\t([^:]+): *(.+)$").matcher(line);
|
---|
369 | m.matches();
|
---|
370 | try
|
---|
371 | {
|
---|
372 | if(last != null)
|
---|
373 | {
|
---|
374 | String key = m.group(1);
|
---|
375 | String value = m.group(2);
|
---|
376 | if("author".equals(key) && last.author == null) {
|
---|
377 | last.author = value;
|
---|
378 | } else if("version".equals(key)) {
|
---|
379 | last.version = value;
|
---|
380 | } else if("link".equals(key) && last.link == null) {
|
---|
381 | last.link = value;
|
---|
382 | } else if("description".equals(key) && last.description == null) {
|
---|
383 | last.description = value;
|
---|
384 | } else if("shortdescription".equals(key) && last.shortdescription == null) {
|
---|
385 | last.shortdescription = value;
|
---|
386 | } else if((lang+"author").equals(key)) {
|
---|
387 | last.author = value;
|
---|
388 | } else if((lang+"link").equals(key)) {
|
---|
389 | last.link = value;
|
---|
390 | } else if((lang+"description").equals(key)) {
|
---|
391 | last.description = value;
|
---|
392 | } else if((lang+"shortdescription").equals(key)) {
|
---|
393 | last.shortdescription = value;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | }
|
---|
397 | catch (IllegalStateException e)
|
---|
398 | { e.printStackTrace(); }
|
---|
399 | }
|
---|
400 | else
|
---|
401 | {
|
---|
402 | last = null;
|
---|
403 | Matcher m = Pattern.compile("^(.+);(.+)$").matcher(line);
|
---|
404 | m.matches();
|
---|
405 | try
|
---|
406 | {
|
---|
407 | last = new SourceInfo(m.group(1),m.group(2));
|
---|
408 | ((DefaultListModel)sourcesDefaults.getModel()).addElement(last);
|
---|
409 | }
|
---|
410 | catch (IllegalStateException e)
|
---|
411 | { e.printStackTrace(); }
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | catch(Exception e)
|
---|
416 | { e.printStackTrace(); }
|
---|
417 | }
|
---|
418 | }
|
---|