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

Last change on this file since 4269 was 4191, checked in by stoecker, 13 years ago

remove old debug stuff

  • 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 {@see JTree} rendering the hierarchical structure of {@see 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 = RelationTree.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 public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
84 // do nothing
85 }
86
87 public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
88 TreePath path = event.getPath();
89 Relation parent = (Relation)event.getPath().getLastPathComponent();
90 if (! parent.isIncomplete() || parent.isNew())
91 // we don't load complete or new relations
92 return;
93 // launch the download task
94 //
95 Main.worker.submit(new RelationLoader(getParentDialog(),parent, path));
96 }
97 }
98
99 /**
100 * Asynchronous download task for a specific relation
101 *
102 */
103 class RelationLoader extends PleaseWaitRunnable {
104 private boolean cancelled;
105 private Exception lastException;
106 private Relation relation;
107 private DataSet ds;
108 private TreePath path;
109
110 public RelationLoader(Dialog dialog, Relation relation, TreePath path) {
111 super(
112 tr("Load relation"),
113 new PleaseWaitProgressMonitor(
114 dialog
115 ),
116 false /* don't ignore exceptions */
117 );
118 this.relation = relation;
119 this.path = path;
120 }
121 @Override
122 protected void cancel() {
123 OsmApi.getOsmApi().cancel();
124 this.cancelled = true;
125 }
126
127 @Override
128 protected void finish() {
129 if (cancelled)
130 return;
131 if (lastException != null) {
132 lastException.printStackTrace();
133 return;
134 }
135 DataSetMerger visitor = new DataSetMerger(Main.main.getEditLayer().data, ds);
136 visitor.merge();
137 if (! visitor.getConflicts().isEmpty()) {
138 Main.main.getEditLayer().getConflicts().add(visitor.getConflicts());
139 }
140 final RelationTreeModel model = (RelationTreeModel)getModel();
141 SwingUtilities.invokeLater(
142 new Runnable() {
143 public void run() {
144 model.refreshNode(path);
145 }
146 }
147 );
148 }
149
150 @Override
151 protected void realRun() throws SAXException, IOException, OsmTransferException {
152 try {
153 OsmServerObjectReader reader = new OsmServerObjectReader(relation.getId(), OsmPrimitiveType.from(relation), true /* full load */);
154 ds = reader.parseOsm(progressMonitor
155 .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
156 } catch(Exception e) {
157 if (cancelled) {
158 System.out.println(tr("Warning: ignoring exception because task was cancelled. Exception was: {0}", e.toString()));
159 return;
160 }
161 this.lastException = e;
162 }
163 }
164 }
165}
Note: See TracBrowser for help on using the repository browser.