15a1841fb86c1db5d9a37528be18635be1a8ff3b
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / AbstractCdmViewPart.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.view;
12
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.SashForm;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.ui.ISelectionListener;
26 import org.eclipse.ui.ISelectionService;
27 import org.eclipse.ui.IWorkbenchPart;
28 import org.eclipse.ui.part.PageBook;
29 import org.eclipse.ui.part.ViewPart;
30
31 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
32 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
33 import eu.etaxonomy.cdm.model.common.CdmBase;
34 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
35 import eu.etaxonomy.taxeditor.model.AbstractUtility;
36 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
37 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
39 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
40
41 /**
42 * <p>Abstract AbstractCdmViewPart class.</p>
43 *
44 * @author n.hoffmann
45 * @created Jun 15, 2010
46 * @version 1.0
47 */
48 public abstract class AbstractCdmViewPart extends ViewPart implements ISelectionListener, IPostOperationEnabled, IConversationEnabled, ICdmEntitySessionEnabled, IDirtyMarkable{
49
50 protected ISelectionService selectionService;
51
52 public static IStructuredSelection EMPTY_SELECTION = new StructuredSelection();
53
54 protected IWorkbenchPart part;
55
56 private PageBook pageBook;
57
58 private Label emptySelectionLabel;
59
60 private Composite viewerComposite;
61
62 /** {@inheritDoc} */
63 @Override
64 public void createPartControl(Composite parent) {
65 selectionService = getSite().getWorkbenchWindow().getSelectionService();
66 selectionService.addSelectionListener(this);
67
68 pageBook = new PageBook(parent, SWT.NULL);
69 //create viewerComposite
70 viewerComposite = new SashForm(pageBook, SWT.HORIZONTAL);
71 createViewer(viewerComposite);
72
73 // Page 2: Nothing selected
74 emptySelectionLabel = new Label(pageBook, SWT.TOP + SWT.LEFT + SWT.WRAP);
75
76 setInitialSelection();
77 }
78
79 /**
80 *
81 */
82 private void setInitialSelection() {
83 selectionChanged(AbstractUtility.getActivePart(), getInitialSelection());
84 }
85
86 /**
87 * <p>getInitialSelection</p>
88 *
89 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
90 */
91 protected ISelection getInitialSelection() {
92 return selectionService.getSelection();
93 }
94
95 /**
96 * <p>showEmptyPage</p>
97 */
98 public void showEmptyPage(){
99 pageBook.showPage(emptySelectionLabel);
100 }
101
102 /**
103 * <p>showViewer</p>
104 *
105 * @param part a {@link org.eclipse.ui.IWorkbenchPart} object.
106 * @param selection a {@link org.eclipse.jface.viewers.ISelection} object.
107 */
108 public void showViewer(IWorkbenchPart part, IStructuredSelection selection){
109 this.part = part;
110
111 Object element = selection.getFirstElement();
112 //avoid widget is disposed exceptions
113 if (getViewer().getControl().isDisposed()){
114 return;
115 }
116 getViewer().setInput(element);
117 showViewer();
118 }
119
120 /**
121 * <p>getViewer</p>
122 *
123 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
124 */
125 public abstract Viewer getViewer();
126
127 /**
128 * <p>showViewer</p>
129 */
130 public void showViewer(){
131 pageBook.showPage(viewerComposite);
132 }
133
134 public void setEnabled(boolean enabled){
135 pageBook.setEnabled(enabled);
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public void setFocus() {
141 if(getConversationHolder() != null){
142 getConversationHolder().bind();
143 }
144 if(getCdmEntitySession() != null){
145 getCdmEntitySession().bind();
146 }
147 getViewer().getControl().setFocus();
148 }
149
150 /**
151 * <p>createViewer</p>
152 *
153 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
154 */
155 public abstract void createViewer(Composite parent);
156
157 /** {@inheritDoc} */
158 @Override
159 public boolean postOperation(CdmBase objectAffectedByOperation) {
160 changed(objectAffectedByOperation);
161 return true;
162 }
163
164 /** {@inheritDoc} */
165 @Override
166 public abstract void changed(Object object);
167
168 /**
169 * <p>getConversationHolder</p>
170 *
171 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
172 */
173 @Override
174 public ConversationHolder getConversationHolder() {
175 if(part != null && part instanceof IConversationEnabled) {
176 return ((IConversationEnabled) part).getConversationHolder();
177 }
178
179 return null;
180 }
181
182 @Override
183 public ICdmEntitySession getCdmEntitySession() {
184 if(part != null && part instanceof ICdmEntitySessionEnabled) {
185 return ((ICdmEntitySessionEnabled) part).getCdmEntitySession();
186 }
187 return null;
188 }
189
190 @Override
191 public List<? extends CdmBase> getRootEntities() {
192 return Arrays.asList((CdmBase)getViewer().getInput());
193 }
194
195 /** {@inheritDoc} */
196 @Override
197 public void update(CdmDataChangeMap changeEvents) {
198
199 }
200
201 /** {@inheritDoc} */
202 @Override
203 public void dispose() {
204 selectionService.removeSelectionListener(this);
205 super.dispose();
206 }
207
208 /* (non-Javadoc)
209 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
210 */
211 @Override
212 public void forceDirty() {
213 changed(null);
214 }
215
216
217 /* (non-Javadoc)
218 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
219 */
220 @Override
221 public Map<Object, List<String>> getPropertyPathsMap() {
222 // TODO Auto-generated method stub
223 return null;
224 }
225 }