source: osm/applications/editors/josm/plugins/smed/src/panels/PanelCol.java@ 34417

Last change on this file since 34417 was 33054, checked in by donvip, 9 years ago

checkstyle

File size: 11.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package panels;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Rectangle;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.util.ArrayList;
11import java.util.EnumMap;
12
13import javax.swing.BorderFactory;
14import javax.swing.ButtonGroup;
15import javax.swing.ImageIcon;
16import javax.swing.JPanel;
17import javax.swing.JRadioButton;
18
19import messages.Messages;
20import seamarks.SeaMark;
21import seamarks.SeaMark.Att;
22import seamarks.SeaMark.Col;
23import seamarks.SeaMark.Ent;
24import smed.SmedAction;
25
26public class PanelCol extends JPanel {
27
28 private SmedAction dlg;
29 private Ent ent;
30 private ButtonGroup colourButtons = new ButtonGroup();
31 public JRadioButton delButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/DelButton.png")));
32 public JRadioButton addButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/AddButton.png")));
33 public JRadioButton whiteButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/WhiteButton.png")));
34 public JRadioButton redButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/RedButton.png")));
35 public JRadioButton greenButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/GreenButton.png")));
36 public JRadioButton yellowButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/YellowButton.png")));
37 public JRadioButton orangeButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/OrangeButton.png")));
38 public JRadioButton amberButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/AmberButton.png")));
39 public JRadioButton blueButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/BlueButton.png")));
40 public JRadioButton violetButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/VioletButton.png")));
41 public JRadioButton blackButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/BlackButton.png")));
42 public JRadioButton greyButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/GreyButton.png")));
43 public JRadioButton brownButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/BrownButton.png")));
44 public JRadioButton magentaButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/MagentaButton.png")));
45 public JRadioButton pinkButton = new JRadioButton(new ImageIcon(getClass().getResource("/images/PinkButton.png")));
46 public EnumMap<Col, JRadioButton> colours = new EnumMap<>(Col.class);
47 private ActionListener alColour = new ActionListener() {
48 @Override
49 public void actionPerformed(ActionEvent e) {
50 for (Col col : colours.keySet()) {
51 JRadioButton button = colours.get(col);
52 if (button.isSelected()) {
53 if (ent == Ent.LIGHT) {
54 if (((String) SmedAction.panelMain.mark.getLightAtt(Att.CHR, 0)).contains("Al")) {
55 if (((button == delButton) && (SmedAction.panelMain.mark.getLightAtt(Att.ALT, 0) == Col.UNKCOL))
56 || (SmedAction.panelMain.mark.getLightAtt(Att.COL, 0) == Col.UNKCOL)) {
57 SmedAction.panelMain.mark.setLightAtt(Att.COL, 0, col);
58 SmedAction.panelMain.panelLit.panelChr.col1Label.setBackground(SeaMark.ColMAP.get(col));
59 } else {
60 SmedAction.panelMain.mark.setLightAtt(Att.ALT, 0, col);
61 SmedAction.panelMain.panelLit.panelChr.col2Label.setBackground(SeaMark.ColMAP.get(col));
62 }
63 } else {
64 SmedAction.panelMain.mark.setLightAtt(Att.COL, 0, col);
65 SmedAction.panelMain.panelLit.panelChr.col1Label.setBackground(SeaMark.ColMAP.get(col));
66 SmedAction.panelMain.panelLit.panelChr.col2Label.setBackground(SeaMark.ColMAP.get(col));
67 }
68 button.setBorderPainted(true);
69 } else {
70 if (button == delButton) {
71 SmedAction.panelMain.mark.subColour(ent, stackIdx);
72 } else if (button == addButton) {
73 if (stackCol.size() != 0) {
74 stackIdx++;
75 }
76 if (stackCol.size() == 0) {
77 SmedAction.panelMain.mark.setColour(ent, col);
78 } else {
79 switch (SmedAction.panelMain.mark.getPattern(ent)) {
80 case NOPAT:
81 break;
82 case BORDER:
83 case CROSS:
84 if (stackCol.size() < 2) {
85 SmedAction.panelMain.mark.addColour(ent, stackIdx, col);
86 }
87 break;
88 case SQUARED:
89 if (stackCol.size() < 4) {
90 SmedAction.panelMain.mark.addColour(ent, stackIdx, col);
91 }
92 break;
93 default:
94 SmedAction.panelMain.mark.addColour(ent, stackIdx, col);
95 break;
96 }
97 }
98 } else {
99 SmedAction.panelMain.mark.setColour(ent, stackIdx, col);
100 }
101 syncPanel();
102 }
103 } else {
104 button.setBorderPainted(false);
105 }
106 }
107 }
108 };
109 private JPanel stack;
110 private ButtonGroup stackColours = new ButtonGroup();
111 private ArrayList<JRadioButton> stackCol = new ArrayList<>();
112 private int stackIdx = 0;
113 private ActionListener alStack = new ActionListener() {
114 @Override
115 public void actionPerformed(ActionEvent e) {
116 for (int i = 0; stackCol.size() > i; i++) {
117 JRadioButton button = stackCol.get(i);
118 if (button.isSelected()) {
119 stackIdx = i;
120 button.setBorderPainted(true);
121 } else {
122 button.setBorderPainted(false);
123 }
124 }
125 }
126 };
127
128 public PanelCol(SmedAction dia, Ent entity) {
129 dlg = dia;
130 ent = entity;
131 setLayout(null);
132 add(getColButton(delButton, 0, 0, 34, 16, Messages.getString("RemColour"), Col.UNKCOL));
133 add(getColButton(whiteButton, 0, 16, 34, 16, Messages.getString("White"), Col.WHITE));
134 add(getColButton(redButton, 0, 32, 34, 16, Messages.getString("Red"), Col.RED));
135 add(getColButton(orangeButton, 0, 48, 34, 16, Messages.getString("Orange"), Col.ORANGE));
136 add(getColButton(amberButton, 0, 64, 34, 16, Messages.getString("Amber"), Col.AMBER));
137 add(getColButton(yellowButton, 0, 80, 34, 16, Messages.getString("Yellow"), Col.YELLOW));
138 add(getColButton(greenButton, 0, 96, 34, 16, Messages.getString("Green"), Col.GREEN));
139 add(getColButton(blueButton, 0, 112, 34, 16, Messages.getString("Blue"), Col.BLUE));
140 add(getColButton(violetButton, 0, 128, 34, 16, Messages.getString("Violet"), Col.VIOLET));
141 if (ent != Ent.LIGHT) {
142 add(getColButton(addButton, 0, 144, 34, 16, Messages.getString("AddColour"), Col.BLANK));
143 add(getColButton(blackButton, 37, 0, 34, 16, Messages.getString("Black"), Col.BLACK));
144 add(getColButton(greyButton, 37, 16, 34, 16, Messages.getString("Grey"), Col.GREY));
145 add(getColButton(brownButton, 37, 32, 34, 16, Messages.getString("Brown"), Col.BROWN));
146 add(getColButton(magentaButton, 37, 48, 34, 16, Messages.getString("Magenta"), Col.MAGENTA));
147 add(getColButton(pinkButton, 37, 64, 34, 16, Messages.getString("Pink"), Col.PINK));
148
149 stack = new JPanel();
150 stack.setBorder(BorderFactory.createLineBorder(Color.black, 2));
151 stack.setBounds(38, 87, 34, 64);
152 stack.setLayout(null);
153 add(stack);
154 }
155 }
156
157 public void trimStack(int max) {
158 while (stackCol.size() > max) {
159 stackCol.get(stackCol.size() - 1).setSelected(true);
160 delButton.doClick();
161 }
162 }
163
164 public void syncPanel() {
165 if (ent == Ent.LIGHT) {
166 for (Col col : colours.keySet()) {
167 JRadioButton button = colours.get(col);
168 if (SmedAction.panelMain.mark.getLightAtt(Att.COL, 0) == col) {
169 button.setBorderPainted(true);
170 } else {
171 button.setBorderPainted(false);
172 }
173 }
174 } else {
175 int idx;
176 for (idx = 0; SmedAction.panelMain.mark.getColour(ent, idx) != Col.UNKCOL; idx++) {
177 if (stackCol.size() <= idx) {
178 stackCol.add(idx, new JRadioButton(new ImageIcon(getClass().getResource("/images/ColourButton.png"))));
179 JRadioButton btnI = stackCol.get(idx);
180 btnI.setBorder(BorderFactory.createLoweredBevelBorder());
181 stack.add(btnI);
182 stackColours.add(btnI);
183 btnI.addActionListener(alStack);
184 }
185 }
186 while (idx < stackCol.size()) {
187 JRadioButton btnI = stackCol.get(idx);
188 btnI.removeActionListener(alStack);
189 stackColours.remove(btnI);
190 stack.remove(btnI);
191 stackCol.remove(idx);
192 }
193 if (stackIdx >= stackCol.size()) {
194 stackIdx = stackCol.size() - 1;
195 }
196 if (stackIdx < 0) {
197 stackIdx = 0;
198 }
199 if (stackCol.size() == 0) {
200 stack.repaint();
201 } else {
202 int height = 60 / stackCol.size();
203 for (idx = 0; stackCol.size() > idx; idx++) {
204 JRadioButton btnI = stackCol.get(idx);
205 btnI.setBounds(2, (2 + (idx * height)), 30, height);
206 btnI.setBackground(SeaMark.ColMAP.get(SmedAction.panelMain.mark.getColour(ent, idx)));
207 if (stackIdx == idx) {
208 btnI.setBorderPainted(true);
209 } else {
210 btnI.setBorderPainted(false);
211 }
212 }
213 }
214 }
215 }
216
217 private JRadioButton getColButton(JRadioButton button, int x, int y, int w, int h, String tip, Col col) {
218 button.setBounds(new Rectangle(x, y, w, h));
219 button.setBorder(BorderFactory.createLoweredBevelBorder());
220 button.setToolTipText(tr(tip));
221 button.addActionListener(alColour);
222 colourButtons.add(button);
223 colours.put(col, button);
224 return button;
225 }
226
227}
Note: See TracBrowser for help on using the repository browser.