1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.Point;
|
---|
13 | import java.awt.Rectangle;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.ActionListener;
|
---|
16 | import java.awt.event.KeyEvent;
|
---|
17 | import java.awt.event.MouseEvent;
|
---|
18 | import java.io.BufferedInputStream;
|
---|
19 | import java.io.BufferedOutputStream;
|
---|
20 | import java.io.BufferedReader;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileOutputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.InputStream;
|
---|
25 | import java.io.InputStreamReader;
|
---|
26 | import java.util.ArrayList;
|
---|
27 | import java.util.Arrays;
|
---|
28 | import java.util.List;
|
---|
29 |
|
---|
30 | import javax.swing.AbstractAction;
|
---|
31 | import javax.swing.DefaultButtonModel;
|
---|
32 | import javax.swing.DefaultListSelectionModel;
|
---|
33 | import javax.swing.JCheckBox;
|
---|
34 | import javax.swing.JFileChooser;
|
---|
35 | import javax.swing.JLabel;
|
---|
36 | import javax.swing.JPanel;
|
---|
37 | import javax.swing.JPopupMenu;
|
---|
38 | import javax.swing.JScrollPane;
|
---|
39 | import javax.swing.JTabbedPane;
|
---|
40 | import javax.swing.JTable;
|
---|
41 | import javax.swing.JViewport;
|
---|
42 | import javax.swing.ListSelectionModel;
|
---|
43 | import javax.swing.SingleSelectionModel;
|
---|
44 | import javax.swing.SwingConstants;
|
---|
45 | import javax.swing.SwingUtilities;
|
---|
46 | import javax.swing.UIManager;
|
---|
47 | import javax.swing.border.EmptyBorder;
|
---|
48 | import javax.swing.event.ChangeEvent;
|
---|
49 | import javax.swing.event.ChangeListener;
|
---|
50 | import javax.swing.event.ListSelectionEvent;
|
---|
51 | import javax.swing.event.ListSelectionListener;
|
---|
52 | import javax.swing.filechooser.FileFilter;
|
---|
53 | import javax.swing.table.AbstractTableModel;
|
---|
54 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
55 | import javax.swing.table.TableCellRenderer;
|
---|
56 | import javax.swing.table.TableModel;
|
---|
57 |
|
---|
58 | import org.openstreetmap.josm.Main;
|
---|
59 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
60 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
61 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
62 | import org.openstreetmap.josm.gui.SideButton;
|
---|
63 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
64 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStyleLoader;
|
---|
65 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
|
---|
66 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
67 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
68 | import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
|
---|
69 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
70 | import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
|
---|
71 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
72 | import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
|
---|
73 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
74 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
75 | import org.openstreetmap.josm.tools.GBC;
|
---|
76 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
77 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
78 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
79 | import org.openstreetmap.josm.tools.Utils;
|
---|
80 |
|
---|
81 | public class MapPaintDialog extends ToggleDialog implements Main.WindowSwitchListener {
|
---|
82 |
|
---|
83 | protected StylesTable tblStyles;
|
---|
84 | protected StylesModel model;
|
---|
85 | protected DefaultListSelectionModel selectionModel;
|
---|
86 |
|
---|
87 | protected OnOffAction onoffAction;
|
---|
88 | protected ReloadAction reloadAction;
|
---|
89 | protected MoveUpDownAction upAction;
|
---|
90 | protected MoveUpDownAction downAction;
|
---|
91 | protected JCheckBox cbWireframe;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Constructs a new {@code MapPaintDialog}.
|
---|
95 | */
|
---|
96 | public MapPaintDialog() {
|
---|
97 | super(tr("Map Paint Styles"), "mapstyle", tr("configure the map painting style"),
|
---|
98 | Shortcut.registerShortcut("subwindow:mappaint", tr("Toggle: {0}", tr("MapPaint")),
|
---|
99 | KeyEvent.VK_M, Shortcut.ALT_SHIFT), 150);
|
---|
100 | build();
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected void build() {
|
---|
104 | model = new StylesModel();
|
---|
105 |
|
---|
106 | cbWireframe = new JCheckBox();
|
---|
107 | JLabel wfLabel = new JLabel(tr("Wireframe View"), ImageProvider.get("dialogs/mappaint", "wireframe_small"), JLabel.HORIZONTAL);
|
---|
108 | wfLabel.setFont(wfLabel.getFont().deriveFont(Font.PLAIN));
|
---|
109 |
|
---|
110 | cbWireframe.setModel(new DefaultButtonModel() {
|
---|
111 | @Override
|
---|
112 | public void setSelected(boolean b) {
|
---|
113 | super.setSelected(b);
|
---|
114 | tblStyles.setEnabled(!b);
|
---|
115 | onoffAction.updateEnabledState();
|
---|
116 | upAction.updateEnabledState();
|
---|
117 | downAction.updateEnabledState();
|
---|
118 | }
|
---|
119 | });
|
---|
120 | cbWireframe.addActionListener(new ActionListener() {
|
---|
121 | @Override
|
---|
122 | public void actionPerformed(ActionEvent e) {
|
---|
123 | Main.main.menu.wireFrameToggleAction.actionPerformed(null);
|
---|
124 | }
|
---|
125 | });
|
---|
126 | cbWireframe.setBorder(new EmptyBorder(new Insets(1,1,1,1)));
|
---|
127 |
|
---|
128 | tblStyles = new StylesTable(model);
|
---|
129 | tblStyles.setSelectionModel(selectionModel= new DefaultListSelectionModel());
|
---|
130 | tblStyles.addMouseListener(new PopupMenuHandler());
|
---|
131 | tblStyles.putClientProperty("terminateEditOnFocusLost", true);
|
---|
132 | tblStyles.setBackground(UIManager.getColor("Panel.background"));
|
---|
133 | tblStyles.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
134 | tblStyles.setTableHeader(null);
|
---|
135 | tblStyles.getColumnModel().getColumn(0).setMaxWidth(1);
|
---|
136 | tblStyles.getColumnModel().getColumn(0).setResizable(false);
|
---|
137 | tblStyles.getColumnModel().getColumn(0).setCellRenderer(new MyCheckBoxRenderer());
|
---|
138 | tblStyles.getColumnModel().getColumn(1).setCellRenderer(new StyleSourceRenderer());
|
---|
139 | tblStyles.setShowGrid(false);
|
---|
140 | tblStyles.setIntercellSpacing(new Dimension(0, 0));
|
---|
141 |
|
---|
142 | JPanel p = new JPanel(new GridBagLayout());
|
---|
143 | p.add(cbWireframe, GBC.std(0, 0));
|
---|
144 | p.add(wfLabel, GBC.std(1, 0).weight(1, 0));
|
---|
145 | p.add(tblStyles, GBC.std(0, 1).span(2).fill());
|
---|
146 |
|
---|
147 | reloadAction = new ReloadAction();
|
---|
148 | onoffAction = new OnOffAction();
|
---|
149 | upAction = new MoveUpDownAction(false);
|
---|
150 | downAction = new MoveUpDownAction(true);
|
---|
151 | selectionModel.addListSelectionListener(onoffAction);
|
---|
152 | selectionModel.addListSelectionListener(reloadAction);
|
---|
153 | selectionModel.addListSelectionListener(upAction);
|
---|
154 | selectionModel.addListSelectionListener(downAction);
|
---|
155 |
|
---|
156 | // Toggle style on Enter and Spacebar
|
---|
157 | InputMapUtils.addEnterAction(tblStyles, onoffAction);
|
---|
158 | InputMapUtils.addSpacebarAction(tblStyles, onoffAction);
|
---|
159 |
|
---|
160 | createLayout(p, true, Arrays.asList(new SideButton[] {
|
---|
161 | new SideButton(onoffAction, false),
|
---|
162 | new SideButton(upAction, false),
|
---|
163 | new SideButton(downAction, false),
|
---|
164 | new SideButton(new LaunchMapPaintPreferencesAction(), false)
|
---|
165 | }));
|
---|
166 | }
|
---|
167 |
|
---|
168 | protected static class StylesTable extends JTable {
|
---|
169 |
|
---|
170 | public StylesTable(TableModel dm) {
|
---|
171 | super(dm);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public void scrollToVisible(int row, int col) {
|
---|
175 | if (!(getParent() instanceof JViewport))
|
---|
176 | return;
|
---|
177 | JViewport viewport = (JViewport) getParent();
|
---|
178 | Rectangle rect = getCellRect(row, col, true);
|
---|
179 | Point pt = viewport.getViewPosition();
|
---|
180 | rect.setLocation(rect.x - pt.x, rect.y - pt.y);
|
---|
181 | viewport.scrollRectToVisible(rect);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public void toOtherApplication() {
|
---|
187 | // nothing
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public void fromOtherApplication() {
|
---|
192 | // Reload local styles when they have been changed in an external editor.
|
---|
193 | // Checks file modification time.
|
---|
194 | List<StyleSource> toReload = new ArrayList<StyleSource>();
|
---|
195 | for (StyleSource s : MapPaintStyles.getStyles().getStyleSources()) {
|
---|
196 | if (s.isLocal()) {
|
---|
197 | File f = new File(s.url);
|
---|
198 | long mtime = f.lastModified();
|
---|
199 | if (mtime > s.getLastMTime()) {
|
---|
200 | toReload.add(s);
|
---|
201 | s.setLastMTime(mtime);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 | if (!toReload.isEmpty()) {
|
---|
206 | System.out.println(trn("Reloading {0} map style.", "Reloading {0} map styles.", toReload.size(), toReload.size()));
|
---|
207 | Main.worker.submit(new MapPaintStyleLoader(toReload));
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public void showNotify() {
|
---|
213 | MapPaintStyles.addMapPaintSylesUpdateListener(model);
|
---|
214 | Main.main.menu.wireFrameToggleAction.addButtonModel(cbWireframe.getModel());
|
---|
215 | if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) {
|
---|
216 | Main.addWindowSwitchListener(this);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public void hideNotify() {
|
---|
222 | Main.main.menu.wireFrameToggleAction.removeButtonModel(cbWireframe.getModel());
|
---|
223 | MapPaintStyles.removeMapPaintSylesUpdateListener(model);
|
---|
224 | if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true)) {
|
---|
225 | Main.removeWindowSwitchListener(this);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | protected class StylesModel extends AbstractTableModel implements MapPaintSylesUpdateListener {
|
---|
230 |
|
---|
231 | List<StyleSource> data = new ArrayList<StyleSource>();
|
---|
232 |
|
---|
233 | public StylesModel() {
|
---|
234 | data = new ArrayList<StyleSource>(MapPaintStyles.getStyles().getStyleSources());
|
---|
235 | }
|
---|
236 |
|
---|
237 | private StyleSource getRow(int i) {
|
---|
238 | return data.get(i);
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override
|
---|
242 | public int getColumnCount() {
|
---|
243 | return 2;
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | public int getRowCount() {
|
---|
248 | return data.size();
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public Object getValueAt(int row, int column) {
|
---|
253 | if (column == 0)
|
---|
254 | return getRow(row).active;
|
---|
255 | else
|
---|
256 | return getRow(row);
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public boolean isCellEditable(int row, int column) {
|
---|
261 | return column == 0;
|
---|
262 | }
|
---|
263 |
|
---|
264 | Class<?>[] columnClasses = {Boolean.class, StyleSource.class};
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public Class<?> getColumnClass(int column) {
|
---|
268 | return columnClasses[column];
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public void setValueAt(Object aValue, int row, int column) {
|
---|
273 | if (row < 0 || row >= getRowCount() || aValue == null)
|
---|
274 | return;
|
---|
275 | if (column == 0) {
|
---|
276 | MapPaintStyles.toggleStyleActive(row);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Make sure the first of the selected entry is visible in the
|
---|
282 | * views of this model.
|
---|
283 | */
|
---|
284 | public void ensureSelectedIsVisible() {
|
---|
285 | int index = selectionModel.getMinSelectionIndex();
|
---|
286 | if (index < 0) return;
|
---|
287 | if (index >= getRowCount()) return;
|
---|
288 | tblStyles.scrollToVisible(index, 0);
|
---|
289 | tblStyles.repaint();
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * MapPaintSylesUpdateListener interface
|
---|
294 | */
|
---|
295 |
|
---|
296 | @Override
|
---|
297 | public void mapPaintStylesUpdated() {
|
---|
298 | data = new ArrayList<StyleSource>(MapPaintStyles.getStyles().getStyleSources());
|
---|
299 | fireTableDataChanged();
|
---|
300 | tblStyles.repaint();
|
---|
301 | }
|
---|
302 |
|
---|
303 | @Override
|
---|
304 | public void mapPaintStyleEntryUpdated(int idx) {
|
---|
305 | data = new ArrayList<StyleSource>(MapPaintStyles.getStyles().getStyleSources());
|
---|
306 | fireTableRowsUpdated(idx, idx);
|
---|
307 | tblStyles.repaint();
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | private class MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
|
---|
312 |
|
---|
313 | public MyCheckBoxRenderer() {
|
---|
314 | setHorizontalAlignment(SwingConstants.CENTER);
|
---|
315 | setVerticalAlignment(SwingConstants.CENTER);
|
---|
316 | }
|
---|
317 |
|
---|
318 | @Override
|
---|
319 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row,int column) {
|
---|
320 | if (value == null)
|
---|
321 | return this;
|
---|
322 | boolean b = (Boolean) value;
|
---|
323 | setSelected(b);
|
---|
324 | setEnabled(!cbWireframe.isSelected());
|
---|
325 | return this;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | private class StyleSourceRenderer extends DefaultTableCellRenderer {
|
---|
330 | @Override
|
---|
331 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
332 | if (value == null)
|
---|
333 | return this;
|
---|
334 | StyleSource s = (StyleSource) value;
|
---|
335 | JLabel label = (JLabel)super.getTableCellRendererComponent(table,
|
---|
336 | s.getDisplayString(), isSelected, hasFocus, row, column);
|
---|
337 | label.setIcon(s.getIcon());
|
---|
338 | label.setToolTipText(s.getToolTipText());
|
---|
339 | label.setEnabled(!cbWireframe.isSelected());
|
---|
340 | return label;
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | protected class OnOffAction extends AbstractAction implements ListSelectionListener {
|
---|
345 | public OnOffAction() {
|
---|
346 | putValue(NAME, tr("On/Off"));
|
---|
347 | putValue(SHORT_DESCRIPTION, tr("Turn selected styles on or off"));
|
---|
348 | putValue(SMALL_ICON, ImageProvider.get("apply"));
|
---|
349 | updateEnabledState();
|
---|
350 | }
|
---|
351 |
|
---|
352 | protected void updateEnabledState() {
|
---|
353 | setEnabled(!cbWireframe.isSelected() && tblStyles.getSelectedRowCount() > 0);
|
---|
354 | }
|
---|
355 |
|
---|
356 | @Override
|
---|
357 | public void valueChanged(ListSelectionEvent e) {
|
---|
358 | updateEnabledState();
|
---|
359 | }
|
---|
360 |
|
---|
361 | @Override
|
---|
362 | public void actionPerformed(ActionEvent e) {
|
---|
363 | int[] pos = tblStyles.getSelectedRows();
|
---|
364 | MapPaintStyles.toggleStyleActive(pos);
|
---|
365 | selectionModel.clearSelection();
|
---|
366 | for (int p: pos) {
|
---|
367 | selectionModel.addSelectionInterval(p, p);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * The action to move down the currently selected entries in the list.
|
---|
374 | */
|
---|
375 | protected class MoveUpDownAction extends AbstractAction implements ListSelectionListener {
|
---|
376 |
|
---|
377 | final int increment;
|
---|
378 |
|
---|
379 | public MoveUpDownAction(boolean isDown) {
|
---|
380 | increment = isDown ? 1 : -1;
|
---|
381 | putValue(NAME, isDown?tr("Down"):tr("Up"));
|
---|
382 | putValue(SMALL_ICON, isDown ? ImageProvider.get("dialogs", "down") : ImageProvider.get("dialogs", "up"));
|
---|
383 | putValue(SHORT_DESCRIPTION, isDown ? tr("Move the selected entry one row down.") : tr("Move the selected entry one row up."));
|
---|
384 | updateEnabledState();
|
---|
385 | }
|
---|
386 |
|
---|
387 | public void updateEnabledState() {
|
---|
388 | int[] sel = tblStyles.getSelectedRows();
|
---|
389 | setEnabled(!cbWireframe.isSelected() && MapPaintStyles.canMoveStyles(sel, increment));
|
---|
390 | }
|
---|
391 |
|
---|
392 | @Override
|
---|
393 | public void actionPerformed(ActionEvent e) {
|
---|
394 | int[] sel = tblStyles.getSelectedRows();
|
---|
395 | MapPaintStyles.moveStyles(sel, increment);
|
---|
396 |
|
---|
397 | selectionModel.clearSelection();
|
---|
398 | for (int row: sel) {
|
---|
399 | selectionModel.addSelectionInterval(row + increment, row + increment);
|
---|
400 | }
|
---|
401 | model.ensureSelectedIsVisible();
|
---|
402 | }
|
---|
403 |
|
---|
404 | @Override
|
---|
405 | public void valueChanged(ListSelectionEvent e) {
|
---|
406 | updateEnabledState();
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Opens preferences window and selects the mappaint tab.
|
---|
412 | */
|
---|
413 | public static class LaunchMapPaintPreferencesAction extends AbstractAction {
|
---|
414 | /**
|
---|
415 | * Constructs a new {@code LaunchMapPaintPreferencesAction}.
|
---|
416 | */
|
---|
417 | public LaunchMapPaintPreferencesAction() {
|
---|
418 | putValue(NAME, tr("Preferences"));
|
---|
419 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "mappaintpreference"));
|
---|
420 | }
|
---|
421 |
|
---|
422 | @Override
|
---|
423 | public void actionPerformed(ActionEvent e) {
|
---|
424 | final PreferenceDialog p =new PreferenceDialog(Main.parent);
|
---|
425 | p.selectMapPaintPreferenceTab();
|
---|
426 | p.setVisible(true);
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | protected class ReloadAction extends AbstractAction implements ListSelectionListener {
|
---|
431 | public ReloadAction() {
|
---|
432 | putValue(NAME, tr("Reload from file"));
|
---|
433 | putValue(SHORT_DESCRIPTION, tr("reload selected styles from file"));
|
---|
434 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh"));
|
---|
435 | setEnabled(getEnabledState());
|
---|
436 | }
|
---|
437 |
|
---|
438 | protected boolean getEnabledState() {
|
---|
439 | if (cbWireframe.isSelected())
|
---|
440 | return false;
|
---|
441 | int[] pos = tblStyles.getSelectedRows();
|
---|
442 | if (pos.length == 0)
|
---|
443 | return false;
|
---|
444 | for (int i : pos) {
|
---|
445 | if (!model.getRow(i).isLocal())
|
---|
446 | return false;
|
---|
447 | }
|
---|
448 | return true;
|
---|
449 | }
|
---|
450 |
|
---|
451 | @Override
|
---|
452 | public void valueChanged(ListSelectionEvent e) {
|
---|
453 | setEnabled(getEnabledState());
|
---|
454 | }
|
---|
455 |
|
---|
456 | @Override
|
---|
457 | public void actionPerformed(ActionEvent e) {
|
---|
458 | final int[] rows = tblStyles.getSelectedRows();
|
---|
459 | MapPaintStyles.reloadStyles(rows);
|
---|
460 | Main.worker.submit(new Runnable() {
|
---|
461 | @Override
|
---|
462 | public void run() {
|
---|
463 | SwingUtilities.invokeLater(new Runnable() {
|
---|
464 | @Override
|
---|
465 | public void run() {
|
---|
466 | selectionModel.clearSelection();
|
---|
467 | for (int r: rows) {
|
---|
468 | selectionModel.addSelectionInterval(r, r);
|
---|
469 | }
|
---|
470 | }
|
---|
471 | });
|
---|
472 |
|
---|
473 | }
|
---|
474 | });
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | protected class SaveAsAction extends AbstractAction {
|
---|
479 |
|
---|
480 | public SaveAsAction() {
|
---|
481 | putValue(NAME, tr("Save as..."));
|
---|
482 | putValue(SHORT_DESCRIPTION, tr("Save a copy of this Style to file and add it to the list"));
|
---|
483 | putValue(SMALL_ICON, ImageProvider.get("copy"));
|
---|
484 | setEnabled(tblStyles.getSelectedRows().length == 1);
|
---|
485 | }
|
---|
486 |
|
---|
487 | @Override
|
---|
488 | public void actionPerformed(ActionEvent e) {
|
---|
489 | int sel = tblStyles.getSelectionModel().getLeadSelectionIndex();
|
---|
490 | if (sel < 0 || sel >= model.getRowCount())
|
---|
491 | return;
|
---|
492 | final StyleSource s = model.getRow(sel);
|
---|
493 |
|
---|
494 | JFileChooserManager fcm = new JFileChooserManager(false, "mappaint.clone-style.lastDirectory", System.getProperty("user.home"));
|
---|
495 | String suggestion = fcm.getInitialDirectory() + File.separator + s.getFileNamePart();
|
---|
496 |
|
---|
497 | FileFilter ff;
|
---|
498 | if (s instanceof MapCSSStyleSource) {
|
---|
499 | ff = new ExtensionFileFilter("mapcss,css,zip", "mapcss", tr("Map paint style file (*.mapcss, *.zip)"));
|
---|
500 | } else {
|
---|
501 | ff = new ExtensionFileFilter("xml,zip", "xml", tr("Map paint style file (*.xml, *.zip)"));
|
---|
502 | }
|
---|
503 | fcm.createFileChooser(false, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY)
|
---|
504 | .getFileChooser().setSelectedFile(new File(suggestion));
|
---|
505 | JFileChooser fc = fcm.openFileChooser();
|
---|
506 | if (fc == null)
|
---|
507 | return;
|
---|
508 | Main.worker.submit(new SaveToFileTask(s, fc.getSelectedFile()));
|
---|
509 | }
|
---|
510 |
|
---|
511 | private class SaveToFileTask extends PleaseWaitRunnable {
|
---|
512 | private StyleSource s;
|
---|
513 | private File file;
|
---|
514 |
|
---|
515 | private boolean canceled;
|
---|
516 | private boolean error;
|
---|
517 |
|
---|
518 | public SaveToFileTask(StyleSource s, File file) {
|
---|
519 | super(tr("Reloading style sources"));
|
---|
520 | this.s = s;
|
---|
521 | this.file = file;
|
---|
522 | }
|
---|
523 |
|
---|
524 | @Override
|
---|
525 | protected void cancel() {
|
---|
526 | canceled = true;
|
---|
527 | }
|
---|
528 |
|
---|
529 | @Override
|
---|
530 | protected void realRun() {
|
---|
531 | getProgressMonitor().indeterminateSubTask(
|
---|
532 | tr("Save style ''{0}'' as ''{1}''", s.getDisplayString(), file.getPath()));
|
---|
533 | BufferedInputStream bis = null;
|
---|
534 | BufferedOutputStream bos = null;
|
---|
535 | try {
|
---|
536 | bis = new BufferedInputStream(s.getSourceInputStream());
|
---|
537 | bos = new BufferedOutputStream(new FileOutputStream(file));
|
---|
538 | byte[] buffer = new byte[4096];
|
---|
539 | int length;
|
---|
540 | while ((length = bis.read(buffer)) > -1 && !canceled) {
|
---|
541 | bos.write(buffer, 0, length);
|
---|
542 | }
|
---|
543 | } catch (IOException e) {
|
---|
544 | error = true;
|
---|
545 | } finally {
|
---|
546 | Utils.close(bis);
|
---|
547 | Utils.close(bos);
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | @Override
|
---|
552 | protected void finish() {
|
---|
553 | SwingUtilities.invokeLater(new Runnable() {
|
---|
554 | @Override
|
---|
555 | public void run() {
|
---|
556 | if (!error && !canceled) {
|
---|
557 | SourceEntry se = new SourceEntry(s);
|
---|
558 | se.url = file.getPath();
|
---|
559 | MapPaintStyles.addStyle(se);
|
---|
560 | tblStyles.getSelectionModel().setSelectionInterval(model.getRowCount() - 1 , model.getRowCount() - 1);
|
---|
561 | model.ensureSelectedIsVisible();
|
---|
562 | }
|
---|
563 | }
|
---|
564 | });
|
---|
565 | }
|
---|
566 | }
|
---|
567 | }
|
---|
568 |
|
---|
569 | protected class InfoAction extends AbstractAction {
|
---|
570 |
|
---|
571 | boolean errorsTabLoaded;
|
---|
572 | boolean sourceTabLoaded;
|
---|
573 |
|
---|
574 | public InfoAction() {
|
---|
575 | putValue(NAME, tr("Info"));
|
---|
576 | putValue(SHORT_DESCRIPTION, tr("view meta information, error log and source definition"));
|
---|
577 | putValue(SMALL_ICON, ImageProvider.get("info"));
|
---|
578 | setEnabled(tblStyles.getSelectedRows().length == 1);
|
---|
579 | }
|
---|
580 |
|
---|
581 | @Override
|
---|
582 | public void actionPerformed(ActionEvent e) {
|
---|
583 | int sel = tblStyles.getSelectionModel().getLeadSelectionIndex();
|
---|
584 | if (sel < 0 || sel >= model.getRowCount())
|
---|
585 | return;
|
---|
586 | final StyleSource s = model.getRow(sel);
|
---|
587 | ExtendedDialog info = new ExtendedDialog(Main.parent, tr("Map Style info"), new String[] {tr("Close")});
|
---|
588 | info.setPreferredSize(new Dimension(600, 400));
|
---|
589 | info.setButtonIcons(new String[] {"ok.png"});
|
---|
590 |
|
---|
591 | final JTabbedPane tabs = new JTabbedPane();
|
---|
592 |
|
---|
593 | tabs.add("Info", buildInfoPanel(s));
|
---|
594 | JLabel lblInfo = new JLabel(tr("Info"));
|
---|
595 | lblInfo.setFont(lblInfo.getFont().deriveFont(Font.PLAIN));
|
---|
596 | tabs.setTabComponentAt(0, lblInfo);
|
---|
597 |
|
---|
598 | final JPanel pErrors = new JPanel(new GridBagLayout());
|
---|
599 | tabs.add("Errors", pErrors);
|
---|
600 | JLabel lblErrors;
|
---|
601 | if (s.getErrors().isEmpty()) {
|
---|
602 | lblErrors = new JLabel(tr("Errors"));
|
---|
603 | lblErrors.setFont(lblInfo.getFont().deriveFont(Font.PLAIN));
|
---|
604 | lblErrors.setEnabled(false);
|
---|
605 | tabs.setTabComponentAt(1, lblErrors);
|
---|
606 | tabs.setEnabledAt(1, false);
|
---|
607 | } else {
|
---|
608 | lblErrors = new JLabel(tr("Errors"), ImageProvider.get("misc", "error"), JLabel.HORIZONTAL);
|
---|
609 | tabs.setTabComponentAt(1, lblErrors);
|
---|
610 | }
|
---|
611 |
|
---|
612 | final JPanel pSource = new JPanel(new GridBagLayout());
|
---|
613 | tabs.addTab("Source", pSource);
|
---|
614 | JLabel lblSource = new JLabel(tr("Source"));
|
---|
615 | lblSource.setFont(lblSource.getFont().deriveFont(Font.PLAIN));
|
---|
616 | tabs.setTabComponentAt(2, lblSource);
|
---|
617 |
|
---|
618 | tabs.getModel().addChangeListener(new ChangeListener() {
|
---|
619 | @Override
|
---|
620 | public void stateChanged(ChangeEvent e) {
|
---|
621 | if (!errorsTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 1) {
|
---|
622 | errorsTabLoaded = true;
|
---|
623 | buildErrorsPanel(s, pErrors);
|
---|
624 | }
|
---|
625 | if (!sourceTabLoaded && ((SingleSelectionModel) e.getSource()).getSelectedIndex() == 2) {
|
---|
626 | sourceTabLoaded = true;
|
---|
627 | buildSourcePanel(s, pSource);
|
---|
628 | }
|
---|
629 | }
|
---|
630 | });
|
---|
631 | info.setContent(tabs, false);
|
---|
632 | info.showDialog();
|
---|
633 | }
|
---|
634 |
|
---|
635 | private JPanel buildInfoPanel(StyleSource s) {
|
---|
636 | JPanel p = new JPanel(new GridBagLayout());
|
---|
637 | StringBuilder text = new StringBuilder("<table cellpadding=3>");
|
---|
638 | text.append(tableRow(tr("Title:"), s.getDisplayString()));
|
---|
639 | if (s.url.startsWith("http://")) {
|
---|
640 | text.append(tableRow(tr("URL:"), s.url));
|
---|
641 | } else if (s.url.startsWith("resource://")) {
|
---|
642 | text.append(tableRow(tr("Built-in Style, internal path:"), s.url));
|
---|
643 | } else {
|
---|
644 | text.append(tableRow(tr("Path:"), s.url));
|
---|
645 | }
|
---|
646 | if (s.icon != null) {
|
---|
647 | text.append(tableRow(tr("Icon:"), s.icon));
|
---|
648 | }
|
---|
649 | if (s.getBackgroundColorOverride() != null) {
|
---|
650 | text.append(tableRow(tr("Background:"), Utils.toString(s.getBackgroundColorOverride())));
|
---|
651 | }
|
---|
652 | text.append(tableRow(tr("Style is currently active?"), s.active ? tr("Yes") : tr("No")));
|
---|
653 | text.append("</table>");
|
---|
654 | p.add(new JScrollPane(new HtmlPanel(text.toString())), GBC.eol().fill(GBC.BOTH));
|
---|
655 | return p;
|
---|
656 | }
|
---|
657 |
|
---|
658 | private String tableRow(String firstColumn, String secondColumn) {
|
---|
659 | return "<tr><td><b>" + firstColumn + "</b></td><td>" + secondColumn + "</td></tr>";
|
---|
660 | }
|
---|
661 |
|
---|
662 | private void buildSourcePanel(StyleSource s, JPanel p) {
|
---|
663 | JosmTextArea txtSource = new JosmTextArea();
|
---|
664 | txtSource.setFont(new Font("Monospaced", txtSource.getFont().getStyle(), txtSource.getFont().getSize()));
|
---|
665 | txtSource.setEditable(false);
|
---|
666 | p.add(new JScrollPane(txtSource), GBC.std().fill());
|
---|
667 |
|
---|
668 | InputStream is = null;
|
---|
669 | try {
|
---|
670 | is = s.getSourceInputStream();
|
---|
671 | BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
---|
672 | String line;
|
---|
673 | while ((line = reader.readLine()) != null) {
|
---|
674 | txtSource.append(line + "\n");
|
---|
675 | }
|
---|
676 | } catch (IOException ex) {
|
---|
677 | txtSource.append("<ERROR: failed to read file!>");
|
---|
678 | } finally {
|
---|
679 | Utils.close(is);
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | private void buildErrorsPanel(StyleSource s, JPanel p) {
|
---|
684 | JosmTextArea txtErrors = new JosmTextArea();
|
---|
685 | txtErrors.setFont(new Font("Monospaced", txtErrors.getFont().getStyle(), txtErrors.getFont().getSize()));
|
---|
686 | txtErrors.setEditable(false);
|
---|
687 | p.add(new JScrollPane(txtErrors), GBC.std().fill());
|
---|
688 | for (Throwable t : s.getErrors()) {
|
---|
689 | txtErrors.append(t.toString() + "\n");
|
---|
690 | }
|
---|
691 | }
|
---|
692 | }
|
---|
693 |
|
---|
694 | class PopupMenuHandler extends PopupMenuLauncher {
|
---|
695 | @Override
|
---|
696 | public void launch(MouseEvent evt) {
|
---|
697 | if (cbWireframe.isSelected())
|
---|
698 | return;
|
---|
699 | super.launch(evt);
|
---|
700 | }
|
---|
701 |
|
---|
702 | @Override
|
---|
703 | protected void showMenu(MouseEvent evt) {
|
---|
704 | menu = new MapPaintPopup();
|
---|
705 | super.showMenu(evt);
|
---|
706 | }
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * The popup menu displayed when right-clicking a map paint entry
|
---|
711 | */
|
---|
712 | public class MapPaintPopup extends JPopupMenu {
|
---|
713 | /**
|
---|
714 | * Constructs a new {@code MapPaintPopup}.
|
---|
715 | */
|
---|
716 | public MapPaintPopup() {
|
---|
717 | add(reloadAction);
|
---|
718 | add(new SaveAsAction());
|
---|
719 | addSeparator();
|
---|
720 | add(new InfoAction());
|
---|
721 | }
|
---|
722 | }
|
---|
723 | }
|
---|