source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/RelationTree.java@ 10212

Last change on this file since 10212 was 10212, checked in by Don-vip, 8 years ago

sonar - squid:S2221 - "Exception" should not be caught when not required by called methods

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Dialog;
8import java.io.IOException;
9
10import javax.swing.JTree;
11import javax.swing.SwingUtilities;
12import javax.swing.event.TreeExpansionEvent;
13import javax.swing.event.TreeWillExpandListener;
14import javax.swing.tree.ExpandVetoException;
15import javax.swing.tree.TreePath;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.DataSetMerger;
20import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
21import org.openstreetmap.josm.data.osm.Relation;
22import org.openstreetmap.josm.gui.PleaseWaitRunnable;
23import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.io.OsmApi;
26import org.openstreetmap.josm.io.OsmServerObjectReader;
27import org.openstreetmap.josm.io.OsmTransferException;
28import org.xml.sax.SAXException;
29
30/**
31 * This is a {@link JTree} rendering the hierarchical structure of {@link Relation}s.
32 *
33 * @see RelationTreeModel
34 */
35public class RelationTree extends JTree {
36 /**
37 * builds the UI
38 */
39 protected void build() {
40 setRootVisible(false);
41 setCellRenderer(new RelationTreeCellRenderer());
42 addTreeWillExpandListener(new LazyRelationLoader());
43 }
44
45 /**
46 * constructor
47 */
48 public RelationTree() {
49 super();
50 build();
51 }
52
53 /**
54 * constructor
55 * @param model the tree model
56 */
57 public RelationTree(RelationTreeModel model) {
58 super(model);
59 build();
60 }
61
62 /**
63 * replies the parent dialog this tree is embedded in.
64 *
65 * @return the parent dialog; null, if there is no parent dialog
66 */
67 protected Dialog getParentDialog() {
68 Component c = this;
69 while (c != null && !(c instanceof Dialog)) {
70 c = c.getParent();
71 }
72 return (Dialog) c;
73 }
74
75 /**
76 * An adapter for TreeWillExpand-events. If a node is to be expanded which is
77 * not loaded yet this will trigger asynchronous loading of the respective
78 * relation.
79 *
80 */
81 class LazyRelationLoader implements TreeWillExpandListener {
82
83 @Override
84 public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
85 // do nothing
86 }
87
88 @Override
89 public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
90 TreePath path = event.getPath();
91 Relation parent = (Relation) event.getPath().getLastPathComponent();
92 if (!parent.isIncomplete() || parent.isNew())
93 // we don't load complete or new relations
94 return;
95 // launch the download task
96 Main.worker.submit(new RelationLoader(getParentDialog(), parent, path));
97 }
98 }
99
100 /**
101 * Asynchronous download task for a specific relation
102 *
103 */
104 class RelationLoader extends PleaseWaitRunnable {
105 private boolean canceled;
106 private Exception lastException;
107 private final Relation relation;
108 private DataSet ds;
109 private final TreePath path;
110
111 RelationLoader(Dialog dialog, Relation relation, TreePath path) {
112 super(
113 tr("Load relation"),
114 new PleaseWaitProgressMonitor(
115 dialog
116 ),
117 false /* don't ignore exceptions */
118 );
119 this.relation = relation;
120 this.path = path;
121 }
122
123 @Override
124 protected void cancel() {
125 OsmApi.getOsmApi().cancel();
126 this.canceled = true;
127 }
128
129 @Override
130 protected void finish() {
131 if (canceled)
132 return;
133 if (lastException != null) {
134 Main.error(lastException);
135 return;
136 }
137 DataSetMerger visitor = new DataSetMerger(Main.main.getEditLayer().data, ds);
138 visitor.merge();
139 if (!visitor.getConflicts().isEmpty()) {
140 Main.main.getEditLayer().getConflicts().add(visitor.getConflicts());
141 }
142 final RelationTreeModel model = (RelationTreeModel) getModel();
143 SwingUtilities.invokeLater(
144 new Runnable() {
145 @Override
146 public void run() {
147 model.refreshNode(path);
148 }
149 }
150 );
151 }
152
153 @Override
154 protected void realRun() throws SAXException, IOException, OsmTransferException {
155 try {
156 OsmServerObjectReader reader = new OsmServerObjectReader(relation.getId(), OsmPrimitiveType.from(relation), true);
157 ds = reader.parseOsm(progressMonitor
158 .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
159 } catch (OsmTransferException e) {
160 if (canceled) {
161 Main.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
162 return;
163 }
164 this.lastException = e;
165 }
166 }
167 }
168}
Note: See TracBrowser for help on using the repository browser.