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.spi.preferences.Config;
|
---|
25 | import 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 | * @since 2040
|
---|
33 | */
|
---|
34 | public 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 TaggingPresetHandler 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(new BorderLayout());
|
---|
50 | pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
|
---|
51 | if (presetHandler != null) {
|
---|
52 | presetListPanel = new PresetListPanel();
|
---|
53 | if (Config.getPref().getBoolean("relation.editor.presets.visible", true)) {
|
---|
54 | pnl.add(presetListPanel, BorderLayout.NORTH);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | return pnl;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Sets the next component to request focus after navigation (with tab or enter).
|
---|
62 | * @param nextFocusComponent next component to request focus after navigation (with tab or enter)
|
---|
63 | * @see TagTable#setNextFocusComponent
|
---|
64 | */
|
---|
65 | public void setNextFocusComponent(Component nextFocusComponent) {
|
---|
66 | tagTable.setNextFocusComponent(nextFocusComponent);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * builds the panel with the button row
|
---|
71 | *
|
---|
72 | * @return the panel
|
---|
73 | */
|
---|
74 | protected JPanel buildButtonsPanel() {
|
---|
75 | JPanel pnl = new JPanel();
|
---|
76 | pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
|
---|
77 |
|
---|
78 | buildButton(pnl, tagTable.getAddAction());
|
---|
79 | buildButton(pnl, tagTable.getDeleteAction());
|
---|
80 | buildButton(pnl, tagTable.getPasteAction());
|
---|
81 |
|
---|
82 | return pnl;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void buildButton(JPanel pnl, AbstractAction action) {
|
---|
86 | JButton btn = new JButton(action);
|
---|
87 | pnl.add(btn);
|
---|
88 | btn.setMargin(new Insets(0, 0, 0, 0));
|
---|
89 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns the paste action.
|
---|
94 | * @return the paste action
|
---|
95 | */
|
---|
96 | public AbstractAction getPasteAction() {
|
---|
97 | return tagTable.getPasteAction();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * builds the GUI
|
---|
102 | */
|
---|
103 | protected final void build() {
|
---|
104 | setLayout(new GridBagLayout());
|
---|
105 | JPanel tablePanel = buildTagTableEditorPanel();
|
---|
106 | JPanel buttonPanel = buildButtonsPanel();
|
---|
107 |
|
---|
108 | GridBagConstraints gc = new GridBagConstraints();
|
---|
109 |
|
---|
110 | // -- buttons panel
|
---|
111 | //
|
---|
112 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
113 | gc.weightx = 0.0;
|
---|
114 | gc.weighty = 1.0;
|
---|
115 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
116 | add(buttonPanel, gc);
|
---|
117 |
|
---|
118 | // -- the panel with the editor table
|
---|
119 | //
|
---|
120 | gc.gridx = 1;
|
---|
121 | gc.fill = GridBagConstraints.BOTH;
|
---|
122 | gc.weightx = 1.0;
|
---|
123 | gc.weighty = 1.0;
|
---|
124 | gc.anchor = GridBagConstraints.CENTER;
|
---|
125 | add(tablePanel, gc);
|
---|
126 |
|
---|
127 | if (presetHandler != null) {
|
---|
128 | model.addTableModelListener(e -> updatePresets());
|
---|
129 | }
|
---|
130 |
|
---|
131 | addFocusListener(new FocusAdapter() {
|
---|
132 | @Override
|
---|
133 | public void focusGained(FocusEvent e) {
|
---|
134 | tagTable.requestFocusInCell(0, 0);
|
---|
135 | }
|
---|
136 | });
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Creates a new tag editor panel. The editor model is created
|
---|
141 | * internally and can be retrieved with {@link #getModel()}.
|
---|
142 | * @param primitive primitive to consider
|
---|
143 | * @param presetHandler tagging preset handler
|
---|
144 | */
|
---|
145 | public TagEditorPanel(OsmPrimitive primitive, TaggingPresetHandler presetHandler) {
|
---|
146 | this(new TagEditorModel().forPrimitive(primitive), presetHandler, 0);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Creates a new tag editor panel with a supplied model. If {@code model} is null, a new model is created.
|
---|
151 | *
|
---|
152 | * @param model the tag editor model
|
---|
153 | * @param presetHandler tagging preset handler
|
---|
154 | * @param maxCharacters maximum number of characters allowed, 0 for unlimited
|
---|
155 | */
|
---|
156 | public TagEditorPanel(TagEditorModel model, TaggingPresetHandler presetHandler, final int maxCharacters) {
|
---|
157 | this.model = model;
|
---|
158 | this.presetHandler = presetHandler;
|
---|
159 | if (this.model == null) {
|
---|
160 | this.model = new TagEditorModel();
|
---|
161 | }
|
---|
162 | this.tagTable = new TagTable(this.model, maxCharacters);
|
---|
163 | build();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Replies the tag editor model used by this panel.
|
---|
168 | *
|
---|
169 | * @return the tag editor model used by this panel
|
---|
170 | */
|
---|
171 | public TagEditorModel getModel() {
|
---|
172 | return model;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Initializes the auto completion infrastructure used in this
|
---|
177 | * tag editor panel. {@code layer} is the data layer from whose data set
|
---|
178 | * tag values are proposed as auto completion items.
|
---|
179 | *
|
---|
180 | * @param layer the data layer. Must not be null.
|
---|
181 | * @throws IllegalArgumentException if {@code layer} is null
|
---|
182 | */
|
---|
183 | public void initAutoCompletion(OsmDataLayer layer) {
|
---|
184 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
185 |
|
---|
186 | AutoCompletionManager autocomplete = AutoCompletionManager.of(layer.data);
|
---|
187 | AutoCompletionList acList = new AutoCompletionList();
|
---|
188 |
|
---|
189 | TagCellEditor editor = (TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor();
|
---|
190 | editor.setAutoCompletionManager(autocomplete);
|
---|
191 | editor.setAutoCompletionList(acList);
|
---|
192 | editor = (TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor();
|
---|
193 | editor.setAutoCompletionManager(autocomplete);
|
---|
194 | editor.setAutoCompletionList(acList);
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override
|
---|
198 | public void setEnabled(boolean enabled) {
|
---|
199 | tagTable.setEnabled(enabled);
|
---|
200 | super.setEnabled(enabled);
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void updatePresets() {
|
---|
204 | presetListPanel.updatePresets(
|
---|
205 | model.getTaggingPresetTypes(),
|
---|
206 | model.getTags(), presetHandler);
|
---|
207 | validate();
|
---|
208 | }
|
---|
209 | }
|
---|