Ignore:
Timestamp:
2010-09-15T19:01:04+02:00 (15 years ago)
Author:
stoecker
Message:

remove tabs

Location:
applications/editors/josm/plugins/smed/src/smed
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/smed/src/smed/Smed.java

    r23178 r23193  
    2626
    2727public 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.jar
    44                 JarEntry ent = null;
    45                 BufferedInputStream inp = null;
    46                 String entName = null;
    47                 byte[] buffer = new byte[16384];
    48                 int len;
    4928
    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;
    5531
    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);
    5934
    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);
    6464            }
    6565
    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);
    7676            }
    7777
    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();
    8783
    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        }
    9287
    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);
    10588
    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    }
    117117
    118118}
  • applications/editors/josm/plugins/smed/src/smed/plug/SmedPluginApp.java

    r23178 r23193  
    1212public class SmedPluginApp implements Runnable {
    1313
    14         @Override
    15         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    }
    2222
    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    }
    2929
    3030}
  • applications/editors/josm/plugins/smed/src/smed/plug/ifc/SmedPluggable.java

    r23178 r23193  
    66public interface SmedPluggable {
    77
    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);
    1414}
    1515
  • applications/editors/josm/plugins/smed/src/smed/plug/ifc/SmedPluginManager.java

    r23178 r23193  
    22
    33public interface SmedPluginManager {
    4         void showVisualMessage(String message);
     4    void showVisualMessage(String message);
    55}
  • applications/editors/josm/plugins/smed/src/smed/plug/util/JARFileFilter.java

    r23178 r23193  
    66public class JARFileFilter implements FileFilter {
    77
    8         @Override
    9         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    }
    1212
    1313}
  • applications/editors/josm/plugins/smed/src/smed/plug/util/SmedPluginLoader.java

    r23178 r23193  
    1919public class SmedPluginLoader {
    2020
    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    }
    3333
    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    }
    5050
    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        }
    5757
    58                 if(classes.isEmpty()) return null;
    59                 else return classes;
    60         }
     58        if(classes.isEmpty()) return null;
     59        else return classes;
     60    }
    6161
    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    }
    8686
    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    }
    9494
    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         }
     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    }
    108108}
  • applications/editors/josm/plugins/smed/src/smed/tabs/SmedTabAction.java

    r23178 r23193  
    1717public class SmedTabAction extends JosmAction {
    1818
    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;
    2923
    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);
    3629
    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    }
    4636
    4737
    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() {
    4949        //Create and set up the window.
    5050        JFrame frame = new JFrame("TabbedPaneDemo");
     
    5252        // frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    5353        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    54        
     54
    5555        //Add content to the window.
    5656        frame.add(new SmedTabbedPane(), BorderLayout.CENTER);
     
    5959        frame.pack();
    6060        frame.setVisible(true);
    61         }
     61    }
    6262
    6363
  • applications/editors/josm/plugins/smed/src/smed/tabs/SmedTabbedPane.java

    r23178 r23193  
    2121public class SmedTabbedPane extends JPanel {
    2222
    23         /**
    24          *
    25         */
    26         private static final long serialVersionUID = 1L;
     23    /**
     24     *
     25    */
     26    private static final long serialVersionUID = 1L;
    2727
    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
    4040        Icon icon = null;
    41                 JTabbedPane tabbedPane = new JTabbedPane();
     41        JTabbedPane tabbedPane = new JTabbedPane();
    4242
    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");
    4747        } 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");
    5151        }
    52        
    53         tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
    5452
    55                
     53        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
     54
     55
    5656        JComponent panel2 = makeTextPanel("Panel #2");
    5757        tabbedPane.addTab("Tab 2", icon, panel2, "Does twice as much nothing");
    5858        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
    59        
     59
    6060        JComponent panel3 = makeTextPanel("Panel #3");
    6161        tabbedPane.addTab("Tab 3", icon, panel3, "Still does nothing");
    6262        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
    63        
     63
    6464        JComponent panel4 = makeTextPanel( "Panel #4 (has a preferred size of 410 x 50).");
    6565        panel4.setPreferredSize(new Dimension(410, 50));
    6666        tabbedPane.addTab("Tab 4", icon, panel4, "Does nothing at all");
    6767        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
    68                
     68
    6969        //Add the tabbed pane to this panel.
    7070        add(tabbedPane);
    71                
     71
    7272        //The following line enables to use scrolling tabs.
    7373        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    74         }
     74    }
    7575
    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));
    8181        panel.add(filler);
    82        
    83                 return panel;
    84         }
     82
     83        return panel;
     84    }
    8585}
Note: See TracChangeset for help on using the changeset viewer.