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

Last change on this file since 9588 was 9588, checked in by Don-vip, 8 years ago

see #12412 - improve support of multipolygon presets type

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