Changeset 9603 in osm for applications
- Timestamp:
- 2008-08-10T11:42:16+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/osmarender/src/OsmarenderPlugin.java
r7705 r9603 4 4 import java.io.FileOutputStream; 5 5 import java.io.IOException; 6 import java.io.PrintWriter; 7 import java.io.BufferedReader; 8 import java.io.FileReader; 6 9 import java.util.Collection; 7 10 import java.util.HashSet; … … 30 33 import org.openstreetmap.josm.tools.GBC; 31 34 32 33 35 public class OsmarenderPlugin extends Plugin { 34 36 35 private class Action extends AbstractAction {37 private class Action extends AbstractAction { 36 38 37 public Action() {38 super("Osmarender");39 }39 public Action() { 40 super("Osmarender"); 41 } 40 42 41 public void actionPerformed(ActionEvent e) { 42 // get all stuff visible on screen 43 LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight()); 44 LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0); 45 Bounds b = new Bounds(bottomLeft, topRight); 46 CollectBackReferencesVisitor backRefsV = 47 new CollectBackReferencesVisitor(Main.ds, true); 48 DataSet fromDataSet = new DataSet(); 49 for (Node n : Main.ds.nodes) { 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) { 50 58 if (n.deleted || n.incomplete) continue; 51 if (n.coor.isWithin(b)) {59 if (n.coor.isWithin(b)) { 52 60 fromDataSet.nodes.add(n); 53 n.visit(backRefsV);54 }55 }61 n.visit(backRefsV); 62 } 63 } 56 64 for (OsmPrimitive p : new HashSet<OsmPrimitive>(backRefsV.data)) { 57 65 if (p instanceof Way) { … … 62 70 fromDataSet.addPrimitive(p); 63 71 64 String firefox = Main.pref.get("osmarender.firefox", "firefox");65 try {66 // write to plugin dir67 OsmWriter.output(new FileOutputStream(getPluginDir()+"data.osm"), new OsmWriter.All(fromDataSet, true));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)); 68 76 69 // get the exec line70 String exec = firefox;71 if (System.getProperty("os.name").startsWith("Windows"))72 exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+"osm-map-features.xml\"";73 else74 exec += " "+getPluginDir()+"osm-map-features.xml";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"; 75 83 76 // launch up the viewer77 Runtime.getRuntime().exec(exec);78 } catch (IOException e1) {79 JOptionPane.showMessageDialog(Main.parent, tr("Firefox not found. Please set firefox executable in the Map Settings page of the preferences."));80 }81 }82 }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 } 83 91 84 private JMenu view;85 private JMenuItem osmarenderMenu = new JMenuItem(new Action());92 private JMenu view; 93 private JMenuItem osmarenderMenu = new JMenuItem(new Action()); 86 94 87 public OsmarenderPlugin() throws IOException {88 JMenuBar menu = Main.main.menu;89 view = null;90 for (int i = 0; i < menu.getMenuCount(); ++i) {91 if (menu.getMenu(i) != null && tr("View").equals(menu.getMenu(i).getText())) {92 view = menu.getMenu(i);93 break;94 }95 }96 if (view == null) {97 view = new JMenu(tr("View"));98 menu.add(view, 2);99 view.setVisible(false);100 }101 view.add(osmarenderMenu);102 osmarenderMenu.setVisible(false);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); 103 111 104 // install the xsl and xml file105 copy("/osmarender.xsl", "osmarender.xsl");106 copy("/osm-map-features.xml", "osm-map-features.xml");107 }112 // install the xsl and xml file 113 copy("/osmarender.xsl", "osmarender.xsl"); 114 copy("/osm-map-features.xml", "osm-map-features.xml"); 115 } 108 116 109 @Override110 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {111 if (oldFrame != null && newFrame == null) {112 // disable113 osmarenderMenu.setVisible(false);114 if (view.getMenuComponentCount() == 1)115 view.setVisible(false);116 } else if (oldFrame == null && newFrame != null) {117 // enable118 osmarenderMenu.setVisible(true);119 if (view.getMenuComponentCount() == 1)120 view.setVisible(true);121 }122 }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 } 123 131 124 @Override public PreferenceSetting getPreferenceSetting() { 125 return new PreferenceSetting(){ 126 private JTextField firefox = new JTextField(10); 127 public void addGui(PreferenceDialog gui) { 128 gui.map.add(new JLabel(tr("osmarender options")), GBC.eol().insets(0,5,0,0)); 129 gui.map.add(new JLabel(tr("Firefox executable")), GBC.std().insets(10,5,5,0)); 130 gui.map.add(firefox, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL)); 131 firefox.setText(Main.pref.get("osmarender.firefox")); 132 } 133 public void ok() { 134 Main.pref.put("osmarender.firefox", firefox.getText()); 135 } 136 }; 137 } 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 } 138 177 }
Note:
See TracChangeset
for help on using the changeset viewer.
