Fix expansion state preference for details and suppData view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / AbstractCdmDataViewerE4.java
index 120fa100fcfa2d5dcc6267a1c528814f0d11b2ad..7cfea33174acbfba766e5aeedcc497af8fc526fa 100644 (file)
@@ -9,19 +9,29 @@
 
 package eu.etaxonomy.taxeditor.view.e4;
 
-import org.eclipse.jface.viewers.StructuredSelection;
+import javax.inject.Inject;
+
+import org.eclipse.e4.core.contexts.ContextInjectionFactory;
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.services.log.Logger;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTException;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.forms.IFormPart;
 import org.eclipse.ui.forms.ManagedForm;
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
 import org.eclipse.ui.forms.widgets.ScrolledForm;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
@@ -36,6 +46,8 @@ import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
  *
  */
 public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConversationEnabled{
+        @Inject
+        private Logger logger;
 
        protected ManagedForm managedForm;
 
@@ -43,17 +55,24 @@ public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConvers
 
        protected ScrolledForm scrolledForm;
 
-       private final Composite body;
+       private Composite body;
 
        protected RootElement rootElement;
 
+       @Inject
+       protected IEclipseContext context;
+
        //TODO: create a super class for this?
        private Object input;
 
-       private final IDirtyMarkable part;
+       protected IDirtyMarkable part;
 
+       @Inject
+       public AbstractCdmDataViewerE4() {
+
+       }
 
-       public AbstractCdmDataViewerE4(Composite parent, IDirtyMarkable part) {
+       public void init(Composite parent, IDirtyMarkable part) {
            this.part = part;
 
                managedForm = new ManagedForm(parent){
@@ -76,17 +95,17 @@ public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConvers
        }
 
 
-    protected void createEmptySection(RootElement parent) {
+    protected void createEmptySection(String message, RootElement parent) {
         destroySections();
 
-        EmptySection emptySection = formFactory.createEmptySection(formFactory, parent, SWT.NONE);
+        EmptySection emptySection = formFactory.createEmptySection(message, formFactory, parent, SWT.NONE);
 
         addPart(emptySection);
     }
 
-    public void showEmptyPage(){
-        destroySections();
-        createEmptySection(rootElement);
+    public void showEmptyPage(String message){
+       destroySections();
+        createEmptySection(message, rootElement);
     }
 
     protected void markViewPartDirty(){
@@ -98,18 +117,21 @@ public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConvers
                        formFactory.dispose();
                        formFactory = null;
                }
-               formFactory = new CdmFormFactory(Display.getCurrent(), this);
+               formFactory =  new CdmFormFactory(Display.getCurrent(), this);
+               ContextInjectionFactory.inject(formFactory, context);
        }
 
        /** {@inheritDoc} */
        @Override
        public Control getControl() {
-               if(body.isDisposed()){
-                       return null;
-               }
-               for(Control child : body.getChildren()){
-                       return child;
-               }
+           if(body!=null){
+               if(body.isDisposed()){
+                   return null;
+               }
+               for(Control child : body.getChildren()){
+                   return child;
+               }
+           }
 
                return body;
        }
@@ -119,9 +141,11 @@ public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConvers
        public void setInput(Object input) {
                this.input = input;
                if(input!=null){
-                   // reset selection
-                   setSelection(new StructuredSelection(input));
-                   refresh();
+                   try{
+                       refresh();
+                   }catch(CdmEagerLoadingException e){
+                       logger.error(e);
+                   }
                }
        }
 
@@ -136,20 +160,38 @@ public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConvers
        public void refresh() {
                showParts();
 
-               managedForm.setInput(input);
+               if(input!=null){
+                   managedForm.setInput(input);
+               }
 
                managedForm.refresh();
 
-               managedForm.reflow(true);
+//             managedForm.reflow(true);
        }
 
        public void layout(){
                body.layout();
        }
 
+       protected int getSectionStyle(Class<? extends AbstractFormSection> clazz){
+           return getSectionStyle(clazz, false);
+       }
+
+    protected int getSectionStyle(Class<? extends AbstractFormSection> clazz, boolean initiallyExpanded){
+        int style = ExpandableComposite.TWISTIE;
+        String prefKey = StoreUtil.getPrefKey(clazz, getInput());
+        if(PreferencesUtil.contains(prefKey)){
+            style = PreferencesUtil.getStringValue(prefKey)==CdmSectionPart.EXPANDED?style |= ExpandableComposite.EXPANDED:style;
+        }
+        else{
+            style = initiallyExpanded?style |= ExpandableComposite.EXPANDED:style;
+        }
+        return style;
+    }
+
        protected abstract void showParts();
 
-       protected void destroySections() {
+       public void destroySections() {
                for (IFormPart formPart : managedForm.getParts()){
                        removePart((CdmSectionPart<?>) formPart);
                }
@@ -163,7 +205,21 @@ public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConvers
                rootElement = new RootElement(formFactory, body);
 
                for(Control control : body.getChildren()){
-                       control.dispose();
+                       try{
+                               if (control != null && !control.isDisposed()){
+                                       control.dispose();
+                               }
+                       }catch(SWTException e){
+                               if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
+                    MessagingUtils.errorDialog("Widget is disposed",
+                            null,
+                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
+                            null,
+                            e,
+                            true);
+
+                }
+                       }
                        control = null;
                }
        }