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