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

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

removed tab stop usage

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