1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import javax.xml.bind.JAXBException;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtensionPoint;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.plugin.AbstractUIPlugin;
26 import org.osgi.framework.BundleContext;
27 import sk.baka.xml.gene.ICoordinatorFactory;
28 import sk.baka.xml.gene.IExporterFactory;
29 import sk.baka.xml.gene.InstanceProvider;
30 import sk.uniba.euromath.config.EuromathConfig;
31 import sk.uniba.euromath.document.schema.SchematicUtils;
32 import sk.uniba.euromath.tools.URLDir;
33 /***
34 * This is the plugin class for the EuroMath editor.
35 * @author TV, Martin Vysny
36 */
37 public class EuroMath extends AbstractUIPlugin {
38 /***
39 * The shared instance.
40 */
41 private static EuroMath instance = null;
42 /***
43 * Zero-argument constructor.
44 */
45 public EuroMath() {
46 super();
47 instance = this;
48 }
49 /***
50 * Loads the configuration file. Does nothing if the config file is already
51 * loaded.
52 * @throws JAXBException if config loading fails
53 * @throws IOException if config loading fails
54 */
55 private final void initConfig() throws JAXBException, IOException {
56 if (EuromathConfig.isInitialized())
57 return;
58 URL install = getBundle().getEntry("/");
59 try {
60 EuromathConfig.setPluginRoot(new URLDir(install));
61 EuromathConfig.initInstance();
62 } catch (URISyntaxException ex) {
63 throw new AssertionError(ex);
64 }
65 }
66 /***
67 * GENE's {@link ICoordinatorFactory coordinator factory} extension point
68 * ID.
69 */
70 public final static String COORDINATOR_FACTORY_EXTENSION_POINT_ID = "sk.uniba.euromath.ICoordinatorFactory";
71 /***
72 * GENE's {@link IExporterFactory exporter factory} extension point ID.
73 */
74 public final static String EXPORTER_FACTORY_EXTENSION_POINT_ID = "sk.uniba.euromath.IExporterFactory";
75 /***
76 * Finds all exporters and coordinators and registers them to GENE.
77 */
78 private void initGene() {
79
80
81
82 final IExtensionRegistry r = Platform.getExtensionRegistry();
83
84 final IExtensionPoint editorFactoryPoint = r
85 .getExtensionPoint(COORDINATOR_FACTORY_EXTENSION_POINT_ID);
86 if (editorFactoryPoint == null)
87 throw new AssertionError();
88 IConfigurationElement[] elements = editorFactoryPoint
89 .getConfigurationElements();
90 for (final IConfigurationElement element : elements) {
91 try {
92 final ICoordinatorFactory factory = (ICoordinatorFactory) element
93 .createExecutableExtension("class");
94 if (factory != null)
95 InstanceProvider.getInstance().registerCoordinatorFactory(
96 factory);
97 } catch (CoreException ex) {
98 EuroMath.log(IStatus.ERROR, 0,
99 "Error loading coordinator factory",
100 ex);
101 }
102 }
103
104 final IExtensionPoint rendererFactoryPoint = r
105 .getExtensionPoint(EXPORTER_FACTORY_EXTENSION_POINT_ID);
106 if (rendererFactoryPoint == null)
107 throw new AssertionError();
108 elements = rendererFactoryPoint.getConfigurationElements();
109 for (final IConfigurationElement element : elements) {
110 try {
111 final IExporterFactory factory = (IExporterFactory) element
112 .createExecutableExtension("class");
113 if (factory != null)
114 InstanceProvider.getInstance().registerExporterFactory(
115 factory);
116 } catch (CoreException ex) {
117 EuroMath.log(IStatus.ERROR, 0,
118 "Error loading renderer factory", ex);
119 }
120 }
121 }
122 /***
123 * Accessor to the singleton instance of the plugin.
124 * @return singleton instance, never <code>null</code>.
125 */
126 public static EuroMath getInstance() {
127 if (instance == null)
128 throw new IllegalStateException("Plugin is not loaded.");
129 return instance;
130 }
131 /***
132 * @return plugin id.
133 */
134 public static String getID() {
135 return "sk.uniba.euromath";
136 }
137
138
139
140
141 @Override
142 public void start(BundleContext context) throws Exception {
143 super.start(context);
144 initConfig();
145 initGene();
146 SchematicUtils.initializeSchematic();
147 }
148
149
150
151
152 @Override
153 public void stop(BundleContext context) throws Exception {
154 super.stop(context);
155 instance = null;
156 }
157 /***
158 * Returns an image descriptor for the image file at the given plug-in
159 * relative path.
160 * @param path the path
161 * @return the image descriptor
162 */
163 public static ImageDescriptor getImageDescriptor(String path) {
164 return AbstractUIPlugin.imageDescriptorFromPlugin(getID(), path);
165 }
166 /***
167 * Shorthand for <code>EuroMath.getInstance().getLog().log()</code>.
168 * @param severity the severity; one of <code>IStatus.OK</code>,
169 * <code>IStatus.ERROR</code>, <code>IStatus.INFO</code>,
170 * <code>IStatus.WARNING</code>, or <code>IStatus.CANCEL</code>
171 * @param code the plug-in-specific status code, or <code>IStatus.OK</code>
172 * @param message a human-readable message, localized to the current locale
173 * @param exception a low-level exception, or <code>null</code> if not
174 * applicable
175 */
176 public static void log(int severity, int code, String message,
177 Throwable exception) {
178 EuroMath.getInstance().getLog().log(
179 new Status(severity, getID(), code, message, exception));
180 if (exception != null)
181 exception.printStackTrace();
182 }
183 }