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

Last change on this file since 30532 was 29894, checked in by malcolmh, 11 years ago

clean

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