Add null check for session
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / DerivateSearchView.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.view.derivateSearch;
11
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.ui.IMemento;
22 import org.eclipse.ui.part.ViewPart;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
27 import eu.etaxonomy.taxeditor.model.IContextListener;
28 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
29 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31
32 /**
33 * This view allows to search for and filter {@link SpecimenOrObservationBase}s and display
34 * the results in a list. The results can be selected and opened in an editor.
35 */
36 public class DerivateSearchView extends ViewPart implements IContextListener, ICdmEntitySessionEnabled {
37
38 public static final String ID = "eu.etaxonomy.taxeditor.view.derivateSearch.DerivateSearchView";
39 private DerivateSearchCompositeController derivateSearchCompositeController;
40 private ConversationHolder conversationHolder;
41 private ICdmEntitySession cdmEntitySession;
42
43 /**
44 * Constructs a new DerivateSearchView and registers it to listen to context changes
45 */
46 public DerivateSearchView() {
47 CdmStore.getContextManager().addContextListener(this);
48 }
49
50 @Override
51 public void createPartControl(Composite parent) {
52
53 derivateSearchCompositeController = new DerivateSearchCompositeController(parent, this);
54 getSite().setSelectionProvider(derivateSearchCompositeController.getResultViewer());
55 derivateSearchCompositeController.setEnabled(CdmStore.isActive());
56
57 //create context menu
58 MenuManager menuManager = new MenuManager();
59 getSite().registerContextMenu(menuManager, derivateSearchCompositeController.getResultViewer());
60 Control control = derivateSearchCompositeController.getResultViewer().getControl();
61 Menu menu = menuManager.createContextMenu(control);
62 control.setMenu(menu);
63 }
64
65 @Override
66 public void setFocus() {
67 derivateSearchCompositeController.setFocus();
68 //make sure to bind again if maybe in another view the conversation was unbound
69 if(getConversationHolder()!=null && !getConversationHolder().isClosed() && !getConversationHolder().isBound()){
70 getConversationHolder().bind();
71 }
72 if(getCdmEntitySession() != null) {
73 getCdmEntitySession().bind();
74 }
75 }
76
77 @Override
78 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
79 }
80
81 @Override
82 public void contextStop(IMemento memento, IProgressMonitor monitor) {
83 derivateSearchCompositeController.setEnabled(false);
84 derivateSearchCompositeController.reset();
85 }
86
87 @Override
88 public void contextStart(IMemento memento, IProgressMonitor monitor) {
89 derivateSearchCompositeController.setEnabled(true);
90 }
91
92 @Override
93 public void contextRefresh(IProgressMonitor monitor) {
94 initConversation();
95 initSession();
96 }
97
98 private void initSession() {
99 if(CdmStore.isActive()) {
100 cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
101 }
102 }
103
104 private void initConversation(){
105 if(conversationHolder==null){
106 conversationHolder = CdmStore.createConversation();
107 derivateSearchCompositeController.setConversation(conversationHolder);
108 }
109 }
110
111 @Override
112 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
113 }
114
115 public ConversationHolder getConversationHolder() {
116 if(CdmStore.isActive() && conversationHolder==null){
117 initConversation();
118 }
119 return conversationHolder;
120 }
121
122
123 @Override
124 public void dispose() {
125 if(conversationHolder!=null){
126 conversationHolder.close();
127 }
128
129 if(cdmEntitySession != null) {
130 cdmEntitySession.dispose();
131 }
132 super.dispose();
133 }
134
135
136 /* (non-Javadoc)
137 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
138 */
139 @Override
140 public ICdmEntitySession getCdmEntitySession() {
141 initSession();
142 return cdmEntitySession;
143 }
144
145 /* (non-Javadoc)
146 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
147 */
148 @Override
149 public <T extends CdmBase> Collection<T> getRootEntities() {
150 // TODO Auto-generated method stub
151 return null;
152 }
153
154 /* (non-Javadoc)
155 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
156 */
157 @Override
158 public Map<Object, List<String>> getPropertyPathsMap() {
159 // TODO Auto-generated method stub
160 return null;
161 }
162 }