#5533 writing and reading cdmserver configuration from and to JSON file
[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.swt.widgets.Composite;
8 import org.eclipse.swt.widgets.Display;
9 import org.eclipse.ui.IEditorPart;
10 import org.eclipse.ui.IWorkbenchPart;
11
12 import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
13 import eu.etaxonomy.taxeditor.model.AbstractUtility;
14 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
15
16 /**
17 * <p>
18 * Abstract AbstractCdmEditorViewPart class.
19 * </p>
20 *
21 * @author n.hoffmann
22 * @created Sep 21, 2010
23 * @version 1.0
24 */
25 public abstract class AbstractCdmEditorViewPart extends AbstractCdmViewPart {
26
27 private DelaySelection delaySelection = null;
28 /**
29 * This is the monitor for the DelaySelection runnable.
30 * If it is <code>true</code> then it is currently delaying a selection.
31 */
32 private boolean isInDelay;
33
34
35 /**
36 * This class invokes internal_selectionChanged() in a separate thread.
37 * This allows an asynchronous and/or delayed handling of selection changes
38 */
39 private class DelaySelection implements Runnable{
40 private IWorkbenchPart part;
41 private ISelection selection;
42
43 public DelaySelection(IWorkbenchPart part, ISelection selection) {
44 super();
45 this.part = part;
46 this.selection = selection;
47 }
48
49 @Override
50 public void run() {
51 try{
52 selectionChanged_internal(part, selection);
53 }
54 finally{
55 isInDelay = false;
56 }
57 }
58
59 public synchronized void setSelection(ISelection selection) {
60 this.selection = selection;
61 }
62
63 public synchronized void setPart(IWorkbenchPart part) {
64 this.part = part;
65 }
66
67 }
68
69 /**
70 * Handle selection change events here and <b>not</b> in
71 * {@link AbstractCdmEditorViewPart#selectionChanged(IWorkbenchPart, ISelection)}
72 * if you want asynchronous selection handling.
73 * @param part The workbench part that has issued the selection change
74 * @param selection the new selection
75 */
76 protected abstract void selectionChanged_internal(IWorkbenchPart part, ISelection selection);
77
78 /** {@inheritDoc} */
79 @Override
80 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
81 if(delaySelection==null){
82 delaySelection = new DelaySelection(part, selection);
83 }
84 delaySelection.setPart(part);
85 delaySelection.setSelection(selection);
86 if(!isInDelay){
87 isInDelay = true;
88 Display.getCurrent().asyncExec(delaySelection);
89 }
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public void createPartControl(Composite parent) {
95 super.createPartControl(parent);
96 isInDelay = false;
97 }
98
99 /**
100 * <p>
101 * getEditor
102 * </p>
103 *
104 * @return the currently active editor
105 */
106 public IEditorPart getEditor() {
107 return AbstractUtility.getActiveEditor();
108 }
109
110 /** {@inheritDoc} */
111 @Override
112 public void setFocus() {
113 super.setFocus();
114
115 if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
116 boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
117 setEnabled(doEnable);
118 }
119 }
120
121 /** {@inheritDoc} */
122 @Override
123 public void changed(Object object) {
124 if (part instanceof AbstractCdmViewPart) {
125 ((AbstractCdmViewPart) part).changed(object);
126 } else {
127 IEditorPart editor = getEditor();
128 if (editor != null && editor instanceof IDirtyMarkable) {
129 ((IDirtyMarkable) editor).changed(object);
130 }
131 }
132 }
133
134 }