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