Nothing works.
[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.databinding.observable.list.WritableList;
18 import org.eclipse.core.runtime.FileLocator;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.resource.FontRegistry;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.resource.ImageRegistry;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Color;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.graphics.FontData;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Tree;
31 import org.eclipse.ui.plugin.AbstractUIPlugin;
32 import org.eclipse.ui.views.properties.PropertySheetPage;
33 import org.osgi.framework.BundleContext;
34
35 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
36 import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
37 import eu.etaxonomy.cdm.database.DbSchemaValidation;
38 import eu.etaxonomy.cdm.database.ICdmDataSource;
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 return applicationController;
174 }
175
176 public CdmApplicationController getApplicationController() {
177 if (applicationController == null) {
178 throw new IllegalStateException("CdmApplicationController not yet set.");
179 }
180 return applicationController;
181 }
182
183 /***************************************************************************
184 * IMAGE REGISTRY
185 **************************************************************************/
186 public ImageDescriptor getImageDescriptor(String key) {
187 return getImageRegistry().getDescriptor(key);
188 }
189
190 public Image getImage(String key) {
191 return getImageRegistry().get(key);
192 }
193
194 protected void initializeImageRegistry(ImageRegistry registry) {
195 registerImage(registry, ITaxEditorConstants.EDIT_ICON, "edit_16x16.ico");
196 registerImage(registry, ITaxEditorConstants.WARNING_ICON,
197 "warn_tsk.gif");
198 registerImage(registry, ITaxEditorConstants.BLACK_SQUARE_ICON,
199 "accepted_small.gif");
200 registerImage(registry, ITaxEditorConstants.HOMOTYPIC_SYN_ICON,
201 "homosyn_no_bg.gif");
202 registerImage(registry,
203 ITaxEditorConstants.HOMOTYPIC_SYN_ORIGINAL_ICON,
204 "homosyn_original_no_bg.gif");
205 registerImage(registry, ITaxEditorConstants.HETEROTYPIC_SYN_ICON,
206 "heterosyn_no_bg.gif");
207 registerImage(registry,
208 ITaxEditorConstants.HETEROTYPIC_SYN_ORIGINAL_ICON,
209 "heterosyn_original_no_bg.gif");
210 registerImage(registry, ITaxEditorConstants.MISAPPLIED_NAME_ICON,
211 "misapplied_no_bg.gif");
212 registerImage(registry, ITaxEditorConstants.CONCEPT_ICON,
213 "concept_no_bg.gif");
214 registerImage(registry, ITaxEditorConstants.AUTONYM_ICON,
215 "autonym_no_bg.gif");
216 registerImage(registry, ITaxEditorConstants.BASIONYM_ICON,
217 "basionym_no_bg.gif");
218 registerImage(registry, ITaxEditorConstants.ORTHOGRAPHIC_VARIANT_ICON,
219 "orthovariant_no_bg.gif");
220 registerImage(registry, ITaxEditorConstants.DB_ICON, "db.gif");
221 registerImage(registry, ITaxEditorConstants.MOVE_ICON,
222 "correction_change.gif");
223 registerImage(registry, ITaxEditorConstants.ACTIVE_DELETE_ICON,
224 "delete_edit.gif");
225 registerImage(registry, ITaxEditorConstants.SYNONYM_TO_TAXON_ICON,
226 "change.gif");
227 registerImage(registry, ITaxEditorConstants.OPEN_TAXON_ICON, "open.gif");
228 registerImage(registry, ITaxEditorConstants.ADD_CHILD_TAXON_ICON,
229 "new_child.gif");
230 registerImage(registry,
231 ITaxEditorConstants.SWAP_SYNONYM_AND_TAXON_ICON, "swap2.gif");
232 registerImage(registry, ITaxEditorConstants.QUICK_ADD_ICON,
233 "quick_add.gif");
234 registerImage(registry, ITaxEditorConstants.TAXON_TO_SYNONYM_ICON,
235 "tax_to_syn.gif");
236 registerImage(registry, ITaxEditorConstants.ERROR_ANNOTATION_ICON,
237 "quickfix_error_obj.gif");
238 registerImage(registry, ITaxEditorConstants.EDIT_BITMAP_ICON,
239 "256color_16x16.bmp");
240 }
241
242 private void registerImage(ImageRegistry registry, String key,
243 String fileName) {
244 try {
245 IPath path = new Path("icons/" + fileName); //$NON-NLS-1$
246 URL url = FileLocator.find(getBundle(), path, null);
247 if (url != null) {
248 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
249 registry.put(key, desc);
250 }
251 } catch (Exception e) {
252 }
253 }
254
255 /***************************************************************************
256 * FONT REGISTRY
257 **************************************************************************/
258 private FontRegistry fontRegistry;
259
260 private FontRegistry getFontRegistry() {
261 if (fontRegistry == null) {
262 fontRegistry = new FontRegistry(Display.getCurrent());
263
264 fontRegistry.put(ITaxEditorConstants.DATASOURCE_FONT,
265 new FontData[] { new FontData("Arial", 8, SWT.NONE) });
266 fontRegistry.put(ITaxEditorConstants.MENU_ITEM_ITALICS_FONT,
267 new FontData[] { new FontData("Arial", 9, SWT.ITALIC) });
268 fontRegistry.put(ITaxEditorConstants.ACCEPTED_TAXON_FONT,
269 new FontData[] { new FontData("Georgia", 12, SWT.NONE) });
270 fontRegistry.put(ITaxEditorConstants.SYNONYM_FONT,
271 new FontData[] { new FontData("Georgia", 10, SWT.NONE) });
272 fontRegistry.put(ITaxEditorConstants.MISAPPLIEDNAME_FONT,
273 new FontData[] { new FontData("Georgia", 10, SWT.NONE) });
274 fontRegistry.put(ITaxEditorConstants.CONCEPT_FONT,
275 new FontData[] { new FontData("Georgia", 10, SWT.NONE) });
276 fontRegistry.put(ITaxEditorConstants.CHOOSE_NAME_TEXT_FONT,
277 new FontData[] { new FontData("Arial", 12, SWT.BOLD) });
278 fontRegistry.put(ITaxEditorConstants.DEFAULT_PROMPT_FONT,
279 new FontData[] { new FontData("Georgia", 10, SWT.ITALIC) });
280 }
281 return fontRegistry;
282 }
283
284 public Font getFont(String key) {
285 return getFontRegistry().get(key);
286 }
287
288 /***************************************************************************
289 * COLOR MAP
290 **************************************************************************/
291 private static HashMap<String, Color> colorRegistry;
292
293 public Color getColor(String key) {
294 return getColorRegistry().get(key);
295 }
296
297 private HashMap<String, Color> getColorRegistry() {
298 if (colorRegistry == null) {
299 colorRegistry = new HashMap<String, Color>();
300
301 colorRegistry.put(ITaxEditorConstants.GROUP_GRAY_BKG_COLOR,
302 new Color(null, 240, 240, 240));
303 colorRegistry.put(ITaxEditorConstants.PROP_SHEET_RED,
304 new Color(null, 255, 0, 0));
305 }
306 return colorRegistry;
307 }
308
309 private void disposeColors() {
310 if (colorRegistry == null) {
311 return;
312 }
313 for (Color color : colorRegistry.values()) {
314 color.dispose();
315 }
316 colorRegistry.clear();
317 }
318
319 /***************************************************************************
320 * PROPERTY SHEET
321 **************************************************************************/
322
323 private Tree propertySheetTree;
324
325 public void setPropertySheetTree(Tree tree) {
326 this.propertySheetTree = tree;
327 }
328
329 public Tree getPropertySheetTree() {
330 return propertySheetTree;
331 }
332
333 private PropertySheetPage propertySheetPage;
334
335 public void setPropertySheetPage(PropertySheetPage page) {
336 this.propertySheetPage = page;
337 }
338
339 public PropertySheetPage getPropertySheetPage() {
340 return propertySheetPage;
341 }
342 }