source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Text.java@ 16687

Last change on this file since 16687 was 16643, checked in by simon04, 4 years ago

see #19334 - https://errorprone.info/bugpattern/StringSplitter

File size: 9.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets.items;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.text.NumberFormat;
10import java.text.ParseException;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.List;
14
15import javax.swing.AbstractButton;
16import javax.swing.BorderFactory;
17import javax.swing.ButtonGroup;
18import javax.swing.JButton;
19import javax.swing.JComponent;
20import javax.swing.JLabel;
21import javax.swing.JPanel;
22import javax.swing.JToggleButton;
23
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.Tag;
26import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
27import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
28import org.openstreetmap.josm.gui.widgets.JosmComboBox;
29import org.openstreetmap.josm.gui.widgets.JosmTextField;
30import org.openstreetmap.josm.spi.preferences.Config;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.Logging;
33import org.openstreetmap.josm.tools.Utils;
34
35/**
36 * Text field type.
37 */
38public class Text extends KeyedItem {
39
40 private static int auto_increment_selected; // NOSONAR
41
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 short 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(",", -1));
70 } else {
71 initAutoCompletionField(textField, key);
72 }
73 if (Config.getPref().getBoolean("taggingpreset.display-keys-as-hint", true)) {
74 textField.setHint(key);
75 }
76 if (length > 0) {
77 textField.setMaxChars((int) 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 Logging.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[0]));
109 comboBox.setEditable(true);
110 comboBox.setEditor(textField);
111 comboBox.getEditor().setItem(DIFFERENT);
112 value = comboBox;
113 originalValue = DIFFERENT;
114 }
115 initializeLocaleText(null);
116
117 // if there's an auto_increment setting, then wrap the text field
118 // into a panel, appending a number of buttons.
119 // auto_increment has a format like -2,-1,1,2
120 // the text box being the first component in the panel is relied
121 // on in a rather ugly fashion further down.
122 if (auto_increment != null) {
123 ButtonGroup bg = new ButtonGroup();
124 JPanel pnl = new JPanel(new GridBagLayout());
125 pnl.add(value, GBC.std().fill(GBC.HORIZONTAL));
126
127 // first, one button for each auto_increment value
128 for (final String ai : auto_increment.split(",", -1)) {
129 JToggleButton aibutton = new JToggleButton(ai);
130 aibutton.setToolTipText(tr("Select auto-increment of {0} for this field", ai));
131 aibutton.setMargin(new Insets(0, 0, 0, 0));
132 aibutton.setFocusable(false);
133 saveHorizontalSpace(aibutton);
134 bg.add(aibutton);
135 try {
136 // TODO there must be a better way to parse a number like "+3" than this.
137 final int buttonvalue = NumberFormat.getIntegerInstance().parse(ai.replace("+", "")).intValue();
138 if (auto_increment_selected == buttonvalue) aibutton.setSelected(true);
139 aibutton.addActionListener(e -> auto_increment_selected = buttonvalue);
140 pnl.add(aibutton, GBC.std());
141 } catch (ParseException ex) {
142 Logging.error("Cannot parse auto-increment value of '" + ai + "' into an integer");
143 }
144 }
145
146 // an invisible toggle button for "release" of the button group
147 final JToggleButton clearbutton = new JToggleButton("X");
148 clearbutton.setVisible(false);
149 clearbutton.setFocusable(false);
150 bg.add(clearbutton);
151 // and its visible counterpart. - this mechanism allows us to
152 // have *no* button selected after the X is clicked, instead
153 // of the X remaining selected
154 JButton releasebutton = new JButton("X");
155 releasebutton.setToolTipText(tr("Cancel auto-increment for this field"));
156 releasebutton.setMargin(new Insets(0, 0, 0, 0));
157 releasebutton.setFocusable(false);
158 releasebutton.addActionListener(e -> {
159 auto_increment_selected = 0;
160 clearbutton.setSelected(true);
161 });
162 saveHorizontalSpace(releasebutton);
163 pnl.add(releasebutton, GBC.eol());
164 value = pnl;
165 }
166 final JLabel label = new JLabel(locale_text + ':');
167 label.setToolTipText(getKeyTooltipText());
168 label.setComponentPopupMenu(getPopupMenu());
169 label.setLabelFor(value);
170 p.add(label, GBC.std().insets(0, 0, 10, 0));
171 p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
172 value.setToolTipText(getKeyTooltipText());
173 return true;
174 }
175
176 private static void saveHorizontalSpace(AbstractButton button) {
177 Insets insets = button.getBorder().getBorderInsets(button);
178 // Ensure the current look&feel does not waste horizontal space (as seen in Nimbus & Aqua)
179 if (insets != null && insets.left+insets.right > insets.top+insets.bottom) {
180 int min = Math.min(insets.top, insets.bottom);
181 button.setBorder(BorderFactory.createEmptyBorder(insets.top, min, insets.bottom, min));
182 }
183 }
184
185 private static String getValue(Component comp) {
186 if (comp instanceof JosmComboBox) {
187 return ((JosmComboBox<?>) comp).getEditor().getItem().toString();
188 } else if (comp instanceof JosmTextField) {
189 return ((JosmTextField) comp).getText();
190 } else if (comp instanceof JPanel) {
191 return getValue(((JPanel) comp).getComponent(0));
192 } else {
193 return null;
194 }
195 }
196
197 @Override
198 public void addCommands(List<Tag> changedTags) {
199
200 // return if unchanged
201 String v = getValue(value);
202 if (v == null) {
203 Logging.error("No 'last value' support for component " + value);
204 return;
205 }
206
207 v = Utils.removeWhiteSpaces(v);
208
209 if (!"false".equals(use_last_as_default) || auto_increment != null) {
210 LAST_VALUES.put(key, v);
211 }
212 if (v.equals(originalValue) || (originalValue == null && v.isEmpty()))
213 return;
214
215 changedTags.add(new Tag(key, v));
216 AutoCompletionManager.rememberUserInput(key, v, true);
217 }
218
219 @Override
220 public MatchType getDefaultMatch() {
221 return MatchType.NONE;
222 }
223
224 @Override
225 public Collection<String> getValues() {
226 if (default_ == null || default_.isEmpty())
227 return Collections.emptyList();
228 return Collections.singleton(default_);
229 }
230}
Note: See TracBrowser for help on using the repository browser.