1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.util.EventObject;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.ActionMap;
|
---|
15 | import javax.swing.JCheckBox;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JTable;
|
---|
18 | import javax.swing.event.CellEditorListener;
|
---|
19 | import javax.swing.table.TableCellEditor;
|
---|
20 | import javax.swing.table.TableCellRenderer;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.gui.util.CellEditorSupport;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This class creates a table cell that features two checkboxes, Upload and Save. It
|
---|
27 | * handles everything on its own, in other words it renders itself and also functions
|
---|
28 | * as editor so the checkboxes may be set by the user.
|
---|
29 | *
|
---|
30 | * Intended usage is like this:
|
---|
31 | * <code>
|
---|
32 | * <br>ActionFlagsTableCell aftc = new ActionFlagsTableCell();
|
---|
33 | * <br>col = new TableColumn(0);
|
---|
34 | * <br>col.setCellRenderer(aftc);
|
---|
35 | * <br>col.setCellEditor(aftc);
|
---|
36 | * </code>
|
---|
37 | */
|
---|
38 | class ActionFlagsTableCell extends JPanel implements TableCellRenderer, TableCellEditor {
|
---|
39 | private final JCheckBox[] checkBoxes = new JCheckBox[2];
|
---|
40 | private final transient CellEditorSupport cellEditorSupport = new CellEditorSupport(this);
|
---|
41 |
|
---|
42 | private final transient ActionListener al = new ActionListener() {
|
---|
43 | @Override
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
45 | cellEditorSupport.fireEditingStopped();
|
---|
46 | }
|
---|
47 | };
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Constructs a new {@code ActionFlagsTableCell}.
|
---|
51 | */
|
---|
52 | ActionFlagsTableCell() {
|
---|
53 | checkBoxes[0] = new JCheckBox(tr("Upload"));
|
---|
54 | checkBoxes[1] = new JCheckBox(tr("Save"));
|
---|
55 | setLayout(new GridBagLayout());
|
---|
56 |
|
---|
57 | ActionMap am = getActionMap();
|
---|
58 | for (final JCheckBox b : checkBoxes) {
|
---|
59 | b.setPreferredSize(new Dimension(b.getPreferredSize().width, 19));
|
---|
60 | b.addActionListener(al);
|
---|
61 | am.put(b.getText(), new AbstractAction() {
|
---|
62 | @Override
|
---|
63 | public void actionPerformed(ActionEvent e) {
|
---|
64 | b.setSelected(!b.isSelected());
|
---|
65 | cellEditorSupport.fireEditingStopped();
|
---|
66 | }
|
---|
67 | });
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected void updateCheckboxes(Object v) {
|
---|
72 | if (v != null && checkBoxes[0] != null && checkBoxes[1] != null) {
|
---|
73 | boolean[] values;
|
---|
74 | if (v instanceof SaveLayerInfo) {
|
---|
75 | values = new boolean[2];
|
---|
76 | values[0] = ((SaveLayerInfo) v).isDoUploadToServer();
|
---|
77 | values[1] = ((SaveLayerInfo) v).isDoSaveToFile();
|
---|
78 | } else {
|
---|
79 | values = (boolean[]) v;
|
---|
80 | }
|
---|
81 | checkBoxes[0].setSelected(values[0]);
|
---|
82 | checkBoxes[1].setSelected(values[1]);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void updatePanel(SaveLayerInfo info) {
|
---|
87 | StringBuilder sb = new StringBuilder(128)
|
---|
88 | .append("<html>")
|
---|
89 | .append(tr("Select which actions to perform for this layer, if you click the leftmost button."));
|
---|
90 | removeAll();
|
---|
91 | if (info != null) {
|
---|
92 | if (info.isUploadable()) {
|
---|
93 | sb.append("<br/>")
|
---|
94 | .append(tr("Check \"Upload\" to upload the changes to the OSM server."));
|
---|
95 | add(checkBoxes[0], GBC.eol().fill(GBC.HORIZONTAL));
|
---|
96 | }
|
---|
97 | if (info.isSavable()) {
|
---|
98 | sb.append("<br/>")
|
---|
99 | .append(tr("Check \"Save\" to save the layer to the file specified on the left."));
|
---|
100 | add(checkBoxes[1], GBC.eol().fill(GBC.HORIZONTAL));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | sb.append("</html>");
|
---|
104 | setToolTipText(sb.toString());
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
109 | updatePanel((SaveLayerInfo) value);
|
---|
110 | updateCheckboxes(value);
|
---|
111 | return this;
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public void addCellEditorListener(CellEditorListener l) {
|
---|
116 | cellEditorSupport.addCellEditorListener(l);
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public void cancelCellEditing() {
|
---|
121 | cellEditorSupport.fireEditingCanceled();
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public Object getCellEditorValue() {
|
---|
126 | boolean[] values = new boolean[2];
|
---|
127 | values[0] = checkBoxes[0].isSelected();
|
---|
128 | values[1] = checkBoxes[1].isSelected();
|
---|
129 | return values;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public boolean isCellEditable(EventObject anEvent) {
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public void removeCellEditorListener(CellEditorListener l) {
|
---|
139 | cellEditorSupport.removeCellEditorListener(l);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public boolean shouldSelectCell(EventObject anEvent) {
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public boolean stopCellEditing() {
|
---|
149 | cellEditorSupport.fireEditingStopped();
|
---|
150 | return true;
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
155 | updatePanel((SaveLayerInfo) value);
|
---|
156 | updateCheckboxes(value);
|
---|
157 | return this;
|
---|
158 | }
|
---|
159 | }
|
---|