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

Last change on this file since 10760 was 10717, checked in by simon04, 8 years ago

see #11390, see #12890 - Lambda can be replaced with method reference

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