1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.text.NumberFormat;
|
---|
10 | import java.text.ParseException;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractButton;
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.ButtonGroup;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JComponent;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JToggleButton;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
27 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
28 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Text field type.
|
---|
35 | */
|
---|
36 | public class Text extends KeyedItem {
|
---|
37 |
|
---|
38 | private static int auto_increment_selected; // NOSONAR
|
---|
39 |
|
---|
40 | /** The localized version of {@link #text}. */
|
---|
41 | public String locale_text; // NOSONAR
|
---|
42 | /** The default value for the item. If not specified, the current value of the key is chosen as default (if applicable). Defaults to "". */
|
---|
43 | public String default_; // NOSONAR
|
---|
44 | /** The original value */
|
---|
45 | public String originalValue; // NOSONAR
|
---|
46 | /** whether the last value is used as default. Using "force" enforces this behaviour also for already tagged objects. Default is "false".*/
|
---|
47 | public String use_last_as_default = "false"; // NOSONAR
|
---|
48 | /**
|
---|
49 | * May contain a comma separated list of integer increments or decrements, e.g. "-2,-1,+1,+2".
|
---|
50 | * A button will be shown next to the text field for each value, allowing the user to select auto-increment with the given stepping.
|
---|
51 | * Auto-increment only happens if the user selects it. There is also a button to deselect auto-increment.
|
---|
52 | * Default is no auto-increment. Mutually exclusive with {@link #use_last_as_default}.
|
---|
53 | */
|
---|
54 | public String auto_increment; // NOSONAR
|
---|
55 | /** The length of the text box (number of characters allowed). */
|
---|
56 | public String length; // NOSONAR
|
---|
57 | /** A comma separated list of alternative keys to use for autocompletion. */
|
---|
58 | public String alternative_autocomplete_keys; // NOSONAR
|
---|
59 |
|
---|
60 | private JComponent value;
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
64 |
|
---|
65 | // find out if our key is already used in the selection.
|
---|
66 | Usage usage = determineTextUsage(sel, key);
|
---|
67 | AutoCompletingTextField textField = new AutoCompletingTextField();
|
---|
68 | if (alternative_autocomplete_keys != null) {
|
---|
69 | initAutoCompletionField(textField, (key + ',' + alternative_autocomplete_keys).split(","));
|
---|
70 | } else {
|
---|
71 | initAutoCompletionField(textField, key);
|
---|
72 | }
|
---|
73 | if (Main.pref.getBoolean("taggingpreset.display-keys-as-hint", true)) {
|
---|
74 | textField.setHint(key);
|
---|
75 | }
|
---|
76 | if (length != null && !length.isEmpty()) {
|
---|
77 | textField.setMaxChars(Integer.valueOf(length));
|
---|
78 | }
|
---|
79 | if (usage.unused()) {
|
---|
80 | if (auto_increment_selected != 0 && auto_increment != null) {
|
---|
81 | try {
|
---|
82 | textField.setText(Integer.toString(Integer.parseInt(
|
---|
83 | LAST_VALUES.get(key)) + auto_increment_selected));
|
---|
84 | } catch (NumberFormatException ex) {
|
---|
85 | // Ignore - cannot auto-increment if last was non-numeric
|
---|
86 | Main.trace(ex);
|
---|
87 | }
|
---|
88 | } else if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
89 | // selected osm primitives are untagged or filling default values feature is enabled
|
---|
90 | if (!presetInitiallyMatches && !"false".equals(use_last_as_default) && LAST_VALUES.containsKey(key)) {
|
---|
91 | textField.setText(LAST_VALUES.get(key));
|
---|
92 | } else {
|
---|
93 | textField.setText(default_);
|
---|
94 | }
|
---|
95 | } else {
|
---|
96 | // selected osm primitives are tagged and filling default values feature is disabled
|
---|
97 | textField.setText("");
|
---|
98 | }
|
---|
99 | value = textField;
|
---|
100 | originalValue = null;
|
---|
101 | } else if (usage.hasUniqueValue()) {
|
---|
102 | // all objects use the same value
|
---|
103 | textField.setText(usage.getFirst());
|
---|
104 | value = textField;
|
---|
105 | originalValue = usage.getFirst();
|
---|
106 | } else {
|
---|
107 | // the objects have different values
|
---|
108 | JosmComboBox<String> comboBox = new JosmComboBox<>(usage.values.toArray(new String[usage.values.size()]));
|
---|
109 | comboBox.setEditable(true);
|
---|
110 | comboBox.setEditor(textField);
|
---|
111 | comboBox.getEditor().setItem(DIFFERENT);
|
---|
112 | value = comboBox;
|
---|
113 | originalValue = DIFFERENT;
|
---|
114 | }
|
---|
115 | if (locale_text == null) {
|
---|
116 | locale_text = getLocaleText(text, text_context, null);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // if there's an auto_increment setting, then wrap the text field
|
---|
120 | // into a panel, appending a number of buttons.
|
---|
121 | // auto_increment has a format like -2,-1,1,2
|
---|
122 | // the text box being the first component in the panel is relied
|
---|
123 | // on in a rather ugly fashion further down.
|
---|
124 | if (auto_increment != null) {
|
---|
125 | ButtonGroup bg = new ButtonGroup();
|
---|
126 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
127 | pnl.add(value, GBC.std().fill(GBC.HORIZONTAL));
|
---|
128 |
|
---|
129 | // first, one button for each auto_increment value
|
---|
130 | for (final String ai : auto_increment.split(",")) {
|
---|
131 | JToggleButton aibutton = new JToggleButton(ai);
|
---|
132 | aibutton.setToolTipText(tr("Select auto-increment of {0} for this field", ai));
|
---|
133 | aibutton.setMargin(new Insets(0, 0, 0, 0));
|
---|
134 | aibutton.setFocusable(false);
|
---|
135 | saveHorizontalSpace(aibutton);
|
---|
136 | bg.add(aibutton);
|
---|
137 | try {
|
---|
138 | // TODO there must be a better way to parse a number like "+3" than this.
|
---|
139 | final int buttonvalue = (NumberFormat.getIntegerInstance().parse(ai.replace("+", ""))).intValue();
|
---|
140 | if (auto_increment_selected == buttonvalue) aibutton.setSelected(true);
|
---|
141 | aibutton.addActionListener(e -> auto_increment_selected = buttonvalue);
|
---|
142 | pnl.add(aibutton, GBC.std());
|
---|
143 | } catch (ParseException ex) {
|
---|
144 | Main.error("Cannot parse auto-increment value of '" + ai + "' into an integer");
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | // an invisible toggle button for "release" of the button group
|
---|
149 | final JToggleButton clearbutton = new JToggleButton("X");
|
---|
150 | clearbutton.setVisible(false);
|
---|
151 | clearbutton.setFocusable(false);
|
---|
152 | bg.add(clearbutton);
|
---|
153 | // and its visible counterpart. - this mechanism allows us to
|
---|
154 | // have *no* button selected after the X is clicked, instead
|
---|
155 | // of the X remaining selected
|
---|
156 | JButton releasebutton = new JButton("X");
|
---|
157 | releasebutton.setToolTipText(tr("Cancel auto-increment for this field"));
|
---|
158 | releasebutton.setMargin(new Insets(0, 0, 0, 0));
|
---|
159 | releasebutton.setFocusable(false);
|
---|
160 | releasebutton.addActionListener(e -> {
|
---|
161 | auto_increment_selected = 0;
|
---|
162 | clearbutton.setSelected(true);
|
---|
163 | });
|
---|
164 | saveHorizontalSpace(releasebutton);
|
---|
165 | pnl.add(releasebutton, GBC.eol());
|
---|
166 | value = pnl;
|
---|
167 | }
|
---|
168 | final JLabel label = new JLabel(locale_text + ':');
|
---|
169 | label.setToolTipText(getKeyTooltipText());
|
---|
170 | label.setLabelFor(value);
|
---|
171 | p.add(label, GBC.std().insets(0, 0, 10, 0));
|
---|
172 | p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
173 | value.setToolTipText(getKeyTooltipText());
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | private static void saveHorizontalSpace(AbstractButton button) {
|
---|
178 | Insets insets = button.getBorder().getBorderInsets(button);
|
---|
179 | // Ensure the current look&feel does not waste horizontal space (as seen in Nimbus & Aqua)
|
---|
180 | if (insets != null && insets.left+insets.right > insets.top+insets.bottom) {
|
---|
181 | int min = Math.min(insets.top, insets.bottom);
|
---|
182 | button.setBorder(BorderFactory.createEmptyBorder(insets.top, min, insets.bottom, min));
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | private static String getValue(Component comp) {
|
---|
187 | if (comp instanceof JosmComboBox) {
|
---|
188 | return ((JosmComboBox<?>) comp).getEditor().getItem().toString();
|
---|
189 | } else if (comp instanceof JosmTextField) {
|
---|
190 | return ((JosmTextField) comp).getText();
|
---|
191 | } else if (comp instanceof JPanel) {
|
---|
192 | return getValue(((JPanel) comp).getComponent(0));
|
---|
193 | } else {
|
---|
194 | return null;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public void addCommands(List<Tag> changedTags) {
|
---|
200 |
|
---|
201 | // return if unchanged
|
---|
202 | String v = getValue(value);
|
---|
203 | if (v == null) {
|
---|
204 | Main.error("No 'last value' support for component " + value);
|
---|
205 | return;
|
---|
206 | }
|
---|
207 |
|
---|
208 | v = Tag.removeWhiteSpaces(v);
|
---|
209 |
|
---|
210 | if (!"false".equals(use_last_as_default) || auto_increment != null) {
|
---|
211 | LAST_VALUES.put(key, v);
|
---|
212 | }
|
---|
213 | if (v.equals(originalValue) || (originalValue == null && v.isEmpty()))
|
---|
214 | return;
|
---|
215 |
|
---|
216 | changedTags.add(new Tag(key, v));
|
---|
217 | AutoCompletionManager.rememberUserInput(key, v, true);
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public MatchType getDefaultMatch() {
|
---|
222 | return MatchType.NONE;
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public Collection<String> getValues() {
|
---|
227 | if (default_ == null || default_.isEmpty())
|
---|
228 | return Collections.emptyList();
|
---|
229 | return Collections.singleton(default_);
|
---|
230 | }
|
---|
231 | }
|
---|