Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55b406d64e1c3f5fcf750e69e457c42e5c147103 (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
/*******************************************************************************
 * Copyright (c) 2012 Tilera Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     William R. Swanson (Tilera Corporation)
 *******************************************************************************/

package org.eclipse.cdt.visualizer.ui.util;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;

// ---------------------------------------------------------------------------
// SelectionManager
// ---------------------------------------------------------------------------

/**
 * Selection management utility class
 */
public class SelectionManager implements ISelectionProvider {
	// --- members ---

	/** Actual source to report for selection change events. */
	protected ISelectionProvider m_source = null;

	/** Manager label, also used on listener list. */
	protected String m_label = null;

	/** Current selection, if any. */
	protected ISelection m_selection = SelectionUtils.EMPTY_SELECTION;

	/** Selection changed listeners */
	protected ListenerList m_selectionListeners = null;

	/** Whether selection events are enabled */
	protected boolean m_selectionEventsEnabled = true;

	// --- constructors/destructors ---

	/** Constructor. */
	public SelectionManager(ISelectionProvider source, String label) {
		m_source = (source == null) ? this : source;
		m_label = label;
		m_selectionListeners = new ListenerList(this, label + ", listener list") {
			public void raise(Object listener, Object event) {
				if (listener instanceof ISelectionChangedListener && event instanceof SelectionChangedEvent) {
					ISelectionChangedListener typedListener = (ISelectionChangedListener) listener;
					SelectionChangedEvent typedEvent = (SelectionChangedEvent) event;
					typedListener.selectionChanged(typedEvent);
				}
			}
		};
	}

	/** Dispose method. */
	public void dispose() {
		m_selectionEventsEnabled = false;
		m_selection = SelectionUtils.EMPTY_SELECTION;
		if (m_selectionListeners != null) {
			m_selectionListeners.clear();
			m_selectionListeners = null;
		}
		// m_label = null; // leave label, to aid in debugging cleanup
		m_source = null;
	}

	// --- ISelectionProvider implementation ---

	/** Adds selection changed listener. */
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		if (listener == null)
			return;
		m_selectionListeners.addListener(listener);
		// fake a selection changed event so new listener can update itself properly
		listener.selectionChanged(new SelectionChangedEvent(m_source, getSelection()));
	}

	/** Removes selection changed listener. */
	public void removeSelectionChangedListener(ISelectionChangedListener listener) {
		if (listener == null)
			return;
		m_selectionListeners.removeListener(listener);
	}

	/** Returns current selection. */
	public ISelection getSelection() {
		return m_selection;
	}

	/** Sets selection, and raises change event. */
	public void setSelection(ISelection selection) {
		setSelection(selection, true);
	}

	/** Sets selection, and raises change event with specified provider as the source. */
	public void setSelection(ISelectionProvider provider, ISelection selection) {
		setSelection(provider, selection, true);
	}

	/** Sets selection, and raises change event
	 *  if raiseEvent is true. */
	public void setSelection(ISelection selection, boolean raiseEvent) {
		if (selection == null)
			selection = SelectionUtils.EMPTY_SELECTION;
		m_selection = selection;
		if (raiseEvent)
			raiseSelectionChangedEvent();
	}

	/** Sets selection, and raises change event with specified provider as the source
	 *  if raiseEvent is true. */
	public void setSelection(ISelectionProvider provider, ISelection selection, boolean raiseEvent) {
		if (selection == null)
			selection = SelectionUtils.EMPTY_SELECTION;
		m_selection = selection;
		if (raiseEvent)
			raiseSelectionChangedEvent(provider);
	}

	/** Returns true if we currently have a non-emptr selection. */
	public boolean hasSelection() {
		return (SelectionUtils.getSelectionSize(m_selection) > 0);
	}

	// --- methods ---

	/** Gets whether selection events are enabled. */
	public boolean getSelectionEventsEnabled() {
		return m_selectionEventsEnabled;
	}

	/** Sets whether selection events are enabled. */
	public void setSelectionEventsEnabled(boolean enabled) {
		m_selectionEventsEnabled = enabled;
	}

	/** Raises selection changed event. */
	public void raiseSelectionChangedEvent() {
		if (m_selectionEventsEnabled)
			m_selectionListeners.raise(new SelectionChangedEvent(m_source, getSelection()));
	}

	/** Raises selection changed event with specified provider as source. */
	public void raiseSelectionChangedEvent(ISelectionProvider provider) {
		if (m_selectionEventsEnabled)
			m_selectionListeners.raise(new SelectionChangedEvent(provider, getSelection()));
	}
}

Back to the top