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.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.Action;
|
---|
15 | import javax.swing.JColorChooser;
|
---|
16 | import javax.swing.JMenuItem;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
---|
21 | import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 |
|
---|
24 | public class CustomizeColor extends AbstractAction implements LayerAction, MultiLayerAction {
|
---|
25 | private final transient List<Layer> layers;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code CustomizeColor} for a given list of layers.
|
---|
29 | * @param l list of layers
|
---|
30 | */
|
---|
31 | public CustomizeColor(List<Layer> l) {
|
---|
32 | super(tr("Customize Color"), ImageProvider.get("colorchooser"));
|
---|
33 | putValue("help", ht("/Action/LayerCustomizeColor"));
|
---|
34 | layers = l;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Constructs a new {@code CustomizeColor} for a single layer.
|
---|
39 | * @param l layer
|
---|
40 | */
|
---|
41 | public CustomizeColor(Layer l) {
|
---|
42 | this(new LinkedList<Layer>());
|
---|
43 | layers.add(l);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public boolean supportLayers(List<Layer> layers) {
|
---|
48 | for (Layer layer: layers) {
|
---|
49 | if (layer.getColor(false) == null)
|
---|
50 | return false;
|
---|
51 | }
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public Component createMenuComponent() {
|
---|
57 | return new JMenuItem(this);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public Action getMultiLayerAction(List<Layer> layers) {
|
---|
62 | return new CustomizeColor(layers);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void actionPerformed(ActionEvent e) {
|
---|
67 | Color cl = layers.get(0).getColor(false);
|
---|
68 | if (cl == null)
|
---|
69 | cl = Color.gray;
|
---|
70 | JColorChooser c = new JColorChooser(cl);
|
---|
71 | Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
|
---|
72 | int answer = JOptionPane.showOptionDialog(
|
---|
73 | Main.parent,
|
---|
74 | c,
|
---|
75 | tr("Choose a color"),
|
---|
76 | JOptionPane.OK_CANCEL_OPTION,
|
---|
77 | JOptionPane.PLAIN_MESSAGE,
|
---|
78 | null,
|
---|
79 | options,
|
---|
80 | options[0]
|
---|
81 | );
|
---|
82 | switch (answer) {
|
---|
83 | case 0:
|
---|
84 | for (Layer layer : layers) {
|
---|
85 | Main.pref.putColor("layer "+layer.getName(), c.getColor());
|
---|
86 | }
|
---|
87 | break;
|
---|
88 | case 1:
|
---|
89 | return;
|
---|
90 | case 2:
|
---|
91 | for (Layer layer : layers) {
|
---|
92 | Main.pref.putColor("layer "+layer.getName(), null);
|
---|
93 | }
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | Main.map.repaint();
|
---|
97 | }
|
---|
98 | }
|
---|