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.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("/"); //$NON-NLS-1$
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"; //$NON-NLS-1$
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"; //$NON-NLS-1$
75  	/***
76  	 * Finds all exporters and coordinators and registers them to GENE.
77  	 */
78  	private void initGene() {
79  		// register exporter and coordinator factories using the extension
80  		// points. EuroMath2 itself defines some default factories extending
81  		// its own extension points.
82  		final IExtensionRegistry r = Platform.getExtensionRegistry();
83  		// editor factories
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"); //$NON-NLS-1$
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", //$NON-NLS-1$
100 						ex);
101 			}
102 		}
103 		// renderer factories
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"); //$NON-NLS-1$
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); //$NON-NLS-1$
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."); //$NON-NLS-1$
129 		return instance;
130 	}
131 	/***
132 	 * @return plugin id.
133 	 */
134 	public static String getID() {
135 		return "sk.uniba.euromath"; //$NON-NLS-1$
136 	}
137 	/*
138 	 * (non-Javadoc)
139 	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
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 	 * (non-Javadoc)
150 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
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 }