source: osm/applications/editors/josm/plugins/osmarender/src/OsmarenderPlugin.java@ 14406

Last change on this file since 14406 was 14406, checked in by frederik, 17 years ago

minof fixes to some JOSM plugins to reflect changes in the Visitor interface

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1import static org.openstreetmap.josm.tools.I18n.tr;
2
3import java.awt.event.ActionEvent;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.PrintWriter;
7import java.io.BufferedReader;
8import java.io.FileReader;
9import java.io.File;
10import java.util.Collection;
11import java.util.HashSet;
12
13import javax.swing.AbstractAction;
14import javax.swing.JLabel;
15import javax.swing.JMenuItem;
16import javax.swing.JOptionPane;
17import javax.swing.JTextField;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.actions.JosmAction;
21import org.openstreetmap.josm.data.Bounds;
22import org.openstreetmap.josm.data.coor.LatLon;
23import org.openstreetmap.josm.data.osm.DataSet;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.data.osm.OsmPrimitive;
27import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
28import org.openstreetmap.josm.gui.MapFrame;
29import org.openstreetmap.josm.gui.MainMenu;
30import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
31import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
32import org.openstreetmap.josm.io.OsmWriter;
33import org.openstreetmap.josm.plugins.Plugin;
34import org.openstreetmap.josm.tools.GBC;
35
36public class OsmarenderPlugin extends Plugin {
37
38 private class Action extends JosmAction {
39
40 public Action() {
41 super(tr("Osmarender"), null, tr("Osmarender"), null, true);
42 }
43
44 public void actionPerformed(ActionEvent e) {
45 // get all stuff visible on screen
46 LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
47 LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
48 Bounds b = new Bounds(bottomLeft, topRight);
49
50 try {
51 writeGenerated(b);
52 } catch(Exception ex) {
53 //how handle the exception?
54 }
55
56 CollectBackReferencesVisitor backRefsV = new CollectBackReferencesVisitor(Main.ds, true);
57 DataSet fromDataSet = new DataSet();
58 for (Node n : Main.ds.nodes) {
59 if (n.deleted || n.incomplete) continue;
60 if (n.coor.isWithin(b)) {
61 fromDataSet.nodes.add(n);
62 n.visit(backRefsV);
63 }
64 }
65 for (OsmPrimitive p : new HashSet<OsmPrimitive>(backRefsV.data)) {
66 if (p instanceof Way) {
67 backRefsV.data.addAll(((Way) p).nodes);
68 }
69 }
70 for (OsmPrimitive p : backRefsV.data)
71 fromDataSet.addPrimitive(p);
72
73 String firefox = Main.pref.get("osmarender.firefox", "firefox");
74 try {
75 // write to plugin dir
76 OsmWriter w = new OsmWriter(new PrintWriter(new FileOutputStream(getPluginDir()+File.separator+"data.osm")), false, fromDataSet.version);
77 w.header();
78 w.writeDataSources(fromDataSet);
79 w.writeContent(fromDataSet);
80 w.footer();
81
82 // get the exec line
83 String exec = firefox;
84 if (System.getProperty("os.name").startsWith("Windows"))
85 exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+File.separator+"generated.xml\"";
86 else
87 exec += " "+getPluginDir()+File.separator+"generated.xml";
88
89 // launch up the viewer
90 Runtime.getRuntime().exec(exec);
91 } catch (IOException e1) {
92 JOptionPane.showMessageDialog(Main.parent, tr("Firefox not found. Please set firefox executable in the Map Settings page of the preferences."));
93 }
94 }
95 }
96
97 private JMenuItem osmarenderMenu;
98
99 public OsmarenderPlugin() throws IOException {
100 osmarenderMenu = MainMenu.add(Main.main.menu.viewMenu, new Action());
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 } else if (oldFrame == null && newFrame != null) {
114 // enable
115 osmarenderMenu.setVisible(true);
116 }
117 }
118
119 @Override public PreferenceSetting getPreferenceSetting() {
120 return new PreferenceSetting(){
121 private JTextField firefox = new JTextField(10);
122 public void addGui(PreferenceDialog gui) {
123 gui.map.add(new JLabel(tr("osmarender options")), GBC.eol().insets(0,5,0,0));
124 gui.map.add(new JLabel(tr("Firefox executable")), GBC.std().insets(10,5,5,0));
125 gui.map.add(firefox, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
126 firefox.setText(Main.pref.get("osmarender.firefox"));
127 }
128 public boolean ok() {
129 Main.pref.put("osmarender.firefox", firefox.getText());
130 return false;
131 }
132 };
133 }
134
135 private void writeGenerated(Bounds b) throws IOException {
136 String bounds_tag = "<bounds " +
137 "minlat=\"" + b.min.lat() + "\" " +
138 "maxlat=\"" + b.max.lat() + "\" " +
139 "minlon=\"" + b.min.lon() + "\" " +
140 "maxlon=\"" + b.max.lon() + "\" " + "/>";
141
142 BufferedReader reader = new BufferedReader(
143 new FileReader( getPluginDir() + File.separator + "osm-map-features.xml") );
144 PrintWriter writer = new PrintWriter( getPluginDir() + File.separator + "generated.xml");
145
146 // osm-map-fetaures.xml contain two placemark
147 // (bounds_mkr1 and bounds_mkr2). We write the bounds tag
148 // between the two
149 String str = null;
150 while( (str = reader.readLine()) != null ) {
151 if(str.contains("<!--bounds_mkr1-->")) {
152 writer.println(str);
153 writer.println(" " + bounds_tag);
154 while(!str.contains("<!--bounds_mkr2-->")) {
155 str = reader.readLine();
156 }
157 writer.println(str);
158 } else {
159 writer.println(str);
160 }
161 }
162
163 writer.close();
164 }
165}
Note: See TracBrowser for help on using the repository browser.