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