[1438] | 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;
|
---|
[1638] | 10 | import javax.swing.JLabel;
|
---|
[1438] | 11 | import javax.swing.JMenu;
|
---|
| 12 | import javax.swing.JMenuBar;
|
---|
| 13 | import javax.swing.JMenuItem;
|
---|
| 14 | import javax.swing.JOptionPane;
|
---|
[1638] | 15 | import javax.swing.JTextField;
|
---|
[1438] | 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[4972] | 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 24 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
[1438] | 25 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[1638] | 26 | import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
|
---|
| 27 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
[1438] | 28 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
| 29 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
[1638] | 30 | import org.openstreetmap.josm.tools.GBC;
|
---|
[1438] | 31 |
|
---|
| 32 |
|
---|
| 33 | public class OsmarenderPlugin extends Plugin {
|
---|
| 34 |
|
---|
[1638] | 35 | private class Action extends AbstractAction {
|
---|
[1438] | 36 |
|
---|
[1638] | 37 | public Action() {
|
---|
| 38 | super("Osmarender");
|
---|
| 39 | }
|
---|
[1438] | 40 |
|
---|
[1638] | 41 | public void actionPerformed(ActionEvent e) {
|
---|
| 42 | // get all stuff visible on screen
|
---|
| 43 | LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
|
---|
| 44 | LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
|
---|
| 45 | Bounds b = new Bounds(bottomLeft, topRight);
|
---|
[4972] | 46 | CollectBackReferencesVisitor backRefsV =
|
---|
| 47 | new CollectBackReferencesVisitor(Main.ds, true);
|
---|
[1638] | 48 | DataSet fromDataSet = new DataSet();
|
---|
| 49 | for (Node n : Main.ds.nodes) {
|
---|
[6064] | 50 | if (n.deleted || n.incomplete) continue;
|
---|
[1638] | 51 | if (n.coor.isWithin(b)) {
|
---|
[4972] | 52 | fromDataSet.nodes.add(n);
|
---|
| 53 | n.visit(backRefsV);
|
---|
[1638] | 54 | }
|
---|
| 55 | }
|
---|
[4972] | 56 | for (OsmPrimitive p : new HashSet<OsmPrimitive>(backRefsV.data)) {
|
---|
| 57 | if (p instanceof Way) {
|
---|
| 58 | backRefsV.data.addAll(((Way) p).nodes);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | for (OsmPrimitive p : backRefsV.data)
|
---|
| 62 | fromDataSet.addPrimitive(p);
|
---|
[1438] | 63 |
|
---|
[1638] | 64 | String firefox = Main.pref.get("osmarender.firefox", "firefox");
|
---|
| 65 | try {
|
---|
| 66 | // write to plugin dir
|
---|
| 67 | OsmWriter.output(new FileOutputStream(getPluginDir()+"data.osm"), new OsmWriter.All(fromDataSet, true));
|
---|
[1438] | 68 |
|
---|
[1638] | 69 | // get the exec line
|
---|
| 70 | String exec = firefox;
|
---|
| 71 | if (System.getProperty("os.name").startsWith("Windows"))
|
---|
| 72 | exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+"osm-map-features.xml\"";
|
---|
| 73 | else
|
---|
| 74 | exec += " "+getPluginDir()+"osm-map-features.xml";
|
---|
[1467] | 75 |
|
---|
[1638] | 76 | // launch up the viewer
|
---|
| 77 | Runtime.getRuntime().exec(exec);
|
---|
| 78 | } catch (IOException e1) {
|
---|
[7705] | 79 | JOptionPane.showMessageDialog(Main.parent, tr("Firefox not found. Please set firefox executable in the Map Settings page of the preferences."));
|
---|
[1638] | 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[1467] | 83 |
|
---|
[1638] | 84 | private JMenu view;
|
---|
| 85 | private JMenuItem osmarenderMenu = new JMenuItem(new Action());
|
---|
[1438] | 86 |
|
---|
[1638] | 87 | public OsmarenderPlugin() throws IOException {
|
---|
| 88 | JMenuBar menu = Main.main.menu;
|
---|
| 89 | view = null;
|
---|
| 90 | for (int i = 0; i < menu.getMenuCount(); ++i) {
|
---|
[2398] | 91 | if (menu.getMenu(i) != null && tr("View").equals(menu.getMenu(i).getText())) {
|
---|
[1638] | 92 | view = menu.getMenu(i);
|
---|
| 93 | break;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | if (view == null) {
|
---|
| 97 | view = new JMenu(tr("View"));
|
---|
| 98 | menu.add(view, 2);
|
---|
| 99 | view.setVisible(false);
|
---|
| 100 | }
|
---|
| 101 | view.add(osmarenderMenu);
|
---|
| 102 | osmarenderMenu.setVisible(false);
|
---|
[1438] | 103 |
|
---|
[1638] | 104 | // install the xsl and xml file
|
---|
| 105 | copy("/osmarender.xsl", "osmarender.xsl");
|
---|
| 106 | copy("/osm-map-features.xml", "osm-map-features.xml");
|
---|
| 107 | }
|
---|
[1438] | 108 |
|
---|
[1638] | 109 | @Override
|
---|
| 110 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
| 111 | if (oldFrame != null && newFrame == null) {
|
---|
| 112 | // disable
|
---|
| 113 | osmarenderMenu.setVisible(false);
|
---|
| 114 | if (view.getMenuComponentCount() == 1)
|
---|
| 115 | view.setVisible(false);
|
---|
| 116 | } else if (oldFrame == null && newFrame != null) {
|
---|
| 117 | // enable
|
---|
| 118 | osmarenderMenu.setVisible(true);
|
---|
| 119 | if (view.getMenuComponentCount() == 1)
|
---|
| 120 | view.setVisible(true);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[1438] | 123 |
|
---|
[1638] | 124 | @Override public PreferenceSetting getPreferenceSetting() {
|
---|
| 125 | return new PreferenceSetting(){
|
---|
| 126 | private JTextField firefox = new JTextField(10);
|
---|
| 127 | public void addGui(PreferenceDialog gui) {
|
---|
| 128 | gui.map.add(new JLabel(tr("osmarender options")), GBC.eol().insets(0,5,0,0));
|
---|
[7705] | 129 | gui.map.add(new JLabel(tr("Firefox executable")), GBC.std().insets(10,5,5,0));
|
---|
[1638] | 130 | gui.map.add(firefox, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
|
---|
| 131 | firefox.setText(Main.pref.get("osmarender.firefox"));
|
---|
| 132 | }
|
---|
| 133 | public void ok() {
|
---|
| 134 | Main.pref.put("osmarender.firefox", firefox.getText());
|
---|
| 135 | }
|
---|
| 136 | };
|
---|
| 137 | }
|
---|
[1438] | 138 | }
|
---|