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

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

code style

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