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