Project

General

Profile

Download (3.82 KB) Statistics
| Branch: | Tag: | Revision:
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
    	/*to avoid widget is disposed exceptions
82
        if(getViewer().getControl().isDisposed()){
83
            return;
84
        }*/
85
    	if(delaySelection==null){
86
            delaySelection = new DelaySelection(part, selection);
87
        }
88
        delaySelection.setPart(part);
89
        delaySelection.setSelection(selection);
90
        if(!isInDelay){
91
            isInDelay = true;
92
            Display.getCurrent().asyncExec(delaySelection);
93
        }
94
    }
95
    
96
    
97

    
98
    /** {@inheritDoc} */
99
    @Override
100
    public void createPartControl(Composite parent) {
101
        super.createPartControl(parent);
102
        isInDelay = false;
103
    }
104

    
105
    /**
106
     * <p>
107
     * getEditor
108
     * </p>
109
     *
110
     * @return the currently active editor
111
     */
112
    public IEditorPart getEditor() {
113
        return AbstractUtility.getActiveEditor();
114
    }
115

    
116
    /** {@inheritDoc} */
117
    @Override
118
    public void setFocus() {
119
        super.setFocus();
120

    
121
        if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
122
            boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
123
            setEnabled(doEnable);
124
        }
125
    }
126

    
127
    /** {@inheritDoc} */
128
    @Override
129
    public void changed(Object object) {
130
        if (part instanceof AbstractCdmViewPart) {
131
            ((AbstractCdmViewPart) part).changed(object);
132
        } else {
133
            IEditorPart editor = getEditor();
134
            if (editor != null && editor instanceof IDirtyMarkable) {
135
                ((IDirtyMarkable) editor).changed(object);
136
            }
137
        }
138
    }
139

    
140
}
(3-3/7)