#5067 Integrate remoting sessions in derivate hierachy views, Adapt existing actions
[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
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Menu;
19 import org.eclipse.ui.IMemento;
20 import org.eclipse.ui.part.ViewPart;
21
22 import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
26 import eu.etaxonomy.taxeditor.model.IContextListener;
27 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
28 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30
31 /**
32 * This view allows to search for and filter {@link SpecimenOrObservationBase}s and display
33 * the results in a list. The results can be selected and opened in an editor.
34 */
35 public class DerivateSearchView extends ViewPart implements IContextListener, ICdmEntitySessionEnabled {
36
37 public static final String ID = "eu.etaxonomy.taxeditor.view.derivateSearch.DerivateSearchView";
38 private DerivateSearchCompositeController derivateSearchCompositeController;
39 private ConversationHolder conversationHolder;
40 private ICdmEntitySession cdmEntitySession;
41
42 /**
43 * Constructs a new DerivateSearchView and registers it to listen to context changes
44 */
45 public DerivateSearchView() {
46 CdmStore.getContextManager().addContextListener(this);
47 }
48
49 /* (non-Javadoc)
50 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
51 */
52 @Override
53 public void createPartControl(Composite parent) {
54
55 derivateSearchCompositeController = new DerivateSearchCompositeController(parent, this);
56 getSite().setSelectionProvider(derivateSearchCompositeController.getResultViewer());
57 derivateSearchCompositeController.setEnabled(CdmStore.isActive());
58
59 //create context menu
60 MenuManager menuManager = new MenuManager();
61 getSite().registerContextMenu(menuManager, derivateSearchCompositeController.getResultViewer());
62 Control control = derivateSearchCompositeController.getResultViewer().getControl();
63 Menu menu = menuManager.createContextMenu(control);
64 control.setMenu(menu);
65 }
66
67 /* (non-Javadoc)
68 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
69 */
70 @Override
71 public void setFocus() {
72 derivateSearchCompositeController.setFocus();
73 //make sure to bind again if maybe in another view the conversation was unbound
74 if(getConversationHolder()!=null && !getConversationHolder().isClosed() && !getConversationHolder().isBound()){
75 getConversationHolder().bind();
76 }
77 if(getCdmEntitySession() != null) {
78 getCdmEntitySession().bind();
79 }
80 }
81
82 /* (non-Javadoc)
83 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
84 */
85 @Override
86 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
87 }
88
89 /* (non-Javadoc)
90 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
91 */
92 @Override
93 public void contextStop(IMemento memento, IProgressMonitor monitor) {
94 derivateSearchCompositeController.setEnabled(false);
95 derivateSearchCompositeController.reset();
96 }
97
98 /* (non-Javadoc)
99 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
100 */
101 @Override
102 public void contextStart(IMemento memento, IProgressMonitor monitor) {
103 derivateSearchCompositeController.setEnabled(true);
104 }
105
106 /* (non-Javadoc)
107 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
108 */
109 @Override
110 public void contextRefresh(IProgressMonitor monitor) {
111 initConversation();
112 initSession();
113 }
114
115 private void initSession() {
116 if(CdmStore.isActive() && cdmEntitySession == null) {
117 cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
118 }
119 }
120
121 private void initConversation(){
122 if(conversationHolder==null){
123 conversationHolder = CdmStore.createConversation();
124 derivateSearchCompositeController.setConversation(conversationHolder);
125 }
126 }
127
128 /* (non-Javadoc)
129 * @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
130 */
131 @Override
132 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
133 }
134
135 /**
136 * @return the conversationHolder
137 */
138 public ConversationHolder getConversationHolder() {
139 if(CdmStore.isActive() && conversationHolder==null){
140 initConversation();
141 }
142 return conversationHolder;
143 }
144
145 @Override
146 public void dispose() {
147 cdmEntitySession.dispose();
148 super.dispose();
149 }
150
151 /* (non-Javadoc)
152 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
153 */
154 @Override
155 public void onChange(CdmChangeEvent event) {
156 // TODO Auto-generated method stub
157
158 }
159
160 /* (non-Javadoc)
161 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
162 */
163 @Override
164 public ICdmEntitySession getCdmEntitySession() {
165 initSession();
166 return cdmEntitySession;
167 }
168
169 /* (non-Javadoc)
170 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
171 */
172 @Override
173 public <T extends CdmBase> Collection<T> getRootEntities() {
174 // TODO Auto-generated method stub
175 return null;
176 }
177 }