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

Last change on this file since 32767 was 32767, checked in by donvip, 8 years ago

code style

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