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