Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 27e18d71bef8084cd824faabb8873e92fcd90d76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.INullSelectionListener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Provides per-part selection tracking for the selection service.
 */
public abstract class AbstractPartSelectionTracker {
    /**
     * List of selection listeners for this tracker
     */
    private ListenerList fListeners = new ListenerList();

    /**
     * List of post selection listeners for this tracker
     */
    private ListenerList postListeners = new ListenerList();

    /**
     * The id of the part this tracls
     */
    private String fPartId;

    /**
     * Constructs a part selection tracker for the part with the given id.
     *
     * @param id part identifier
     */
    public AbstractPartSelectionTracker(String partId) {
        setPartId(partId);
    }

    /**
     * Adds a selection listener to this tracker
     *
     * @param listener the listener to add
     */
    public void addSelectionListener(ISelectionListener listener) {
        fListeners.add(listener);
    }

    /**
     * Adds a post selection listener to this tracker
     *
     * @param listener the listener to add
     */
    public void addPostSelectionListener(ISelectionListener listener) {
        postListeners.add(listener);
    }

    /**
     * Returns the selection from the part being tracked,
     * or <code>null</code> if the part is closed or has no selection.
     */
    public abstract ISelection getSelection();

    /**
     * Removes a selection listener from this tracker.
     *
     * @param listener the listener to remove
     */
    public void removeSelectionListener(ISelectionListener listener) {
        fListeners.remove(listener);
    }

    /**
     * Removes a post selection listener from this tracker.
     *
     * @param listener the listener to remove
     */
    public void removePostSelectionListener(ISelectionListener listener) {
        postListeners.remove(listener);
    }

    /**
     * Disposes this selection tracker.  This removes all listeners currently registered.
     */
    public void dispose() {
        synchronized (fListeners) {
            Object[] listeners = fListeners.getListeners();
            for (int i = 0; i < listeners.length; i++) {
                fListeners.remove(listeners[i]);
                postListeners.remove(listeners[i]);
            }
        }
    }

    /**
     * Fires a selection event to the listeners.
     *
     * @param part the part or <code>null</code> if no active part
     * @param sel the selection or <code>null</code> if no active selection
     * @param listeners the list of listeners to notify
     */
    protected void fireSelection(final IWorkbenchPart part, final ISelection sel) {
        Object[] array = fListeners.getListeners();
        for (int i = 0; i < array.length; i++) {
            final ISelectionListener l = (ISelectionListener) array[i];
            if ((part != null && sel != null)
                    || l instanceof INullSelectionListener) {
                SafeRunner.run(new SafeRunnable() {
                    @Override
					public void run() {
                        l.selectionChanged(part, sel);
                    }
                });
            }
        }
    }

    /**
     * Fires a post selection event to the listeners.
     *
     * @param part the part or <code>null</code> if no active part
     * @param sel the selection or <code>null</code> if no active selection
     * @param listeners the list of listeners to notify
     */
    protected void firePostSelection(final IWorkbenchPart part,
            final ISelection sel) {
        Object[] array = postListeners.getListeners();
        for (int i = 0; i < array.length; i++) {
            final ISelectionListener l = (ISelectionListener) array[i];
            if ((part != null && sel != null)
                    || l instanceof INullSelectionListener) {
                SafeRunner.run(new SafeRunnable() {
                    @Override
					public void run() {
                        l.selectionChanged(part, sel);
                    }
                });
            }
        }
    }

    /**
     * Sets the id of the part that this tracks.
     *
     * @param id view identifier
     */
    private void setPartId(String partId) {
        fPartId = partId;
    }

    /**
     * Returns the id of the part that this tracks.
     *
     * @return part identifier
     */
    protected String getPartId() {
        return fPartId;
    }

}

Back to the top