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