1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Objects;
|
---|
13 | import java.util.stream.Collectors;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.Action;
|
---|
17 | import javax.swing.JColorChooser;
|
---|
18 | import javax.swing.JMenuItem;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
23 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
---|
24 | import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
|
---|
25 | import 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 | */
|
---|
33 | public 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 | }
|
---|