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