avoid widgetIsDisposed exceptions by check of control is disposed
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / concept / graph / e4 / ConceptGraphViewE4.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.view.concept.graph.e4;
5
6 import javax.annotation.PostConstruct;
7
8 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
9 import org.eclipse.e4.ui.services.EMenuService;
10 import org.eclipse.jface.viewers.StructuredSelection;
11 import org.eclipse.jface.viewers.Viewer;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.zest.core.viewers.GraphViewer;
15 import org.eclipse.zest.core.widgets.ZestStyles;
16 import org.eclipse.zest.layouts.LayoutAlgorithm;
17 import org.eclipse.zest.layouts.LayoutStyles;
18 import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
19
20 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
21 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
22 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
23 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
24 import eu.etaxonomy.taxeditor.editor.view.concept.graph.ConceptGraphContentProvider;
25 import eu.etaxonomy.taxeditor.editor.view.concept.graph.ConceptGraphLabelProvider;
26 import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
27 import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
28 import eu.etaxonomy.taxeditor.model.AbstractUtility;
29 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
30 import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
31 import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
32
33 /**
34 *
35 * @author pplitzner
36 * @since Sep 27, 2017
37 *
38 */
39 public class ConceptGraphViewE4 extends AbstractCdmEditorPartE4 {
40
41 public static final String ID = "eu.etaxonomy.taxeditor.editor.view.concept.graph"; //$NON-NLS-1$
42
43 private LayoutAlgorithm layoutAlgoritm;
44
45 /**
46 * {@inheritDoc}
47 */
48 @Override
49 public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
50 if(activePart==thisPart){
51 return;
52 }
53
54 Object partObject = createPartObject(activePart);
55
56 if (partObject instanceof DetailsPartE4 || partObject instanceof SupplementalDataPartE4
57 || partObject instanceof FactualDataPartE4|| partObject instanceof MediaViewPartE4) {
58 // do not show empty page as these views are also used to edit the
59 // description selected in this view
60 return;
61 }
62 if(AbstractUtility.getActiveEditor() == null){
63 showEmptyPage();
64 return;
65 }
66
67 if(partObject instanceof BulkEditor){
68 showEmptyPage();
69 return;
70 }
71
72
73 if(partObject instanceof TaxonNameEditorE4){
74 if (viewer.getControl() != null && !viewer.getControl().isDisposed()) {
75 viewer.getControl().setEnabled(true);
76 }
77 TaxonEditorInputE4 input = ((TaxonNameEditorE4) partObject).getEditorInput();
78 showViewer(new StructuredSelection(input), activePart, viewer);
79 }
80
81 }
82
83 @Override
84 public boolean onComplete() {
85 return false;
86 }
87
88 @Override
89 public Viewer getViewer() {
90 return viewer;
91 }
92
93 @PostConstruct
94 public void create(Composite parent, EMenuService menuService, MPart thisPart) {
95
96 this.thisPart = thisPart;
97
98 GraphViewer viewer = new GraphViewer(parent, SWT.NONE);
99
100 viewer.setContentProvider(new ConceptGraphContentProvider());
101 viewer.setLabelProvider(new ConceptGraphLabelProvider());
102
103 viewer.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
104
105 viewer.setLayoutAlgorithm(getLayoutAlgoritm(), false);
106
107 this.viewer = viewer;
108
109 // Propagate selection from viewer
110 selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
111 viewer.addSelectionChangedListener(selectionChangedListener);
112
113 // createMenu();
114 //
115 // createToolbar();
116
117
118 }
119
120 private LayoutAlgorithm getLayoutAlgoritm() {
121
122 if (layoutAlgoritm == null) {
123 // layoutAlgoritm = new CompositeLayoutAlgorithm(
124 // LayoutStyles.NO_LAYOUT_NODE_RESIZING,
125 // new LayoutAlgorithm[] {
126 // new TreeLayoutAlgorithm(
127 // LayoutStyles.NO_LAYOUT_NODE_RESIZING),
128 // new HorizontalShift(
129 // LayoutStyles.NO_LAYOUT_NODE_RESIZING) });
130
131 layoutAlgoritm = new TreeLayoutAlgorithm(
132 LayoutStyles.NO_LAYOUT_NODE_RESIZING);
133 layoutAlgoritm.setEntityAspectRatio(0.5);
134 }
135 return layoutAlgoritm;
136 }
137
138 @Override
139 protected String getViewName() {
140 return Messages.ConceptGraphView_VIEWER_NAME;
141 }
142
143 }