1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Component;
|
---|
6 | import java.awt.GridBagConstraints;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.awt.event.FocusAdapter;
|
---|
10 | import java.awt.event.FocusEvent;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.BoxLayout;
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JScrollPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
22 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetHandler;
|
---|
24 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * TagEditorPanel is a {@link 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 adding
|
---|
30 | * a new tag and one for deleting the currently selected tags.
|
---|
31 | * @since 2040
|
---|
32 | */
|
---|
33 | public class TagEditorPanel extends JPanel {
|
---|
34 | /** the tag editor model */
|
---|
35 | private TagEditorModel model;
|
---|
36 | /** the tag table */
|
---|
37 | private final TagTable tagTable;
|
---|
38 |
|
---|
39 | private PresetListPanel presetListPanel;
|
---|
40 | private final transient TaggingPresetHandler presetHandler;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * builds the panel with the table for editing tags
|
---|
44 | *
|
---|
45 | * @return the panel
|
---|
46 | */
|
---|
47 | protected JPanel buildTagTableEditorPanel() {
|
---|
48 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
49 | pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
|
---|
50 | if (presetHandler != null) {
|
---|
51 | presetListPanel = new PresetListPanel();
|
---|
52 | pnl.add(presetListPanel, BorderLayout.NORTH);
|
---|
53 | }
|
---|
54 | return pnl;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Sets the next component to request focus after navigation (with tab or enter).
|
---|
59 | * @param nextFocusComponent next component to request focus after navigation (with tab or enter)
|
---|
60 | * @see TagTable#setNextFocusComponent
|
---|
61 | */
|
---|
62 | public void setNextFocusComponent(Component nextFocusComponent) {
|
---|
63 | tagTable.setNextFocusComponent(nextFocusComponent);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * builds the panel with the button row
|
---|
68 | *
|
---|
69 | * @return the panel
|
---|
70 | */
|
---|
71 | protected JPanel buildButtonsPanel() {
|
---|
72 | JPanel pnl = new JPanel();
|
---|
73 | pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
|
---|
74 |
|
---|
75 | buildButton(pnl, tagTable.getAddAction());
|
---|
76 | buildButton(pnl, tagTable.getDeleteAction());
|
---|
77 | buildButton(pnl, tagTable.getPasteAction());
|
---|
78 |
|
---|
79 | return pnl;
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void buildButton(JPanel pnl, AbstractAction action) {
|
---|
83 | JButton btn = new JButton(action);
|
---|
84 | pnl.add(btn);
|
---|
85 | btn.setMargin(new Insets(0, 0, 0, 0));
|
---|
86 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Returns the paste action.
|
---|
91 | * @return the paste action
|
---|
92 | */
|
---|
93 | public AbstractAction getPasteAction() {
|
---|
94 | return tagTable.getPasteAction();
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * builds the GUI
|
---|
99 | */
|
---|
100 | protected final void build() {
|
---|
101 | setLayout(new GridBagLayout());
|
---|
102 | JPanel tablePanel = buildTagTableEditorPanel();
|
---|
103 | JPanel buttonPanel = buildButtonsPanel();
|
---|
104 |
|
---|
105 | GridBagConstraints gc = new GridBagConstraints();
|
---|
106 |
|
---|
107 | // -- buttons panel
|
---|
108 | //
|
---|
109 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
110 | gc.weightx = 0.0;
|
---|
111 | gc.weighty = 1.0;
|
---|
112 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
113 | add(buttonPanel, gc);
|
---|
114 |
|
---|
115 | // -- the panel with the editor table
|
---|
116 | //
|
---|
117 | gc.gridx = 1;
|
---|
118 | gc.fill = GridBagConstraints.BOTH;
|
---|
119 | gc.weightx = 1.0;
|
---|
120 | gc.weighty = 1.0;
|
---|
121 | gc.anchor = GridBagConstraints.CENTER;
|
---|
122 | add(tablePanel, gc);
|
---|
123 |
|
---|
124 | if (presetHandler != null) {
|
---|
125 | model.addTableModelListener(e -> updatePresets());
|
---|
126 | }
|
---|
127 |
|
---|
128 | addFocusListener(new FocusAdapter() {
|
---|
129 | @Override
|
---|
130 | public void focusGained(FocusEvent e) {
|
---|
131 | tagTable.requestFocusInCell(0, 0);
|
---|
132 | }
|
---|
133 | });
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Creates a new tag editor panel. The editor model is created
|
---|
138 | * internally and can be retrieved with {@link #getModel()}.
|
---|
139 | * @param primitive primitive to consider
|
---|
140 | * @param presetHandler tagging preset handler
|
---|
141 | */
|
---|
142 | public TagEditorPanel(OsmPrimitive primitive, TaggingPresetHandler presetHandler) {
|
---|
143 | this(new TagEditorModel().forPrimitive(primitive), presetHandler, 0);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Creates a new tag editor panel with a supplied model. If {@code model} is null, a new model is created.
|
---|
148 | *
|
---|
149 | * @param model the tag editor model
|
---|
150 | * @param presetHandler tagging preset handler
|
---|
151 | * @param maxCharacters maximum number of characters allowed, 0 for unlimited
|
---|
152 | */
|
---|
153 | public TagEditorPanel(TagEditorModel model, TaggingPresetHandler 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 | model.getTaggingPresetTypes(),
|
---|
203 | model.getTags(), presetHandler);
|
---|
204 | validate();
|
---|
205 | }
|
---|
206 | }
|
---|