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

Last change on this file since 12649 was 12649, checked in by Don-vip, 7 years ago

see #15182 - code refactoring to avoid dependence on GUI packages from Preferences

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