Project

General

Profile

Download (3.93 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
        /* (non-Javadoc)
50
         * @see java.lang.Runnable#run()
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
        if(delaySelection==null){
85
            delaySelection = new DelaySelection(part, selection);
86
        }
87
        delaySelection.setPart(part);
88
        delaySelection.setSelection(selection);
89
        if(!isInDelay){
90
            isInDelay = true;
91
            Display.getCurrent().asyncExec(delaySelection);
92
        }
93
    }
94

    
95
    /*
96
     * (non-Javadoc)
97
     *
98
     * @see
99
     * eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createPartControl(org
100
     * .eclipse.swt.widgets.Composite)
101
     */
102
    /** {@inheritDoc} */
103
    @Override
104
    public void createPartControl(Composite parent) {
105
        super.createPartControl(parent);
106
        isInDelay = false;
107
    }
108

    
109
    /**
110
     * <p>
111
     * getEditor
112
     * </p>
113
     *
114
     * @return the currently active editor
115
     */
116
    public IEditorPart getEditor() {
117
        return AbstractUtility.getActiveEditor();
118
    }
119

    
120
    /** {@inheritDoc} */
121
    @Override
122
    public void setFocus() {
123
        super.setFocus();
124

    
125
        if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
126
            boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
127
            setEnabled(doEnable);
128
        }
129
    }
130

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

    
144
}
(3-3/5)