- added null check (fixes #4351)
authorPatric Plitzner <p.plitzner@bgbm.org>
Mon, 18 Aug 2014 14:38:49 +0000 (14:38 +0000)
committerPatric Plitzner <p.plitzner@bgbm.org>
Mon, 18 Aug 2014 14:38:49 +0000 (14:38 +0000)
eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/key/polytomous/PolytomousKeyViewPart.java

index 085783c8d807567b1e26d0611896cbbbf11c8ae9..e61e1584228ed0a5a8a7412c5ab8e8d1e85a3cc3 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * 
+ *
  */
 package eu.etaxonomy.taxeditor.navigation.key.polytomous;
 
@@ -14,7 +14,6 @@ import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ListViewer;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jface.viewers.ViewerFilter;
 import org.eclipse.swt.SWT;
@@ -63,28 +62,28 @@ public class PolytomousKeyViewPart extends ViewPart implements
                                @Override
                                public boolean select(Viewer viewer, Object parentElement,
                                                Object element) {
-                                       
+
                                        if(element instanceof PolytomousKey){
                                                PolytomousKey key = (PolytomousKey) element;
                                                if(key.getTitleCache().contains(text_filter.getText())){
                                                        return true;
                                                }
                                        }
-                                       
+
                                        return false;
                                }
-                               
+
                        };
-                       
+
                        viewer.setFilters(new ViewerFilter[]{filter});
                }
        }
-       
-       
+
+
        public static final String ID = "eu.etaxonomy.taxeditor.navigation.key.polytomous.polytomousKeyViewPart";
 
        public static final String OPEN_COMMAND_ID = "eu.etaxonomy.taxeditor.navigation.key.polytomous.editNodes";
-       
+
        private class ContextListener extends ContextListenerAdapter{
                /* (non-Javadoc)
                 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
@@ -94,7 +93,7 @@ public class PolytomousKeyViewPart extends ViewPart implements
                        monitor.subTask("Shutting down Polytomous Key Viewer");
                        viewer.setInput(null);
                }
-               
+
                /* (non-Javadoc)
                 * @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
                 */
@@ -104,7 +103,7 @@ public class PolytomousKeyViewPart extends ViewPart implements
                        setInput();
                }
        }
-       
+
        private ListViewer viewer;
        private ConversationHolder conversation;
        private IContextListener contextListener;
@@ -116,7 +115,7 @@ public class PolytomousKeyViewPart extends ViewPart implements
        public PolytomousKeyViewPart(){
                formFactory = new CdmFormFactory(Display.getDefault());
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
         */
@@ -125,46 +124,46 @@ public class PolytomousKeyViewPart extends ViewPart implements
 
                Composite container = new Composite(parent, SWT.NONE);
                container.setLayout(new GridLayout());
-               
+
                contextListener = new ContextListener();
                CdmStore.getContextManager().addContextListener(contextListener);
-               
+
                text_filter = formFactory.createText(container, "");
                text_filter.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
                text_filter.addModifyListener(new FilterModifyListener());
-               
+
                viewer = new ListViewer(container, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
                viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-               
+
                viewer.setContentProvider(new PolytomousKeyContentProvider());
                viewer.setLabelProvider(new PolytomousKeyLabelProvider());
-               
+
                viewer.addDoubleClickListener(new IDoubleClickListener() {
-                       
+
                        @Override
-                       public void doubleClick(DoubleClickEvent event) {                               
+                       public void doubleClick(DoubleClickEvent event) {
                            openSelectedKeyNodes();
                        }
                });
-               
-               
+
+
                getSite().setSelectionProvider(viewer);
-                               
+
                createMenu();
-               
+
                if(CdmStore.isActive()){
                        setInput();
                }
        }
-       
+
        public void openSelectedKeyNodes() {
         ICommandService commandService = (ICommandService)getSite().getService(ICommandService.class);
-        
-        Command command = commandService.getCommand(OPEN_COMMAND_ID); 
+
+        Command command = commandService.getCommand(OPEN_COMMAND_ID);
         if(command.isEnabled()) {
             IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
-            try {                
-                handlerService.executeCommand(OPEN_COMMAND_ID, null);                  
+            try {
+                handlerService.executeCommand(OPEN_COMMAND_ID, null);
             } catch (NotDefinedException e) {
                 throw new RuntimeException("Could not find open command: " + OPEN_COMMAND_ID);
             } catch (Exception e) {
@@ -172,29 +171,29 @@ public class PolytomousKeyViewPart extends ViewPart implements
             }
         }
        }
-       
-       public void setViewerSelection(ISelection selection, boolean reveal) {    
-           getSite().setSelectionProvider(viewer);         
-           viewer.setSelection(selection,reveal);          
-           viewer.getList().notifyListeners(SWT.Selection,new Event());            
+
+       public void setViewerSelection(ISelection selection, boolean reveal) {
+           getSite().setSelectionProvider(viewer);
+           viewer.setSelection(selection,reveal);
+           viewer.getList().notifyListeners(SWT.Selection,new Event());
        }
-       
+
        private void createMenu(){
                MenuManager menuManager = new MenuManager();
                menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
-               
+
                getSite().registerContextMenu(menuManager, viewer);
-                               
+
                Control control = viewer.getControl();
                Menu menu = menuManager.createContextMenu(control);
-               
-               control.setMenu(menu);  
+
+               control.setMenu(menu);
        }
 
        private void setInput() {
                conversation = CdmStore.createConversation();
                conversation.registerForDataStoreChanges(this);
-               List<PolytomousKey> input = CdmStore.getService(IPolytomousKeyService.class).list(PolytomousKey.class, null, null, null, null);                 
+               List<PolytomousKey> input = CdmStore.getService(IPolytomousKeyService.class).list(PolytomousKey.class, null, null, null, null);
                viewer.setInput(input);
        }
 
@@ -205,7 +204,7 @@ public class PolytomousKeyViewPart extends ViewPart implements
        public void setFocus() {
                viewer.getControl().setFocus();
        }
-       
+
        /* (non-Javadoc)
         * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
         */
@@ -225,15 +224,17 @@ public class PolytomousKeyViewPart extends ViewPart implements
        public ConversationHolder getConversationHolder() {
                return conversation;
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.ui.part.WorkbenchPart#dispose()
         */
        @Override
        public void dispose() {
-               conversation.unregisterForDataStoreChanges(this);
-               conversation.close();
-               CdmStore.getContextManager().removeContextListener(contextListener);
+           if(conversation!=null){
+               conversation.unregisterForDataStoreChanges(this);
+               conversation.close();
+               CdmStore.getContextManager().removeContextListener(contextListener);
+           }
                super.dispose();
        }
 
@@ -255,16 +256,16 @@ public class PolytomousKeyViewPart extends ViewPart implements
        }
 
        /**
-        * 
+        *
         */
        public void refresh() {
                getConversationHolder().bind();
                //FIXME : Need to make sure this is a stable fix (ticket 3822)
                getConversationHolder().commit();
-               List<PolytomousKey> input = CdmStore.getService(IPolytomousKeyService.class).list(PolytomousKey.class, null, null, null, null);                 
+               List<PolytomousKey> input = CdmStore.getService(IPolytomousKeyService.class).list(PolytomousKey.class, null, null, null, null);
                viewer.setInput(input);
        }
-       
+
        public List<PolytomousKey> getKeys() {
                return (List<PolytomousKey>)viewer.getInput();
        }