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