Changeset 12778 in osm for applications/editors/josm/plugins/osmarender/src/OsmarenderPlugin.java
- Timestamp:
- 2009-01-01T18:28:53+01:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/osmarender/src/OsmarenderPlugin.java
r12603 r12778 38 38 public class OsmarenderPlugin extends Plugin { 39 39 40 private class Action extends JosmAction {40 private class Action extends JosmAction { 41 41 42 public Action() {43 super(tr("Osmarender"), null, tr("Osmarender"), null, true);44 }42 public Action() { 43 super(tr("Osmarender"), null, tr("Osmarender"), null, true); 44 } 45 45 46 public void actionPerformed(ActionEvent e) {47 // get all stuff visible on screen48 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);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 51 52 try {53 writeGenerated(b);54 } catch(Exception ex) {55 //how handle the exception?56 }52 try { 53 writeGenerated(b); 54 } catch(Exception ex) { 55 //how handle the exception? 56 } 57 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);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 74 75 String firefox = Main.pref.get("osmarender.firefox", "firefox");76 try {77 // write to plugin dir78 OsmWriter.output(new FileOutputStream(getPluginDir()+File.separator+"data.osm"), new OsmWriter.All(fromDataSet, true));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 79 80 // get the exec line81 String exec = firefox;82 if (System.getProperty("os.name").startsWith("Windows"))83 exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+File.separator+"generated.xml\"";84 else85 exec += " "+getPluginDir()+File.separator+"generated.xml";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 86 87 // launch up the viewer88 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 }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 94 95 private JMenuItem osmarenderMenu;95 private JMenuItem osmarenderMenu; 96 96 97 public OsmarenderPlugin() throws IOException {98 osmarenderMenu = MainMenu.add(Main.main.menu.viewMenu, new Action());99 osmarenderMenu.setVisible(false);97 public OsmarenderPlugin() throws IOException { 98 osmarenderMenu = MainMenu.add(Main.main.menu.viewMenu, new Action()); 99 osmarenderMenu.setVisible(false); 100 100 101 // install the xsl and xml file102 copy("/osmarender.xsl", "osmarender.xsl");103 copy("/osm-map-features.xml", "osm-map-features.xml");104 }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 105 106 @Override107 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {108 if (oldFrame != null && newFrame == null) {109 // disable110 osmarenderMenu.setVisible(false);111 } else if (oldFrame == null && newFrame != null) {112 // enable113 osmarenderMenu.setVisible(true);114 }115 }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 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 }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 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() + "\" " + "/>";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 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");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 143 144 // osm-map-fetaures.xml contain two placemark145 // (bounds_mkr1 and bounds_mkr2). We write the bounds tag146 // between the two147 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 }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 160 161 writer.close();162 }161 writer.close(); 162 } 163 163 }
Note:
See TracChangeset
for help on using the changeset viewer.
