View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
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("/"); //$NON-NLS-1$
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"; //$NON-NLS-1$
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"; //$NON-NLS-1$
74  	/***
75  	 * Finds all exporters and coordinators and registers them to GENE.
76  	 */
77  	private void initGene() {
78  		// register exporter and coordinator factories using the extension
79  		// points. EuroMath2 itself defines some default factories extending
80  		// its own extension points.
81  		final IExtensionRegistry r = Platform.getExtensionRegistry();
82  		// editor factories
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"); //$NON-NLS-1$
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", //$NON-NLS-1$
99  						ex);
100 			}
101 		}
102 		// renderer factories
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"); //$NON-NLS-1$
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); //$NON-NLS-1$
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."); //$NON-NLS-1$
128 		return instance;
129 	}
130 	/***
131 	 * @return plugin id.
132 	 */
133 	public static String getID() {
134 		return "sk.uniba.euromath"; //$NON-NLS-1$
135 	}
136 	/*
137 	 * (non-Javadoc)
138 	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
139 	 */
140 	@Override
141 	public void start(BundleContext context) throws Exception {
142 		super.start(context);
143 		initConfig();
144 		initGene();
145 	}
146 	/*
147 	 * (non-Javadoc)
148 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
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 }