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