Fix type check in CdmViewerChooser
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / sessions / SessionsViewPart.java
index baf62fab72d80a5b53a41ab976581ed1b3fd945e..9947664f697bc0e42968293e65e2a22d5690f3d9 100644 (file)
@@ -1,18 +1,19 @@
 // $Id$
 /**
-* Copyright (C) 2015 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.
-*/
+ * Copyright (C) 2015 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.view.sessions;
 
 import java.text.DecimalFormat;
 import java.util.Collection;
 
 import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.statistics.LiveCacheStatistics;
 
 import org.eclipse.jface.action.IMenuManager;
@@ -33,10 +34,12 @@ import org.eclipse.ui.part.ViewPart;
 import org.eclipse.wb.swt.SWTResourceManager;
 
 import eu.etaxonomy.cdm.api.cache.CdmCacher;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
+import eu.etaxonomy.taxeditor.session.NullSession;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
@@ -47,6 +50,7 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
 public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManagerObserver {
 
     public static final String ID = "eu.etaxonomy.taxeditor.view.sessions.SessionsViewPart"; //$NON-NLS-1$
+    public static final String OPEN_INSPECT_SESSIONS_DIALOG_ID =  "eu.etaxonomy.taxeditor.store.open.InspectSessionsDialog";
     private Table tableSessions;
 
     private final String[] titles = { "Owner", "In Memory", "On Disk"};
@@ -58,6 +62,7 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
     private Text txtCdmModelInMemory;
     private Text txtCdmModelOnDisk;
     private Text txtDefaultOnDisk;
+
     public SessionsViewPart() {
         this.cdmEntitySessionManager = CdmStore.getCurrentSessionManager();
         if(cdmEntitySessionManager != null) {
@@ -72,7 +77,7 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
     @Override
     public void createPartControl(Composite parent) {
         Composite container = new Composite(parent, SWT.NONE);
-        container.setLayout(new GridLayout(6, false));
+        container.setLayout(new GridLayout(5, false));
         {
             Label lblNoOfSessions = new Label(container, SWT.NONE);
             lblNoOfSessions.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
@@ -94,7 +99,6 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
         new Label(container, SWT.NONE);
         new Label(container, SWT.NONE);
         new Label(container, SWT.NONE);
-        new Label(container, SWT.NONE);
         {
             Label lblNoOfCaches = new Label(container, SWT.CENTER);
             lblNoOfCaches.setAlignment(SWT.RIGHT);
@@ -113,16 +117,15 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
         }
         new Label(container, SWT.NONE);
         new Label(container, SWT.NONE);
-        new Label(container, SWT.NONE);
         new Label(container, SWT.NONE);
 
-                {
-                    Label lblDefaultCache = new Label(container, SWT.CENTER);
-                    lblDefaultCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
-                    lblDefaultCache.setText("Default Cache, ");
-                    lblDefaultCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
-                    lblDefaultCache.setAlignment(SWT.RIGHT);
-                }
+        {
+            Label lblDefaultCache = new Label(container, SWT.CENTER);
+            lblDefaultCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+            lblDefaultCache.setText("Default Cache, ");
+            lblDefaultCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
+            lblDefaultCache.setAlignment(SWT.RIGHT);
+        }
         {
             Label lblDefaultInMemory = new Label(container, SWT.CENTER);
             lblDefaultInMemory.setText("in Memory : ");
@@ -152,7 +155,6 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
             gd_txtDefaultOnDisk.widthHint = 130;
             txtDefaultOnDisk.setLayoutData(gd_txtDefaultOnDisk);
         }
-        new Label(container, SWT.NONE);
         {
             Label lblCdmModelCache = new Label(container, SWT.CENTER);
             lblCdmModelCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
@@ -191,7 +193,6 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
             gd_txtCdmModelOnDisk.widthHint = 130;
             txtCdmModelOnDisk.setLayoutData(gd_txtCdmModelOnDisk);
         }
-        new Label(container, SWT.NONE);
         {
             Button btnRefresh = new Button(container, SWT.NONE);
             btnRefresh.addSelectionListener(new SelectionAdapter() {
@@ -205,19 +206,28 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
             btnRefresh.setLayoutData(gd_btnRefresh);
             btnRefresh.setText("Refresh");
         }
-                new Label(container, SWT.NONE);
-                new Label(container, SWT.NONE);
-                new Label(container, SWT.NONE);
-                new Label(container, SWT.NONE);
-                new Label(container, SWT.NONE);
-
-                {
-                    tableSessions = new Table(container, SWT.BORDER | SWT.FULL_SELECTION);
-                    tableSessions.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1));
-                    tableSessions.setHeaderVisible(true);
-                    tableSessions.setLinesVisible(true);
+        {
+            Button btnInspectActiveSession = new Button(container, SWT.NONE);
+            btnInspectActiveSession.addSelectionListener(new SelectionAdapter() {
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    AbstractUtility.executeCommand(OPEN_INSPECT_SESSIONS_DIALOG_ID, this, null);
                 }
-                new Label(container, SWT.NONE);
+            });
+            btnInspectActiveSession.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
+            btnInspectActiveSession.setText("Inspect Active Session");
+        }
+        new Label(container, SWT.NONE);
+        new Label(container, SWT.NONE);
+
+        {
+            tableSessions = new Table(container, SWT.BORDER | SWT.FULL_SELECTION);
+            GridData gd_tableSessions = new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1);
+            gd_tableSessions.widthHint = 769;
+            tableSessions.setLayoutData(gd_tableSessions);
+            tableSessions.setHeaderVisible(true);
+            tableSessions.setLinesVisible(true);
+        }
 
         createActions();
         initializeToolBar();
@@ -231,9 +241,9 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
 
 
         for (String title : titles) {
-          TableColumn column = new TableColumn(tableSessions, SWT.NULL);
-          column.setWidth(200);
-          column.setText(title);
+            TableColumn column = new TableColumn(tableSessions, SWT.NULL);
+            column.setWidth(200);
+            column.setText(title);
         }
         changed();
 
@@ -242,23 +252,19 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
 
     @Override
     public void changed() {
+        if(tableSessions.isDisposed()) {
+            return;
+        }
         tableSessions.removeAll();
         if(cdmEntitySessionManager != null) {
             Collection<ICdmEntitySession> sessions = cdmEntitySessionManager.getSessions();
             txtNoOfSessions.setText(String.valueOf(sessions.size()));
             for(ICdmEntitySession session : sessions) {
-                if(session.getOwner() != null) {
-                    TableItem item = new TableItem(tableSessions, SWT.NULL);
-                    item.setText(session.getOwner().toString());
-                    String activePrefix = "";
-                    if(session.isActive()) {
-                        activePrefix = "*";
-                    }
-                    item.setText(0, activePrefix + session.getOwner().toString());
-                    LiveCacheStatistics cacheStatistics = session.getCacheStatistics();
-                    item.setText(1, getInMemoryStats(cacheStatistics));
-                    item.setText(2, getOnDiskStats(cacheStatistics));
-                }
+                addSession(session);
+            }
+            ICdmEntitySession nullSession = cdmEntitySessionManager.getNullSession();
+            if(nullSession != null) {
+                addSession(nullSession);
             }
             for (int i=0; i<titles.length; i++) {
                 tableSessions.getColumn (i).pack ();
@@ -266,10 +272,10 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
         }
 
         CdmRemoteCacheManager instance = CdmRemoteCacheManager.getInstance();
-        int cdmEnityCacheSize = instance.getDefaultCacheManager().getCacheNames().length;
+        int cdmEnityCacheSize = CacheManager.create().getCacheNames().length;
         txtNoOfCaches.setText(String.valueOf(cdmEnityCacheSize));
 
-        Cache defaultCache = instance.getDefaultCacheManager().getCache(CdmCacher.DEFAULT_CACHE_NAME);
+        Cache defaultCache = CacheManager.create().getCache(CdmCacher.DEFAULT_CACHE_NAME);
         txtDefaultInMemory.setText("");
         txtDefaultOnDisk.setText("");
         if(defaultCache != null) {
@@ -289,22 +295,50 @@ public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManag
 
     }
 
+    private void addSession(ICdmEntitySession session) {
+
+        TableItem item = new TableItem(tableSessions, SWT.NULL);
+
+        String activePrefix = "";
+        if(session.isActive()) {
+            activePrefix = "*";
+        }
+        String sessionOwnerString = "";
+        if(session instanceof NullSession) {
+            sessionOwnerString = "Null Session";
+        } else {
+            if(session.getOwner() != null) {
+                sessionOwnerString = session.getOwner().toString();
+            }
+        }
+        item.setText(0, activePrefix + sessionOwnerString);
+        LiveCacheStatistics cacheStatistics = session.getCacheStatistics();
+        item.setText(1, getInMemoryStats(cacheStatistics));
+        item.setText(2, getOnDiskStats(cacheStatistics));
+    }
+
     private String getInMemoryStats(LiveCacheStatistics statistics) {
-        long localHeapSize = statistics.getLocalHeapSize();
+        if(statistics != null) {
+            long localHeapSize = statistics.getLocalHeapSize();
 
-        long localHeapSizeInBytes = statistics.getLocalHeapSizeInBytes();
-        String localHeapSizeInBytesStr =
-                (new DecimalFormat("#.##")).format(localHeapSizeInBytes/1048576.0);
-        return String.valueOf(localHeapSize) + " objects / " + localHeapSizeInBytesStr + " Mb";
+            long localHeapSizeInBytes = statistics.getLocalHeapSizeInBytes();
+            String localHeapSizeInBytesStr =
+                    (new DecimalFormat("#.##")).format(localHeapSizeInBytes/1048576.0);
+            return String.valueOf(localHeapSize) + " objects / " + localHeapSizeInBytesStr + " Mb";
+        }
+        return "";
     }
 
     private String getOnDiskStats(LiveCacheStatistics statistics) {
-        long localDiskSize = statistics.getLocalDiskSize();
+        if(statistics != null) {
+            long localDiskSize = statistics.getLocalDiskSize();
 
-        long localDiskSizeInBytes = statistics.getLocalDiskSizeInBytes();
-        String localDiskSizeInBytesStr =
-                (new DecimalFormat("#.##")).format(localDiskSizeInBytes/1048576.0);
-        return String.valueOf(localDiskSize) + " objects / " + localDiskSizeInBytesStr + " Mb";
+            long localDiskSizeInBytes = statistics.getLocalDiskSizeInBytes();
+            String localDiskSizeInBytesStr =
+                    (new DecimalFormat("#.##")).format(localDiskSizeInBytes/1048576.0);
+            return String.valueOf(localDiskSize) + " objects / " + localDiskSizeInBytesStr + " Mb";
+        }
+        return "";
     }
 
     /**