1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
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.ItemEvent;
|
---|
10 | import java.awt.event.ItemListener;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.event.WindowEvent;
|
---|
13 | import java.awt.event.WindowListener;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import javax.swing.BorderFactory;
|
---|
19 | import javax.swing.GroupLayout;
|
---|
20 | import javax.swing.JCheckBox;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JTextField;
|
---|
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.gui.ExtendedDialog;
|
---|
33 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
34 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
35 | import org.openstreetmap.josm.gui.widgets.OsmIdTextField;
|
---|
36 | import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox;
|
---|
37 | import org.openstreetmap.josm.tools.Utils;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Dialog prompt to user to let him choose OSM primitives to download by specifying their type and IDs
|
---|
41 | * @since 5765
|
---|
42 | */
|
---|
43 | public class DownloadObjectDialog extends ExtendedDialog implements WindowListener {
|
---|
44 |
|
---|
45 | protected final JPanel panel = new JPanel();
|
---|
46 | protected final OsmPrimitiveTypesComboBox cbType = new OsmPrimitiveTypesComboBox();
|
---|
47 | protected final OsmIdTextField tfId = new OsmIdTextField();
|
---|
48 | protected final HistoryComboBox cbId = new HistoryComboBox();
|
---|
49 |
|
---|
50 | protected final JCheckBox referrers = new JCheckBox(tr("Download referrers (parent relations)"));
|
---|
51 | protected final JCheckBox fullRel = new JCheckBox(tr("Download relation members"));
|
---|
52 | protected final JCheckBox newLayer = new JCheckBox(tr("Separate Layer"));
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Constructs a new DownloadObjectDialog with Main.parent as parent component.
|
---|
56 | */
|
---|
57 | public DownloadObjectDialog() {
|
---|
58 | this(Main.parent);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructs a new DownloadObjectDialog.
|
---|
63 | * @param parent The parent component
|
---|
64 | */
|
---|
65 | public DownloadObjectDialog(Component parent) {
|
---|
66 | super(parent, tr("Download object"), new String[] {tr("Download object"), tr("Cancel")});
|
---|
67 | init();
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected void init() {
|
---|
71 | GroupLayout layout = new GroupLayout(panel);
|
---|
72 | panel.setLayout(layout);
|
---|
73 | layout.setAutoCreateGaps(true);
|
---|
74 | layout.setAutoCreateContainerGaps(true);
|
---|
75 |
|
---|
76 | JLabel lbl1 = new JLabel(tr("Object type:"));
|
---|
77 |
|
---|
78 | cbType.addItem(trc("osm object types", "mixed"));
|
---|
79 | cbType.setToolTipText(tr("Choose the OSM object type"));
|
---|
80 | JLabel lbl2 = new JLabel(tr("Object ID:"));
|
---|
81 |
|
---|
82 | cbId.setEditor(new BasicComboBoxEditor() {
|
---|
83 | @Override
|
---|
84 | protected JTextField createEditorComponent() {
|
---|
85 | return tfId;
|
---|
86 | }
|
---|
87 | });
|
---|
88 | cbId.setToolTipText(tr("Enter the ID of the object that should be downloaded"));
|
---|
89 | restorePrimitivesHistory(cbId);
|
---|
90 |
|
---|
91 | // forward the enter key stroke to the download button
|
---|
92 | tfId.getKeymap().removeKeyStrokeBinding(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false));
|
---|
93 | tfId.setPreferredSize(new Dimension(400, tfId.getPreferredSize().height));
|
---|
94 |
|
---|
95 | newLayer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
|
---|
96 | newLayer.setSelected(Main.pref.getBoolean("download.newlayer"));
|
---|
97 |
|
---|
98 | referrers.setToolTipText(tr("Select if the referrers of the object should be downloaded as well, i.e.,"
|
---|
99 | + "parent relations and for nodes, additionally, parent ways"));
|
---|
100 | referrers.setSelected(Main.pref.getBoolean("downloadprimitive.referrers", true));
|
---|
101 |
|
---|
102 | fullRel.setToolTipText(tr("Select if the members of a relation should be downloaded as well"));
|
---|
103 | fullRel.setSelected(Main.pref.getBoolean("downloadprimitive.full", true));
|
---|
104 |
|
---|
105 | HtmlPanel help = new HtmlPanel(tr("Object IDs can be separated by comma or space.<br/>"
|
---|
106 | + " Examples: <b><ul><li>1 2 5</li><li>1,2,5</li></ul><br/></b>"
|
---|
107 | + " In mixed mode, specify objects like this: <b>w123, n110, w12, r15</b><br/>"));
|
---|
108 | help.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
|
---|
109 |
|
---|
110 | layout.setVerticalGroup(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 | .addComponent(referrers)
|
---|
118 | .addComponent(fullRel)
|
---|
119 | .addComponent(newLayer)
|
---|
120 | .addComponent(help)
|
---|
121 | );
|
---|
122 |
|
---|
123 | cbType.addItemListener(new ItemListener() {
|
---|
124 | @Override
|
---|
125 | public void itemStateChanged(ItemEvent e) {
|
---|
126 | tfId.setType(cbType.getType());
|
---|
127 | tfId.performValidation();
|
---|
128 | referrers.setText(cbType.getType() == OsmPrimitiveType.NODE
|
---|
129 | ? tr("Download referrers (parent relations and ways)")
|
---|
130 | : tr("Download referrers (parent relations)"));
|
---|
131 | }
|
---|
132 | });
|
---|
133 |
|
---|
134 | layout.setHorizontalGroup(layout.createParallelGroup()
|
---|
135 | .addGroup(layout.createSequentialGroup()
|
---|
136 | .addGroup(layout.createParallelGroup()
|
---|
137 | .addComponent(lbl1)
|
---|
138 | .addComponent(lbl2)
|
---|
139 | )
|
---|
140 | .addGroup(layout.createParallelGroup()
|
---|
141 | .addComponent(cbType)
|
---|
142 | .addComponent(cbId))
|
---|
143 | )
|
---|
144 | .addComponent(referrers)
|
---|
145 | .addComponent(fullRel)
|
---|
146 | .addComponent(newLayer)
|
---|
147 | .addComponent(help)
|
---|
148 | );
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public void setupDialog() {
|
---|
153 |
|
---|
154 | setContent(panel, false);
|
---|
155 | setButtonIcons(new String[] {"download.png", "cancel.png"});
|
---|
156 | setToolTipTexts(new String[] {
|
---|
157 | tr("Start downloading"),
|
---|
158 | tr("Close dialog and cancel downloading")
|
---|
159 | });
|
---|
160 | setDefaultButton(1);
|
---|
161 | configureContextsensitiveHelp("/Action/DownloadObject", true /* show help button */);
|
---|
162 | cbType.setSelectedIndex(Main.pref.getInteger("downloadprimitive.lasttype", 0));
|
---|
163 | tfId.setType(cbType.getType());
|
---|
164 | if (Main.pref.getBoolean("downloadprimitive.autopaste", true)) {
|
---|
165 | tryToPasteFromClipboard(tfId, cbType);
|
---|
166 | }
|
---|
167 |
|
---|
168 | addWindowListener(this);
|
---|
169 | super.setupDialog();
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Restore the current history from the preferences
|
---|
174 | *
|
---|
175 | * @param cbHistory
|
---|
176 | */
|
---|
177 | protected void restorePrimitivesHistory(HistoryComboBox cbHistory) {
|
---|
178 | List<String> cmtHistory = new LinkedList<String>(Main.pref.getCollection(getClass().getName() + ".primitivesHistory", new LinkedList<String>()));
|
---|
179 | // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
|
---|
180 | Collections.reverse(cmtHistory);
|
---|
181 | cbHistory.setPossibleItems(cmtHistory);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Remind the current history in the preferences
|
---|
186 | * @param cbHistory
|
---|
187 | */
|
---|
188 | protected void remindPrimitivesHistory(HistoryComboBox cbHistory) {
|
---|
189 | cbHistory.addCurrentItemToHistory();
|
---|
190 | Main.pref.putCollection(getClass().getName() + ".primitivesHistory", cbHistory.getHistory());
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected void tryToPasteFromClipboard(OsmIdTextField tfId, OsmPrimitiveTypesComboBox cbType) {
|
---|
194 | String buf = Utils.getClipboardContent();
|
---|
195 | if (buf != null) {
|
---|
196 | if (buf.contains("node")) cbType.setSelectedIndex(0);
|
---|
197 | if (buf.contains("way")) cbType.setSelectedIndex(1);
|
---|
198 | if (buf.contains("relation")) cbType.setSelectedIndex(2);
|
---|
199 | String[] res = buf.split("/");
|
---|
200 | String txt;
|
---|
201 | if (res.length>0) {
|
---|
202 | txt = res[res.length-1];
|
---|
203 | if (txt.isEmpty() && txt.length()>1) txt=res[res.length-2];
|
---|
204 | } else {
|
---|
205 | txt=buf;
|
---|
206 | }
|
---|
207 | if (buf.length() <= Main.pref.getInteger("downloadprimitive.max-autopaste-length", 2000)) {
|
---|
208 | tfId.tryToPasteFrom(txt);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Determines if a new layer has been requested.
|
---|
215 | * @return true if a new layer has been requested, false otherwise
|
---|
216 | */
|
---|
217 | public final boolean isNewLayerRequested() {
|
---|
218 | return newLayer.isSelected();
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Determines if relation members have been requested.
|
---|
223 | * @return true if relation members have been requested, false otherwise
|
---|
224 | */
|
---|
225 | public final boolean isFullRelationRequested() {
|
---|
226 | return fullRel.isSelected();
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Determines if referrers have been requested.
|
---|
231 | * @return true if referrers have been requested, false otherwise
|
---|
232 | */
|
---|
233 | public final boolean isReferrersRequested() {
|
---|
234 | return referrers.isSelected();
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Gets the requested OSM object IDs.
|
---|
239 | * @return The list of requested OSM object IDs
|
---|
240 | */
|
---|
241 | public final List<PrimitiveId> getOsmIds() {
|
---|
242 | return tfId.getIds();
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Override public void windowClosed(WindowEvent e) {
|
---|
246 | if (e != null && e.getComponent() == this && getValue() == 1) {
|
---|
247 | Main.pref.putInteger("downloadprimitive.lasttype", cbType.getSelectedIndex());
|
---|
248 | Main.pref.put("downloadprimitive.referrers", referrers.isSelected());
|
---|
249 | Main.pref.put("downloadprimitive.full", fullRel.isSelected());
|
---|
250 | Main.pref.put("download.newlayer", newLayer.isSelected());
|
---|
251 |
|
---|
252 | if (!tfId.readIds()) {
|
---|
253 | JOptionPane.showMessageDialog(getParent(),
|
---|
254 | tr("Invalid ID list specified\n"
|
---|
255 | + "Cannot download object."),
|
---|
256 | tr("Information"),
|
---|
257 | JOptionPane.INFORMATION_MESSAGE
|
---|
258 | );
|
---|
259 | return;
|
---|
260 | }
|
---|
261 |
|
---|
262 | remindPrimitivesHistory(cbId);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override public void windowOpened(WindowEvent e) {}
|
---|
267 | @Override public void windowClosing(WindowEvent e) {}
|
---|
268 | @Override public void windowIconified(WindowEvent e) {}
|
---|
269 | @Override public void windowDeiconified(WindowEvent e) {}
|
---|
270 | @Override public void windowActivated(WindowEvent e) {}
|
---|
271 | @Override public void windowDeactivated(WindowEvent e) {}
|
---|
272 | }
|
---|