migrating to cdmlib-plugin 2.0.0.20 including new term loading
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / TaxEditorPlugin.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor;
11
12 import java.net.URL;
13 import java.util.HashMap;
14 import java.util.Locale;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.runtime.FileLocator;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.resource.FontRegistry;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.resource.ImageRegistry;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.FontData;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Tree;
30 import org.eclipse.ui.plugin.AbstractUIPlugin;
31 import org.eclipse.ui.views.properties.PropertySheetPage;
32 import org.osgi.framework.BundleContext;
33
34 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
35 import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
36 import eu.etaxonomy.cdm.database.DbSchemaValidation;
37 import eu.etaxonomy.cdm.database.ICdmDataSource;
38 import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
39 import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
40 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
41 import eu.etaxonomy.taxeditor.datasource.CdmTransactionController;
42 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
43
44 /**
45 * The class controlling the plug-in life cycle.
46 * </p>
47 * <ul>
48 * <li>Initializes datastore as necessary.</li>
49 * <li>Initializes CDM application controller.</li>
50 * <li>Initializes default preferences.</li>
51 * <li>Stores registries for colors, fonts, images.</li>
52 * </ul>
53 *
54 * @author p.ciardelli
55 * @created 15.05.2008
56 * @version 1.0
57 */
58 public class TaxEditorPlugin extends AbstractUIPlugin {
59 private static final Logger logger = Logger
60 .getLogger(TaxEditorPlugin.class);
61
62 /**
63 * The plug-in ID
64 */
65 public static final String PLUGIN_ID = "eu.etaxonomy.taxeditor.plugin";
66 /**
67 * The shared instance
68 */
69 private static TaxEditorPlugin plugin;
70
71 // private DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;
72 private DbSchemaValidation dbSchemaValidation = DbSchemaValidation.UPDATE;
73
74 /*
75 * (non-Javadoc)
76 *
77 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
78 */
79 public void start(BundleContext context) throws Exception {
80
81 super.start(context);
82 plugin = this;
83
84 // Check whether this is the first time the application has ever been run
85 checkInitialExecution();
86
87 // Initialize application controller
88 initApplicationController();
89
90 // Start a transaction
91 CdmTransactionController.startTransaction();
92
93 // Forgot what this is ...
94 Locale locale = new Locale("en", "", "icbn");
95 Locale.setDefault(locale);
96 }
97
98
99 /*
100 * (non-Javadoc)
101 *
102 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
103 */
104 public void stop(BundleContext context) throws Exception {
105 CdmTransactionController.commitTransaction();
106 plugin = null;
107 super.stop(context);
108
109 disposeColors();
110 }
111
112 /**
113 * Returns the shared instance
114 *
115 * @return the shared instance
116 */
117 public static TaxEditorPlugin getDefault() {
118 return plugin;
119 }
120
121 private void checkInitialExecution() {
122
123 // Check in the prefs store whether the application (i.e. its default
124 // data source) has been initialized
125 boolean initialized = false;
126 if (getPreferenceStore().contains(ITaxEditorConstants.INITIALIZED)) {
127 initialized = getPreferenceStore().getBoolean(
128 ITaxEditorConstants.INITIALIZED);
129 }
130
131 // If not ...
132 if (!initialized) {
133
134 // ... set validation to CREATE
135 logger.warn("Initializing datastore");
136 dbSchemaValidation = DbSchemaValidation.CREATE;
137
138 // ... and note in prefs that application has been initialized
139 getPreferenceStore().setValue(ITaxEditorConstants.INITIALIZED, true);
140 }
141 }
142
143 /***************************************************************************
144 * CDM SERVICES
145 **************************************************************************/
146
147 /**
148 * All CDM services are called via the application controller
149 */
150 private CdmApplicationController applicationController;
151
152 private CdmApplicationController initApplicationController() {
153
154 // Get the "current" - i.e. most recently used - data source
155 ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().
156 getCurrentDataSource();
157
158 // Initialize the controller with the data source
159 try {
160 applicationController = CdmApplicationController.NewInstance(cdmDatasource, dbSchemaValidation);
161 } catch (DataSourceNotFoundException e) {
162 // TODO user-friendly failure here
163 e.printStackTrace();
164 } catch (TermNotFoundException e) {
165 e.printStackTrace();
166 }
167
168 // Set application controller for objects that use it
169 CdmTransactionController.setApplicationController(applicationController);
170 CdmDataSourceRepository.getDefault().setCdmApplicationController(applicationController);
171 CdmSessionDataRepository.getDefault().setApplicationController(applicationController);
172
173 // Terms have to be initialized explicitly as of now
174 DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
175 vocabularyStore.initialize();
176
177 return applicationController;
178 }
179
180 public CdmApplicationController getApplicationController() {
181 if (applicationController == null) {
182 throw new IllegalStateException("CdmApplicationController not yet set.");
183 }
184 return applicationController;
185 }
186
187 /***************************************************************************
188 * IMAGE REGISTRY
189 **************************************************************************/
190 public ImageDescriptor getImageDescriptor(String key) {
191 return getImageRegistry().getDescriptor(key);
192 }
193
194 public Image getImage(String key) {
195 return getImageRegistry().get(key);
196 }
197
198 protected void initializeImageRegistry(ImageRegistry registry) {
199 registerImage(registry, ITaxEditorConstants.EDIT_ICON, "edit_16x16.ico");
200 registerImage(registry, ITaxEditorConstants.WARNING_ICON,
201 "warn_tsk.gif");
202 registerImage(registry, ITaxEditorConstants.BLACK_SQUARE_ICON,
203 "accepted_small.gif");
204 registerImage(registry, ITaxEditorConstants.HOMOTYPIC_SYN_ICON,
205 "homosyn_no_bg.gif");
206 registerImage(registry,
207 ITaxEditorConstants.HOMOTYPIC_SYN_ORIGINAL_ICON,
208 "homosyn_original_no_bg.gif");
209 registerImage(registry, ITaxEditorConstants.HETEROTYPIC_SYN_ICON,
210 "heterosyn_no_bg.gif");
211 registerImage(registry,
212 ITaxEditorConstants.HETEROTYPIC_SYN_ORIGINAL_ICON,
213 "heterosyn_original_no_bg.gif");
214 registerImage(registry, ITaxEditorConstants.MISAPPLIED_NAME_ICON,
215 "misapplied_no_bg.gif");
216 registerImage(registry, ITaxEditorConstants.CONCEPT_ICON,
217 "concept_no_bg.gif");
218 registerImage(registry, ITaxEditorConstants.AUTONYM_ICON,
219 "autonym_no_bg.gif");
220 registerImage(registry, ITaxEditorConstants.BASIONYM_ICON,
221 "basionym_no_bg.gif");
222 registerImage(registry, ITaxEditorConstants.ORTHOGRAPHIC_VARIANT_ICON,
223 "orthovariant_no_bg.gif");
224 registerImage(registry, ITaxEditorConstants.DB_ICON, "db.gif");
225 registerImage(registry, ITaxEditorConstants.MOVE_ICON,
226 "correction_change.gif");
227 registerImage(registry, ITaxEditorConstants.ACTIVE_DELETE_ICON,
228 "delete_edit.gif");
229 registerImage(registry, ITaxEditorConstants.SYNONYM_TO_TAXON_ICON,
230 "change.gif");
231 registerImage(registry, ITaxEditorConstants.OPEN_TAXON_ICON, "open.gif");
232 registerImage(registry, ITaxEditorConstants.ADD_CHILD_TAXON_ICON,
233 "new_child.gif");
234 registerImage(registry,
235 ITaxEditorConstants.SWAP_SYNONYM_AND_TAXON_ICON, "swap2.gif");
236 registerImage(registry, ITaxEditorConstants.QUICK_ADD_ICON,
237 "quick_add.gif");
238 registerImage(registry, ITaxEditorConstants.TAXON_TO_SYNONYM_ICON,
239 "tax_to_syn.gif");
240 registerImage(registry, ITaxEditorConstants.ERROR_ANNOTATION_ICON,
241 "error_co.gif");
242 registerImage(registry, ITaxEditorConstants.EDIT_BITMAP_ICON,
243 "256color_16x16.bmp");
244 }
245
246 private void registerImage(ImageRegistry registry, String key,
247 String fileName) {
248 try {
249 IPath path = new Path("icons/" + fileName); //$NON-NLS-1$
250 URL url = FileLocator.find(getBundle(), path, null);
251 if (url != null) {
252 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
253 registry.put(key, desc);
254 }
255 } catch (Exception e) {
256 }
257 }
258
259 /***************************************************************************
260 * FONT REGISTRY
261 **************************************************************************/
262 private FontRegistry fontRegistry;
263
264 private FontRegistry getFontRegistry() {
265 if (fontRegistry == null) {
266 fontRegistry = new FontRegistry(Display.getCurrent());
267
268 fontRegistry.put(ITaxEditorConstants.DATASOURCE_FONT,
269 new FontData[] { new FontData("Arial", 8, SWT.NONE) });
270 fontRegistry.put(ITaxEditorConstants.MENU_ITEM_ITALICS_FONT,
271 new FontData[] { new FontData("Arial", 9, SWT.ITALIC) });
272 fontRegistry.put(ITaxEditorConstants.ACCEPTED_TAXON_FONT,
273 new FontData[] { new FontData("Georgia", 12, SWT.NONE) });
274 fontRegistry.put(ITaxEditorConstants.SYNONYM_FONT,
275 new FontData[] { new FontData("Georgia", 10, SWT.NONE) });
276 fontRegistry.put(ITaxEditorConstants.MISAPPLIEDNAME_FONT,
277 new FontData[] { new FontData("Georgia", 10, SWT.NONE) });
278 fontRegistry.put(ITaxEditorConstants.CONCEPT_FONT,
279 new FontData[] { new FontData("Georgia", 10, SWT.NONE) });
280 fontRegistry.put(ITaxEditorConstants.CHOOSE_NAME_TEXT_FONT,
281 new FontData[] { new FontData("Arial", 12, SWT.BOLD) });
282 fontRegistry.put(ITaxEditorConstants.DEFAULT_PROMPT_FONT,
283 new FontData[] { new FontData("Georgia", 10, SWT.ITALIC) });
284 }
285 return fontRegistry;
286 }
287
288 public Font getFont(String key) {
289 return getFontRegistry().get(key);
290 }
291
292 /***************************************************************************
293 * COLOR MAP
294 **************************************************************************/
295 private static HashMap<String, Color> colorRegistry;
296
297 public Color getColor(String key) {
298 return getColorRegistry().get(key);
299 }
300
301 private HashMap<String, Color> getColorRegistry() {
302 if (colorRegistry == null) {
303 colorRegistry = new HashMap<String, Color>();
304
305 colorRegistry.put(ITaxEditorConstants.GROUP_GRAY_BKG_COLOR,
306 new Color(null, 240, 240, 240));
307 colorRegistry.put(ITaxEditorConstants.PROP_SHEET_RED,
308 new Color(null, 255, 0, 0));
309 }
310 return colorRegistry;
311 }
312
313 private void disposeColors() {
314 if (colorRegistry == null) {
315 return;
316 }
317 for (Color color : colorRegistry.values()) {
318 color.dispose();
319 }
320 colorRegistry.clear();
321 }
322
323 /***************************************************************************
324 * PROPERTY SHEET
325 **************************************************************************/
326
327 private Tree propertySheetTree;
328
329 public void setPropertySheetTree(Tree tree) {
330 this.propertySheetTree = tree;
331 }
332
333 public Tree getPropertySheetTree() {
334 return propertySheetTree;
335 }
336
337 private PropertySheetPage propertySheetPage;
338
339 public void setPropertySheetPage(PropertySheetPage page) {
340 this.propertySheetPage = page;
341 }
342
343 public PropertySheetPage getPropertySheetPage() {
344 return propertySheetPage;
345 }
346 }