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