source: josm/trunk/src/org/openstreetmap/josm/gui/layer/CustomizeColor.java@ 15496

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

fix #16796 - Rework of GPX track colors / layer preferences (patch by Bjoeni)

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.event.ActionEvent;
10import java.util.Collections;
11import java.util.List;
12import java.util.Objects;
13import java.util.stream.Collectors;
14
15import javax.swing.AbstractAction;
16import javax.swing.Action;
17import javax.swing.JColorChooser;
18import javax.swing.JMenuItem;
19import javax.swing.JOptionPane;
20
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
23import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
24import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
25import org.openstreetmap.josm.tools.ImageProvider;
26
27/**
28 * Action to show a dialog for picking a color.
29 *
30 * By calling this action, the user can choose a color to customize the painting
31 * of a certain {@link GpxLayer} or {@link org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer}.
32 */
33public class CustomizeColor extends AbstractAction implements LayerAction, MultiLayerAction {
34 private final transient List<Layer> colorLayers;
35
36 /**
37 * Constructs a new {@code CustomizeColor} for a given list of layers.
38 * @param l list of layers
39 */
40 public CustomizeColor(List<Layer> l) {
41 super(tr("Customize Color"));
42 new ImageProvider("colorchooser").getResource().attachImageIcon(this, true);
43 colorLayers = l.stream().filter(Objects::nonNull).filter(Layer::hasColor).collect(Collectors.toList());
44 putValue("help", ht("/Action/LayerCustomizeColor"));
45 }
46
47 /**
48 * Constructs a new {@code CustomizeColor} for a single layer.
49 * @param l layer
50 */
51 public CustomizeColor(Layer l) {
52 this(Collections.singletonList(l));
53 }
54
55 @Override
56 public boolean supportLayers(List<Layer> layers) {
57 return layers.stream().allMatch(Layer::hasColor);
58 }
59
60 @Override
61 public Component createMenuComponent() {
62 return new JMenuItem(this);
63 }
64
65 @Override
66 public Action getMultiLayerAction(List<Layer> layers) {
67 return new CustomizeColor(layers);
68 }
69
70 @Override
71 public void actionPerformed(ActionEvent e) {
72 Color cl = colorLayers.stream().filter(Objects::nonNull).map(Layer::getColor).filter(Objects::nonNull).findAny().orElse(Color.GRAY);
73 JColorChooser c = new JColorChooser(cl);
74 Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
75 int answer = JOptionPane.showOptionDialog(
76 MainApplication.getMainFrame(),
77 c,
78 tr("Choose a color"),
79 JOptionPane.OK_CANCEL_OPTION,
80 JOptionPane.PLAIN_MESSAGE,
81 null,
82 options,
83 options[0]
84 );
85 switch (answer) {
86 case 0:
87 colorLayers.stream().forEach(l -> l.setColor(c.getColor()));
88 break;
89 case 1:
90 return;
91 case 2:
92 colorLayers.stream().forEach(l -> l.setColor(null));
93 break;
94 }
95 // TODO: Make the layer dialog listen to property change events so that this is not needed any more.
96 LayerListDialog.getInstance().repaint();
97 }
98}
Note: See TracBrowser for help on using the repository browser.