Project

General

Profile

Download (3.89 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.IDirtyMarkableSelectionProvider;
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 = false;
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
        /* (non-Javadoc)
50
         * @see java.lang.Runnable#run()
51
         */
52
        @Override
53
        public void run() {
54
            internal_selectionChanged(part, selection);
55
            isInDelay = false;
56
        }
57

    
58
        public synchronized void setSelection(ISelection selection) {
59
            this.selection = selection;
60
        }
61

    
62
        public synchronized void setPart(IWorkbenchPart part) {
63
            this.part = part;
64
        }
65

    
66
    }
67

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

    
77
    /** {@inheritDoc} */
78
    @Override
79
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
80
        if(delaySelection==null){
81
            delaySelection = new DelaySelection(part, selection);
82
        }
83
        delaySelection.setPart(part);
84
        delaySelection.setSelection(selection);
85
        if(!isInDelay){
86
            isInDelay = true;
87
            Display.getCurrent().asyncExec(delaySelection);
88
        }
89
    }
90

    
91
    /*
92
     * (non-Javadoc)
93
     *
94
     * @see
95
     * eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createPartControl(org
96
     * .eclipse.swt.widgets.Composite)
97
     */
98
    /** {@inheritDoc} */
99
    @Override
100
    public void createPartControl(Composite parent) {
101
        super.createPartControl(parent);
102
    }
103

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

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

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

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

    
139
}
(3-3/5)