1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.Objects;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.Icon;
|
---|
15 | import javax.swing.JColorChooser;
|
---|
16 | import javax.swing.JMenu;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.StyleSetting.ColorStyleSetting;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A GUI to set a color style
|
---|
26 | * @author Taylor Smock
|
---|
27 | * @since 16842
|
---|
28 | */
|
---|
29 | public class ColorStyleSettingGui implements StyleSettingGui {
|
---|
30 |
|
---|
31 | private final ColorStyleSetting setting;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create a new ColorStyleSettingGui
|
---|
35 | * @param setting The setting to create the GUI for
|
---|
36 | */
|
---|
37 | public ColorStyleSettingGui(ColorStyleSetting setting) {
|
---|
38 | this.setting = Objects.requireNonNull(setting);
|
---|
39 | }
|
---|
40 |
|
---|
41 | static class ColorIcon implements Icon {
|
---|
42 |
|
---|
43 | private final Color color;
|
---|
44 | private final ImageSizes size;
|
---|
45 |
|
---|
46 | ColorIcon(Color color, ImageProvider.ImageSizes size) {
|
---|
47 | this.color = color;
|
---|
48 | this.size = size;
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void paintIcon(Component c, Graphics g, int x, int y) {
|
---|
53 | Color current = g.getColor();
|
---|
54 | g.setColor(color);
|
---|
55 | g.drawRect(x, y, getIconWidth(), getIconHeight());
|
---|
56 | g.fillRect(x, y, getIconWidth(), getIconHeight());
|
---|
57 | g.setColor(current); // So that the text is still black
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public int getIconWidth() {
|
---|
62 | return size.getAdjustedWidth();
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public int getIconHeight() {
|
---|
67 | return size.getAdjustedHeight();
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | class ColorStyleSettingAction extends AbstractAction {
|
---|
73 | ColorStyleSettingAction() {
|
---|
74 | super(setting.label, new ColorIcon(setting.getValue(), ImageSizes.SMALLICON));
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public void actionPerformed(ActionEvent e) {
|
---|
79 | setting.setValue(JColorChooser.showDialog(MainApplication.getMainPanel(), tr("Choose a color"), setting.getValue()));
|
---|
80 | MainApplication.worker.submit(new MapPaintStyleLoader(Collections.singleton(setting.parentStyle)));
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public void addMenuEntry(JMenu menu) {
|
---|
86 | menu.add(new ColorStyleSettingAction());
|
---|
87 | }
|
---|
88 |
|
---|
89 | }
|
---|