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

Last change on this file since 14206 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

  • Property svn:eol-style set to native
File size: 3.5 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.data.preferences.AbstractProperty;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
24import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
25import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
26import org.openstreetmap.josm.tools.CheckParameterUtil;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29/**
30 * Action to show a dialog for picking a color.
31 *
32 * By calling this action, the user can choose a color to customize the painting
33 * of a certain {@link GpxLayer} or {@link org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer}.
34 */
35public class CustomizeColor extends AbstractAction implements LayerAction, MultiLayerAction {
36 private final transient List<AbstractProperty<Color>> colors;
37
38 /**
39 * Constructs a new {@code CustomizeColor} for a given list of layers.
40 * @param l list of layers
41 */
42 public CustomizeColor(List<Layer> l) {
43 super(tr("Customize Color"));
44 new ImageProvider("colorchooser").getResource().attachImageIcon(this, true);
45 colors = l.stream().map(Layer::getColorProperty).collect(Collectors.toList());
46 CheckParameterUtil.ensureThat(colors.stream().allMatch(Objects::nonNull), "All layers must have colors.");
47 putValue("help", ht("/Action/LayerCustomizeColor"));
48 }
49
50 /**
51 * Constructs a new {@code CustomizeColor} for a single layer.
52 * @param l layer
53 */
54 public CustomizeColor(Layer l) {
55 this(Collections.singletonList(l));
56 }
57
58 @Override
59 public boolean supportLayers(List<Layer> layers) {
60 return layers.stream().allMatch(l -> l.getColorProperty() != null);
61 }
62
63 @Override
64 public Component createMenuComponent() {
65 return new JMenuItem(this);
66 }
67
68 @Override
69 public Action getMultiLayerAction(List<Layer> layers) {
70 return new CustomizeColor(layers);
71 }
72
73 @Override
74 public void actionPerformed(ActionEvent e) {
75 Color cl = colors.stream().map(AbstractProperty::get).filter(Objects::nonNull).findAny().orElse(Color.GRAY);
76 JColorChooser c = new JColorChooser(cl);
77 Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
78 int answer = JOptionPane.showOptionDialog(
79 MainApplication.getMainFrame(),
80 c,
81 tr("Choose a color"),
82 JOptionPane.OK_CANCEL_OPTION,
83 JOptionPane.PLAIN_MESSAGE,
84 null,
85 options,
86 options[0]
87 );
88 switch (answer) {
89 case 0:
90 colors.stream().forEach(prop -> prop.put(c.getColor()));
91 break;
92 case 1:
93 return;
94 case 2:
95 colors.stream().forEach(prop -> prop.put(null));
96 break;
97 }
98 // TODO: Make the layer dialog listen to property change events so that this is not needed any more.
99 LayerListDialog.getInstance().repaint();
100 }
101}
Note: See TracBrowser for help on using the repository browser.