Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / TaxonEditorInputFactory.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.editor;
11
12 import java.util.UUID;
13
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.ui.IElementFactory;
16 import org.eclipse.ui.IMemento;
17
18 import eu.etaxonomy.cdm.api.service.IClassificationService;
19 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
20 import eu.etaxonomy.taxeditor.model.MessagingUtils;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22
23
24 /**
25 * <p>TaxonEditorInputFactory class.</p>
26 *
27 * @author p.ciardelli
28 * @created 23.04.2009
29 * @version 1.0
30 */
31 public class TaxonEditorInputFactory implements IElementFactory {
32
33 /**
34 * Factory id. The workbench plug-in registers a factory by this name
35 * with the "org.eclipse.ui.elementFactories" extension point.
36 */
37 private static final String ID_FACTORY = "eu.etaxonomy.taxeditor.editor.name.taxonEditorInputFactory"; //$NON-NLS-1$
38
39 /**
40 * Tag for the IFile.fullPath of the file resource.
41 */
42 private static final String TAXON_NODE_UUID = "uuid"; //$NON-NLS-1$
43
44 /* (non-Javadoc)
45 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
46 */
47 /** {@inheritDoc} */
48 public IAdaptable createElement(IMemento memento) {
49 // Get the uuid
50 String taxonNodeUuid = memento.getString(TAXON_NODE_UUID);
51 if(! CdmStore.isActive()){
52 return null;
53 }
54 if (taxonNodeUuid == null || CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(UUID.fromString(taxonNodeUuid)) == null) {
55 MessagingUtils.warn(this.getClass(), Messages.TaxonEditorInputFactory_NOT_FOUND_TAXON + taxonNodeUuid);
56 return null;
57 }
58 try {
59 return TaxonEditorInput.NewInstance(UUID.fromString(taxonNodeUuid));
60 } catch (Exception e) {
61 MessagingUtils.warningDialog(Messages.TaxonEditorInputFactory_COULD_NOT_CREATE, TaxonEditorInputFactory.class, e.getMessage());
62 }
63 return null;
64 }
65
66 /**
67 * Returns the element factory id for this class.
68 *
69 * @return the element factory id
70 */
71 public static String getFactoryId() {
72 return ID_FACTORY;
73 }
74
75 /**
76 * Saves the state of the given file editor input into the given memento.
77 *
78 * @param memento the storage area for element state
79 * @param input the file editor input
80 */
81 public static void saveState(IMemento memento, TaxonEditorInput input) {
82 String uuid = input.getTaxonNode().getUuid().toString();
83 memento.putString(TAXON_NODE_UUID, uuid);
84 }
85 }