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

Last change on this file since 2602 was 2599, checked in by Gubaer, 14 years ago

fixed #4130: Chunked upload mode counter is wrong
fixed #4118: Upload dialog too complicated
fixed #4129: Hide the new "Upload data in one request/chunks/individually" behind an expanding "Upload method" box
fixed #2075: API 0.6: don't upload more than 50K edits at once
fixed #4044: Huge uploads never end [should be solved with chunked upload mode]
fixed #4110: Upload dialog spacing wrong
fixed #3386: Upload dialog has empty areas when the changeset doesn't include all of add/modify/delete operations
see #3369: bulk import helper [JOSM now supports multi changesets uploads]

See online help for more details.

Completes r2598

File size: 7.3 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.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
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 * 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}
Note: See TracBrowser for help on using the repository browser.