Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8f557ee171dd676915a2f125bf342a92f67f647c (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************************
 * 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 java.util.List;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;

// ---------------------------------------------------------------------------
// WorkbenchSelectionAdapter
// ---------------------------------------------------------------------------

/**
 * Selection change event manager
 */
public class WorkbenchSelectionAdapter implements ISelectionListener, ISelectionProvider {
	// --- members ---

	/** Workbench part (view) this adapter is associated with. */
	protected IViewPart m_view = null;

	/** Current selection */
	protected ISelection m_selection = null;

	/** Listeners for selection changed events. */
	protected ListenerList m_selectionListeners = null;

	/** Whether selection events are reported */
	protected boolean m_trackSelection = true;

	// --- constructors/destructors ---

	/** Constructor. */
	public WorkbenchSelectionAdapter(IViewPart view) {
		m_view = view;
		m_selection = null;
		m_selectionListeners = new ListenerList(view,
				"WorkbenchSelectionAdapter for view " + view.getClass().getSimpleName()) {
			/** Dispatches event to listeners */
			@Override
			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);
				}
			}
		};

		// listen for external selection changed events
		m_view.getSite().getPage().addSelectionListener(this);

		// set selection provider for this view
		m_view.getSite().setSelectionProvider(this);

		// initialize selection
		setSelection(m_view.getSite().getPage().getSelection());

	}

	/** Dispose method. */
	public void dispose() {
		if (m_view != null) {
			m_view.getSite().getPage().removeSelectionListener(this);
			m_view.getViewSite().setSelectionProvider(null);
			m_view = null;
		}
		m_selection = null;
		if (m_selectionListeners != null) {
			m_selectionListeners.clear();
			m_selectionListeners = null;
		}
	}

	// --- accessors ---

	/** Gets whether selection change events are reported. */
	public boolean getTrackSelection() {
		return m_trackSelection;
	}

	/** Sets whether selection change events are reported. */
	public void setTrackSelection(boolean trackSelection) {
		m_trackSelection = trackSelection;
	}

	// --- ISelectionListener implementation ---

	/** Invoked when selection changes externally. */
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		// ignore selection change events that came from us
		if (part == m_view)
			return;
		if (m_trackSelection) {
			workbenchSelectionChanged(part, selection);
		}
	}

	/** Invoked when selection changes.
	 *  Intended to be overloaded by derived implementations.
	 */
	public void workbenchSelectionChanged(ISelection selection) {
		setSelection(selection);
	}

	/** Invoked when selection changes.
	 *  Intended to be overloaded by derived implementations.
	 */
	public void workbenchSelectionChanged(ISelectionProvider provider, ISelection selection) {
		setSelection(provider, selection);
	}

	/** Invoked when selection changes.
	 *  Intended to be overloaded by derived implementations.
	 */
	public void workbenchSelectionChanged(Object provider, ISelection selection) {
		setSelection(provider, selection);
	}

	// --- ISelectionProvider implementation ---

	/** Gets current selection. */
	@Override
	public ISelection getSelection() {
		return m_selection;
	}

	/** Sets current selection, and raises selection changed event. */
	@Override
	public void setSelection(ISelection selection) {
		// for some reason, SelectionChangedEvent can't stand a null selection
		if (selection == null)
			selection = StructuredSelection.EMPTY;
		m_selection = selection;
		m_selectionListeners.raise(new SelectionChangedEvent(this, m_selection));
	}

	/** Sets current selection, and raises selection changed event. */
	public void setSelection(ISelectionProvider source, ISelection selection) {
		// for some reason, SelectionChangedEvent can't stand a null selection
		if (selection == null)
			selection = StructuredSelection.EMPTY;
		m_selection = selection;
		m_selectionListeners.raise(new SelectionChangedEvent(source, m_selection));
	}

	/** Sets current selection, and raises selection changed event. */
	public void setSelection(Object source, ISelection selection) {
		// for some reason, SelectionChangedEvent can't stand a null selection
		if (selection == null)
			selection = StructuredSelection.EMPTY;
		m_selection = selection;
		m_selectionListeners.raise(new SelectionChangedEvent(new SelectionProviderAdapter(source), m_selection));
	}

	/** Adds external listener for selection change events. */
	@Override
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		m_selectionListeners.addListener(listener);
	}

	/** Removes external listener for selection change events. */
	@Override
	public void removeSelectionChangedListener(ISelectionChangedListener listener) {
		m_selectionListeners.removeListener(listener);
	}

	// --- utilities ---

	/** Converts selection to string, for debug display. */
	protected String selectionToString(ISelection selection) {
		String result = null;
		// convert selection to text string
		if (m_selection == null) {
			result = "No Selection";
		} else if (m_selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) m_selection;
			List<?> elements = structuredSelection.toList();
			int size = elements.size();
			if (size == 0) {
				result = "Empty Selection";
			} else {
				result = "";
				for (int i = 0; i < size; i++) {
					if (i > 0)
						result += "\n";
					Object o = elements.get(i);
					String type = o.getClass().getName();
					String value = o.toString();
					result += "[" + i + "]: type= + " + type + ", value='" + value + "'";
				}
			}
		} else {
			String type = m_selection.getClass().getName();
			String value = m_selection.toString();
			result = "type=" + type + ", value='" + value + "'";
		}
		return result;
	}
}

Back to the top