1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.key;
|
5
|
|
6
|
import org.eclipse.core.runtime.IProgressMonitor;
|
7
|
import org.eclipse.jface.action.GroupMarker;
|
8
|
import org.eclipse.jface.action.IToolBarManager;
|
9
|
import org.eclipse.jface.action.MenuManager;
|
10
|
import org.eclipse.jface.viewers.IBaseLabelProvider;
|
11
|
import org.eclipse.jface.viewers.IContentProvider;
|
12
|
import org.eclipse.jface.viewers.StructuredSelection;
|
13
|
import org.eclipse.swt.SWT;
|
14
|
import org.eclipse.swt.events.ControlEvent;
|
15
|
import org.eclipse.swt.events.ControlListener;
|
16
|
import org.eclipse.swt.layout.FillLayout;
|
17
|
import org.eclipse.swt.widgets.Composite;
|
18
|
import org.eclipse.swt.widgets.Control;
|
19
|
import org.eclipse.swt.widgets.Display;
|
20
|
import org.eclipse.swt.widgets.Menu;
|
21
|
import org.eclipse.ui.IEditorInput;
|
22
|
import org.eclipse.ui.IEditorSite;
|
23
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
24
|
import org.eclipse.ui.PartInitException;
|
25
|
import org.eclipse.ui.part.EditorPart;
|
26
|
import org.eclipse.zest.core.viewers.AbstractZoomableViewer;
|
27
|
import org.eclipse.zest.core.viewers.GraphViewer;
|
28
|
import org.eclipse.zest.core.viewers.IZoomableWorkbenchPart;
|
29
|
import org.eclipse.zest.core.viewers.ZoomContributionViewItem;
|
30
|
import org.eclipse.zest.core.widgets.ZestStyles;
|
31
|
import org.eclipse.zest.layouts.LayoutAlgorithm;
|
32
|
import org.eclipse.zest.layouts.LayoutStyles;
|
33
|
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
|
34
|
|
35
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
36
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
37
|
import eu.etaxonomy.cdm.model.description.IIdentificationKey;
|
38
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
39
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
40
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
|
41
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
42
|
import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
|
43
|
|
44
|
/**
|
45
|
* @author n.hoffmann
|
46
|
*
|
47
|
*/
|
48
|
public abstract class AbstractKeyEditor<T extends IIdentificationKey> extends EditorPart implements
|
49
|
IConversationEnabled,
|
50
|
IZoomableWorkbenchPart,
|
51
|
IPostOperationEnabled,
|
52
|
IDirtyMarkableSelectionProvider{
|
53
|
|
54
|
private CdmFormFactory formFactory;
|
55
|
private Composite container;
|
56
|
private GraphViewer graphViewer;
|
57
|
|
58
|
private LayoutAlgorithm layoutAlgoritm;
|
59
|
private ZoomContributionViewItem zoomContributionViewItem;
|
60
|
|
61
|
private boolean dirty = false;
|
62
|
private IToolBarManager toolBarManager;
|
63
|
|
64
|
|
65
|
public AbstractKeyEditor(){
|
66
|
formFactory = new CdmFormFactory(Display.getDefault());
|
67
|
}
|
68
|
|
69
|
/* (non-Javadoc)
|
70
|
* @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
|
71
|
*/
|
72
|
@Override
|
73
|
public void doSave(IProgressMonitor monitor) {
|
74
|
getConversationHolder().commit(true);
|
75
|
setDirty(false);
|
76
|
|
77
|
}
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see org.eclipse.ui.part.EditorPart#doSaveAs()
|
81
|
*/
|
82
|
@Override
|
83
|
public void doSaveAs() {
|
84
|
doSave(null);
|
85
|
}
|
86
|
|
87
|
/* (non-Javadoc)
|
88
|
* @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
|
89
|
*/
|
90
|
@Override
|
91
|
public void init(IEditorSite site, IEditorInput input)
|
92
|
throws PartInitException {
|
93
|
setSite(site);
|
94
|
setInput(input);
|
95
|
}
|
96
|
|
97
|
/* (non-Javadoc)
|
98
|
* @see org.eclipse.ui.part.EditorPart#isDirty()
|
99
|
*/
|
100
|
@Override
|
101
|
public boolean isDirty() {
|
102
|
return dirty ;
|
103
|
}
|
104
|
|
105
|
/* (non-Javadoc)
|
106
|
* @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
|
107
|
*/
|
108
|
@Override
|
109
|
public boolean isSaveAsAllowed() {
|
110
|
return false;
|
111
|
}
|
112
|
|
113
|
/* (non-Javadoc)
|
114
|
* @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
|
115
|
*/
|
116
|
@Override
|
117
|
public void createPartControl(Composite parent) {
|
118
|
container = formFactory.createComposite(parent);
|
119
|
|
120
|
container.setLayout(new FillLayout());
|
121
|
graphViewer = new GraphViewer(container, SWT.NONE);
|
122
|
getSite().setSelectionProvider(graphViewer);
|
123
|
|
124
|
graphViewer.setContentProvider(getContentProvider());
|
125
|
graphViewer.setLabelProvider(getLabelProvider());
|
126
|
|
127
|
graphViewer.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
|
128
|
|
129
|
graphViewer.setLayoutAlgorithm(getLayoutAlgoritm(), false);
|
130
|
|
131
|
createMenu();
|
132
|
|
133
|
createToolbar();
|
134
|
|
135
|
graphViewer.setInput(getKey());
|
136
|
|
137
|
container.addControlListener(new ControlListener() {
|
138
|
|
139
|
@Override
|
140
|
public void controlResized(ControlEvent e) {
|
141
|
applyLayout();
|
142
|
}
|
143
|
|
144
|
@Override
|
145
|
public void controlMoved(ControlEvent e) {
|
146
|
// TODO Auto-generated method stub
|
147
|
|
148
|
}
|
149
|
});
|
150
|
}
|
151
|
|
152
|
private void createToolbar() {
|
153
|
getToolBarManager().add(getZoomContributionViewItem());
|
154
|
}
|
155
|
|
156
|
private void createMenu(){
|
157
|
// Add context menu to tree
|
158
|
MenuManager menuMgr = new MenuManager();
|
159
|
menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
160
|
|
161
|
// MenuManager zoomMenu = new MenuManager("Zoom");
|
162
|
// zoomMenu.add(getZoomContributionViewItem());
|
163
|
|
164
|
ZoomContributionViewItem zoomItem = getZoomContributionViewItem();
|
165
|
menuMgr.add(zoomItem);
|
166
|
getSite().registerContextMenu(menuMgr, graphViewer);
|
167
|
|
168
|
Control control = graphViewer.getGraphControl();
|
169
|
Menu menu = menuMgr.createContextMenu(control);
|
170
|
|
171
|
control.setMenu(menu);
|
172
|
|
173
|
}
|
174
|
|
175
|
protected abstract IBaseLabelProvider getLabelProvider();
|
176
|
|
177
|
protected abstract IContentProvider getContentProvider();
|
178
|
|
179
|
/**
|
180
|
* Return the key that this editor
|
181
|
*
|
182
|
* @return
|
183
|
*/
|
184
|
public abstract T getKey();
|
185
|
|
186
|
@Override
|
187
|
public void setFocus() {
|
188
|
graphViewer.getControl().setFocus();
|
189
|
}
|
190
|
|
191
|
public void refresh(){
|
192
|
graphViewer.refresh();
|
193
|
graphViewer.applyLayout();
|
194
|
}
|
195
|
|
196
|
@Override
|
197
|
public void update(CdmDataChangeMap changeEvents) {
|
198
|
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public AbstractZoomableViewer getZoomableViewer() {
|
203
|
return graphViewer;
|
204
|
}
|
205
|
|
206
|
private LayoutAlgorithm getLayoutAlgoritm() {
|
207
|
if (layoutAlgoritm == null){
|
208
|
layoutAlgoritm = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
|
209
|
layoutAlgoritm.setEntityAspectRatio(2.5);
|
210
|
}
|
211
|
return layoutAlgoritm;
|
212
|
}
|
213
|
|
214
|
private ZoomContributionViewItem getZoomContributionViewItem() {
|
215
|
if(zoomContributionViewItem == null){
|
216
|
zoomContributionViewItem = new ZoomContributionViewItem(this);
|
217
|
}
|
218
|
return new ZoomContributionViewItem(this);
|
219
|
}
|
220
|
|
221
|
@Override
|
222
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
223
|
setDirty(true);
|
224
|
refresh();
|
225
|
|
226
|
if(objectAffectedByOperation instanceof PolytomousKeyNode){
|
227
|
graphViewer.setSelection(new StructuredSelection(objectAffectedByOperation), true);
|
228
|
}
|
229
|
|
230
|
return true;
|
231
|
}
|
232
|
|
233
|
private IToolBarManager getToolBarManager(){
|
234
|
if(toolBarManager == null){
|
235
|
toolBarManager = getEditorSite().getActionBars().getToolBarManager();
|
236
|
}
|
237
|
return toolBarManager;
|
238
|
}
|
239
|
|
240
|
public void applyLayout(){
|
241
|
graphViewer.applyLayout();
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
public boolean onComplete() {
|
246
|
return true;
|
247
|
}
|
248
|
|
249
|
public void setDirty(boolean dirty) {
|
250
|
this.dirty = dirty;
|
251
|
firePropertyChange(PROP_DIRTY);
|
252
|
}
|
253
|
|
254
|
/* (non-Javadoc)
|
255
|
* @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(java.lang.Object)
|
256
|
*/
|
257
|
@Override
|
258
|
public void changed(Object element) {
|
259
|
setDirty(true);
|
260
|
graphViewer.update(element, null);
|
261
|
}
|
262
|
|
263
|
}
|