f8bd22a3fab9210cb856ea17f5c565e47a0e64e2
[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.store.CdmStore;
21
22
23 /**
24 * @author p.ciardelli
25 * @created 23.04.2009
26 * @version 1.0
27 */
28 public class TaxonEditorInputFactory implements IElementFactory {
29 private static final Logger logger = Logger
30 .getLogger(TaxonEditorInputFactory.class);
31
32 /**
33 * Factory id. The workbench plug-in registers a factory by this name
34 * with the "org.eclipse.ui.elementFactories" extension point.
35 */
36 private static final String ID_FACTORY = "eu.etaxonomy.taxeditor.editor.TaxonEditorInputFactory"; //$NON-NLS-1$
37
38 /**
39 * Tag for the IFile.fullPath of the file resource.
40 */
41 private static final String TAXON_UUID = "uuid"; //$NON-NLS-1$
42
43 /* (non-Javadoc)
44 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
45 */
46 public IAdaptable createElement(IMemento memento) {
47 // Get the uuid
48 String uuid = memento.getString(TAXON_UUID);
49 if (uuid == null) {
50 return null;
51 }
52 if (CdmStore.getTaxonService().getTaxonByUuid(UUID.fromString(uuid)) == null) {
53 logger.warn("Couldn't find taxon with UUID " + uuid);
54 return null;
55 }
56 logger.info("Creating taxon from memento w UUID " + uuid);
57 return TaxonEditorInput.NewInstance(UUID.fromString(uuid));
58 }
59
60 /**
61 * Returns the element factory id for this class.
62 *
63 * @return the element factory id
64 */
65 public static String getFactoryId() {
66 return ID_FACTORY;
67 }
68
69 /**
70 * Saves the state of the given file editor input into the given memento.
71 *
72 * @param memento the storage area for element state
73 * @param input the file editor input
74 */
75 public static void saveState(IMemento memento, TaxonEditorInput input) {
76 String uuid = input.getTaxon().getUuid().toString();
77 memento.putString(TAXON_UUID, uuid);
78 }
79 }