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

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

fix some Sonar issues (Constructor Calls Overridable Method)

  • 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 /**
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();
51 tagTable = new TagTable(model);
52 pnl.setLayout(new BorderLayout());
53 pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
54 if (presetHandler != null) {
55 presetListPanel = new PresetListPanel();
56 pnl.add(presetListPanel, BorderLayout.NORTH);
57 }
58 return pnl;
59 }
60
61 public void setNextFocusComponent(Component nextFocusComponent) {
62 tagTable.setNextFocusComponent(nextFocusComponent);
63 }
64
65 /**
66 * builds the panel with the button row
67 *
68 * @return the panel
69 */
70 protected JPanel buildButtonsPanel() {
71 JPanel pnl = new JPanel();
72 pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
73
74 // add action
75 //
76 JButton btn;
77 pnl.add(btn = new JButton(tagTable.getAddAction()));
78 btn.setMargin(new Insets(0,0,0,0));
79 tagTable.addComponentNotStoppingCellEditing(btn);
80
81 // delete action
82 pnl.add(btn = new JButton(tagTable.getDeleteAction()));
83 btn.setMargin(new Insets(0,0,0,0));
84 tagTable.addComponentNotStoppingCellEditing(btn);
85
86 // paste action
87 pnl.add(btn = new JButton(tagTable.getPasteAction()));
88 btn.setMargin(new Insets(0,0,0,0));
89 tagTable.addComponentNotStoppingCellEditing(btn);
90 return pnl;
91 }
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(new TableModelListener() {
126 @Override
127 public void tableChanged(TableModelEvent e) {
128 updatePresets();
129 }
130 });
131 }
132
133 addFocusListener(new FocusAdapter() {
134 @Override public void focusGained(FocusEvent e) {
135 tagTable.requestFocusInCell(0, 0);
136 }
137 });
138 }
139
140 /**
141 * Creates a new tag editor panel. The editor model is created
142 * internally and can be retrieved with {@link #getModel()}.
143 */
144 public TagEditorPanel(PresetHandler presetHandler) {
145 this(null, presetHandler);
146 }
147
148 /**
149 * Creates a new tag editor panel with a supplied model. If
150 * {@code model} is null, a new model is created.
151 *
152 * @param model the tag editor model
153 */
154 public TagEditorPanel(TagEditorModel model, PresetHandler presetHandler) {
155 this.model = model;
156 this.presetHandler = presetHandler;
157 if (this.model == null) {
158 this.model = new TagEditorModel();
159 }
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 thrown if {@code layer} is null
179 */
180 public void initAutoCompletion(OsmDataLayer layer) throws IllegalArgumentException{
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 EnumSet.of(TaggingPresetType.RELATION),
203 model.getTags(), presetHandler);
204 validate();
205 }
206}
Note: See TracBrowser for help on using the repository browser.