[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Component;
|
---|
| 7 | import java.awt.Dialog;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 |
|
---|
| 10 | import javax.swing.JTree;
|
---|
| 11 | import javax.swing.SwingUtilities;
|
---|
| 12 | import javax.swing.event.TreeExpansionEvent;
|
---|
| 13 | import javax.swing.event.TreeWillExpandListener;
|
---|
| 14 | import javax.swing.tree.ExpandVetoException;
|
---|
| 15 | import javax.swing.tree.TreePath;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 22 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 23 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
| 24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 25 | import org.openstreetmap.josm.io.OsmApi;
|
---|
| 26 | import org.openstreetmap.josm.io.OsmServerObjectReader;
|
---|
| 27 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
| 28 | import org.xml.sax.SAXException;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
[5266] | 31 | * This is a {@link JTree} rendering the hierarchical structure of {@link Relation}s.
|
---|
[2512] | 32 | *
|
---|
| 33 | * @see RelationTreeModel
|
---|
| 34 | */
|
---|
| 35 | public 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 |
|
---|
[6084] | 83 | @Override
|
---|
[2512] | 84 | public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
|
---|
| 85 | // do nothing
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[6084] | 88 | @Override
|
---|
[2512] | 89 | public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
|
---|
| 90 | TreePath path = event.getPath();
|
---|
| 91 | Relation parent = (Relation)event.getPath().getLastPathComponent();
|
---|
[2578] | 92 | if (! parent.isIncomplete() || parent.isNew())
|
---|
[2512] | 93 | // we don't load complete or new relations
|
---|
| 94 | return;
|
---|
| 95 | // launch the download task
|
---|
| 96 | //
|
---|
| 97 | Main.worker.submit(new RelationLoader(getParentDialog(),parent, path));
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Asynchronous download task for a specific relation
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
| 105 | class RelationLoader extends PleaseWaitRunnable {
|
---|
[4310] | 106 | private boolean canceled;
|
---|
[2512] | 107 | private Exception lastException;
|
---|
| 108 | private Relation relation;
|
---|
| 109 | private DataSet ds;
|
---|
| 110 | private TreePath path;
|
---|
| 111 |
|
---|
| 112 | public RelationLoader(Dialog dialog, Relation relation, TreePath path) {
|
---|
| 113 | super(
|
---|
| 114 | tr("Load relation"),
|
---|
| 115 | new PleaseWaitProgressMonitor(
|
---|
| 116 | dialog
|
---|
| 117 | ),
|
---|
| 118 | false /* don't ignore exceptions */
|
---|
| 119 | );
|
---|
| 120 | this.relation = relation;
|
---|
| 121 | this.path = path;
|
---|
| 122 | }
|
---|
| 123 | @Override
|
---|
| 124 | protected void cancel() {
|
---|
| 125 | OsmApi.getOsmApi().cancel();
|
---|
[4310] | 126 | this.canceled = true;
|
---|
[2512] | 127 | }
|
---|
| 128 |
|
---|
| 129 | @Override
|
---|
| 130 | protected void finish() {
|
---|
[4310] | 131 | if (canceled)
|
---|
[2512] | 132 | return;
|
---|
| 133 | if (lastException != null) {
|
---|
| 134 | lastException.printStackTrace();
|
---|
| 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() {
|
---|
[6084] | 145 | @Override
|
---|
[2512] | 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) {
|
---|
[4310] | 160 | if (canceled) {
|
---|
| 161 | System.out.println(tr("Warning: Ignoring exception because task was canceled. Exception: {0}", e.toString()));
|
---|
[2512] | 162 | return;
|
---|
| 163 | }
|
---|
| 164 | this.lastException = e;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|