had to rename the packages to make them compliant with buckminster
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / TaxonEditorInputFactory.java
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/TaxonEditorInputFactory.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/TaxonEditorInputFactory.java
new file mode 100644 (file)
index 0000000..c5457f4
--- /dev/null
@@ -0,0 +1,84 @@
+// $Id$
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy 
+* http://www.e-taxonomy.eu
+* 
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.editor;
+
+import java.util.UUID;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.ui.IElementFactory;
+import org.eclipse.ui.IMemento;
+
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+
+/**
+ * <p>TaxonEditorInputFactory class.</p>
+ *
+ * @author p.ciardelli
+ * @created 23.04.2009
+ * @version 1.0
+ */
+public class TaxonEditorInputFactory implements IElementFactory {
+
+    /**
+     * Factory id. The workbench plug-in registers a factory by this name
+     * with the "org.eclipse.ui.elementFactories" extension point.
+     */
+    private static final String ID_FACTORY = "eu.etaxonomy.taxeditor.editor.name.taxonEditorInputFactory"; //$NON-NLS-1$
+       
+    /**
+     * Tag for the IFile.fullPath of the file resource.
+     */
+    private static final String TAXON_NODE_UUID = "uuid"; //$NON-NLS-1$
+    
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
+        */
+       /** {@inheritDoc} */
+       public IAdaptable createElement(IMemento memento) {
+        // Get the uuid
+        String taxonNodeUuid = memento.getString(TAXON_NODE_UUID);
+        if(! CdmStore.isActive()){
+               return null;
+        }
+        if (taxonNodeUuid == null || CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(UUID.fromString(taxonNodeUuid)) == null) {
+               EditorUtil.warn(this.getClass(), "Couldn't find taxon node with UUID " + taxonNodeUuid);
+               return null;
+        }
+        try {
+                       return TaxonEditorInput.NewInstance(UUID.fromString(taxonNodeUuid));
+               } catch (Exception e) {
+                       EditorUtil.warningDialog("Could not create element", TaxonEditorInputFactory.class, e.getMessage());
+               }
+               return null;
+       }       
+       
+    /**
+     * Returns the element factory id for this class.
+     *
+     * @return the element factory id
+     */
+    public static String getFactoryId() {
+        return ID_FACTORY;
+    }
+    
+    /**
+     * Saves the state of the given file editor input into the given memento.
+     *
+     * @param memento the storage area for element state
+     * @param input the file editor input
+     */
+    public static void saveState(IMemento memento, TaxonEditorInput input) {
+       String uuid = input.getTaxonNode().getUuid().toString();
+        memento.putString(TAXON_NODE_UUID, uuid);
+    }
+}