Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2381ecbe997c8c781835a4cd260494b5b4350e7 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.wizards.pages;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.tcf.te.ui.forms.CustomFormToolkit;
import org.eclipse.tcf.te.ui.forms.FormLayoutFactory;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.ManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * Abstract wizard page using forms.
 */
public abstract class AbstractFormsWizardPage extends AbstractValidatingWizardPage {
	// The forms toolkit instance
	private CustomFormToolkit toolkit = null;
	// The managed form reference
	/* default */ ManagedForm mform = null;

	/**
	 * Internal managed form implementation to link the managed form with the parent
	 * wizard page.
	 */
	private static class WizardPageForm extends ManagedForm {

		/**
		 * Constructor.
		 *
		 * @param parentPage The parent wizard page. Must not be <code>null</code>.
		 * @param form The scrolled form. Must not be <code>null</code>.
		 */
		public WizardPageForm(AbstractFormsWizardPage parentPage, ScrolledForm form) {
			super(parentPage.getFormToolkit().getFormToolkit(), form);
			setContainer(parentPage);
		}

		/**
		 * Returns the parent wizard page.
		 *
		 * @return The parent wizard page.
		 */
		public AbstractFormsWizardPage getParentPage() {
			return (AbstractFormsWizardPage)getContainer();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.forms.ManagedForm#dirtyStateChanged()
		 */
		@Override
		public void dirtyStateChanged() {
			getParentPage().validate();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.forms.ManagedForm#staleStateChanged()
		 */
		@Override
		public void staleStateChanged() {
			getParentPage().validate();
		}
	}

	/**
	 * Constructor.
	 *
	 * @param pageName The page name. Must not be <code>null</code>.
	 */
	public AbstractFormsWizardPage(String pageName) {
		super(pageName);
	}

	/**
	 * Constructor.
	 *
	 * @param pageName The page name. Must not be <code>null</code>.
	 * @param title The wizard page title or <code>null</code>.
	 * @param titleImage The wizard page title image or <code>null</code>.
	 */
	public AbstractFormsWizardPage(String pageName, String title, ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
	 */
	@Override
	public void dispose() {
		if (mform != null) { mform.dispose(); mform = null; }
		if (toolkit != null) { toolkit.dispose(); toolkit = null; }
		super.dispose();
	}

	/**
	 * Creates the forms toolkit to use.
	 *
	 * @param display The display. Must not be <code>null</code>.
	 * @return The forms toolkit instance. Must never be <code>null</code>.
	 */
	protected CustomFormToolkit createFormToolkit(Display display) {
		Assert.isNotNull(display);
		return new CustomFormToolkit(new FormToolkit(display));
	}

	/**
	 * Returns the forms toolkit to use.
	 * <p>
	 * If {@link #createControl(Composite)} hasn't been called yet, or {@link #dispose()} has been
	 * called, the method will return <code>null</code>.
	 *
	 * @return The forms toolkit instance or <code>null</code>.
	 */
	public final CustomFormToolkit getFormToolkit() {
		return toolkit;
	}

	/**
	 * Returns the managed form hosted by this wizard page.
	 *
	 * @return The managed form or <code>null</code> if the form hasn't been created yet.
	 */
	public final IManagedForm getManagedForm() {
		return mform;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		Assert.isNotNull(parent);

		// Create the form toolkit
		toolkit = createFormToolkit(parent.getDisplay());
		Assert.isNotNull(toolkit);

		// Create the scrolled form which will hold the launch configuration tab controls
		ScrolledForm form = toolkit.createScrolledForm(parent, null, true);
		Assert.isNotNull(form);
		// The scrolled form is the main control for this tab
		setControl(form);

		// Create the managed form instance
		mform = new WizardPageForm(this, form);

		// Do not validate the page while creating the form content
		boolean changed = setValidationInProgress(true);
		// Create the form content
		createFormContent(mform);
		// Reset the validation in progress action
		if (changed) setValidationInProgress(false);

		// Adjust the font
		Dialog.applyDialogFont(form);

		// Validate the page for the first time
		validate();
	}

	/**
	 * Subclasses should override this method to create content in the form
	 * hosted in this launch configuration tab.
	 *
	 * @param managedForm The managed form hosted in this tab. Must not be <code>null</code>.
	 */
	protected void createFormContent(IManagedForm managedForm) {
		Assert.isNotNull(managedForm);

		// Configure the managed form
		configureManagedForm(managedForm);

		// Do create the content of the form now
		doCreateFormContent(managedForm.getForm().getBody(), toolkit);

		// Re-arrange the controls
		managedForm.reflow(true);
	}

	/**
	 * Configure the managed form to be ready for usage.
	 *
	 * @param managedForm The managed form. Must not be <code>null</code>.
	 */
	protected void configureManagedForm(IManagedForm managedForm) {
		Assert.isNotNull(managedForm);

		// Configure main layout
		Composite body = managedForm.getForm().getBody();
		body.setLayout(FormLayoutFactory.createFormGridLayout(false, 1));

		// Set context help id
		if (getContextHelpId() != null) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(managedForm.getForm(), getContextHelpId());
		}
	}

	/**
	 * Do create the managed form content.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>
	 * @param toolkit The {@link CustomFormToolkit} instance. Must not be <code>null</code>.
	 */
	protected abstract void doCreateFormContent(Composite parent, CustomFormToolkit toolkit);
}

Back to the top