1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.GridBagConstraints;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.ComponentAdapter;
|
---|
14 | import java.awt.event.ComponentEvent;
|
---|
15 | import java.beans.PropertyChangeEvent;
|
---|
16 | import java.beans.PropertyChangeListener;
|
---|
17 | import java.text.DateFormat;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.Date;
|
---|
20 | import java.util.HashSet;
|
---|
21 | import java.util.Set;
|
---|
22 |
|
---|
23 | import javax.swing.AbstractAction;
|
---|
24 | import javax.swing.BorderFactory;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JToolBar;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
32 | import org.openstreetmap.josm.actions.downloadtasks.ChangesetHeaderDownloadTask;
|
---|
33 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
34 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
35 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
36 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
37 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
38 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
39 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
40 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
41 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
42 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
43 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
44 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
45 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
46 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
47 | import org.openstreetmap.josm.tools.Utils;
|
---|
48 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * This panel displays the properties of the currently selected changeset in the
|
---|
52 | * {@link ChangesetCacheManager}.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | public class ChangesetDetailPanel extends JPanel implements PropertyChangeListener, ChangesetAware {
|
---|
56 |
|
---|
57 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
58 | private final JosmTextField tfID = new JosmTextField(10);
|
---|
59 | private final JosmTextArea taComment = new JosmTextArea(5, 40);
|
---|
60 | private final JosmTextField tfOpen = new JosmTextField(10);
|
---|
61 | private final JosmTextField tfUser = new JosmTextField("");
|
---|
62 | private final JosmTextField tfCreatedOn = new JosmTextField(20);
|
---|
63 | private final JosmTextField tfClosedOn = new JosmTextField(20);
|
---|
64 |
|
---|
65 | private final DownloadChangesetContentAction actDownloadChangesetContent = new DownloadChangesetContentAction(this);
|
---|
66 | private final UpdateChangesetAction actUpdateChangesets = new UpdateChangesetAction();
|
---|
67 | private final RemoveFromCacheAction actRemoveFromCache = new RemoveFromCacheAction();
|
---|
68 | private final SelectInCurrentLayerAction actSelectInCurrentLayer = new SelectInCurrentLayerAction();
|
---|
69 | private final ZoomInCurrentLayerAction actZoomInCurrentLayerAction = new ZoomInCurrentLayerAction();
|
---|
70 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
71 |
|
---|
72 | private transient Changeset currentChangeset;
|
---|
73 |
|
---|
74 | protected JPanel buildActionButtonPanel() {
|
---|
75 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
76 |
|
---|
77 | JToolBar tb = new JToolBar(JToolBar.VERTICAL);
|
---|
78 | tb.setFloatable(false);
|
---|
79 |
|
---|
80 | // -- remove from cache action
|
---|
81 | tb.add(actRemoveFromCache);
|
---|
82 | actRemoveFromCache.initProperties(currentChangeset);
|
---|
83 |
|
---|
84 | // -- changeset update
|
---|
85 | tb.add(actUpdateChangesets);
|
---|
86 | actUpdateChangesets.initProperties(currentChangeset);
|
---|
87 |
|
---|
88 | // -- changeset content download
|
---|
89 | tb.add(actDownloadChangesetContent);
|
---|
90 | actDownloadChangesetContent.initProperties();
|
---|
91 |
|
---|
92 | tb.add(actSelectInCurrentLayer);
|
---|
93 | Main.getLayerManager().addActiveLayerChangeListener(actSelectInCurrentLayer);
|
---|
94 |
|
---|
95 | tb.add(actZoomInCurrentLayerAction);
|
---|
96 | Main.getLayerManager().addActiveLayerChangeListener(actZoomInCurrentLayerAction);
|
---|
97 |
|
---|
98 | addComponentListener(
|
---|
99 | new ComponentAdapter() {
|
---|
100 | @Override
|
---|
101 | public void componentShown(ComponentEvent e) {
|
---|
102 | Main.getLayerManager().addAndFireActiveLayerChangeListener(actSelectInCurrentLayer);
|
---|
103 | Main.getLayerManager().addAndFireActiveLayerChangeListener(actZoomInCurrentLayerAction);
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void componentHidden(ComponentEvent e) {
|
---|
108 | // make sure the listener is unregistered when the panel becomes invisible
|
---|
109 | Main.getLayerManager().removeActiveLayerChangeListener(actSelectInCurrentLayer);
|
---|
110 | Main.getLayerManager().removeActiveLayerChangeListener(actZoomInCurrentLayerAction);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | );
|
---|
114 |
|
---|
115 | pnl.add(tb);
|
---|
116 | return pnl;
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected JPanel buildDetailViewPanel() {
|
---|
120 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
121 |
|
---|
122 | GridBagConstraints gc = new GridBagConstraints();
|
---|
123 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
124 | gc.insets = new Insets(0, 0, 2, 3);
|
---|
125 |
|
---|
126 | //-- id
|
---|
127 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
128 | gc.weightx = 0.0;
|
---|
129 | pnl.add(new JLabel(tr("ID:")), gc);
|
---|
130 |
|
---|
131 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
132 | gc.weightx = 0.0;
|
---|
133 | gc.gridx = 1;
|
---|
134 | pnl.add(tfID, gc);
|
---|
135 | tfID.setEditable(false);
|
---|
136 |
|
---|
137 | //-- comment
|
---|
138 | gc.gridx = 0;
|
---|
139 | gc.gridy = 1;
|
---|
140 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
141 | gc.weightx = 0.0;
|
---|
142 | pnl.add(new JLabel(tr("Comment:")), gc);
|
---|
143 |
|
---|
144 | gc.fill = GridBagConstraints.BOTH;
|
---|
145 | gc.weightx = 1.0;
|
---|
146 | gc.weighty = 1.0;
|
---|
147 | gc.gridx = 1;
|
---|
148 | pnl.add(taComment, gc);
|
---|
149 | taComment.setEditable(false);
|
---|
150 |
|
---|
151 | //-- Open/Closed
|
---|
152 | gc.gridx = 0;
|
---|
153 | gc.gridy = 2;
|
---|
154 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
155 | gc.weightx = 0.0;
|
---|
156 | gc.weighty = 0.0;
|
---|
157 | pnl.add(new JLabel(tr("Open/Closed:")), gc);
|
---|
158 |
|
---|
159 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
160 | gc.gridx = 1;
|
---|
161 | pnl.add(tfOpen, gc);
|
---|
162 | tfOpen.setEditable(false);
|
---|
163 |
|
---|
164 | //-- Created by:
|
---|
165 | gc.gridx = 0;
|
---|
166 | gc.gridy = 3;
|
---|
167 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
168 | gc.weightx = 0.0;
|
---|
169 | pnl.add(new JLabel(tr("Created by:")), gc);
|
---|
170 |
|
---|
171 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
172 | gc.weightx = 1.0;
|
---|
173 | gc.gridx = 1;
|
---|
174 | pnl.add(tfUser, gc);
|
---|
175 | tfUser.setEditable(false);
|
---|
176 |
|
---|
177 | //-- Created On:
|
---|
178 | gc.gridx = 0;
|
---|
179 | gc.gridy = 4;
|
---|
180 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
181 | gc.weightx = 0.0;
|
---|
182 | pnl.add(new JLabel(tr("Created on:")), gc);
|
---|
183 |
|
---|
184 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
185 | gc.gridx = 1;
|
---|
186 | pnl.add(tfCreatedOn, gc);
|
---|
187 | tfCreatedOn.setEditable(false);
|
---|
188 |
|
---|
189 | //-- Closed On:
|
---|
190 | gc.gridx = 0;
|
---|
191 | gc.gridy = 5;
|
---|
192 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
193 | gc.weightx = 0.0;
|
---|
194 | pnl.add(new JLabel(tr("Closed on:")), gc);
|
---|
195 |
|
---|
196 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
197 | gc.gridx = 1;
|
---|
198 | pnl.add(tfClosedOn, gc);
|
---|
199 | tfClosedOn.setEditable(false);
|
---|
200 |
|
---|
201 | return pnl;
|
---|
202 | }
|
---|
203 |
|
---|
204 | protected final void build() {
|
---|
205 | setLayout(new BorderLayout());
|
---|
206 | setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
---|
207 | add(buildDetailViewPanel(), BorderLayout.CENTER);
|
---|
208 | add(buildActionButtonPanel(), BorderLayout.WEST);
|
---|
209 | }
|
---|
210 |
|
---|
211 | protected void clearView() {
|
---|
212 | tfID.setText("");
|
---|
213 | taComment.setText("");
|
---|
214 | tfOpen.setText("");
|
---|
215 | tfUser.setText("");
|
---|
216 | tfCreatedOn.setText("");
|
---|
217 | tfClosedOn.setText("");
|
---|
218 | }
|
---|
219 |
|
---|
220 | protected void updateView(Changeset cs) {
|
---|
221 | String msg;
|
---|
222 | if (cs == null) return;
|
---|
223 | tfID.setText(Integer.toString(cs.getId()));
|
---|
224 | taComment.setText(cs.getComment());
|
---|
225 |
|
---|
226 | if (cs.isOpen()) {
|
---|
227 | msg = trc("changeset.state", "Open");
|
---|
228 | } else {
|
---|
229 | msg = trc("changeset.state", "Closed");
|
---|
230 | }
|
---|
231 | tfOpen.setText(msg);
|
---|
232 |
|
---|
233 | if (cs.getUser() == null) {
|
---|
234 | msg = tr("anonymous");
|
---|
235 | } else {
|
---|
236 | msg = cs.getUser().getName();
|
---|
237 | }
|
---|
238 | tfUser.setText(msg);
|
---|
239 | DateFormat sdf = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.SHORT);
|
---|
240 |
|
---|
241 | Date createdDate = cs.getCreatedAt();
|
---|
242 | Date closedDate = cs.getClosedAt();
|
---|
243 | tfCreatedOn.setText(createdDate == null ? "" : sdf.format(createdDate));
|
---|
244 | tfClosedOn.setText(closedDate == null ? "" : sdf.format(closedDate));
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Constructs a new {@code ChangesetDetailPanel}.
|
---|
249 | */
|
---|
250 | public ChangesetDetailPanel() {
|
---|
251 | build();
|
---|
252 | }
|
---|
253 |
|
---|
254 | protected void setCurrentChangeset(Changeset cs) {
|
---|
255 | currentChangeset = cs;
|
---|
256 | if (cs == null) {
|
---|
257 | clearView();
|
---|
258 | } else {
|
---|
259 | updateView(cs);
|
---|
260 | }
|
---|
261 | actDownloadChangesetContent.initProperties();
|
---|
262 | actUpdateChangesets.initProperties(currentChangeset);
|
---|
263 | actRemoveFromCache.initProperties(currentChangeset);
|
---|
264 | actSelectInCurrentLayer.updateEnabledState();
|
---|
265 | actZoomInCurrentLayerAction.updateEnabledState();
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* ---------------------------------------------------------------------------- */
|
---|
269 | /* interface PropertyChangeListener */
|
---|
270 | /* ---------------------------------------------------------------------------- */
|
---|
271 | @Override
|
---|
272 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
273 | if (!evt.getPropertyName().equals(ChangesetCacheManagerModel.CHANGESET_IN_DETAIL_VIEW_PROP))
|
---|
274 | return;
|
---|
275 | setCurrentChangeset((Changeset) evt.getNewValue());
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * The action for removing the currently selected changeset from the changeset cache
|
---|
280 | */
|
---|
281 | class RemoveFromCacheAction extends AbstractAction {
|
---|
282 | RemoveFromCacheAction() {
|
---|
283 | putValue(NAME, tr("Remove from cache"));
|
---|
284 | new ImageProvider("dialogs", "delete").getResource().attachImageIcon(this);
|
---|
285 | putValue(SHORT_DESCRIPTION, tr("Remove the changeset in the detail view panel from the local cache"));
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public void actionPerformed(ActionEvent evt) {
|
---|
290 | if (currentChangeset == null)
|
---|
291 | return;
|
---|
292 | ChangesetCache.getInstance().remove(currentChangeset);
|
---|
293 | }
|
---|
294 |
|
---|
295 | public void initProperties(Changeset cs) {
|
---|
296 | setEnabled(cs != null);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Updates the current changeset from the OSM server
|
---|
302 | *
|
---|
303 | */
|
---|
304 | class UpdateChangesetAction extends AbstractAction {
|
---|
305 | UpdateChangesetAction() {
|
---|
306 | putValue(NAME, tr("Update changeset"));
|
---|
307 | new ImageProvider("dialogs/changeset", "updatechangesetcontent").getResource().attachImageIcon(this);
|
---|
308 | putValue(SHORT_DESCRIPTION, tr("Update the changeset from the OSM server"));
|
---|
309 | }
|
---|
310 |
|
---|
311 | @Override
|
---|
312 | public void actionPerformed(ActionEvent evt) {
|
---|
313 | if (currentChangeset == null)
|
---|
314 | return;
|
---|
315 | ChangesetHeaderDownloadTask task = new ChangesetHeaderDownloadTask(
|
---|
316 | ChangesetDetailPanel.this,
|
---|
317 | Collections.singleton(currentChangeset.getId())
|
---|
318 | );
|
---|
319 | MainApplication.worker.submit(new PostDownloadHandler(task, task.download()));
|
---|
320 | }
|
---|
321 |
|
---|
322 | public void initProperties(Changeset cs) {
|
---|
323 | setEnabled(cs != null && !Main.isOffline(OnlineResource.OSM_API));
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Selects the primitives in the content of this changeset in the current data layer.
|
---|
329 | *
|
---|
330 | */
|
---|
331 | class SelectInCurrentLayerAction extends AbstractAction implements ActiveLayerChangeListener {
|
---|
332 |
|
---|
333 | SelectInCurrentLayerAction() {
|
---|
334 | putValue(NAME, tr("Select in layer"));
|
---|
335 | new ImageProvider("dialogs", "select").getResource().attachImageIcon(this);
|
---|
336 | putValue(SHORT_DESCRIPTION, tr("Select the primitives in the content of this changeset in the current data layer"));
|
---|
337 | updateEnabledState();
|
---|
338 | }
|
---|
339 |
|
---|
340 | protected void alertNoPrimitivesToSelect() {
|
---|
341 | HelpAwareOptionPane.showOptionDialog(
|
---|
342 | ChangesetDetailPanel.this,
|
---|
343 | tr("<html>None of the objects in the content of changeset {0} is available in the current<br>"
|
---|
344 | + "edit layer ''{1}''.</html>",
|
---|
345 | currentChangeset.getId(),
|
---|
346 | Utils.escapeReservedCharactersHTML(Main.getLayerManager().getEditLayer().getName())
|
---|
347 | ),
|
---|
348 | tr("Nothing to select"),
|
---|
349 | JOptionPane.WARNING_MESSAGE,
|
---|
350 | HelpUtil.ht("/Dialog/ChangesetCacheManager#NothingToSelectInLayer")
|
---|
351 | );
|
---|
352 | }
|
---|
353 |
|
---|
354 | @Override
|
---|
355 | public void actionPerformed(ActionEvent arg0) {
|
---|
356 | if (!isEnabled())
|
---|
357 | return;
|
---|
358 | OsmDataLayer layer = Main.getLayerManager().getEditLayer();
|
---|
359 | if (layer == null) {
|
---|
360 | return;
|
---|
361 | }
|
---|
362 | Set<OsmPrimitive> target = new HashSet<>();
|
---|
363 | for (OsmPrimitive p: layer.data.allPrimitives()) {
|
---|
364 | if (p.isUsable() && p.getChangesetId() == currentChangeset.getId()) {
|
---|
365 | target.add(p);
|
---|
366 | }
|
---|
367 | }
|
---|
368 | if (target.isEmpty()) {
|
---|
369 | alertNoPrimitivesToSelect();
|
---|
370 | return;
|
---|
371 | }
|
---|
372 | layer.data.setSelected(target);
|
---|
373 | }
|
---|
374 |
|
---|
375 | public void updateEnabledState() {
|
---|
376 | setEnabled(Main.getLayerManager().getEditLayer() != null && currentChangeset != null);
|
---|
377 | }
|
---|
378 |
|
---|
379 | @Override
|
---|
380 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
381 | updateEnabledState();
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Zooms to the primitives in the content of this changeset in the current
|
---|
387 | * data layer.
|
---|
388 | *
|
---|
389 | */
|
---|
390 | class ZoomInCurrentLayerAction extends AbstractAction implements ActiveLayerChangeListener {
|
---|
391 |
|
---|
392 | ZoomInCurrentLayerAction() {
|
---|
393 | putValue(NAME, tr("Zoom to in layer"));
|
---|
394 | new ImageProvider("dialogs/autoscale", "selection").getResource().attachImageIcon(this);
|
---|
395 | putValue(SHORT_DESCRIPTION, tr("Zoom to the objects in the content of this changeset in the current data layer"));
|
---|
396 | updateEnabledState();
|
---|
397 | }
|
---|
398 |
|
---|
399 | protected void alertNoPrimitivesToZoomTo() {
|
---|
400 | HelpAwareOptionPane.showOptionDialog(
|
---|
401 | ChangesetDetailPanel.this,
|
---|
402 | tr("<html>None of the objects in the content of changeset {0} is available in the current<br>"
|
---|
403 | + "edit layer ''{1}''.</html>",
|
---|
404 | currentChangeset.getId(),
|
---|
405 | Main.getLayerManager().getEditLayer().getName()
|
---|
406 | ),
|
---|
407 | tr("Nothing to zoom to"),
|
---|
408 | JOptionPane.WARNING_MESSAGE,
|
---|
409 | HelpUtil.ht("/Dialog/ChangesetCacheManager#NothingToZoomTo")
|
---|
410 | );
|
---|
411 | }
|
---|
412 |
|
---|
413 | @Override
|
---|
414 | public void actionPerformed(ActionEvent arg0) {
|
---|
415 | if (!isEnabled())
|
---|
416 | return;
|
---|
417 | OsmDataLayer layer = Main.getLayerManager().getEditLayer();
|
---|
418 | if (layer == null) {
|
---|
419 | return;
|
---|
420 | }
|
---|
421 | Set<OsmPrimitive> target = new HashSet<>();
|
---|
422 | for (OsmPrimitive p: layer.data.allPrimitives()) {
|
---|
423 | if (p.isUsable() && p.getChangesetId() == currentChangeset.getId()) {
|
---|
424 | target.add(p);
|
---|
425 | }
|
---|
426 | }
|
---|
427 | if (target.isEmpty()) {
|
---|
428 | alertNoPrimitivesToZoomTo();
|
---|
429 | return;
|
---|
430 | }
|
---|
431 | layer.data.setSelected(target);
|
---|
432 | AutoScaleAction.zoomToSelection();
|
---|
433 | }
|
---|
434 |
|
---|
435 | public void updateEnabledState() {
|
---|
436 | setEnabled(Main.getLayerManager().getEditLayer() != null && currentChangeset != null);
|
---|
437 | }
|
---|
438 |
|
---|
439 | @Override
|
---|
440 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
441 | updateEnabledState();
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | @Override
|
---|
446 | public Changeset getCurrentChangeset() {
|
---|
447 | return currentChangeset;
|
---|
448 | }
|
---|
449 | }
|
---|