source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/TagEditorPanel.java@ 2048

Last change on this file since 2048 was 2048, checked in by Gubaer, 15 years ago

Improved auto completion
fixed #2729: Auto completion with numbers sometimes annoying (only fixed in presets, relation editor, not in property dialog)
fixed #2320: Preset dialog for house numbers should have autocompletion (auto completion now supported in all preset dialogs)

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