48e92ffa1bce34c7411e223c7ecef06a80cc131a
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / TaxonEditorInputFactory.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor;
12
13 import java.util.UUID;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.ui.IElementFactory;
18 import org.eclipse.ui.IMemento;
19
20 import eu.etaxonomy.taxeditor.model.TaxonUtil;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22 import eu.etaxonomy.taxeditor.store.StoreUtil;
23
24
25 /**
26 * @author p.ciardelli
27 * @created 23.04.2009
28 * @version 1.0
29 */
30 public class TaxonEditorInputFactory implements IElementFactory {
31 private static final Logger logger = Logger
32 .getLogger(TaxonEditorInputFactory.class);
33
34 /**
35 * Factory id. The workbench plug-in registers a factory by this name
36 * with the "org.eclipse.ui.elementFactories" extension point.
37 */
38 private static final String ID_FACTORY = "eu.etaxonomy.taxeditor.editor.TaxonEditorInputFactory"; //$NON-NLS-1$
39
40 /**
41 * Tag for the IFile.fullPath of the file resource.
42 */
43 private static final String TAXON_UUID = "uuid"; //$NON-NLS-1$
44
45 /* (non-Javadoc)
46 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
47 */
48 public IAdaptable createElement(IMemento memento) {
49 // Get the uuid
50 String uuid = memento.getString(TAXON_UUID);
51 if (uuid == null) {
52 return null;
53 }
54 // StoreUtil.checktaxonExists(UUID.fromString(uuid));
55 if (CdmStore.getTaxonService().getTaxonByUuid(UUID.fromString(uuid)) == null) {
56 logger.warn("Couldn't find taxon with UUID " + uuid);
57 return null;
58 }
59 logger.info("Creating taxon from memento w UUID " + uuid);
60 return TaxonEditorInput.NewInstance(UUID.fromString(uuid));
61 }
62
63 /**
64 * Returns the element factory id for this class.
65 *
66 * @return the element factory id
67 */
68 public static String getFactoryId() {
69 return ID_FACTORY;
70 }
71
72 /**
73 * Saves the state of the given file editor input into the given memento.
74 *
75 * @param memento the storage area for element state
76 * @param input the file editor input
77 */
78 public static void saveState(IMemento memento, TaxonEditorInput input) {
79 String uuid = input.getTaxon().getUuid().toString();
80 memento.putString(TAXON_UUID, uuid);
81 }
82 }