correct handling of checklist perspective preference
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / LifeCycleManager.java
index 40aab6918b218017f3dbf2f455111b92d15bc74e..d971e3bf44eda50e65c4ef24c41b1c5eec9806d0 100644 (file)
@@ -20,8 +20,6 @@ import org.eclipse.e4.core.contexts.IEclipseContext;
 import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.di.UIEventTopic;
 import org.eclipse.e4.ui.model.application.MApplication;
-import org.eclipse.e4.ui.model.application.ui.MElementContainer;
-import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.workbench.UIEvents;
 import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
@@ -36,9 +34,11 @@ import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.views.IViewDescriptor;
 import org.osgi.service.event.Event;
 
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.taxeditor.editor.AppModelId;
 import eu.etaxonomy.taxeditor.model.IContextListener;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
@@ -57,6 +57,9 @@ public class LifeCycleManager implements IContextListener{
     @Inject
     private EModelService modelService;
 
+    @Inject
+    private EPartService partService;
+
     @Inject
     public LifeCycleManager() {
         CdmStore.getContextManager().addContextListener(this);
@@ -99,18 +102,28 @@ public class LifeCycleManager implements IContextListener{
     }
 
     private void hideParts() {
-        List<MPart> elements = modelService.findElements(application, null, MPart.class, Arrays.asList(NON_RESTORE));
-        for (MPart part : elements) {
-            MElementContainer<MUIElement> parent = part.getParent();
-            if(parent!=null){
-                if(parent.getSelectedElement()!=null
-                        && parent.getSelectedElement().equals(part)){
-                    parent.setSelectedElement(null);
+        if (CdmStore.isActive()) {
+            try{
+                List<MPart> elements = modelService.findElements(application, null, MPart.class, Arrays.asList(NON_RESTORE));
+
+                Collection<MPart> parts = partService.getParts();
+                for (MPart element : elements) {
+                    String elementId = element.getElementId();
+                    for (MPart part : parts) {
+                        String partID = part.getElementId();
+                        if (elementId.equals(partID)){
+                            partService.hidePart(part);
+                        }
+                    }
                 }
-                parent.getChildren().remove(part);
+            }catch(IllegalStateException e){
+                //application does not have an active window
             }
+
         }
 
+
+
     }
 
     /**
@@ -118,6 +131,7 @@ public class LifeCycleManager implements IContextListener{
      */
     @Override
     public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
+
     }
 
     /**
@@ -126,6 +140,19 @@ public class LifeCycleManager implements IContextListener{
     @Override
     public void contextStop(IMemento memento, IProgressMonitor monitor) {
         hideParts();
+        if (CdmStore.isActive()){
+            IWorkbench workbench = PlatformUI.getWorkbench();
+            IWorkbenchWindow window = workbench
+                    .getActiveWorkbenchWindow();
+            IViewDescriptor viewDescriptor = workbench.getViewRegistry().find(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4);
+            if(viewDescriptor == null){
+                memento.putBoolean("facts", false);
+            }
+            viewDescriptor = workbench.getViewRegistry().find(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4);
+            if(viewDescriptor == null){
+                memento.putBoolean("media", false);
+            }
+        }
     }
 
     /**
@@ -137,23 +164,26 @@ public class LifeCycleManager implements IContextListener{
         PreferencesUtil.updateDBPreferences();
 
         hideParts();
+
+        IWorkbench workbench = PlatformUI.getWorkbench();
+        IWorkbenchWindow window = workbench
+                .getActiveWorkbenchWindow();
         if (PreferencesUtil.getBooleanValue(PreferencePredicate.ShowChecklistPerspective.getKey())){
-            IWorkbench workbench = PlatformUI.getWorkbench();
-            IWorkbenchWindow window = workbench
-                    .getActiveWorkbenchWindow();
+
 
             IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
             IWorkbenchPage page = window.getActivePage();
             page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.perspective.checklistperspective"));
         }else{
-            IWorkbench workbench = PlatformUI.getWorkbench();
-            IWorkbenchWindow window = workbench
-                    .getActiveWorkbenchWindow();
+
 
             IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
             IWorkbenchPage page = window.getActivePage();
             page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.application.perspective.taxonomic"));
+
         }
+        hideParts();
+
         PreferencesUtil.checkNomenclaturalCode();
     }