Skip to main content
summaryrefslogtreecommitdiffstats
blob: eafa8ed14e65cf8d97a167e3ea4fb94a706c285f (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
/*******************************************************************************
 * Copyright (c) 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.ui.internal.util;

import org.eclipse.jpt.ui.internal.listeners.SWTPropertyChangeListenerWrapper;
import org.eclipse.jpt.utility.internal.Transformer;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.PageBook;

/**
 * This controller is responsible to switch the active page based on a value. A
 * <code>Transformer</code> is used to transformed that value into a
 * <code>Control</code>.
 *
 * @version 2.0
 * @since 2.0
 */
public final class ControlSwitcher
{
	/**
	 * The widget that is used to show the active <code>Control</code>.
	 */
	private PageBook pageBook;

	/**
	 * The <code>Transformer</code> used to transform the value into a
	 * <code>Control</code>.
	 */
	private Transformer<?, Control> paneTransformer;

	/**
	 * Creates a new <code>ControlSwitcher</code>.
	 *
	 * @param switchHolder The holder of the value that will be used to retrieve
	 * the right <code>Control</code> when passed to the given transformer
	 * @param paneTransformer The <code>Transformer</code> used to transform the value into a
	 * <code>Control</code>
	 * @param pageBook The <code>Transformer</code> used to transform the value
	 * into a <code>Control</code>
	 */
	public <T> ControlSwitcher(PropertyValueModel<? extends T> switchHolder,
	                           Transformer<T, Control> paneTransformer,
	                           PageBook pageBook)
	{
		super();
		initialize(switchHolder, paneTransformer, pageBook);
	}

	private PropertyChangeListener buildPropertyChangeListener() {
		return new SWTPropertyChangeListenerWrapper(
			buildPropertyChangeListener_()
		);
	}

	private PropertyChangeListener buildPropertyChangeListener_() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				switchPanes(e.newValue());
			}
		};
	}

	/**
	 * Initializes this <code>ControlSwitcher</code>.
	 *
	 * @param switchHolder The holder of the value that will be used to retrieve
	 * the right <code>Control</code> when passed to the given transformer
	 * @param paneTransformer The <code>Transformer</code> used to transform the value into a
	 * <code>Control</code>
	 * @param pageBook The <code>Transformer</code> used to transform the value
	 * into a <code>Control</code>
	 */
	private void initialize(PropertyValueModel<?> switchHolder,
	                        Transformer<?, Control> paneTransformer,
	                        PageBook pageBook)
	{
		this.pageBook        = pageBook;
		this.paneTransformer = paneTransformer;

		switchHolder.addPropertyChangeListener(
			PropertyValueModel.VALUE,
			buildPropertyChangeListener()
		);

		switchPanes(switchHolder.value());
	}

	/**
	 * Switches the active page by transforming the given value into its
	 * corresponding pane.
	 *
	 * @param value The state passed to the transformer in order to retrieve the
	 * new pane
	 */
	private void switchPanes(Object value) {

		if (pageBook.isDisposed()) {
			return;
		}

		// Retrieve the Control for the new value
		Control pane = transform(value);
		boolean visible = (pane != null);

		// Show the new page
		if (visible) {
			pageBook.showPage(pane);
		}
		else {
			// Note: We can't null due to a bug in PageBook
			pageBook.showPage(new Label(pageBook, SWT.SEPARATOR | SWT.HORIZONTAL));
		}

		if (pageBook.isVisible() != visible) {
			pageBook.setVisible(visible);
		}

		// Revalidate the parents in order to update the layout
		SWTUtil.reflow(pageBook);
	}

	@SuppressWarnings("unchecked")
	private Control transform(Object value) {
		return ((Transformer<Object, Control>) paneTransformer).transform(value);
	}
}

Back to the top