1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.beans.PropertyChangeEvent;
|
---|
12 | import java.beans.PropertyChangeListener;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.BoxLayout;
|
---|
16 | import javax.swing.JButton;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.event.ListSelectionEvent;
|
---|
20 | import javax.swing.event.ListSelectionListener;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
25 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * TagEditorPanel is a {@see JPanel} which can be embedded as UI component in
|
---|
29 | * UIs. It provides a spreadsheet like tabular control for editing tag names
|
---|
30 | * and tag values. Two action buttons are placed on the left, one for additing
|
---|
31 | * a new tag and one for deleting the currently selected tags.
|
---|
32 | *
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class TagEditorPanel extends JPanel {
|
---|
36 | /** the tag editor model */
|
---|
37 | private TagEditorModel model;
|
---|
38 | /** the tag table */
|
---|
39 | private TagTable tagTable;
|
---|
40 |
|
---|
41 | private AutoCompletionCache acCache;
|
---|
42 | private AutoCompletionList acList;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * builds the panel with the table for editing tags
|
---|
46 | *
|
---|
47 | * @return the panel
|
---|
48 | */
|
---|
49 | protected JPanel buildTagTableEditorPanel() {
|
---|
50 |
|
---|
51 | JPanel pnl = new JPanel();
|
---|
52 | model = new TagEditorModel();
|
---|
53 | tagTable = new TagTable(model);
|
---|
54 |
|
---|
55 | pnl.setLayout(new BorderLayout());
|
---|
56 | pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
|
---|
57 | return pnl;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * builds the panel with the button row
|
---|
62 | *
|
---|
63 | * @return the panel
|
---|
64 | */
|
---|
65 | protected JPanel buildButtonsPanel() {
|
---|
66 | JPanel pnl = new JPanel();
|
---|
67 | pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
|
---|
68 |
|
---|
69 | // add action
|
---|
70 | //
|
---|
71 | AddAction addAction = new AddAction();
|
---|
72 | JButton btn;
|
---|
73 | pnl.add(btn = new JButton(addAction));
|
---|
74 | btn.setMargin(new Insets(0,0,0,0));
|
---|
75 | tagTable.addPropertyChangeListener(addAction);
|
---|
76 |
|
---|
77 | // delete action
|
---|
78 | //
|
---|
79 | DeleteAction deleteAction = new DeleteAction();
|
---|
80 | tagTable.getSelectionModel().addListSelectionListener(deleteAction);
|
---|
81 | tagTable.addPropertyChangeListener(deleteAction);
|
---|
82 | pnl.add(btn = new JButton(deleteAction));
|
---|
83 | btn.setMargin(new Insets(0,0,0,0));
|
---|
84 | return pnl;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * builds the GUI
|
---|
89 | */
|
---|
90 | protected void build() {
|
---|
91 | setLayout(new GridBagLayout());
|
---|
92 | JPanel tablePanel = buildTagTableEditorPanel();
|
---|
93 | JPanel buttonPanel = buildButtonsPanel();
|
---|
94 |
|
---|
95 | GridBagConstraints gc = new GridBagConstraints();
|
---|
96 |
|
---|
97 | // -- buttons panel
|
---|
98 | //
|
---|
99 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
100 | gc.weightx = 0.0;
|
---|
101 | gc.weighty = 1.0;
|
---|
102 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
103 | add(buttonPanel,gc);
|
---|
104 |
|
---|
105 | // -- the panel with the editor table
|
---|
106 | //
|
---|
107 | gc.gridx = 1;
|
---|
108 | gc.fill = GridBagConstraints.BOTH;
|
---|
109 | gc.weightx = 1.0;
|
---|
110 | gc.weighty = 1.0;
|
---|
111 | gc.anchor = GridBagConstraints.CENTER;
|
---|
112 | add(tablePanel,gc);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * constructor
|
---|
117 | */
|
---|
118 | public TagEditorPanel() {
|
---|
119 | build();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Replies the tag editor model used by this panel.
|
---|
124 | *
|
---|
125 | * @return the tag editor model used by this panel
|
---|
126 | */
|
---|
127 | public TagEditorModel getModel() {
|
---|
128 | return model;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * The action for adding a tag
|
---|
133 | *
|
---|
134 | */
|
---|
135 | class AddAction extends AbstractAction implements PropertyChangeListener {
|
---|
136 | public AddAction() {
|
---|
137 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "add"));
|
---|
138 | putValue(SHORT_DESCRIPTION, tr("Add a new tag"));
|
---|
139 | updateEnabledState();
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void actionPerformed(ActionEvent e) {
|
---|
143 | model.appendNewTag();
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected void updateEnabledState() {
|
---|
147 | setEnabled(tagTable.isEnabled());
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
151 | updateEnabledState();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * The action for deleting the currently selected tags
|
---|
157 | *
|
---|
158 | *
|
---|
159 | */
|
---|
160 | class DeleteAction extends AbstractAction implements ListSelectionListener, PropertyChangeListener {
|
---|
161 | public DeleteAction() {
|
---|
162 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
163 | putValue(SHORT_DESCRIPTION, tr("Delete the selection in the tag table"));
|
---|
164 | updateEnabledState();
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void actionPerformed(ActionEvent e) {
|
---|
168 | run();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * delete a selection of tag names
|
---|
173 | */
|
---|
174 | protected void deleteTagNames() {
|
---|
175 | int[] rows = tagTable.getSelectedRows();
|
---|
176 | model.deleteTagNames(rows);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * delete a selection of tag values
|
---|
181 | */
|
---|
182 | protected void deleteTagValues() {
|
---|
183 | int[] rows = tagTable.getSelectedRows();
|
---|
184 | model.deleteTagValues(rows);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * delete a selection of tags
|
---|
189 | */
|
---|
190 | protected void deleteTags() {
|
---|
191 | model.deleteTags(tagTable.getSelectedRows());
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void run() {
|
---|
195 | if (!isEnabled())
|
---|
196 | return;
|
---|
197 | if (tagTable.getSelectedColumnCount() == 1) {
|
---|
198 | if (tagTable.getSelectedColumn() == 0) {
|
---|
199 | deleteTagNames();
|
---|
200 | } else if (tagTable.getSelectedColumn() == 1) {
|
---|
201 | deleteTagValues();
|
---|
202 | } else
|
---|
203 | // should not happen
|
---|
204 | //
|
---|
205 | throw new IllegalStateException("unexpected selected column: getSelectedColumn() is "
|
---|
206 | + tagTable.getSelectedColumn());
|
---|
207 | } else if (tagTable.getSelectedColumnCount() == 2) {
|
---|
208 | deleteTags();
|
---|
209 | }
|
---|
210 | if (model.getRowCount() == 0) {
|
---|
211 | model.ensureOneTag();
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void updateEnabledState() {
|
---|
216 | setEnabled(tagTable.isEnabled() &&
|
---|
217 | (tagTable.getSelectedRowCount() > 0 || tagTable.getSelectedColumnCount() >0));
|
---|
218 | }
|
---|
219 | public void valueChanged(ListSelectionEvent e) {
|
---|
220 | updateEnabledState();
|
---|
221 | }
|
---|
222 |
|
---|
223 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
224 | updateEnabledState();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void initAutoCompletion(OsmDataLayer layer) {
|
---|
229 | // initialize the autocompletion infrastructure
|
---|
230 | //
|
---|
231 | acCache = AutoCompletionCache.getCacheForLayer(layer);
|
---|
232 | acCache.initFromDataSet();
|
---|
233 | acList = new AutoCompletionList();
|
---|
234 |
|
---|
235 | TagCellEditor editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor());
|
---|
236 | editor.setAutoCompletionCache(acCache);
|
---|
237 | editor.setAutoCompletionList(acList);
|
---|
238 | editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
|
---|
239 | editor.setAutoCompletionCache(acCache);
|
---|
240 | editor.setAutoCompletionList(acList);
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Override
|
---|
244 | public void setEnabled(boolean enabled) {
|
---|
245 | tagTable.setEnabled(enabled);
|
---|
246 | super.setEnabled(enabled);
|
---|
247 | }
|
---|
248 | }
|
---|