Changeset 23193 in osm for applications/editors/josm/plugins/smed
- Timestamp:
- 2010-09-15T19:01:04+02:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/smed/src/smed
- Files:
-
- 8 edited
-
Smed.java (modified) (1 diff)
-
plug/SmedPluginApp.java (modified) (1 diff)
-
plug/ifc/SmedPluggable.java (modified) (1 diff)
-
plug/ifc/SmedPluginManager.java (modified) (1 diff)
-
plug/util/JARFileFilter.java (modified) (1 diff)
-
plug/util/SmedPluginLoader.java (modified) (1 diff)
-
tabs/SmedTabAction.java (modified) (3 diffs)
-
tabs/SmedTabbedPane.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/smed/src/smed/Smed.java
r23178 r23193 26 26 27 27 public class Smed extends Plugin{ 28 29 private JMenuItem item;30 private SmedTabAction SmedTab;31 32 public Smed(PluginInformation info) {33 super(info);34 35 String os = "";36 String userHome = "";37 38 File pluginDir = Main.pref.getPluginsDirectory();39 String pluginDirName = pluginDir.getAbsolutePath();40 File splug = new File(pluginDirName + "/splug");41 if(!splug.exists()) splug.mkdir();42 43 // build smed_ifc.jar from smed.jar44 JarEntry ent = null;45 BufferedInputStream inp = null;46 String entName = null;47 byte[] buffer = new byte[16384];48 int len;49 28 50 try { 51 JarFile file = new JarFile(pluginDirName + "/smed.jar"); 52 FileOutputStream fos = new FileOutputStream(pluginDirName + "/splug/smed_ifc.jar"); 53 JarOutputStream jos = new JarOutputStream(fos); 54 BufferedOutputStream oos = new BufferedOutputStream( jos); 29 private JMenuItem item; 30 private SmedTabAction SmedTab; 55 31 56 ent = file.getJarEntry("smed/plug/ifc/SmedPluggable.class"); 57 inp = new BufferedInputStream(file.getInputStream( ent )); 58 entName = ent.getName(); 32 public Smed(PluginInformation info) { 33 super(info); 59 34 60 jos.putNextEntry(new JarEntry(entName)); 61 62 while ((len = inp.read(buffer)) > 0) { 63 oos.write(buffer, 0, len); 35 String os = ""; 36 String userHome = ""; 37 38 File pluginDir = Main.pref.getPluginsDirectory(); 39 String pluginDirName = pluginDir.getAbsolutePath(); 40 File splug = new File(pluginDirName + "/splug"); 41 if(!splug.exists()) splug.mkdir(); 42 43 // build smed_ifc.jar from smed.jar 44 JarEntry ent = null; 45 BufferedInputStream inp = null; 46 String entName = null; 47 byte[] buffer = new byte[16384]; 48 int len; 49 50 try { 51 JarFile file = new JarFile(pluginDirName + "/smed.jar"); 52 FileOutputStream fos = new FileOutputStream(pluginDirName + "/splug/smed_ifc.jar"); 53 JarOutputStream jos = new JarOutputStream(fos); 54 BufferedOutputStream oos = new BufferedOutputStream( jos); 55 56 ent = file.getJarEntry("smed/plug/ifc/SmedPluggable.class"); 57 inp = new BufferedInputStream(file.getInputStream( ent )); 58 entName = ent.getName(); 59 60 jos.putNextEntry(new JarEntry(entName)); 61 62 while ((len = inp.read(buffer)) > 0) { 63 oos.write(buffer, 0, len); 64 64 } 65 65 66 oos.flush();67 inp.close();68 69 ent = file.getJarEntry("smed/plug/ifc/SmedPluginManager.class");70 inp = new BufferedInputStream(file.getInputStream( ent ));71 entName = ent.getName();72 jos.putNextEntry(new JarEntry(entName));73 74 while ((len = inp.read(buffer)) > 0) {75 oos.write(buffer, 0, len);66 oos.flush(); 67 inp.close(); 68 69 ent = file.getJarEntry("smed/plug/ifc/SmedPluginManager.class"); 70 inp = new BufferedInputStream(file.getInputStream( ent )); 71 entName = ent.getName(); 72 jos.putNextEntry(new JarEntry(entName)); 73 74 while ((len = inp.read(buffer)) > 0) { 75 oos.write(buffer, 0, len); 76 76 } 77 77 78 oos.flush(); 79 oos.close(); 80 fos.flush(); 81 fos.close(); 82 inp.close(); 83 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } 78 oos.flush(); 79 oos.close(); 80 fos.flush(); 81 fos.close(); 82 inp.close(); 87 83 88 89 // add smed_ifc.jar to classpath (josm need this archive, or perhaps only the interface) 90 File f = new java.io.File(pluginDirName + "/splug/smed_ifc.jar"); 91 ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } 92 87 93 try {94 Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});95 addUrlMethod.setAccessible(true);96 addUrlMethod.invoke(myClassLoader, f.toURI().toURL());97 } catch (Exception e) {98 e.printStackTrace();99 }100 101 SmedTab = new SmedTabAction();102 item = Main.main.menu.toolsMenu.add(SmedTab);103 104 item.setEnabled(false);105 88 106 } 107 108 @Override 109 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 110 if (oldFrame == null && newFrame != null) { 111 item.setEnabled(true); 112 } else { 113 item.setEnabled(false); 114 // SmpDialog.CloseDialog(); 115 } 116 } 89 // add smed_ifc.jar to classpath (josm need this archive, or perhaps only the interface) 90 File f = new java.io.File(pluginDirName + "/splug/smed_ifc.jar"); 91 ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); 92 93 try { 94 Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class}); 95 addUrlMethod.setAccessible(true); 96 addUrlMethod.invoke(myClassLoader, f.toURI().toURL()); 97 } catch (Exception e) { 98 e.printStackTrace(); 99 } 100 101 SmedTab = new SmedTabAction(); 102 item = Main.main.menu.toolsMenu.add(SmedTab); 103 104 item.setEnabled(false); 105 106 } 107 108 @Override 109 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 110 if (oldFrame == null && newFrame != null) { 111 item.setEnabled(true); 112 } else { 113 item.setEnabled(false); 114 // SmpDialog.CloseDialog(); 115 } 116 } 117 117 118 118 } -
applications/editors/josm/plugins/smed/src/smed/plug/SmedPluginApp.java
r23178 r23193 12 12 public class SmedPluginApp implements Runnable { 13 13 14 @Override15 public void run() {16 try {17 runPlugins();18 } catch (IOException e) {19 e.printStackTrace();20 }21 }14 @Override 15 public void run() { 16 try { 17 runPlugins(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 } 22 22 23 public static void runPlugins() throws IOException {24 String pluginDirName = Main.pref.getPluginsDirectory().getAbsolutePath();25 26 List<SmedPluggable> plugins = SmedPluginLoader.loadPlugins(new File(pluginDirName + "/splug"));27 28 }23 public static void runPlugins() throws IOException { 24 String pluginDirName = Main.pref.getPluginsDirectory().getAbsolutePath(); 25 26 List<SmedPluggable> plugins = SmedPluginLoader.loadPlugins(new File(pluginDirName + "/splug")); 27 28 } 29 29 30 30 } -
applications/editors/josm/plugins/smed/src/smed/plug/ifc/SmedPluggable.java
r23178 r23193 6 6 public interface SmedPluggable { 7 7 8 boolean start();9 boolean start(JComponent panel);10 boolean stop();11 String getName();12 13 void setPluginManager(SmedPluginManager manager);8 boolean start(); 9 boolean start(JComponent panel); 10 boolean stop(); 11 String getName(); 12 13 void setPluginManager(SmedPluginManager manager); 14 14 } 15 15 -
applications/editors/josm/plugins/smed/src/smed/plug/ifc/SmedPluginManager.java
r23178 r23193 2 2 3 3 public interface SmedPluginManager { 4 void showVisualMessage(String message);4 void showVisualMessage(String message); 5 5 } -
applications/editors/josm/plugins/smed/src/smed/plug/util/JARFileFilter.java
r23178 r23193 6 6 public class JARFileFilter implements FileFilter { 7 7 8 @Override9 public boolean accept(File f) {10 return f.getName().toLowerCase().endsWith(".jar");11 }8 @Override 9 public boolean accept(File f) { 10 return f.getName().toLowerCase().endsWith(".jar"); 11 } 12 12 13 13 } -
applications/editors/josm/plugins/smed/src/smed/plug/util/SmedPluginLoader.java
r23178 r23193 19 19 public class SmedPluginLoader { 20 20 21 public static List<SmedPluggable> loadPlugins(File plugDir) throws IOException {22 File[] plugJars = plugDir.listFiles(new JARFileFilter());23 24 URL[] urls = fileArrayToURLArray(plugJars);25 if(urls == null) return null;26 27 ClassLoader cl = new URLClassLoader(urls);28 List<Class<SmedPluggable>> plugClasses = extractClassesFromJARs(plugJars, cl);29 30 if(plugClasses == null) return null;31 else return createPluggableObjects(plugClasses);32 }21 public static List<SmedPluggable> loadPlugins(File plugDir) throws IOException { 22 File[] plugJars = plugDir.listFiles(new JARFileFilter()); 23 24 URL[] urls = fileArrayToURLArray(plugJars); 25 if(urls == null) return null; 26 27 ClassLoader cl = new URLClassLoader(urls); 28 List<Class<SmedPluggable>> plugClasses = extractClassesFromJARs(plugJars, cl); 29 30 if(plugClasses == null) return null; 31 else return createPluggableObjects(plugClasses); 32 } 33 33 34 private static List<SmedPluggable> createPluggableObjects(List<Class<SmedPluggable>> pluggables) {35 List<SmedPluggable> plugs = new ArrayList<SmedPluggable>(pluggables.size());36 for(Class<SmedPluggable> plug : pluggables) {37 try {38 plugs.add(plug.newInstance());39 } catch (InstantiationException e) {40 System.err.println("Can't instantiate plugin: " + plug.getName());41 e.printStackTrace();42 } catch (IllegalAccessException e) {43 System.err.println("IllegalAccess for plugin: " + plug.getName());44 e.printStackTrace();45 }46 }47 48 return plugs;49 }34 private static List<SmedPluggable> createPluggableObjects(List<Class<SmedPluggable>> pluggables) { 35 List<SmedPluggable> plugs = new ArrayList<SmedPluggable>(pluggables.size()); 36 for(Class<SmedPluggable> plug : pluggables) { 37 try { 38 plugs.add(plug.newInstance()); 39 } catch (InstantiationException e) { 40 System.err.println("Can't instantiate plugin: " + plug.getName()); 41 e.printStackTrace(); 42 } catch (IllegalAccessException e) { 43 System.err.println("IllegalAccess for plugin: " + plug.getName()); 44 e.printStackTrace(); 45 } 46 } 47 48 return plugs; 49 } 50 50 51 private static List<Class<SmedPluggable>> extractClassesFromJARs(File[] jars, ClassLoader cl) throws FileNotFoundException, IOException {52 List<Class<SmedPluggable>> classes = new ArrayList<Class<SmedPluggable>>();53 54 for(File jar : jars) {55 classes.addAll(extractClassesFromJAR(jar, cl));56 }51 private static List<Class<SmedPluggable>> extractClassesFromJARs(File[] jars, ClassLoader cl) throws FileNotFoundException, IOException { 52 List<Class<SmedPluggable>> classes = new ArrayList<Class<SmedPluggable>>(); 53 54 for(File jar : jars) { 55 classes.addAll(extractClassesFromJAR(jar, cl)); 56 } 57 57 58 if(classes.isEmpty()) return null;59 else return classes;60 }58 if(classes.isEmpty()) return null; 59 else return classes; 60 } 61 61 62 @SuppressWarnings("unchecked")63 private static Collection<? extends Class<SmedPluggable>> extractClassesFromJAR (File jar, ClassLoader cl) throws FileNotFoundException, IOException {64 List<Class<SmedPluggable>> classes = new ArrayList<Class<SmedPluggable>>();65 JarInputStream jaris = new JarInputStream(new FileInputStream(jar));66 JarEntry ent = null;67 68 while ((ent = jaris.getNextJarEntry()) != null) {69 String entName = ent.getName(); //.toLowerCase();70 71 if (entName.endsWith(".class")) {72 try {73 Class<?> cls = cl.loadClass(entName.substring(0, entName.length()- 6).replace('/', '.'));74 if(isPluggableSmedClass(cls)) classes.add((Class<SmedPluggable>) cls);75 } catch (ClassNotFoundException e) {76 System.err.println("Can't load Class" + entName);77 e.printStackTrace();78 }79 }80 }81 82 jaris.close();83 84 return classes;85 }62 @SuppressWarnings("unchecked") 63 private static Collection<? extends Class<SmedPluggable>> extractClassesFromJAR (File jar, ClassLoader cl) throws FileNotFoundException, IOException { 64 List<Class<SmedPluggable>> classes = new ArrayList<Class<SmedPluggable>>(); 65 JarInputStream jaris = new JarInputStream(new FileInputStream(jar)); 66 JarEntry ent = null; 67 68 while ((ent = jaris.getNextJarEntry()) != null) { 69 String entName = ent.getName(); //.toLowerCase(); 70 71 if (entName.endsWith(".class")) { 72 try { 73 Class<?> cls = cl.loadClass(entName.substring(0, entName.length()- 6).replace('/', '.')); 74 if(isPluggableSmedClass(cls)) classes.add((Class<SmedPluggable>) cls); 75 } catch (ClassNotFoundException e) { 76 System.err.println("Can't load Class" + entName); 77 e.printStackTrace(); 78 } 79 } 80 } 81 82 jaris.close(); 83 84 return classes; 85 } 86 86 87 private static boolean isPluggableSmedClass(Class<?> cls) {88 for (Class<?> i: cls.getInterfaces()) {89 if (i.equals(SmedPluggable.class)) return true;90 }91 92 return false;93 }87 private static boolean isPluggableSmedClass(Class<?> cls) { 88 for (Class<?> i: cls.getInterfaces()) { 89 if (i.equals(SmedPluggable.class)) return true; 90 } 91 92 return false; 93 } 94 94 95 private static URL[] fileArrayToURLArray(File[] files) throws MalformedURLException {96 // splug contains at least smed_ifc.jar, but smed_ifc.jar isn't pluggable97 if(files.length <= 1) return null;98 99 URL[] urls = new URL[files.length];100 101 102 for(int i = 0; i < files.length; i++) {103 urls[i] = files[i].toURI().toURL();104 }105 106 return urls;107 }95 private static URL[] fileArrayToURLArray(File[] files) throws MalformedURLException { 96 // splug contains at least smed_ifc.jar, but smed_ifc.jar isn't pluggable 97 if(files.length <= 1) return null; 98 99 URL[] urls = new URL[files.length]; 100 101 102 for(int i = 0; i < files.length; i++) { 103 urls[i] = files[i].toURI().toURL(); 104 } 105 106 return urls; 107 } 108 108 } -
applications/editors/josm/plugins/smed/src/smed/tabs/SmedTabAction.java
r23178 r23193 17 17 public class SmedTabAction extends JosmAction { 18 18 19 /** 20 * 21 */ 22 private static final long serialVersionUID = 1L; 23 24 public SmedTabAction() { 25 super( "Seekarten Editor", "Smed","Seekarten Editor", Shortcut.registerShortcut( 26 "tools:Semmaps", 27 tr("Tool: {0}", "Seekarten Editor"), KeyEvent.VK_K, //$NON-NLS-1$ //$NON-NLS-2$ 28 Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 19 /** 20 * 21 */ 22 private static final long serialVersionUID = 1L; 29 23 30 try { 31 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 } 24 public SmedTabAction() { 25 super( "Seekarten Editor", "Smed","Seekarten Editor", Shortcut.registerShortcut( 26 "tools:Semmaps", 27 tr("Tool: {0}", "Seekarten Editor"), KeyEvent.VK_K, //$NON-NLS-1$ //$NON-NLS-2$ 28 Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 36 29 37 38 @Override 39 public void actionPerformed(ActionEvent e) { 40 SwingUtilities.invokeLater(new Runnable() { 41 public void run() { 42 createAndShowTabs(); 43 } 44 }); 45 } 30 try { 31 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 } 46 36 47 37 48 protected void createAndShowTabs() { 38 @Override 39 public void actionPerformed(ActionEvent e) { 40 SwingUtilities.invokeLater(new Runnable() { 41 public void run() { 42 createAndShowTabs(); 43 } 44 }); 45 } 46 47 48 protected void createAndShowTabs() { 49 49 //Create and set up the window. 50 50 JFrame frame = new JFrame("TabbedPaneDemo"); … … 52 52 // frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 53 53 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 54 54 55 55 //Add content to the window. 56 56 frame.add(new SmedTabbedPane(), BorderLayout.CENTER); … … 59 59 frame.pack(); 60 60 frame.setVisible(true); 61 }61 } 62 62 63 63 -
applications/editors/josm/plugins/smed/src/smed/tabs/SmedTabbedPane.java
r23178 r23193 21 21 public class SmedTabbedPane extends JPanel { 22 22 23 /**24 * 25 */26 private static final long serialVersionUID = 1L;23 /** 24 * 25 */ 26 private static final long serialVersionUID = 1L; 27 27 28 @SuppressWarnings("null")29 public SmedTabbedPane() {30 super(new GridLayout(1, 1));31 32 List<SmedPluggable> plugins = null;33 String pluginDirName = Main.pref.getPluginsDirectory().getAbsolutePath();34 try {35 plugins = SmedPluginLoader.loadPlugins(new File(pluginDirName + "/splug"));36 } catch (IOException e) {37 e.printStackTrace();38 }39 28 @SuppressWarnings("null") 29 public SmedTabbedPane() { 30 super(new GridLayout(1, 1)); 31 32 List<SmedPluggable> plugins = null; 33 String pluginDirName = Main.pref.getPluginsDirectory().getAbsolutePath(); 34 try { 35 plugins = SmedPluginLoader.loadPlugins(new File(pluginDirName + "/splug")); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } 39 40 40 Icon icon = null; 41 JTabbedPane tabbedPane = new JTabbedPane();41 JTabbedPane tabbedPane = new JTabbedPane(); 42 42 43 JComponent panel1;44 if(plugins == null) {45 panel1 = makeTextPanel("Panel #1");46 tabbedPane.addTab("Tab 1", icon , panel1, "Does nothing");43 JComponent panel1; 44 if(plugins == null) { 45 panel1 = makeTextPanel("Panel #1"); 46 tabbedPane.addTab("Tab 1", icon , panel1, "Does nothing"); 47 47 } else { 48 panel1 = new JPanel();49 plugins.get(0).start(panel1);50 tabbedPane.addTab(plugins.get(0).getName(), icon , panel1, "say hello");48 panel1 = new JPanel(); 49 plugins.get(0).start(panel1); 50 tabbedPane.addTab(plugins.get(0).getName(), icon , panel1, "say hello"); 51 51 } 52 53 tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);54 52 55 53 tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); 54 55 56 56 JComponent panel2 = makeTextPanel("Panel #2"); 57 57 tabbedPane.addTab("Tab 2", icon, panel2, "Does twice as much nothing"); 58 58 tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); 59 59 60 60 JComponent panel3 = makeTextPanel("Panel #3"); 61 61 tabbedPane.addTab("Tab 3", icon, panel3, "Still does nothing"); 62 62 tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); 63 63 64 64 JComponent panel4 = makeTextPanel( "Panel #4 (has a preferred size of 410 x 50)."); 65 65 panel4.setPreferredSize(new Dimension(410, 50)); 66 66 tabbedPane.addTab("Tab 4", icon, panel4, "Does nothing at all"); 67 67 tabbedPane.setMnemonicAt(3, KeyEvent.VK_4); 68 68 69 69 //Add the tabbed pane to this panel. 70 70 add(tabbedPane); 71 71 72 72 //The following line enables to use scrolling tabs. 73 73 tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 74 }74 } 75 75 76 private JComponent makeTextPanel(String text) {77 JPanel panel = new JPanel(false);78 JLabel filler = new JLabel(text);79 filler.setHorizontalAlignment(JLabel.CENTER);80 panel.setLayout(new GridLayout(1, 1));76 private JComponent makeTextPanel(String text) { 77 JPanel panel = new JPanel(false); 78 JLabel filler = new JLabel(text); 79 filler.setHorizontalAlignment(JLabel.CENTER); 80 panel.setLayout(new GridLayout(1, 1)); 81 81 panel.add(filler); 82 83 return panel;84 }82 83 return panel; 84 } 85 85 }
Note:
See TracChangeset
for help on using the changeset viewer.
