create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / plugin / example / OpenRocketSimulationListener.java
1 package net.sf.openrocket.plugin.example;
2
3 import java.awt.Component;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import net.sf.openrocket.plugin.Configurable;
8 import net.sf.openrocket.plugin.SwingConfigurator;
9 import net.sf.openrocket.plugin.framework.Service;
10 import net.sf.openrocket.simulation.listeners.AbstractSimulationListener;
11 import net.sf.openrocket.util.BugException;
12 import net.xeoh.plugins.base.Plugin;
13 import net.xeoh.plugins.base.annotations.Capabilities;
14
15 public abstract class OpenRocketSimulationListener extends AbstractSimulationListener
16                 implements Plugin, Service, SwingConfigurator<OpenRocketSimulationListener>, Configurable {
17         
18         private final String[] ids;
19         private final String[] capabilities;
20         
21         public OpenRocketSimulationListener(String... ids) {
22                 if (ids.length == 0) {
23                         ids = new String[] { this.getClass().getCanonicalName() };
24                 }
25                 
26                 this.ids = ids.clone();
27                 this.capabilities = new String[ids.length * 2];
28                 for (int i = 0; i < ids.length; i++) {
29                         capabilities[i * 2] = ids[i] + ":service";
30                         capabilities[i * 2 + 1] = ids[i] + ":config";
31                 }
32                 
33         }
34         
35         @Capabilities
36         public String[] capabilities() {
37                 return capabilities.clone();
38         }
39         
40         @SuppressWarnings("unchecked")
41         @Override
42         public <E> List<E> getPlugins(Class<E> e, Object... args) {
43                 if (e != this.getClass()) {
44                         throw new BugException("Attempting to get plugin of type " + e + " but I am of type " + this.getClass());
45                 }
46                 try {
47                         return (List<E>) Arrays.asList(this.getClass().newInstance());
48                 } catch (IllegalAccessException e1) {
49                         throw new BugException("Could not instantiate new object of type " + this.getClass(), e1);
50                 } catch (InstantiationException e2) {
51                         throw new BugException("Could not instantiate new object of type " + this.getClass(), e2);
52                 }
53         }
54         
55         
56         @Override
57         public boolean isConfigurable(OpenRocketSimulationListener plugin) {
58                 return plugin.isConfigurable();
59         }
60         
61         @Override
62         public Component getConfigurationComponent(OpenRocketSimulationListener plugin) {
63                 return plugin.getConfigurationComponent();
64         }
65         
66         public abstract boolean isConfigurable();
67         
68         public abstract Component getConfigurationComponent();
69         
70         
71         
72         @Override
73         public String getPluginID() {
74                 return ids[0];
75         }
76         
77         @Override
78         public boolean isCompatible(String pluginID) {
79                 for (String id : ids) {
80                         if (pluginID.equals(id)) {
81                                 return true;
82                         }
83                 }
84                 return false;
85         }
86         
87         
88 }