| 1 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.event.ActionEvent;
|
|---|
| 4 | import java.io.FileOutputStream;
|
|---|
| 5 | import java.io.IOException;
|
|---|
| 6 | import java.util.Collection;
|
|---|
| 7 | import java.util.HashSet;
|
|---|
| 8 |
|
|---|
| 9 | import javax.swing.AbstractAction;
|
|---|
| 10 | import javax.swing.JMenu;
|
|---|
| 11 | import javax.swing.JMenuBar;
|
|---|
| 12 | import javax.swing.JMenuItem;
|
|---|
| 13 | import javax.swing.JOptionPane;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 17 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Segment;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
|
|---|
| 23 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 24 | import org.openstreetmap.josm.io.OsmWriter;
|
|---|
| 25 | import org.openstreetmap.josm.plugins.Plugin;
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | public class OsmarenderPlugin extends Plugin {
|
|---|
| 29 |
|
|---|
| 30 | private class Action extends AbstractAction {
|
|---|
| 31 |
|
|---|
| 32 | public Action() {
|
|---|
| 33 | super("Osmarender");
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public void actionPerformed(ActionEvent e) {
|
|---|
| 37 | // get all stuff visible on screen
|
|---|
| 38 | LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
|
|---|
| 39 | LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
|
|---|
| 40 | Bounds b = new Bounds(bottomLeft, topRight);
|
|---|
| 41 | Collection<Node> nodes = new HashSet<Node>();
|
|---|
| 42 | DataSet fromDataSet = new DataSet();
|
|---|
| 43 | AddVisitor adder = new AddVisitor(fromDataSet);
|
|---|
| 44 | for (Node n : Main.ds.nodes) {
|
|---|
| 45 | if (n.coor.isWithin(b)) {
|
|---|
| 46 | n.visit(adder);
|
|---|
| 47 | nodes.add(n);
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | Collection<Segment> segments = new HashSet<Segment>();
|
|---|
| 51 | for (Segment s : Main.ds.segments) {
|
|---|
| 52 | if (nodes.contains(s.from) || nodes.contains(s.to)) {
|
|---|
| 53 | s.visit(adder);
|
|---|
| 54 | segments.add(s);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | for (Way w : Main.ds.ways) {
|
|---|
| 58 | for (Segment s : w.segments) {
|
|---|
| 59 | if (segments.contains(s)) {
|
|---|
| 60 | w.visit(adder);
|
|---|
| 61 | break;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | String firefox = Main.pref.get("osmarender.firefox", "firefox");
|
|---|
| 67 | boolean retry = false;
|
|---|
| 68 | do {
|
|---|
| 69 | try {
|
|---|
| 70 | retry = false;
|
|---|
| 71 |
|
|---|
| 72 | // write to plugin dir
|
|---|
| 73 | OsmWriter.output(new FileOutputStream(getPluginDir()+"data.osm"), new OsmWriter.All(fromDataSet, true));
|
|---|
| 74 |
|
|---|
| 75 | // get the exec line
|
|---|
| 76 | String exec = firefox;
|
|---|
| 77 | if (System.getProperty("os.name").startsWith("Windows"))
|
|---|
| 78 | exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+"osm-map-features.xml\"";
|
|---|
| 79 | else
|
|---|
| 80 | exec += " "+getPluginDir()+"osm-map-features.xml";
|
|---|
| 81 |
|
|---|
| 82 | // launch up the viewer
|
|---|
| 83 | Runtime.getRuntime().exec(exec);
|
|---|
| 84 | } catch (IOException e1) {
|
|---|
| 85 | firefox = JOptionPane.showInputDialog(Main.parent, "FireFox not found. Please enter location of firefox executable");
|
|---|
| 86 | if (firefox != null) {
|
|---|
| 87 | Main.pref.put("osmarender.firefox", firefox);
|
|---|
| 88 | retry = true;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | } while (retry);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | private JMenu view;
|
|---|
| 96 | private JMenuItem osmarenderMenu = new JMenuItem(new Action());
|
|---|
| 97 |
|
|---|
| 98 | public OsmarenderPlugin() throws IOException {
|
|---|
| 99 | JMenuBar menu = Main.main.menu;
|
|---|
| 100 | view = null;
|
|---|
| 101 | for (int i = 0; i < menu.getMenuCount(); ++i) {
|
|---|
| 102 | if (menu.getMenu(i) != null && tr("View").equals(menu.getMenu(i).getName())) {
|
|---|
| 103 | view = menu.getMenu(i);
|
|---|
| 104 | break;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | if (view == null) {
|
|---|
| 108 | view = new JMenu(tr("View"));
|
|---|
| 109 | menu.add(view, 2);
|
|---|
| 110 | view.setVisible(false);
|
|---|
| 111 | }
|
|---|
| 112 | view.add(osmarenderMenu);
|
|---|
| 113 | osmarenderMenu.setVisible(false);
|
|---|
| 114 |
|
|---|
| 115 | // install the xsl and xml file
|
|---|
| 116 | copy("/osmarender.xsl", "osmarender.xsl");
|
|---|
| 117 | copy("/osm-map-features.xml", "osm-map-features.xml");
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | @Override
|
|---|
| 121 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
|---|
| 122 | if (oldFrame != null && newFrame == null) {
|
|---|
| 123 | // disable
|
|---|
| 124 | osmarenderMenu.setVisible(false);
|
|---|
| 125 | if (view.getMenuComponentCount() == 1)
|
|---|
| 126 | view.setVisible(false);
|
|---|
| 127 | } else if (oldFrame == null && newFrame != null) {
|
|---|
| 128 | // enable
|
|---|
| 129 | osmarenderMenu.setVisible(true);
|
|---|
| 130 | if (view.getMenuComponentCount() == 1)
|
|---|
| 131 | view.setVisible(true);
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|