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 | import java.util.EnumSet;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.BoxLayout;
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 | import javax.swing.event.TableModelEvent;
|
---|
19 | import javax.swing.event.TableModelListener;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
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 | *
|
---|
33 | */
|
---|
34 | public class TagEditorPanel extends JPanel {
|
---|
35 | /** the tag editor model */
|
---|
36 | private TagEditorModel model;
|
---|
37 | /** the tag table */
|
---|
38 | private TagTable tagTable;
|
---|
39 |
|
---|
40 | private PresetListPanel presetListPanel;
|
---|
41 | private final 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 | tagTable = new TagTable(model);
|
---|
51 | pnl.setLayout(new BorderLayout());
|
---|
52 | pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
|
---|
53 | if (presetHandler != null) {
|
---|
54 | presetListPanel = new PresetListPanel();
|
---|
55 | pnl.add(presetListPanel, BorderLayout.NORTH);
|
---|
56 | }
|
---|
57 | return pnl;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void setNextFocusComponent(Component nextFocusComponent) {
|
---|
61 | tagTable.setNextFocusComponent(nextFocusComponent);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * builds the panel with the button row
|
---|
66 | *
|
---|
67 | * @return the panel
|
---|
68 | */
|
---|
69 | protected JPanel buildButtonsPanel() {
|
---|
70 | JPanel pnl = new JPanel();
|
---|
71 | pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
|
---|
72 |
|
---|
73 | // add action
|
---|
74 | //
|
---|
75 | JButton btn;
|
---|
76 | pnl.add(btn = new JButton(tagTable.getAddAction()));
|
---|
77 | btn.setMargin(new Insets(0,0,0,0));
|
---|
78 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
79 |
|
---|
80 | // delete action
|
---|
81 | pnl.add(btn = new JButton(tagTable.getDeleteAction()));
|
---|
82 | btn.setMargin(new Insets(0,0,0,0));
|
---|
83 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
84 |
|
---|
85 | // paste action
|
---|
86 | pnl.add(btn = new JButton(tagTable.getPasteAction()));
|
---|
87 | btn.setMargin(new Insets(0,0,0,0));
|
---|
88 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
89 | return pnl;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public AbstractAction getPasteAction() {
|
---|
93 | return tagTable.getPasteAction();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * builds the GUI
|
---|
98 | */
|
---|
99 | protected final void build() {
|
---|
100 | setLayout(new GridBagLayout());
|
---|
101 | JPanel tablePanel = buildTagTableEditorPanel();
|
---|
102 | JPanel buttonPanel = buildButtonsPanel();
|
---|
103 |
|
---|
104 | GridBagConstraints gc = new GridBagConstraints();
|
---|
105 |
|
---|
106 | // -- buttons panel
|
---|
107 | //
|
---|
108 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
109 | gc.weightx = 0.0;
|
---|
110 | gc.weighty = 1.0;
|
---|
111 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
112 | add(buttonPanel,gc);
|
---|
113 |
|
---|
114 | // -- the panel with the editor table
|
---|
115 | //
|
---|
116 | gc.gridx = 1;
|
---|
117 | gc.fill = GridBagConstraints.BOTH;
|
---|
118 | gc.weightx = 1.0;
|
---|
119 | gc.weighty = 1.0;
|
---|
120 | gc.anchor = GridBagConstraints.CENTER;
|
---|
121 | add(tablePanel,gc);
|
---|
122 |
|
---|
123 | if (presetHandler != null) {
|
---|
124 | model.addTableModelListener(new TableModelListener() {
|
---|
125 | @Override
|
---|
126 | public void tableChanged(TableModelEvent e) {
|
---|
127 | updatePresets();
|
---|
128 | }
|
---|
129 | });
|
---|
130 | }
|
---|
131 |
|
---|
132 | addFocusListener(new FocusAdapter() {
|
---|
133 | @Override 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 | */
|
---|
143 | public TagEditorPanel(PresetHandler presetHandler) {
|
---|
144 | this(null, presetHandler);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Creates a new tag editor panel with a supplied model. If
|
---|
149 | * {@code model} is null, a new model is created.
|
---|
150 | *
|
---|
151 | * @param model the tag editor model
|
---|
152 | */
|
---|
153 | public TagEditorPanel(TagEditorModel model, PresetHandler presetHandler) {
|
---|
154 | this.model = model;
|
---|
155 | this.presetHandler = presetHandler;
|
---|
156 | if (this.model == null) {
|
---|
157 | this.model = new TagEditorModel();
|
---|
158 | }
|
---|
159 | build();
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Replies the tag editor model used by this panel.
|
---|
164 | *
|
---|
165 | * @return the tag editor model used by this panel
|
---|
166 | */
|
---|
167 | public TagEditorModel getModel() {
|
---|
168 | return model;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Initializes the auto completion infrastructure used in this
|
---|
173 | * tag editor panel. {@code layer} is the data layer from whose data set
|
---|
174 | * tag values are proposed as auto completion items.
|
---|
175 | *
|
---|
176 | * @param layer the data layer. Must not be null.
|
---|
177 | * @throws IllegalArgumentException if {@code layer} is null
|
---|
178 | */
|
---|
179 | public void initAutoCompletion(OsmDataLayer layer) {
|
---|
180 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
181 |
|
---|
182 | AutoCompletionManager autocomplete = layer.data.getAutoCompletionManager();
|
---|
183 | AutoCompletionList acList = new AutoCompletionList();
|
---|
184 |
|
---|
185 | TagCellEditor editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor());
|
---|
186 | editor.setAutoCompletionManager(autocomplete);
|
---|
187 | editor.setAutoCompletionList(acList);
|
---|
188 | editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
|
---|
189 | editor.setAutoCompletionManager(autocomplete);
|
---|
190 | editor.setAutoCompletionList(acList);
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public void setEnabled(boolean enabled) {
|
---|
195 | tagTable.setEnabled(enabled);
|
---|
196 | super.setEnabled(enabled);
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void updatePresets() {
|
---|
200 | presetListPanel.updatePresets(
|
---|
201 | EnumSet.of(TaggingPresetType.RELATION),
|
---|
202 | model.getTags(), presetHandler);
|
---|
203 | validate();
|
---|
204 | }
|
---|
205 | }
|
---|