source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java@ 12301

Last change on this file since 12301 was 12301, checked in by michael2402, 7 years ago

Add missing javadoc in the org.openstreetmap.josm.gui.dialogs package.

  • Property svn:eol-style set to native
File size: 11.9 KB
RevLine 
[6448]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
[7834]4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trc;
[6448]6
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.event.KeyEvent;
10import java.awt.event.WindowEvent;
11import java.awt.event.WindowListener;
[6771]12import java.util.Arrays;
[6448]13import java.util.Collection;
14import java.util.Collections;
[11105]15import java.util.EnumSet;
[6448]16import java.util.LinkedList;
17import java.util.List;
[10638]18import java.util.stream.Collectors;
[6448]19
[7834]20import javax.swing.BorderFactory;
21import javax.swing.GroupLayout;
22import javax.swing.JLabel;
23import javax.swing.JOptionPane;
24import javax.swing.JPanel;
25import javax.swing.KeyStroke;
26import javax.swing.border.EtchedBorder;
27import javax.swing.plaf.basic.BasicComboBoxEditor;
[6448]28
[7834]29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
31import org.openstreetmap.josm.data.osm.PrimitiveId;
32import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
33import org.openstreetmap.josm.gui.ExtendedDialog;
[10604]34import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
[7834]35import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
36import org.openstreetmap.josm.gui.widgets.HtmlPanel;
37import org.openstreetmap.josm.gui.widgets.JosmTextField;
38import org.openstreetmap.josm.gui.widgets.OsmIdTextField;
39import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox;
40import org.openstreetmap.josm.tools.Utils;
41
[6448]42/**
43 * Dialog prompt to user to let him choose OSM primitives by specifying their type and IDs.
44 * @since 6448, split from DownloadObjectDialog
45 */
46public class OsmIdSelectionDialog extends ExtendedDialog implements WindowListener {
47
48 protected final JPanel panel = new JPanel();
49 protected final OsmPrimitiveTypesComboBox cbType = new OsmPrimitiveTypesComboBox();
50 protected final OsmIdTextField tfId = new OsmIdTextField();
51 protected final HistoryComboBox cbId = new HistoryComboBox();
[8308]52 protected final transient GroupLayout layout = new GroupLayout(panel);
[6448]53
[12301]54 /**
55 * Creates a new OsmIdSelectionDialog
56 * @param parent The parent element that will be used for position and maximum size
57 * @param title The text that will be shown in the window titlebar
58 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
59 */
[11747]60 public OsmIdSelectionDialog(Component parent, String title, String... buttonTexts) {
[6448]61 super(parent, title, buttonTexts);
62 }
63
[12301]64 /**
65 * Creates a new OsmIdSelectionDialog
66 * @param parent The parent element that will be used for position and maximum size
67 * @param title The text that will be shown in the window titlebar
68 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
69 * @param modal Set it to {@code true} if you want the dialog to be modal
70 */
[6448]71 public OsmIdSelectionDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
72 super(parent, title, buttonTexts, modal);
73 }
74
[12301]75 /**
76 * Creates a new OsmIdSelectionDialog
77 * @param parent The parent element that will be used for position and maximum size
78 * @param title The text that will be shown in the window titlebar
79 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
80 * @param modal Set it to {@code true} if you want the dialog to be modal
81 * @param disposeOnClose whether to call {@link #dispose} when closing the dialog
82 */
[6448]83 public OsmIdSelectionDialog(Component parent, String title, String[] buttonTexts, boolean modal, boolean disposeOnClose) {
84 super(parent, title, buttonTexts, modal, disposeOnClose);
85 }
86
87 protected void init() {
88 panel.setLayout(layout);
89 layout.setAutoCreateGaps(true);
90 layout.setAutoCreateContainerGaps(true);
91
92 JLabel lbl1 = new JLabel(tr("Object type:"));
[8426]93 lbl1.setLabelFor(cbType);
[6448]94
95 cbType.addItem(trc("osm object types", "mixed"));
96 cbType.setToolTipText(tr("Choose the OSM object type"));
97 JLabel lbl2 = new JLabel(tr("Object ID:"));
[8426]98 lbl2.setLabelFor(cbId);
[6448]99
100 cbId.setEditor(new BasicComboBoxEditor() {
101 @Override
102 protected JosmTextField createEditorComponent() {
103 return tfId;
104 }
105 });
106 cbId.setToolTipText(tr("Enter the ID of the object that should be downloaded"));
107 restorePrimitivesHistory(cbId);
108
109 // forward the enter key stroke to the download button
110 tfId.getKeymap().removeKeyStrokeBinding(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false));
111 tfId.setPreferredSize(new Dimension(400, tfId.getPreferredSize().height));
112
[8496]113 final String help1 = /* I18n: {0} and contains example strings not meant for translation. */
114 tr("Object IDs can be separated by comma or space, for instance: {0}",
115 "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("1 2 5", "1,2,5")) + "</b>");
116 final String help2 = /* I18n: {0} and contains example strings not meant for translation. {1}=n, {2}=w, {3}=r. */
117 tr("In mixed mode, specify objects like this: {0}<br/>"
118 + "({1} stands for <i>node</i>, {2} for <i>way</i>, and {3} for <i>relation</i>)",
119 "<b>w123, n110, w12, r15</b>", "<b>n</b>", "<b>w</b>", "<b>r</b>");
120 final String help3 = /* I18n: {0} and contains example strings not meant for translation. */
121 tr("Ranges of object IDs are specified with a hyphen, for instance: {0}",
122 "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("w1-5", "n30-37", "r501-5")) + "</b>");
123 HtmlPanel help = new HtmlPanel(help1 + "<br/>" + help2 + "<br/><br/>" + help3);
[6448]124 help.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
125
[10611]126 cbType.addItemListener(e -> {
127 tfId.setType(cbType.getType());
128 tfId.performValidation();
[6448]129 });
130
131 final GroupLayout.SequentialGroup sequentialGroup = layout.createSequentialGroup()
132 .addGroup(layout.createParallelGroup()
133 .addComponent(lbl1)
134 .addComponent(cbType, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
135 .addGroup(layout.createParallelGroup()
136 .addComponent(lbl2)
137 .addComponent(cbId, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE));
138
139 final GroupLayout.ParallelGroup parallelGroup = layout.createParallelGroup()
140 .addGroup(layout.createSequentialGroup()
141 .addGroup(layout.createParallelGroup()
142 .addComponent(lbl1)
143 .addComponent(lbl2)
144 )
145 .addGroup(layout.createParallelGroup()
146 .addComponent(cbType)
147 .addComponent(cbId))
148 );
149
150 for (Component i : getComponentsBeforeHelp()) {
151 sequentialGroup.addComponent(i);
152 parallelGroup.addComponent(i);
153 }
154
155 layout.setVerticalGroup(sequentialGroup.addComponent(help));
156 layout.setHorizontalGroup(parallelGroup.addComponent(help));
157 }
158
159 /**
160 * Let subclasses add custom components between the id input field and the help text
161 * @return the collections to add
162 */
163 protected Collection<Component> getComponentsBeforeHelp() {
164 return Collections.emptySet();
165 }
166
167 /**
168 * Allows subclasses to specify a different continue button index. If this button is pressed, the history is updated.
169 * @return the button index
170 */
171 public int getContinueButtonIndex() {
172 return 1;
173 }
174
175 /**
176 * Restore the current history from the preferences
177 *
178 * @param cbHistory the {@link HistoryComboBox} to which the history is restored to
179 */
180 protected void restorePrimitivesHistory(HistoryComboBox cbHistory) {
[9972]181 List<String> cmtHistory = new LinkedList<>(
[8509]182 Main.pref.getCollection(getClass().getName() + ".primitivesHistory", new LinkedList<String>()));
[6448]183 // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
184 Collections.reverse(cmtHistory);
185 cbHistory.setPossibleItems(cmtHistory);
186 }
187
188 /**
189 * Remind the current history in the preferences
190 *
191 * @param cbHistory the {@link HistoryComboBox} of which to restore the history
192 */
193 protected void remindPrimitivesHistory(HistoryComboBox cbHistory) {
194 cbHistory.addCurrentItemToHistory();
195 Main.pref.putCollection(getClass().getName() + ".primitivesHistory", cbHistory.getHistory());
196 }
197
198 /**
199 * Gets the requested OSM object IDs.
200 *
201 * @return The list of requested OSM object IDs
202 */
203 public final List<PrimitiveId> getOsmIds() {
204 return tfId.getIds();
205 }
206
207 @Override
208 public void setupDialog() {
209 setContent(panel, false);
210 cbType.setSelectedIndex(Main.pref.getInteger("downloadprimitive.lasttype", 0));
211 tfId.setType(cbType.getType());
212 if (Main.pref.getBoolean("downloadprimitive.autopaste", true)) {
213 tryToPasteFromClipboard(tfId, cbType);
214 }
215 setDefaultButton(getContinueButtonIndex());
216 addWindowListener(this);
217 super.setupDialog();
218 }
219
220 protected void tryToPasteFromClipboard(OsmIdTextField tfId, OsmPrimitiveTypesComboBox cbType) {
[10604]221 String buf = ClipboardUtils.getClipboardStringContent();
[7834]222 if (buf == null || buf.isEmpty()) return;
[6816]223 if (buf.length() > Main.pref.getInteger("downloadprimitive.max-autopaste-length", 2000)) return;
224 final List<SimplePrimitiveId> ids = SimplePrimitiveId.fuzzyParse(buf);
225 if (!ids.isEmpty()) {
[10638]226 final String parsedText = ids.stream().map(x -> x.getType().getAPIName().charAt(0) + String.valueOf(x.getUniqueId()))
227 .collect(Collectors.joining(", "));
[6816]228 tfId.tryToPasteFrom(parsedText);
[11105]229 final EnumSet<OsmPrimitiveType> types = ids.stream().map(SimplePrimitiveId::getType).collect(
230 Collectors.toCollection(() -> EnumSet.noneOf(OsmPrimitiveType.class)));
[6816]231 if (types.size() == 1) {
232 // select corresponding type
233 cbType.setSelectedItem(types.iterator().next());
[6448]234 } else {
[6816]235 // select "mixed"
236 cbType.setSelectedIndex(3);
[6448]237 }
[7509]238 } else if (buf.matches("[\\d,v\\s]+")) {
239 //fallback solution for id1,id2,id3 format
[6816]240 tfId.tryToPasteFrom(buf);
[6448]241 }
242 }
243
244 @Override public void windowClosed(WindowEvent e) {
245 if (e != null && e.getComponent() == this && getValue() == getContinueButtonIndex()) {
246 Main.pref.putInteger("downloadprimitive.lasttype", cbType.getSelectedIndex());
247
248 if (!tfId.readIds()) {
249 JOptionPane.showMessageDialog(getParent(),
250 tr("Invalid ID list specified\n"
251 + "Cannot continue."),
252 tr("Information"),
253 JOptionPane.INFORMATION_MESSAGE
254 );
255 return;
256 }
257
258 remindPrimitivesHistory(cbId);
259 }
260 }
261
[10173]262 @Override public void windowOpened(WindowEvent e) {
263 // Do nothing
264 }
[8510]265
[10173]266 @Override public void windowClosing(WindowEvent e) {
267 // Do nothing
268 }
[8510]269
[10173]270 @Override public void windowIconified(WindowEvent e) {
271 // Do nothing
272 }
[8510]273
[10173]274 @Override public void windowDeiconified(WindowEvent e) {
275 // Do nothing
276 }
[8510]277
[10173]278 @Override public void windowActivated(WindowEvent e) {
279 // Do nothing
280 }
[8510]281
[10173]282 @Override public void windowDeactivated(WindowEvent e) {
283 // Do nothing
284 }
[6448]285}
Note: See TracBrowser for help on using the repository browser.