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

Last change on this file since 6442 was 6267, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Replace singular fields by local variables

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