+++ /dev/null
-/**
-* 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.HashSet;
-import java.util.Set;
-import java.util.UUID;
-
-import org.apache.log4j.Logger;
-import org.eclipse.jface.resource.ImageDescriptor;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IMemento;
-import org.eclipse.ui.IPersistableElement;
-
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
-import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
-import eu.etaxonomy.cdm.model.taxon.ITreeNode;
-import eu.etaxonomy.cdm.model.taxon.Synonym;
-import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
-import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.taxeditor.model.DataChangeBridge;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.store.CdmStore;
-
-/**
- * @author n.hoffmann
- * @created 19.03.2009
- * @version 1.0
- */
-public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
- private static final Logger logger = Logger
- .getLogger(TaxonEditorInput.class);
-
- private ConversationHolder conversation;
-
- private TaxonNode taxonNode;
-
- private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
-
- private TaxonBase initiallySelectedTaxonBase;
-
- private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
- this.conversation = conversation;
- this.taxonNode = taxonNode;
- logger.trace(TaxonEditorInput.class.getSimpleName() + " created");
- }
-
-
-
- public static TaxonEditorInput NewInstance(UUID taxonNodeUuid) throws Exception{
- try{
- ConversationHolder conversation = CdmStore.createConversation();
- return NewInstance(taxonNodeUuid, conversation);
- }catch(Exception e){
- throw e;
- }
- }
-
- /**
- *
- * @param taxonNodeUuid
- * @param conversation
- * @return
- */
- private static TaxonEditorInput NewInstance(UUID taxonNodeUuid, ConversationHolder conversation){
-
-
- TaxonNode taxonNode = CdmStore.getTaxonNodeService().load(taxonNodeUuid, null);
-
- if(taxonNode == null){
- EditorUtil.warningDialog("Not yet implemented", "Selected element is not type TaxonBase.");
- return null;
- }
-
- return new TaxonEditorInput(taxonNode, conversation);
- }
-
- public static TaxonEditorInput NewInstanceFromTaxonBase(UUID taxonBaseUuid){
- ConversationHolder conversation = CdmStore.createConversation();
-
- TaxonEditorInput input = null;
-
- TaxonBase taxonBase = CdmStore.getTaxonService().find(taxonBaseUuid);
-
- if(taxonBase instanceof Taxon){
- Taxon taxon = (Taxon) taxonBase;
-
- if (taxon.isMisappliedName()){
- // TODO get accepted taxon
- logger.warn("trying to open Mispplied Name ");
-
- Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
- Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
- for(TaxonRelationship relation : relations){
- if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
- acceptedTaxa.add(relation.getToTaxon());
- }
- }
- input = getInputForMultipleTaxa(conversation, acceptedTaxa);
-
- }else{
- input = getInputForMultipleNodes(conversation, taxon.getTaxonNodes());
- }
- }else if(taxonBase instanceof Synonym){
- Synonym synonym = (Synonym) taxonBase;
-
- Set<Taxon> taxa = synonym.getAcceptedTaxa();
- input = getInputForMultipleTaxa(conversation, taxa);
- }
-
- input.setInitiallySelectedTaxonBase(taxonBase);
-
- return input;
- }
-
-
-
-
- private static TaxonEditorInput getInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
- if(taxonNodes.size() == 1){
- TaxonNode taxonNode = taxonNodes.iterator().next();
- return NewInstance(taxonNode.getUuid(), conversation);
- }else if(taxonNodes.size() > 1){
- // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
- EditorUtil.warningDialog("Not implemented yet", "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
- " This case is not handled yet by the software.");
- }else if(taxonNodes.size() == 0){
- // this is an undesired state
- EditorUtil.warningDialog("Incorrect state", "The accepted taxon is not in a taxonomic view. This should not have happened.");
- }
- return null;
- }
-
- private static TaxonEditorInput getInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
- if(taxa.size() == 1){
- Taxon taxon = taxa.iterator().next();
- Set<TaxonNode> nodes = taxon.getTaxonNodes();
- return getInputForMultipleNodes(conversation, nodes);
- }else if(taxa.size() > 1){
- // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
- EditorUtil.warningDialog("Not implemented yet", "There are multiple accepted taxa for the current selection. We currently do not know which one you want to open." +
- " This case is not handled yet by the software.");
- }else if(taxa.size() == 0){
- // this is an undesired state
- EditorUtil.warningDialog("Incorrect state", "Trying to open accepted taxon for a synonym or misapplication but" +
- " no accepted taxa are present. This should not have happened.");
- }
- return null;
- }
-
- public static TaxonEditorInput NewEmptyInstance(UUID parentNodeUuid){
- ConversationHolder conversation = CdmStore.createConversation();
-
- TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
- ITreeNode parentNode = CdmStore.getTaxonTreeService().getTreeNodeByUuid(parentNodeUuid);
-
- Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
- TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference(), null);
-
- // add the new taxon to the editors persistence context
- UUID newTaxonNodeUuid = CdmStore.getTaxonNodeService().save(newTaxonNode);
-
- return new TaxonEditorInput(newTaxonNode, conversation);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IEditorInput#exists()
- */
- public boolean exists() {
- return taxonNode != null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
- */
- public ImageDescriptor getImageDescriptor() {
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IEditorInput#getName()
- */
- public String getName() {
- if(getTaxon() == null){
- return null;
- }
- TaxonNameBase<?, ?> name = getTaxon().getName();
- if (name == null || name.getTitleCache() == null) {
- return "New taxon";
- } else {
- return name.getTitleCache();
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IEditorInput#getPersistable()
- */
- public IPersistableElement getPersistable() {
-// if(CdmStore.isActive()){
-// TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
-// boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
-// if (isPersistable) {
-// return this;
-// }
-// }
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IEditorInput#getToolTipText()
- */
- public String getToolTipText() {
- return getName();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
- */
- public Object getAdapter(Class adapter) {
-
- if (adapter == Taxon.class) {
- return taxonNode.getTaxon();
- }
-
- if (adapter == TaxonNode.class) {
- return taxonNode;
- }
-
- return null;
- }
-
- /**
- * Overrides equals to ensure that a taxon can only be edited by
- * one editor at a time.
- *
- * @return boolean
- */
- public boolean equals(Object obj) {
- if (TaxonEditorInput.class.equals(obj.getClass())
- && getTaxon() != null
- && getTaxon().equals(((TaxonEditorInput) obj).getTaxon()))
- return true;
- return false;
- }
-
- /**
- * @return the taxon
- */
- public Taxon getTaxon(){
- return taxonNode.getTaxon();
- }
-
- /**
- * @return the taxonNode
- */
- public TaxonNode getTaxonNode() {
- return taxonNode;
- }
-
- /*
- * (non-Javadoc)
- * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
- */
- public ConversationHolder getConversationHolder() {
- return conversation;
- }
-
- /*
- * (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
- */
- public void update(CdmDataChangeMap events) {
- if(dataChangeBehavior == null){
- dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
- }
-
- DataChangeBridge.handleDataChange(events, dataChangeBehavior);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPersistableElement#getFactoryId()
- */
- public String getFactoryId() {
- return TaxonEditorInputFactory.getFactoryId();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
- */
- public void saveState(IMemento memento) {
- TaxonEditorInputFactory.saveState(memento, this);
- }
-
-
- private void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
- this.initiallySelectedTaxonBase = taxonBase;
- }
-
- public TaxonBase getInitiallySelectedTaxonBase() {
- return initiallySelectedTaxonBase;
- }
-}