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

Last change on this file since 4191 was 4191, checked in by stoecker, 13 years ago

remove old debug stuff

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