Project

General

Profile

Download (4.42 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
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
16

    
17
/**
18
 * <p>
19
 * Abstract AbstractCdmEditorViewPart class.
20
 * </p>
21
 *
22
 * @author n.hoffmann
23
 * @created Sep 21, 2010
24
 * @version 1.0
25
 */
26
public abstract class AbstractCdmEditorViewPart extends AbstractCdmViewPart implements ISelectionElementEditingPart {
27

    
28
    private DelaySelection delaySelection = null;
29
    /**
30
     * This is the monitor for the DelaySelection runnable.
31
     * If it is <code>true</code> then it is currently delaying a selection.
32
     */
33
    private boolean isInDelay;
34
    
35
    protected Object selectionProvidingPart;
36

    
37

    
38
    /**
39
     * This class invokes internal_selectionChanged() in a separate thread.
40
     * This allows an asynchronous and/or delayed handling of selection changes
41
     */
42
    private class DelaySelection implements Runnable{
43
        private IWorkbenchPart part;
44
        private ISelection selection;
45

    
46
        public DelaySelection(IWorkbenchPart part, ISelection selection) {
47
            super();
48
            this.part = part;
49
            this.selection = selection;
50
        }
51

    
52
        @Override
53
        public void run() {
54
            try{
55
                selectionChanged_internal(part, selection);
56
            }
57
            finally{
58
                isInDelay = false;
59
            }
60
        }
61

    
62
        public synchronized void setSelection(ISelection selection) {
63
            this.selection = selection;
64
        }
65

    
66
        public synchronized void setPart(IWorkbenchPart part) {
67
            this.part = part;
68
        }
69

    
70
    }
71

    
72
    /**
73
     * Handle selection change events here and <b>not</b> in
74
     * {@link AbstractCdmEditorViewPart#selectionChanged(IWorkbenchPart, ISelection)}
75
     * if you want asynchronous selection handling.
76
     * @param part The workbench part that has issued the selection change
77
     * @param selection the new selection
78
     */
79
    protected abstract void selectionChanged_internal(IWorkbenchPart part, ISelection selection);
80

    
81
    /** {@inheritDoc} */
82
    @Override
83
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
84
    	/*to avoid widget is disposed exceptions
85
        if(getViewer().getControl().isDisposed()){
86
            return;
87
        }*/
88
    	if(delaySelection==null){
89
            delaySelection = new DelaySelection(part, selection);
90
        }
91
        delaySelection.setPart(part);
92
        delaySelection.setSelection(selection);
93
        if(!isInDelay){
94
            isInDelay = true;
95
            Display.getCurrent().asyncExec(delaySelection);
96
        }
97
        selectionProvidingPart = part;
98
    }
99

    
100
    @Override
101
    public Object getSelectionProvidingPart() {
102
        return selectionProvidingPart;
103
    }
104

    
105

    
106

    
107
    /** {@inheritDoc} */
108
    @Override
109
    public void createPartControl(Composite parent) {
110
        super.createPartControl(parent);
111
        isInDelay = false;
112
    }
113

    
114
    /**
115
     * <p>
116
     * getEditor
117
     * </p>
118
     *
119
     * @return the currently active editor
120
     */
121
    public IEditorPart getEditor() {
122
        return AbstractUtility.getActiveEditor();
123
    }
124

    
125
    /**
126
     * {@inheritDoc}
127
     */
128
    @Override
129
    public void showEmptyPage() {
130
        selectionProvidingPart = null;
131
        super.showEmptyPage();
132
    }
133

    
134
    /** {@inheritDoc} */
135
    @Override
136
    public void setFocus() {
137
        super.setFocus();
138

    
139
        if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
140
            boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
141
            setEnabled(doEnable);
142
        }
143
    }
144

    
145
    /**
146
     * {@inheritDoc}
147
     */
148
    @Override
149
    public void dispose() {
150
        selectionProvidingPart = null;
151
        super.dispose();
152
    }
153

    
154
    /** {@inheritDoc} */
155
    @Override
156
    public void changed(Object object) {
157
        if(part instanceof IDirtyMarkable){
158
            ((IDirtyMarkable) part).changed(object);
159
        }
160
        else {
161
            IEditorPart editor = getEditor();
162
            if (editor != null && editor instanceof IDirtyMarkable) {
163
                ((IDirtyMarkable) editor).changed(object);
164
            }
165
        }
166
    }
167

    
168
}
(3-3/9)