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.DefaultListSelectionModel;
|
---|
12 | import javax.swing.JButton;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.JScrollPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
|
---|
18 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
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 | static private final Logger logger = Logger.getLogger(TagEditorPanel.class.getName());
|
---|
29 | /** the tag editor model */
|
---|
30 | private TagEditorModel model;
|
---|
31 | /** the tag table */
|
---|
32 | private TagTable tagTable;
|
---|
33 |
|
---|
34 | private AutoCompletionCache acCache;
|
---|
35 | private AutoCompletionList acList;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * builds the panel with the table for editing tags
|
---|
39 | *
|
---|
40 | * @return the panel
|
---|
41 | */
|
---|
42 | protected JPanel buildTagTableEditorPanel() {
|
---|
43 |
|
---|
44 | JPanel pnl = new JPanel();
|
---|
45 | DefaultListSelectionModel rowSelectionModel = new DefaultListSelectionModel();
|
---|
46 | DefaultListSelectionModel colSelectionModel = new DefaultListSelectionModel();
|
---|
47 |
|
---|
48 | model = new TagEditorModel(rowSelectionModel, colSelectionModel);
|
---|
49 | tagTable = new TagTable(model, rowSelectionModel, colSelectionModel);
|
---|
50 |
|
---|
51 | pnl.setLayout(new BorderLayout());
|
---|
52 | pnl.add(new JScrollPane(tagTable), BorderLayout.CENTER);
|
---|
53 | return pnl;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * builds the panel with the button row
|
---|
58 | *
|
---|
59 | * @return the panel
|
---|
60 | */
|
---|
61 | protected JPanel buildButtonsPanel() {
|
---|
62 | JPanel pnl = new JPanel();
|
---|
63 | pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
|
---|
64 |
|
---|
65 | // add action
|
---|
66 | //
|
---|
67 | JButton btn;
|
---|
68 | pnl.add(btn = new JButton(tagTable.getAddAction()));
|
---|
69 | btn.setMargin(new Insets(0,0,0,0));
|
---|
70 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
71 |
|
---|
72 | // delete action
|
---|
73 | pnl.add(btn = new JButton(tagTable.getDeleteAction()));
|
---|
74 | btn.setMargin(new Insets(0,0,0,0));
|
---|
75 | tagTable.addComponentNotStoppingCellEditing(btn);
|
---|
76 | return pnl;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * builds the GUI
|
---|
81 | */
|
---|
82 | protected void build() {
|
---|
83 | setLayout(new GridBagLayout());
|
---|
84 | JPanel tablePanel = buildTagTableEditorPanel();
|
---|
85 | JPanel buttonPanel = buildButtonsPanel();
|
---|
86 |
|
---|
87 | GridBagConstraints gc = new GridBagConstraints();
|
---|
88 |
|
---|
89 | // -- buttons panel
|
---|
90 | //
|
---|
91 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
92 | gc.weightx = 0.0;
|
---|
93 | gc.weighty = 1.0;
|
---|
94 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
95 | add(buttonPanel,gc);
|
---|
96 |
|
---|
97 | // -- the panel with the editor table
|
---|
98 | //
|
---|
99 | gc.gridx = 1;
|
---|
100 | gc.fill = GridBagConstraints.BOTH;
|
---|
101 | gc.weightx = 1.0;
|
---|
102 | gc.weighty = 1.0;
|
---|
103 | gc.anchor = GridBagConstraints.CENTER;
|
---|
104 | add(tablePanel,gc);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * constructor
|
---|
109 | */
|
---|
110 | public TagEditorPanel() {
|
---|
111 | build();
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Replies the tag editor model used by this panel.
|
---|
116 | *
|
---|
117 | * @return the tag editor model used by this panel
|
---|
118 | */
|
---|
119 | public TagEditorModel getModel() {
|
---|
120 | return model;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void initAutoCompletion(OsmDataLayer layer) {
|
---|
124 | // initialize the autocompletion infrastructure
|
---|
125 | //
|
---|
126 | acCache = AutoCompletionCache.getCacheForLayer(layer);
|
---|
127 | acCache.initFromDataSet();
|
---|
128 | acList = new AutoCompletionList();
|
---|
129 |
|
---|
130 | TagCellEditor editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor());
|
---|
131 | editor.setAutoCompletionCache(acCache);
|
---|
132 | editor.setAutoCompletionList(acList);
|
---|
133 | editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
|
---|
134 | editor.setAutoCompletionCache(acCache);
|
---|
135 | editor.setAutoCompletionList(acList);
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void setEnabled(boolean enabled) {
|
---|
140 | tagTable.setEnabled(enabled);
|
---|
141 | super.setEnabled(enabled);
|
---|
142 | }
|
---|
143 | }
|
---|