Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fc040e1a752d4004dd952168ccff190709144cef (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*******************************************************************************
 * Copyright (c) 2000, 2011 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.compare;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.ACC;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * A <code>CompareViewerPane</code> is a convenience class which installs a
 * <code>CLabel</code> and a <code>Toolbar</code> in a <code>ViewForm</code>.
 * <P>
 * Double clicking onto the <code>CompareViewerPane</code>'s title bar maximizes
 * the <code>CompareViewerPane</code> to the size of an enclosing <code>Splitter</code>
 * (if there is one).
 * If more <code>Splitters</code> are nested maximizing walks up and
 * maximizes to the outermost <code>Splitter</code>.
 *
 * @since 2.0
 */
public class CompareViewerPane extends ViewForm implements ISelectionProvider,
		IDoubleClickListener, ISelectionChangedListener, IOpenListener, IAdaptable {
	private ToolBarManager fToolBarManager;
	private Object fInput;
	private ListenerList<ISelectionChangedListener> fSelectionListeners= new ListenerList<>();
	private ListenerList<IDoubleClickListener> fDoubleClickListener= new ListenerList<>();
	private ListenerList<IOpenListener> fOpenListener= new ListenerList<>();

	/**
	 * Constructs a new instance of this class given its parent
	 * and a style value describing its behavior and appearance.
	 *
	 * @param container a widget which will be the container of the new instance (cannot be null)
	 * @param style the style of widget to construct
	 *
	 * @exception IllegalArgumentException <ul>
	 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
	 * </ul>
	 * @exception org.eclipse.swt.SWTException <ul>
	 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
	 * </ul>
	 */
	public CompareViewerPane(Composite container, int style) {
		super(container, style);

		marginWidth= 0;
		marginHeight= 0;

		Control topLeft = createTopLeft(this);
		setTopLeft(topLeft);

		MouseAdapter ml= new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				Control content= getContent();
				if (content != null && content.getBounds().contains(e.x, e.y))
					return;
				Control parent= getParent();
				if (parent instanceof Splitter)
					((Splitter)parent).setMaximizedControl(CompareViewerPane.this);
			}
		};

		addMouseListener(ml);
		getTopLeft().addMouseListener(ml);

		addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent e) {
				if (fToolBarManager != null) {
					fToolBarManager.removeAll();
					fToolBarManager.dispose();
				}
				fInput= null;
				fSelectionListeners= null;
				setImage(null);
			}
		});
	}

	/**
	 * @param parent
	 *            a widget which will be the parent of the control (cannot be
	 *            null)
	 * @return the control to be placed in the top left corner of the pane
	 * @noreference This method is not intended to be referenced by clients.
	 * @nooverride This method is not intended to be re-implemented or extended
	 *             by clients.
	 */
	protected Control createTopLeft(Composite parent) {
		CLabel label = new CLabel(this, SWT.NONE) {
			@Override
			public Point computeSize(int wHint, int hHint, boolean changed) {
				return super.computeSize(wHint, Math.max(24, hHint), changed);
			}
		};
		return label;
	}

	/**
	 * Set the pane's title text.
	 * The value <code>null</code> clears it.
	 *
	 * @param label the text to be displayed in the pane or null
	 */
	public void setText(String label) {
		CLabel cl= (CLabel) getTopLeft();
		if (cl != null && !cl.isDisposed())
			cl.setText(label);
	}

	/**
	 * Set the pane's title Image.
	 * The value <code>null</code> clears it.
	 *
	 * @param image the image to be displayed in the pane or null
	 */
	public void setImage(Image image) {
		CLabel cl= (CLabel) getTopLeft();
		if (cl != null)
			cl.setImage(image);
	}

	/**
	 * Returns a <code>ToolBarManager</code> if the given parent is a
	 * <code>CompareViewerPane</code> or <code>null</code> otherwise.
	 *
	 * @param parent a <code>Composite</code> or <code>null</code>
	 * @return a <code>ToolBarManager</code> if the given parent is a <code>CompareViewerPane</code> otherwise <code>null</code>
	 */
	public static ToolBarManager getToolBarManager(Composite parent) {
		if (parent instanceof CompareViewerPane) {
			CompareViewerPane pane= (CompareViewerPane) parent;
			return pane.getToolBarManager();
		}
		return null;
	}

	/**
	 * Clears tool items in the <code>CompareViewerPane</code>'s control bar.
	 *
	 * @param parent a <code>Composite</code> or <code>null</code>
	 */
	public static void clearToolBar(Composite parent) {
		ToolBarManager tbm= getToolBarManager(parent);
		if (tbm != null) {
			tbm.removeAll();
			tbm.update(true);
		}
	}

	//---- private stuff

	private ToolBarManager getToolBarManager() {
		if (fToolBarManager != null && fToolBarManager.getControl() == null)
			return null;
		if (fToolBarManager == null) {
			final ToolBar tb = new ToolBar(this, SWT.FLAT);
			setTopCenter(tb);
			fToolBarManager = new ToolBarManager(tb);
			tb.getAccessible().addAccessibleListener(new AccessibleAdapter() {
				@Override
				public void getName(AccessibleEvent e) {
					if (e.childID != ACC.CHILDID_SELF) {
						ToolItem item = tb.getItem(e.childID);
						if (item != null) {
							String toolTip = item.getToolTipText();
							if (toolTip != null) {
								e.result = toolTip;
							}
						}
					}
				}
			});
		}
		return fToolBarManager;
	}

	/**
	 * Returns the current input of this pane or null if the pane has no input.
	 *
	 * @return an <code>Object</code> that is the input to this pane or null if the pane has no input.
	 *
	 * @since 3.3
	 */
	public Object getInput() {
		return fInput;
	}

	/**
	 * Sets the input object of this pane.
	 *
	 * @param input the new input object or <code>null</code>
	 * @since 3.3
	 */
	public void setInput(Object input) {
		if (fInput != input)
			fInput= input;
	}

	@Override
	public void addSelectionChangedListener(ISelectionChangedListener l) {
		fSelectionListeners.add(l);
	}

	@Override
	public void removeSelectionChangedListener(ISelectionChangedListener l) {
		fSelectionListeners.remove(l);
	}

	@Override
	public ISelection getSelection() {
		return null;
	}

	@Override
	public void setSelection(ISelection s) {
		// Default is to do nothing
	}

	@Override
	public void selectionChanged(SelectionChangedEvent ev) {
		for (ISelectionChangedListener listener : fSelectionListeners) {
			listener.selectionChanged(ev);
		}
	}

	@Override
	public void doubleClick(DoubleClickEvent event) {
		for (IDoubleClickListener listener : fDoubleClickListener) {
			listener.doubleClick(event);
		}
	}

	/**
	 * Add a double-click listener to the pane. The listener will get
	 * invoked when the contents of the pane are double-clicked. Adding
	 * a listener that is already registered has no effect.
	 * @param listener the listener
	 * @since 3.3
	 */
	public void addDoubleClickListener(IDoubleClickListener listener) {
		fDoubleClickListener.add(listener);
	}

	/**
	 * Remove a double-click listener. Removing a listener that is not
	 * registered has no effect.
	 * @param listener the listener
	 * @since 3.3
	 */
	public void removeDoubleClickListener(IDoubleClickListener listener) {
		fDoubleClickListener.remove(listener);
	}

	/**
	 * Add an open listener to the pane. The listener will get
	 * invoked when the contents of the pane are double-clicked. Adding
	 * a listener that is already registered has no effect.
	 * @param listener the listener
	 * @since 3.3
	 */
	public void addOpenListener(IOpenListener listener) {
		fOpenListener.add(listener);
	}

	/**
	 * Remove an open listener. Removing a listener that is not
	 * registered has no effect.
	 * @param listener the listener
	 * @since 3.3
	 */
	public void removeOpenListener(IOpenListener listener) {
		fOpenListener.remove(listener);
	}

	@Override
	public void open(OpenEvent event) {
		Object[] listeners= fOpenListener.getListeners();
		for (int i= 0; i < listeners.length; i++)
			((IOpenListener) listeners[i]).open(event);
	}

	@Override
	public <T> T getAdapter(Class<T> adapter) {
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}
}

Back to the top