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

Last change on this file since 5155 was 5155, checked in by simon04, 12 years ago

fix #5933 - tagging presets: allow to change the matching process (match=none|key|key!|keyvalue), remove delete_if_empty, default defaults to "", adapted comments in defaultpresets.xml, refactoring of the matching process (removes some duplicate code and some magical arithmetic)

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
3
4import java.awt.BorderLayout;
5import java.awt.GridBagConstraints;
6import java.awt.GridBagLayout;
7import java.awt.Insets;
8import java.util.EnumSet;
9
10import javax.swing.BoxLayout;
11import javax.swing.JButton;
12import javax.swing.JPanel;
13import javax.swing.JScrollPane;
14import javax.swing.event.TableModelEvent;
15import javax.swing.event.TableModelListener;
16
17import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel;
18import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel.PresetHandler;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
21import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
22import org.openstreetmap.josm.tools.CheckParameterUtil;
23
24/**
25 * TagEditorPanel is a {@see JPanel} which can be embedded as UI component in
26 * UIs. It provides a spreadsheet like tabular control for editing tag names
27 * and tag values. Two action buttons are placed on the left, one for adding
28 * a new tag and one for deleting the currently selected tags.
29 *
30 */
31public class TagEditorPanel extends JPanel {
32 /** the tag editor model */
33 private TagEditorModel model;
34 /** the tag table */
35 private TagTable tagTable;
36
37 private PresetListPanel presetListPanel;
38 private final PresetHandler presetHandler;
39
40 private AutoCompletionManager autocomplete;
41 private AutoCompletionList acList;
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 /**
61 * builds the panel with the button row
62 *
63 * @return the panel
64 */
65 protected JPanel buildButtonsPanel() {
66 JPanel pnl = new JPanel();
67 pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
68
69 // add action
70 //
71 JButton btn;
72 pnl.add(btn = new JButton(tagTable.getAddAction()));
73 btn.setMargin(new Insets(0,0,0,0));
74 tagTable.addComponentNotStoppingCellEditing(btn);
75
76 // delete action
77 pnl.add(btn = new JButton(tagTable.getDeleteAction()));
78 btn.setMargin(new Insets(0,0,0,0));
79 tagTable.addComponentNotStoppingCellEditing(btn);
80 return pnl;
81 }
82
83 /**
84 * builds the GUI
85 */
86 protected void build() {
87 setLayout(new GridBagLayout());
88 JPanel tablePanel = buildTagTableEditorPanel();
89 JPanel buttonPanel = buildButtonsPanel();
90
91 GridBagConstraints gc = new GridBagConstraints();
92
93 // -- buttons panel
94 //
95 gc.fill = GridBagConstraints.VERTICAL;
96 gc.weightx = 0.0;
97 gc.weighty = 1.0;
98 gc.anchor = GridBagConstraints.NORTHWEST;
99 add(buttonPanel,gc);
100
101 // -- the panel with the editor table
102 //
103 gc.gridx = 1;
104 gc.fill = GridBagConstraints.BOTH;
105 gc.weightx = 1.0;
106 gc.weighty = 1.0;
107 gc.anchor = GridBagConstraints.CENTER;
108 add(tablePanel,gc);
109
110 if (presetHandler != null) {
111 model.addTableModelListener(new TableModelListener() {
112 @Override
113 public void tableChanged(TableModelEvent e) {
114 updatePresets();
115 }
116 });
117 }
118 }
119
120 /**
121 * Creates a new tag editor panel. The editor model is created
122 * internally and can be retrieved with {@see #getModel()}.
123 */
124 public TagEditorPanel(PresetHandler presetHandler) {
125 this(null, presetHandler);
126 }
127
128 /**
129 * Creates a new tag editor panel with a supplied model. If
130 * {@code model} is null, a new model is created.
131 *
132 * @param model the tag editor model
133 */
134 public TagEditorPanel(TagEditorModel model, PresetHandler presetHandler) {
135 this.model = model;
136 this.presetHandler = presetHandler;
137 if (this.model == null) {
138 this.model = new TagEditorModel();
139 }
140 build();
141 }
142
143 /**
144 * Replies the tag editor model used by this panel.
145 *
146 * @return the tag editor model used by this panel
147 */
148 public TagEditorModel getModel() {
149 return model;
150 }
151
152 /**
153 * Initializes the auto completion infrastructure used in this
154 * tag editor panel. {@code layer} is the data layer from whose data set
155 * tag values are proposed as auto completion items.
156 *
157 * @param layer the data layer. Must not be null.
158 * @throws IllegalArgumentException thrown if {@code layer} is null
159 */
160 public void initAutoCompletion(OsmDataLayer layer) throws IllegalArgumentException{
161 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
162
163 autocomplete = layer.data.getAutoCompletionManager();
164 acList = new AutoCompletionList();
165
166 TagCellEditor editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor());
167 editor.setAutoCompletionManager(autocomplete);
168 editor.setAutoCompletionList(acList);
169 editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
170 editor.setAutoCompletionManager(autocomplete);
171 editor.setAutoCompletionList(acList);
172 }
173
174 @Override
175 public void setEnabled(boolean enabled) {
176 tagTable.setEnabled(enabled);
177 super.setEnabled(enabled);
178 }
179
180 private void updatePresets() {
181 presetListPanel.updatePresets(
182 EnumSet.of(TaggingPreset.PresetType.RELATION),
183 model.getTags(), presetHandler);
184 validate();
185 }
186}
Note: See TracBrowser for help on using the repository browser.