source: osm/applications/editors/josm/plugins/smed/src/panels/PanelSectors.java@ 33865

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

checkstyle

File size: 13.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package panels;
3
4import java.awt.BorderLayout;
5import java.awt.Component;
6import java.awt.GridLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.util.EnumMap;
10
11import javax.swing.DefaultCellEditor;
12import javax.swing.ImageIcon;
13import javax.swing.JButton;
14import javax.swing.JComboBox;
15import javax.swing.JFrame;
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18import javax.swing.JScrollPane;
19import javax.swing.JTable;
20import javax.swing.SwingConstants;
21import javax.swing.table.AbstractTableModel;
22import javax.swing.table.DefaultTableCellRenderer;
23import javax.swing.table.TableCellRenderer;
24import javax.swing.table.TableColumn;
25import javax.swing.table.TableModel;
26
27import messages.Messages;
28import seamarks.SeaMark;
29import seamarks.SeaMark.Att;
30import seamarks.SeaMark.Col;
31import seamarks.SeaMark.Exh;
32import seamarks.SeaMark.Lit;
33import seamarks.SeaMark.Vis;
34import smed.SmedAction;
35
36public class PanelSectors extends JFrame {
37
38 private SmedAction dlg;
39 private JPanel panel;
40 private TableModel model;
41 private JTable table;
42
43 public JButton minusButton;
44 private ActionListener alMinusButton = new ActionListener() {
45 @Override
46 public void actionPerformed(ActionEvent e) {
47 if ((getSectorCount() > 1) && (table.getSelectedRow() != 0)) {
48 deleteSector(table.getSelectedRow());
49 }
50 }
51 };
52 public JButton plusButton;
53 private ActionListener alPlusButton = new ActionListener() {
54 @Override
55 public void actionPerformed(ActionEvent e) {
56 if (table.getSelectedRow() < 0) {
57 addSector(table.getRowCount());
58 } else {
59 addSector(table.getSelectedRow()+1);
60 }
61 }
62 };
63 public JComboBox<ImageIcon> colourBox;
64 public EnumMap<Col, ImageIcon> colours = new EnumMap<>(Col.class);
65 public JComboBox<String> visibilityBox;
66 public EnumMap<Vis, String> visibilities = new EnumMap<>(Vis.class);
67 public JComboBox<String> exhibitionBox;
68 public EnumMap<Exh, String> exhibitions = new EnumMap<>(Exh.class);
69
70 public PanelSectors(SmedAction dia) {
71 super(Messages.getString("SectorTable"));
72 dlg = dia;
73 setLayout(null);
74 setSize(900, 100);
75 setAlwaysOnTop(true);
76 setLocation(450, 0);
77 setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
78 minusButton = new JButton(new ImageIcon(getClass().getResource("/images/MinusButton.png")));
79 minusButton.setBounds(0, 0, 32, 34);
80 minusButton.addActionListener(alMinusButton);
81 add(minusButton);
82 plusButton = new JButton(new ImageIcon(getClass().getResource("/images/PlusButton.png")));
83 plusButton.setBounds(0, 34, 32, 34);
84 plusButton.addActionListener(alPlusButton);
85 add(plusButton);
86 panel = new JPanel(new BorderLayout());
87 panel.setBounds(40, 0, 860, 512);
88 model = new SectorTable();
89 table = new JTable(model);
90 table.setBounds(0, 0, 860, 34);
91 table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
92 panel.add(new JScrollPane(table));
93 getContentPane().add(panel);
94
95 table.setSize(860, ((table.getRowCount() * 16) + 28));
96
97 table.setDefaultRenderer(String.class, new CentreRenderer());
98 table.getColumnModel().getColumn(1).setCellRenderer(new ColourCellRenderer());
99
100 TableColumn colColumn = table.getColumnModel().getColumn(1);
101 colourBox = new JComboBox<>();
102 addColItem(new ImageIcon(getClass().getResource("/images/DelButton.png")), Col.UNKCOL);
103 addColItem(new ImageIcon(getClass().getResource("/images/WhiteButton.png")), Col.WHITE);
104 addColItem(new ImageIcon(getClass().getResource("/images/RedButton.png")), Col.RED);
105 addColItem(new ImageIcon(getClass().getResource("/images/GreenButton.png")), Col.GREEN);
106 addColItem(new ImageIcon(getClass().getResource("/images/YellowButton.png")), Col.YELLOW);
107 addColItem(new ImageIcon(getClass().getResource("/images/OrangeButton.png")), Col.ORANGE);
108 addColItem(new ImageIcon(getClass().getResource("/images/AmberButton.png")), Col.AMBER);
109 addColItem(new ImageIcon(getClass().getResource("/images/BlueButton.png")), Col.BLUE);
110 addColItem(new ImageIcon(getClass().getResource("/images/VioletButton.png")), Col.VIOLET);
111 colColumn.setCellEditor(new DefaultCellEditor(colourBox));
112
113 TableColumn visColumn = table.getColumnModel().getColumn(12);
114 visibilityBox = new JComboBox<>();
115 addVisibItem("", Vis.UNKVIS);
116 addVisibItem(Messages.getString("Intensified"), Vis.INTEN);
117 addVisibItem(Messages.getString("Unintensified"), Vis.UNINTEN);
118 addVisibItem(Messages.getString("PartiallyObscured"), Vis.PARTOBS);
119 visColumn.setCellEditor(new DefaultCellEditor(visibilityBox));
120
121 TableColumn exhColumn = table.getColumnModel().getColumn(13);
122 exhibitionBox = new JComboBox<>();
123 addExhibItem("", Exh.UNKEXH);
124 addExhibItem(Messages.getString("24h"), Exh.H24);
125 addExhibItem(Messages.getString("Day"), Exh.DAY);
126 addExhibItem(Messages.getString("Night"), Exh.NIGHT);
127 addExhibItem(Messages.getString("Fog"), Exh.FOG);
128 exhColumn.setCellEditor(new DefaultCellEditor(exhibitionBox));
129 }
130
131 private class SectorTable extends AbstractTableModel {
132
133 private String[] headings = {Messages.getString("Sector"), Messages.getString("Colour"), Messages.getString("Character"),
134 Messages.getString("Group"), Messages.getString("Sequence"), Messages.getString("Period"), Messages.getString("Directional"),
135 Messages.getString("Start"), Messages.getString("End"), Messages.getString("Radius"), Messages.getString("Height"),
136 Messages.getString("Range"), Messages.getString("Visibility"), Messages.getString("Exhibition") };
137
138 SectorTable() {
139 }
140
141 @Override
142 public String getColumnName(int col) {
143 return headings[col];
144 }
145
146 @Override
147 public int getColumnCount() {
148 return headings.length;
149 }
150
151 @Override
152 public int getRowCount() {
153 if (SmedAction.panelMain == null)
154 return 1;
155 else
156 return SmedAction.panelMain.mark.getSectorCount();
157 }
158
159 @Override
160 public boolean isCellEditable(int row, int col) {
161 return ((col > 0) && (row > 0));
162 }
163
164 @Override
165 public Class getColumnClass(int col) {
166 switch (col) {
167 case 1:
168 return Col.class;
169 case 6:
170 return Boolean.class;
171 default:
172 return String.class;
173 }
174 }
175
176 @Override
177 public Object getValueAt(int row, int col) {
178 switch (col) {
179 case 0:
180 if (row == 0)
181 return Messages.getString("Default");
182 else
183 return row;
184 case 1:
185 if (((String) SmedAction.panelMain.mark.getLightAtt(Att.CHR, row)).contains("Al")) {
186 if (SmedAction.panelMain.mark.getLightAtt(Att.COL, row) == Col.UNKCOL)
187 return Col.UNKCOL;
188 else
189 return SmedAction.panelMain.mark.getLightAtt(Att.ALT, row);
190 } else
191 return SmedAction.panelMain.mark.getLightAtt(Att.COL, row);
192 case 6:
193 return (SmedAction.panelMain.mark.getLightAtt(Att.LIT, row) == Lit.DIR);
194 case 7:
195 case 8:
196 if (SmedAction.panelMain.mark.getLightAtt(Att.LIT, row) == Lit.DIR)
197 return SmedAction.panelMain.mark.getLightAtt(Att.ORT, row);
198 else
199 return SmedAction.panelMain.mark.getLightAtt(col - 1, row);
200 case 12:
201 return visibilities.get(SmedAction.panelMain.mark.getLightAtt(Att.VIS, row));
202 case 13:
203 return exhibitions.get(SmedAction.panelMain.mark.getLightAtt(Att.EXH, row));
204 default:
205 return SmedAction.panelMain.mark.getLightAtt(col - 1, row);
206 }
207 }
208
209 @Override
210 public void setValueAt(Object value, int row, int col) {
211 switch (col) {
212 case 1:
213 for (Col colour : colours.keySet()) {
214 ImageIcon img = colours.get(colour);
215 if (img == value)
216 if (((String) SmedAction.panelMain.mark.getLightAtt(Att.CHR, row)).contains("Al")) {
217 if (((colour == Col.UNKCOL) && (SmedAction.panelMain.mark.getLightAtt(Att.ALT, row) == Col.UNKCOL))
218 || (SmedAction.panelMain.mark.getLightAtt(Att.COL, row) == Col.UNKCOL)) {
219 SmedAction.panelMain.mark.setLightAtt(Att.COL, row, colour);
220 } else {
221 SmedAction.panelMain.mark.setLightAtt(Att.ALT, row, colour);
222 }
223 } else {
224 SmedAction.panelMain.mark.setLightAtt(Att.COL, row, colour);
225 }
226 }
227 break;
228 case 5:
229 case 9:
230 case 10:
231 case 11:
232 SmedAction.panelMain.mark.setLightAtt(col - 1, row, value);
233 break;
234 case 6:
235 if ((Boolean) value == true) {
236 SmedAction.panelMain.mark.setLightAtt(Att.LIT, row, Lit.DIR);
237 SmedAction.panelMain.mark.setLightAtt(Att.BEG, row, "");
238 SmedAction.panelMain.mark.setLightAtt(Att.END, row, "");
239 } else {
240 SmedAction.panelMain.mark.setLightAtt(Att.LIT, row, Lit.UNKLIT);
241 SmedAction.panelMain.mark.setLightAtt(Att.ORT, row, "");
242 }
243 break;
244 case 7:
245 case 8:
246 if (SmedAction.panelMain.mark.getLightAtt(Att.LIT, row) == Lit.DIR) {
247 SmedAction.panelMain.mark.setLightAtt(Att.ORT, row, value);
248 } else {
249 SmedAction.panelMain.mark.setLightAtt(col - 1, row, value);
250 }
251 break;
252 case 12:
253 for (Vis vis : visibilities.keySet()) {
254 String str = visibilities.get(vis);
255 if (str.equals(value)) {
256 SmedAction.panelMain.mark.setLightAtt(Att.VIS, row, vis);
257 }
258 }
259 break;
260 case 13:
261 for (Exh exh : exhibitions.keySet()) {
262 String str = exhibitions.get(exh);
263 if (str.equals(value)) {
264 SmedAction.panelMain.mark.setLightAtt(Att.EXH, row, exh);
265 }
266 }
267 break;
268 default:
269 SmedAction.panelMain.mark.setLightAtt(col - 1, row, value);
270 }
271 }
272 }
273
274 static class CentreRenderer extends DefaultTableCellRenderer {
275 CentreRenderer() {
276 super();
277 setHorizontalAlignment(SwingConstants.CENTER);
278 }
279 }
280
281 public static class ColourCellRenderer extends JPanel implements TableCellRenderer {
282 private JLabel col1Label;
283 private JLabel col2Label;
284 public ColourCellRenderer() {
285 super();
286 setLayout(new GridLayout(1, 2, 0, 0));
287 col1Label = new JLabel();
288 col1Label.setOpaque(true);
289 add(col1Label);
290 col2Label = new JLabel();
291 col2Label.setOpaque(true);
292 add(col2Label);
293 }
294
295 @Override
296 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
297 int rowIndex, int vColIndex) {
298 if (!((String) SmedAction.panelMain.mark.getLightAtt(Att.CHR, rowIndex)).contains("Al")) {
299 col2Label.setBackground(SeaMark.ColMAP.get(SmedAction.panelMain.mark.getLightAtt(Att.COL, rowIndex)));
300 } else {
301 col2Label.setBackground(SeaMark.ColMAP.get(SmedAction.panelMain.mark.getLightAtt(Att.ALT, rowIndex)));
302 }
303 col1Label.setBackground(SeaMark.ColMAP.get(SmedAction.panelMain.mark.getLightAtt(Att.COL, rowIndex)));
304 return this;
305 }
306 }
307
308 public int getSectorCount() {
309 return model.getRowCount();
310 }
311
312 public void addSector(int idx) {
313 SmedAction.panelMain.mark.addLight(idx);
314 table.setSize(860, ((table.getRowCount() * 16) + 28));
315 if (table.getRowCount() > 3) {
316 setSize(900, ((table.getRowCount() * 16) + 48));
317 } else {
318 setSize(900, 100);
319 }
320 }
321
322 public void deleteSector(int idx) {
323 if (idx > 0) {
324 SmedAction.panelMain.mark.delLight(idx);
325 table.setSize(860, ((table.getRowCount() * 16) + 28));
326 if (table.getRowCount() > 3) {
327 setSize(900, ((table.getRowCount() * 16) + 48));
328 } else {
329 setSize(900, 100);
330 }
331 }
332 }
333
334 public void syncPanel() {
335 table.updateUI();
336 table.setSize(860, ((table.getRowCount() * 16) + 28));
337 if (table.getRowCount() > 3) {
338 setSize(900, ((table.getRowCount() * 16) + 48));
339 } else {
340 setSize(900, 100);
341 }
342 }
343
344 private void addColItem(ImageIcon img, Col col) {
345 colours.put(col, img);
346 colourBox.addItem(img);
347 }
348
349 private void addVisibItem(String str, Vis vis) {
350 visibilities.put(vis, str);
351 visibilityBox.addItem(str);
352 }
353
354 private void addExhibItem(String str, Exh exh) {
355 exhibitions.put(exh, str);
356 exhibitionBox.addItem(str);
357 }
358
359}
Note: See TracBrowser for help on using the repository browser.