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.GridBagConstraints;
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.Insets;
|
---|
8 | import java.util.logging.Logger;
|
---|
9 |
|
---|
10 | import javax.swing.BoxLayout;
|
---|
11 | import javax.swing.JButton;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
17 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * TagEditorPanel is a {@see JPanel} which can be embedded as UI component in
|
---|
22 | * UIs. It provides a spreadsheet like tabular control for editing tag names
|
---|
23 | * and tag values. Two action buttons are placed on the left, one for adding
|
---|
24 | * a new tag and one for deleting the currently selected tags.
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public class TagEditorPanel extends JPanel {
|
---|
28 | @SuppressWarnings("unused")
|
---|
29 | static private final Logger logger = Logger.getLogger(TagEditorPanel.class.getName());
|
---|
30 | /** the tag editor model */
|
---|
31 | private TagEditorModel model;
|
---|
32 | /** the tag table */
|
---|
33 | private TagTable tagTable;
|
---|
34 |
|
---|
35 | private AutoCompletionManager autocomplete;
|
---|
36 | private AutoCompletionList acList;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * builds the panel with the table for editing tags
|
---|
40 | *
|
---|
41 | * @return the panel
|
---|
42 | */
|
---|
43 | protected JPanel buildTagTableEditorPanel() {
|
---|
44 | JPanel pnl = new JPanel();
|
---|
45 | tagTable = new TagTable(model);
|
---|
46 | pnl.setLayout(new BorderLayout());
|
---|
47 | pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
|
---|
48 | return pnl;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * builds the panel with the button row
|
---|
53 | *
|
---|
54 | * @return the panel
|
---|
55 | */
|
---|
56 | protected JPanel buildButtonsPanel() {
|
---|
57 | JPanel pnl = new JPanel();
|
---|
58 | pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
|
---|
59 |
|
---|
60 | // add action
|
---|
61 | //
|
---|
62 | JButton btn;
|
---|
63 | pnl.add(btn = new JButton(tagTable.getAddAction()));
|
---|
64 | btn.setMargin(new Insets(0,0,0,0));
|
---|
65 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
66 |
|
---|
67 | // delete action
|
---|
68 | pnl.add(btn = new JButton(tagTable.getDeleteAction()));
|
---|
69 | btn.setMargin(new Insets(0,0,0,0));
|
---|
70 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
71 | return pnl;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * builds the GUI
|
---|
76 | */
|
---|
77 | protected void build() {
|
---|
78 | setLayout(new GridBagLayout());
|
---|
79 | JPanel tablePanel = buildTagTableEditorPanel();
|
---|
80 | JPanel buttonPanel = buildButtonsPanel();
|
---|
81 |
|
---|
82 | GridBagConstraints gc = new GridBagConstraints();
|
---|
83 |
|
---|
84 | // -- buttons panel
|
---|
85 | //
|
---|
86 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
87 | gc.weightx = 0.0;
|
---|
88 | gc.weighty = 1.0;
|
---|
89 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
90 | add(buttonPanel,gc);
|
---|
91 |
|
---|
92 | // -- the panel with the editor table
|
---|
93 | //
|
---|
94 | gc.gridx = 1;
|
---|
95 | gc.fill = GridBagConstraints.BOTH;
|
---|
96 | gc.weightx = 1.0;
|
---|
97 | gc.weighty = 1.0;
|
---|
98 | gc.anchor = GridBagConstraints.CENTER;
|
---|
99 | add(tablePanel,gc);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Creates a new tag editor panel. The editor model is created
|
---|
104 | * internally and can be retrieved with {@see #getModel()}.
|
---|
105 | */
|
---|
106 | public TagEditorPanel() {
|
---|
107 | this(null);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Creates a new tag editor panel with a supplied model. If
|
---|
112 | * {@code model} is null, a new model is created.
|
---|
113 | *
|
---|
114 | * @param model the tag editor model
|
---|
115 | */
|
---|
116 | public TagEditorPanel(TagEditorModel model) {
|
---|
117 | this.model = model;
|
---|
118 | if (this.model == null) {
|
---|
119 | this.model = new TagEditorModel();
|
---|
120 | }
|
---|
121 | build();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Replies the tag editor model used by this panel.
|
---|
126 | *
|
---|
127 | * @return the tag editor model used by this panel
|
---|
128 | */
|
---|
129 | public TagEditorModel getModel() {
|
---|
130 | return model;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Initializes the auto completion infrastructure used in this
|
---|
135 | * tag editor panel. {@code layer} is the data layer from whose data set
|
---|
136 | * tag values are proposed as auto completion items.
|
---|
137 | *
|
---|
138 | * @param layer the data layer. Must not be null.
|
---|
139 | * @throws IllegalArgumentException thrown if {@code layer} is null
|
---|
140 | */
|
---|
141 | public void initAutoCompletion(OsmDataLayer layer) throws IllegalArgumentException{
|
---|
142 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
143 |
|
---|
144 | autocomplete = layer.data.getAutoCompletionManager();
|
---|
145 | acList = new AutoCompletionList();
|
---|
146 |
|
---|
147 | TagCellEditor editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor());
|
---|
148 | editor.setAutoCompletionManager(autocomplete);
|
---|
149 | editor.setAutoCompletionList(acList);
|
---|
150 | editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
|
---|
151 | editor.setAutoCompletionManager(autocomplete);
|
---|
152 | editor.setAutoCompletionList(acList);
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public void setEnabled(boolean enabled) {
|
---|
157 | tagTable.setEnabled(enabled);
|
---|
158 | super.setEnabled(enabled);
|
---|
159 | }
|
---|
160 | }
|
---|