Skip to main content
summaryrefslogtreecommitdiffstats
blob: a8c2d2f0bfd0e78065e65675ff127b19077521ba (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) 2003 - 2005 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.bugzilla.ui.wizard;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IWorkbench;

/**
 * Class that contains shared functions for the first page of the wizards that
 * submit bug reports. This page provides the user with a list of items to
 * choose from.
 */
public abstract class AbstractWizardListPage extends WizardPage implements
		Listener {

	/** The instance of the workbench */
	protected IWorkbench workbench;

	/** The list box for the list of items to choose from */
	protected List listBox;

	/** Status variable for the possible errors on this page */
	protected IStatus listStatus;

	/**
	 * Constructor for AbstractWizardListPage
	 * 
	 * @param pageName
	 *            the name of the page
	 * @param title
	 *            the title of the page
	 * @param description
	 *            the description text for the page
	 * @param workbench
	 *            the instance of the workbench
	 */
	public AbstractWizardListPage(String pageName, String title,
			String description, IWorkbench workbench) {
		super(pageName);
		setTitle(title);
		setDescription(description);
		this.workbench = workbench;

		// set the status for the page
		listStatus = new Status(IStatus.OK, "not_used", 0, "", null);
	}

	public abstract void createAdditionalControls(Composite parent);
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		// create the composite to hold the widgets
		GridData gd;
		Composite composite = new Composite(parent, SWT.NULL);

		// create the desired layout for this wizard page
		GridLayout gl = new GridLayout();
		int ncol = 1;
		gl.numColumns = ncol;
		composite.setLayout(gl);

		// create the bug report label
		Label label = new Label(composite, SWT.NONE);
		label.setText(getTableName());
		FontData fontData = label.getFont().getFontData()[0];
		fontData.setStyle(SWT.BOLD | fontData.getStyle());
		int height = (int) Math.abs(fontData.getHeight() * 1.25);
		fontData.setHeight(height);
		Font font = new Font(null, fontData);
		label.setFont(font);

		// create the list of bug reports
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 200;
		listBox = new List(composite, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY
				| SWT.V_SCROLL);
		listBox.setLayoutData(gd);

		createLine(composite, ncol);

		// Each wizard has different types of items to add to the list
		populateList();
		
		createAdditionalControls(composite);
		
		// set the composite as the control for this page
		setControl(composite);
		addListeners();
	}

	public abstract String getTableName();

	/**
	 * Populate the list of items
	 */
	abstract protected void populateList();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
	 */
	abstract public void handleEvent(Event event);

	/**
	 * A helper function for "handleEvent"
	 * 
	 * @param event
	 *            the event which occurred
	 * @param errorMessage
	 *            the error message unique to the wizard calling this function
	 */
	protected void handleEventHelper(Event event, String errorMessage) {
		// Initialize a variable with the no error status
		Status status = new Status(IStatus.OK, "not_used", 0, "", null);

		// If the event is triggered by the list of items, respond with the
		// corresponding status
		if (event.widget == listBox) {
			if (listBox.getSelectionIndex() == -1)
				status = new Status(IStatus.ERROR, "not_used", 0, errorMessage,
						null);
			listStatus = status;
		}

		// Show the most serious error
		applyToStatusLine(listStatus);
		getWizard().getContainer().updateButtons();
	}

	/**
	 * Applies the status to the status line of a dialog page.
	 * 
	 * @param status
	 *            The status to apply to the status line
	 */
	protected void applyToStatusLine(IStatus status) {
		String message = status.getMessage();
		if (message.length() == 0)
			message = null;
		switch (status.getSeverity()) {
		case IStatus.OK:
			setErrorMessage(null);
			setMessage(message);
			break;
		case IStatus.WARNING:
			setErrorMessage(null);
			setMessage(message, WizardPage.WARNING);
			break;
		case IStatus.INFO:
			setErrorMessage(null);
			setMessage(message, WizardPage.INFORMATION);
			break;
		default:
			setErrorMessage(null);
			setMessage(message, WizardPage.ERROR);
			break;
		}
	}

	/**
	 * Create a separator line in the dialog
	 * 
	 * @param parent
	 *            The composite to create the line on
	 * @param ncol
	 *            The number of columns to span
	 */
	protected void createLine(Composite parent, int ncol) {
		Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = ncol;
		line.setLayoutData(gridData);
	}

	@Override
	public boolean canFlipToNextPage() {
		if (getErrorMessage() != null)
			return false;
		if (listBox.getSelectionIndex() != -1)
			return true;
		return false;
	}

	/**
	 * Add any listeners that we need
	 */
	protected void addListeners() {
		listBox.addListener(SWT.Selection, this);
	}

}

Back to the top