Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / KeyEditor.java
index d45c500b7e9073e8abbe6137248a7cfbc30fa797..b6ca9645abd52bb6e75739fd5a5f90b63c2d3fe1 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
  * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy 
+ * 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.key;
 
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.forms.editor.FormEditor;
 
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
+import eu.etaxonomy.cdm.api.application.ICdmChangeListener;
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.taxeditor.editor.EditorUtil;
+import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyGraphEditor;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
-import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
+import eu.etaxonomy.taxeditor.model.DataChangeBridge;
+import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
  * @author n.hoffmann
@@ -29,19 +39,35 @@ import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
  * @version 1.0
  */
 public class KeyEditor extends FormEditor implements IConversationEnabled,
-               IDirtyMarkableSelectionProvider, IPartContentHasDetails {
+               IDirtyMarkable, IPartContentHasDetails, ICdmChangeListener {
 
        public static final String ID = "eu.etaxonomy.taxeditor.editor.key";
 
        private ConversationHolder conversation;
 
-       private AbstractIdentificationEditorInput input;
-
        private boolean dirty;
 
+       private KeyEditorDataChangeBehaviour dataChangeBehavior;
+
+       private PolytomousKeyEditorInput polytomousKeyEditorInput;
+
+       /*
+     * (non-Javadoc)
+     *
+     * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
+     * org.eclipse.ui.IEditorInput)
+     */
+    @Override
+    public void init(IEditorSite site, IEditorInput input)
+            throws PartInitException {
+        polytomousKeyEditorInput = (PolytomousKeyEditorInput)input;
+        CdmApplicationState.getCurrentDataChangeService().register(this);
+        super.init(site, input);
+    }
+
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
         * IProgressMonitor)
         */
@@ -49,7 +75,9 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
        public void doSave(IProgressMonitor monitor) {
                try {
                        monitor.beginTask("Saving Editor", 1);
+                       getConversationHolder().bind();
                        getConversationHolder().commit(true);
+                       polytomousKeyEditorInput.merge();
                        setDirty(false);
                        monitor.worked(1);
                } finally {
@@ -59,7 +87,7 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.part.EditorPart#doSaveAs()
         */
        @Override
@@ -69,7 +97,7 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
         */
        @Override
@@ -77,21 +105,31 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
                return false;
        }
 
+       @Override
+       public void setFocus() {
+               getConversationHolder().commit(true);
+               getConversationHolder().bind();
+               polytomousKeyEditorInput.bind();
+               super.setFocus();
+       }
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see
         * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
         * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
         */
        @Override
-       public void update(CdmDataChangeMap arg0) {
-               // TODO Auto-generated method stub
+       public void update(CdmDataChangeMap map) {
+               if(dataChangeBehavior == null){
+                       dataChangeBehavior = new KeyEditorDataChangeBehaviour(this);
+               }
+               DataChangeBridge.handleDataChange(map, dataChangeBehavior);
        }
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see
         * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
         * ()
@@ -103,7 +141,7 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see
         * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
         * java.lang.Object)
@@ -111,11 +149,23 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
        @Override
        public void changed(Object element) {
                editorDirtyStateChanged();
+               IEditorPart activeEditor = getActiveEditor();
+               if (activeEditor instanceof IDirtyMarkable) {
+                       ((IDirtyMarkable) activeEditor).changed(element);
+               }
        }
 
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
+     */
+    @Override
+    public void forceDirty() {
+        changed(null);
+    }
+
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
         */
        @Override
@@ -139,7 +189,7 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
         */
        @Override
@@ -149,35 +199,62 @@ public class KeyEditor extends FormEditor implements IConversationEnabled,
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.forms.editor.FormEditor#dispose()
         */
        @Override
        public void dispose() {
                conversation.unregisterForDataStoreChanges(this);
                conversation.close();
+               polytomousKeyEditorInput.dispose();
+               CdmApplicationState.getCurrentDataChangeService().unregister(this);
                super.dispose();
        }
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
         */
        @Override
        protected void addPages() {
-               input = (AbstractIdentificationEditorInput) getEditorInput();
-               conversation = input.getConversationHolder();
+               conversation = polytomousKeyEditorInput.getConversationHolder();
                conversation.registerForDataStoreChanges(this);
 
                try {
-                       addPage(0, new PolytomousKeyListEditor(), input);
+                       addPage(0, new PolytomousKeyListEditor(this), polytomousKeyEditorInput);
                        setPageText(0, "List");
-                       addPage(1, new PolytomousKeyGraphEditor(), input);
+                       addPage(1, new PolytomousKeyGraphEditor(this), polytomousKeyEditorInput);
                        setPageText(1, "Graph");
                } catch (PartInitException e) {
-                       EditorUtil.error(getClass(), e);
+                       MessagingUtils.error(getClass(), e);
                }
 
+
+
+               setPartName();
+
        }
+
+       /**
+        *
+        */
+       public void setPartName() {
+               PolytomousKey key = polytomousKeyEditorInput.getKey();
+               setPartName(key.getTitleCache());
+       }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
+     */
+    @Override
+    public void onChange(CdmChangeEvent event) {
+        //editorDirtyStateChanged();
+        IEditorPart activeEditor = getActiveEditor();
+        if (activeEditor instanceof IDirtyMarkable) {
+            for(CdmBase cdmBase : event.getChangedObjects()) {
+                ((IDirtyMarkable) activeEditor).changed(cdmBase);
+            }
+        }
+    }
 }