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

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

added bounds patch by Matteo

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