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

Last change on this file since 17841 was 17707, checked in by stoecker, 17 years ago

some fixes for josm 2166

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