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

Last change on this file since 15938 was 15938, checked in by stoecker, 16 years ago

close #2737

  • Property svn:eol-style set to native
File size: 6.5 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 for (Node n : ((Way) p).nodes) {
68 if (n.coor.isWithin(b))
69 backRefsV.data.add(n);
70 }
71 }
72 }
73 for (OsmPrimitive p : backRefsV.data)
74 fromDataSet.addPrimitive(p);
75
76 String firefox = Main.pref.get("osmarender.firefox", "firefox");
77 try {
78 // write to plugin dir
79 OsmWriter w = new OsmWriter(new PrintWriter(new FileOutputStream(getPluginDir()+File.separator+"data.osm")), false, fromDataSet.version);
80 w.header();
81 w.writeDataSources(fromDataSet);
82 w.writeContent(fromDataSet);
83 w.footer();
84 w.close();
85
86 // get the exec line
87 String exec = firefox;
88 if (System.getProperty("os.name").startsWith("Windows"))
89 exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+File.separator+"generated.xml\"";
90 else
91 exec += " "+getPluginDir()+File.separator+"generated.xml";
92
93 // launch up the viewer
94 Runtime.getRuntime().exec(exec);
95 } catch (IOException e1) {
96 JOptionPane.showMessageDialog(Main.parent, tr("Firefox not found. Please set firefox executable in the Map Settings page of the preferences."));
97 }
98 }
99 }
100
101 private JMenuItem osmarenderMenu;
102
103 public OsmarenderPlugin() throws IOException {
104 osmarenderMenu = MainMenu.add(Main.main.menu.viewMenu, new Action());
105 osmarenderMenu.setVisible(false);
106
107 // install the xsl and xml file
108 copy("/osmarender.xsl", "osmarender.xsl");
109 copy("/osm-map-features.xml", "osm-map-features.xml");
110 }
111
112 @Override
113 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
114 if (oldFrame != null && newFrame == null) {
115 // disable
116 osmarenderMenu.setVisible(false);
117 } else if (oldFrame == null && newFrame != null) {
118 // enable
119 osmarenderMenu.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 boolean ok() {
133 Main.pref.put("osmarender.firefox", firefox.getText());
134 return false;
135 }
136 };
137 }
138
139 private void writeGenerated(Bounds b) throws IOException {
140 String bounds_tag = "<bounds " +
141 "minlat=\"" + b.min.lat() + "\" " +
142 "maxlat=\"" + b.max.lat() + "\" " +
143 "minlon=\"" + b.min.lon() + "\" " +
144 "maxlon=\"" + b.max.lon() + "\" " + "/>";
145
146 BufferedReader reader = new BufferedReader(
147 new FileReader( getPluginDir() + File.separator + "osm-map-features.xml") );
148 PrintWriter writer = new PrintWriter( getPluginDir() + File.separator + "generated.xml");
149
150 // osm-map-fetaures.xml contain two placemark
151 // (bounds_mkr1 and bounds_mkr2). We write the bounds tag
152 // between the two
153 String str = null;
154 while( (str = reader.readLine()) != null ) {
155 if(str.contains("<!--bounds_mkr1-->")) {
156 writer.println(str);
157 writer.println(" " + bounds_tag);
158 while(!str.contains("<!--bounds_mkr2-->")) {
159 str = reader.readLine();
160 }
161 writer.println(str);
162 } else {
163 writer.println(str);
164 }
165 }
166
167 writer.close();
168 }
169}
Note: See TracBrowser for help on using the repository browser.