ref #6596 Keep track of active "editor"
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / AbstractCdmEditorViewPart.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.view;
5
6 import org.eclipse.jface.viewers.ISelection;
7 import org.eclipse.jface.viewers.IStructuredSelection;
8 import org.eclipse.swt.widgets.Composite;
9 import org.eclipse.swt.widgets.Display;
10 import org.eclipse.ui.IEditorPart;
11 import org.eclipse.ui.IWorkbenchPart;
12
13 import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
14 import eu.etaxonomy.taxeditor.model.AbstractUtility;
15 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
16 import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
17
18 /**
19 * <p>
20 * Abstract AbstractCdmEditorViewPart class.
21 * </p>
22 *
23 * @author n.hoffmann
24 * @created Sep 21, 2010
25 * @version 1.0
26 */
27 public abstract class AbstractCdmEditorViewPart extends AbstractCdmViewPart implements ISelectionElementEditingPart {
28
29 private DelaySelection delaySelection = null;
30 /**
31 * This is the monitor for the DelaySelection runnable.
32 * If it is <code>true</code> then it is currently delaying a selection.
33 */
34 private boolean isInDelay;
35
36 protected Object selectionProvidingPart;
37
38
39 /**
40 * This class invokes internal_selectionChanged() in a separate thread.
41 * This allows an asynchronous and/or delayed handling of selection changes
42 */
43 private class DelaySelection implements Runnable{
44 private IWorkbenchPart part;
45 private ISelection selection;
46
47 public DelaySelection(IWorkbenchPart part, ISelection selection) {
48 super();
49 this.part = part;
50 this.selection = selection;
51 }
52
53 @Override
54 public void run() {
55 try{
56 selectionChanged_internal(part, selection);
57 }
58 finally{
59 isInDelay = false;
60 }
61 }
62
63 public synchronized void setSelection(ISelection selection) {
64 this.selection = selection;
65 }
66
67 public synchronized void setPart(IWorkbenchPart part) {
68 this.part = part;
69 }
70
71 }
72
73 /**
74 * Handle selection change events here and <b>not</b> in
75 * {@link AbstractCdmEditorViewPart#selectionChanged(IWorkbenchPart, ISelection)}
76 * if you want asynchronous selection handling.
77 * @param part The workbench part that has issued the selection change
78 * @param selection the new selection
79 */
80 protected abstract void selectionChanged_internal(IWorkbenchPart part, ISelection selection);
81
82 /** {@inheritDoc} */
83 @Override
84 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
85 /*to avoid widget is disposed exceptions
86 if(getViewer().getControl().isDisposed()){
87 return;
88 }*/
89 if(delaySelection==null){
90 delaySelection = new DelaySelection(part, selection);
91 }
92 delaySelection.setPart(part);
93 delaySelection.setSelection(selection);
94 if(!isInDelay){
95 isInDelay = true;
96 Display.getCurrent().asyncExec(delaySelection);
97 }
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public void showViewer(Object part, IStructuredSelection selection) {
105 super.showViewer(part, selection);
106 selectionProvidingPart = part;
107 }
108
109 @Override
110 public Object getSelectionProvidingPart() {
111 return selectionProvidingPart;
112 }
113
114
115
116 /** {@inheritDoc} */
117 @Override
118 public void createPartControl(Composite parent) {
119 super.createPartControl(parent);
120 isInDelay = false;
121 }
122
123 /**
124 * <p>
125 * getEditor
126 * </p>
127 *
128 * @return the currently active editor
129 */
130 public IEditorPart getEditor() {
131 return AbstractUtility.getActiveEditor();
132 }
133
134 /**
135 * {@inheritDoc}
136 */
137 @Override
138 public void showEmptyPage() {
139 selectionProvidingPart = null;
140 super.showEmptyPage();
141 }
142
143 /** {@inheritDoc} */
144 @Override
145 public void setFocus() {
146 super.setFocus();
147
148 if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
149 boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
150 setEnabled(doEnable);
151 }
152 }
153
154 /**
155 * {@inheritDoc}
156 */
157 @Override
158 public void dispose() {
159 selectionProvidingPart = null;
160 super.dispose();
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 public void changed(Object object) {
166 if(part instanceof IDirtyMarkable){
167 ((IDirtyMarkable) part).changed(object);
168 }
169 else {
170 IEditorPart editor = getEditor();
171 if (editor != null && editor instanceof IDirtyMarkable) {
172 ((IDirtyMarkable) editor).changed(object);
173 }
174 }
175 }
176
177 }