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

Last change on this file since 11167 was 11097, checked in by Don-vip, 8 years ago

sonar - squid:S1612 - Lambdas should be replaced with method references

  • Property svn:eol-style set to native
File size: 3.2 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.Main;
22import org.openstreetmap.josm.data.preferences.AbstractProperty;
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
29public 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}
Note: See TracBrowser for help on using the repository browser.