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

Last change on this file since 2700 was 2578, checked in by jttt, 14 years ago

Encalupse OsmPrimitive.incomplete

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