source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java@ 18715

Last change on this file since 18715 was 18715, checked in by taylor.smock, 13 months ago

Fix #22798: Convert dialog actions which extend AbstractAction to ones which extend JosmAction

This will allow users to bind shortcuts to actions they make frequently, such as
creating new relations.

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