source: osm/applications/editors/josm/plugins/smed/src/smed/Smed.java@ 29778

Last change on this file since 29778 was 29778, checked in by akks, 11 years ago

[josm_plugins]: see #josm6355 - Move most of the other plugins to suitable menus, add Netbeans projects

  • Property svn:executable set to *
File size: 5.3 KB
Line 
1package smed;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedOutputStream;
5import java.io.File;
6import java.io.FileOutputStream;
7import java.lang.reflect.Method;
8import java.net.URL;
9import java.net.URLClassLoader;
10import java.util.Enumeration;
11import java.util.List;
12import java.util.ResourceBundle;
13import java.util.jar.JarEntry;
14import java.util.jar.JarFile;
15import java.util.jar.JarOutputStream;
16
17import javax.swing.JMenuItem;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.gui.MainMenu;
21import org.openstreetmap.josm.gui.MapFrame;
22import org.openstreetmap.josm.plugins.Plugin;
23import org.openstreetmap.josm.plugins.PluginInformation;
24
25import smed.io.SmedFile;
26import smed.plug.ifc.SmedPluggable;
27import smed.plug.util.JARFileFilter;
28import smed.tabs.SmedTabAction;
29import smed.tabs.SmedTabbedPane;
30
31public class Smed extends Plugin{
32
33 private JMenuItem item;
34 private SmedTabAction smedTab;
35 // private List<SmedPluggable> plugins = null;
36
37 public Smed(PluginInformation info) {
38 super(info);
39
40 String os = "";
41 String userHome = "";
42
43 File pluginDir = Main.pref.getPluginsDirectory();
44 String pluginDirName = pluginDir.getAbsolutePath();
45 SmedFile splugDir = new SmedFile(pluginDirName + "/splug");
46
47 if(!splugDir.exists()) splugDir.mkdir();
48
49 File[] jars = splugDir.listFiles(new JARFileFilter());
50 SmedFile.init();
51
52 // build smed_ifc.jar from smed.jar
53 JarEntry e = null;
54 BufferedInputStream inp = null;
55 String eName = null;
56 byte[] buffer = new byte[16384];
57 int len;
58
59 try {
60 JarFile file = new JarFile(pluginDirName + "/SeaMapEditor.jar");
61 FileOutputStream fos = new FileOutputStream(pluginDirName + "/splug/smed_ifc.jar");
62 JarOutputStream jos = new JarOutputStream(fos);
63 BufferedOutputStream oos = new BufferedOutputStream( jos);
64
65 // extract *.jar to splug
66 Enumeration<JarEntry> ent = file.entries();
67 while(ent.hasMoreElements()) {
68 e = ent.nextElement();
69 eName = e.getName();
70 if(eName.endsWith(".jar")) {
71 if(splugDir.needUpdate(jars,eName)) {
72 FileOutputStream pfos = new FileOutputStream(pluginDirName + "/splug/" + eName);
73 BufferedOutputStream pos = new BufferedOutputStream(pfos);
74 inp = new BufferedInputStream(file.getInputStream( e ));
75
76 while ((len = inp.read(buffer)) > 0) {
77 pos.write(buffer, 0, len);
78 }
79
80 pos.flush();
81 pos.close();
82 inp.close();
83 pfos.close();
84
85 SmedFile.createMF(eName);
86 }
87 }
88 }
89
90
91
92 // write smed_ifc.jar to splug
93 e = file.getJarEntry("smed/plug/ifc/SmedPluggable.class");
94 inp = new BufferedInputStream(file.getInputStream( e ));
95 eName = e.getName();
96
97 jos.putNextEntry(new JarEntry(eName));
98
99 while ((len = inp.read(buffer)) > 0) {
100 oos.write(buffer, 0, len);
101 }
102
103 oos.flush();
104 inp.close();
105
106 e = file.getJarEntry("smed/plug/ifc/SmedPluginManager.class");
107 inp = new BufferedInputStream(file.getInputStream( e ));
108 eName = e.getName();
109 jos.putNextEntry(new JarEntry(eName));
110
111 while ((len = inp.read(buffer)) > 0) {
112 oos.write(buffer, 0, len);
113 }
114
115 oos.flush();
116 oos.close();
117 fos.flush();
118 fos.close();
119 inp.close();
120
121 } catch (Exception ex) {
122 ex.printStackTrace();
123 }
124
125 // add smed_ifc.jar to classpath (josm need this archive, or perhaps only the interface)
126 File f = new java.io.File(pluginDirName + "/splug/smed_ifc.jar");
127 ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
128
129 try {
130 Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
131 addUrlMethod.setAccessible(true);
132 addUrlMethod.invoke(myClassLoader, f.toURI().toURL());
133 } catch (Exception ex) {
134 ex.printStackTrace();
135 }
136
137 smedTab = new SmedTabAction();
138 item = MainMenu.add(Main.main.menu.dataMenu, smedTab);
139 smedTab.setOsmItem(item);
140
141 item.setEnabled(false);
142
143 }
144
145 @Override
146 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
147 if (oldFrame == null && newFrame != null) {
148 item.setEnabled(true);
149 } else {
150 item.setEnabled(false);
151 List<SmedPluggable> plugins = SmedTabbedPane.getPlugins();
152
153 if(plugins != null) {
154 for(SmedPluggable p : plugins) p.stop();
155 }
156 smedTab.closeDialog();
157 }
158
159 if(Main.map != null) {
160 // von SmedTabbedPane nach hier verlagert, damit sicher ist Main.map != null
161 SmedPluggable p = SmedTabbedPane.getCurPlugin();
162
163 if(p != null) p.hasFocus();
164 }
165 }
166
167}
Note: See TracBrowser for help on using the repository browser.