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

Last change on this file since 8846 was 8760, checked in by simon04, 9 years ago

fix #11686 - Error by uploading changeset with too long tag

Changeset tags table: limit key/value length to allowed maximum of API

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
3
4import java.awt.BorderLayout;
5import java.awt.Component;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.event.FocusAdapter;
10import java.awt.event.FocusEvent;
11import java.util.EnumSet;
12
13import javax.swing.AbstractAction;
14import javax.swing.BoxLayout;
15import javax.swing.JButton;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.event.TableModelEvent;
19import javax.swing.event.TableModelListener;
20
21import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
24import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
26
27/**
28 * TagEditorPanel is a {@link 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 adding
31 * a new tag and one for deleting the currently selected tags.
32 *
33 */
34public class TagEditorPanel extends JPanel {
35 /** the tag editor model */
36 private TagEditorModel model;
37 /** the tag table */
38 private final TagTable tagTable;
39
40 private PresetListPanel presetListPanel;
41 private final transient PresetHandler presetHandler;
42
43 /**
44 * builds the panel with the table for editing tags
45 *
46 * @return the panel
47 */
48 protected JPanel buildTagTableEditorPanel() {
49 JPanel pnl = new JPanel();
50 pnl.setLayout(new BorderLayout());
51 pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
52 if (presetHandler != null) {
53 presetListPanel = new PresetListPanel();
54 pnl.add(presetListPanel, BorderLayout.NORTH);
55 }
56 return pnl;
57 }
58
59 public void setNextFocusComponent(Component nextFocusComponent) {
60 tagTable.setNextFocusComponent(nextFocusComponent);
61 }
62
63 /**
64 * builds the panel with the button row
65 *
66 * @return the panel
67 */
68 protected JPanel buildButtonsPanel() {
69 JPanel pnl = new JPanel();
70 pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
71
72 // add action
73 //
74 JButton btn;
75 pnl.add(btn = new JButton(tagTable.getAddAction()));
76 btn.setMargin(new Insets(0, 0, 0, 0));
77 tagTable.addComponentNotStoppingCellEditing(btn);
78
79 // delete action
80 pnl.add(btn = new JButton(tagTable.getDeleteAction()));
81 btn.setMargin(new Insets(0, 0, 0, 0));
82 tagTable.addComponentNotStoppingCellEditing(btn);
83
84 // paste action
85 pnl.add(btn = new JButton(tagTable.getPasteAction()));
86 btn.setMargin(new Insets(0, 0, 0, 0));
87 tagTable.addComponentNotStoppingCellEditing(btn);
88 return pnl;
89 }
90
91 public AbstractAction getPasteAction() {
92 return tagTable.getPasteAction();
93 }
94
95 /**
96 * builds the GUI
97 */
98 protected final void build() {
99 setLayout(new GridBagLayout());
100 JPanel tablePanel = buildTagTableEditorPanel();
101 JPanel buttonPanel = buildButtonsPanel();
102
103 GridBagConstraints gc = new GridBagConstraints();
104
105 // -- buttons panel
106 //
107 gc.fill = GridBagConstraints.VERTICAL;
108 gc.weightx = 0.0;
109 gc.weighty = 1.0;
110 gc.anchor = GridBagConstraints.NORTHWEST;
111 add(buttonPanel, gc);
112
113 // -- the panel with the editor table
114 //
115 gc.gridx = 1;
116 gc.fill = GridBagConstraints.BOTH;
117 gc.weightx = 1.0;
118 gc.weighty = 1.0;
119 gc.anchor = GridBagConstraints.CENTER;
120 add(tablePanel, gc);
121
122 if (presetHandler != null) {
123 model.addTableModelListener(new TableModelListener() {
124 @Override
125 public void tableChanged(TableModelEvent e) {
126 updatePresets();
127 }
128 });
129 }
130
131 addFocusListener(new FocusAdapter() {
132 @Override public void focusGained(FocusEvent e) {
133 tagTable.requestFocusInCell(0, 0);
134 }
135 });
136 }
137
138 /**
139 * Creates a new tag editor panel. The editor model is created
140 * internally and can be retrieved with {@link #getModel()}.
141 */
142 public TagEditorPanel(PresetHandler presetHandler) {
143 this(null, presetHandler, 0);
144 }
145
146 /**
147 * Creates a new tag editor panel with a supplied model. If
148 * {@code model} is null, a new model is created.
149 *
150 * @param model the tag editor model
151 * @param maxCharacters maximum number of characters allowed, 0 for unlimited
152 */
153 public TagEditorPanel(TagEditorModel model, PresetHandler presetHandler, final int maxCharacters) {
154 this.model = model;
155 this.presetHandler = presetHandler;
156 if (this.model == null) {
157 this.model = new TagEditorModel();
158 }
159 this.tagTable = new TagTable(this.model, maxCharacters);
160 build();
161 }
162
163 /**
164 * Replies the tag editor model used by this panel.
165 *
166 * @return the tag editor model used by this panel
167 */
168 public TagEditorModel getModel() {
169 return model;
170 }
171
172 /**
173 * Initializes the auto completion infrastructure used in this
174 * tag editor panel. {@code layer} is the data layer from whose data set
175 * tag values are proposed as auto completion items.
176 *
177 * @param layer the data layer. Must not be null.
178 * @throws IllegalArgumentException if {@code layer} is null
179 */
180 public void initAutoCompletion(OsmDataLayer layer) {
181 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
182
183 AutoCompletionManager autocomplete = layer.data.getAutoCompletionManager();
184 AutoCompletionList acList = new AutoCompletionList();
185
186 TagCellEditor editor = (TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor();
187 editor.setAutoCompletionManager(autocomplete);
188 editor.setAutoCompletionList(acList);
189 editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
190 editor.setAutoCompletionManager(autocomplete);
191 editor.setAutoCompletionList(acList);
192 }
193
194 @Override
195 public void setEnabled(boolean enabled) {
196 tagTable.setEnabled(enabled);
197 super.setEnabled(enabled);
198 }
199
200 private void updatePresets() {
201 presetListPanel.updatePresets(
202 EnumSet.of(TaggingPresetType.RELATION),
203 model.getTags(), presetHandler);
204 validate();
205 }
206}
Note: See TracBrowser for help on using the repository browser.