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