001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.testplugin.util;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.jface.dialogs.Dialog;
015: import org.eclipse.jface.dialogs.IDialogConstants;
016: import org.eclipse.jface.dialogs.TitleAreaDialog;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.ShellAdapter;
021: import org.eclipse.swt.events.ShellEvent;
022: import org.eclipse.swt.graphics.Point;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Button;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Group;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Shell;
031:
032: /*
033: * This dialog is intended to verify a dialogs in a testing
034: * environment. The tester can test for sizing, initial focus,
035: * or accessibility.
036: */
037: public class VerifyDialog extends TitleAreaDialog {
038: private int SIZING_WIDTH = 400;
039:
040: private static int TEST_TYPE;
041: public static final int TEST_SIZING = 0;
042: public static final int TEST_FOCUS = 1;
043: public static final int TEST_ACCESS = 2;
044: private IDialogTestPass _dialogTests[] = new IDialogTestPass[3];
045:
046: private Dialog _testDialog; //the dialog to test
047: private Point _testDialogSize;
048:
049: private Label _queryLabel;
050: private Button _yesButton;
051: private Button _checkList[];
052: private String _failureText;
053:
054: /*
055: * Create an instance of the verification dialog.
056: */
057: public VerifyDialog(Shell parent) {
058: super (parent);
059: if (!(TEST_TYPE <= 2) && !(TEST_TYPE >= 0)) {
060: TEST_TYPE = TEST_SIZING;
061: }
062: _failureText = "";
063: _dialogTests[0] = new SizingTestPass();
064: _dialogTests[1] = new FocusTestPass();
065: _dialogTests[2] = new AccessibilityTestPass();
066: }
067:
068: /* (non-Javadoc)
069: * Method declared on Window.
070: */
071: protected void configureShell(Shell newShell) {
072: super .configureShell(newShell);
073: newShell.setText("Dialog Verification");
074: setShellStyle(SWT.NONE);
075: }
076:
077: /* (non-Javadoc)
078: * Method declared on Dialog.
079: */
080: protected void createButtonsForButtonBar(Composite parent) {
081: _yesButton = createButton(parent, IDialogConstants.YES_ID,
082: IDialogConstants.YES_LABEL, true);
083: createButton(parent, IDialogConstants.NO_ID,
084: IDialogConstants.NO_LABEL, false);
085: }
086:
087: /* (non-Javadoc)
088: * Method declared on Dialog.
089: */
090: protected void buttonPressed(int buttonId) {
091: if (IDialogConstants.YES_ID == buttonId) {
092: setReturnCode(IDialogConstants.YES_ID);
093: if (_testDialog.getShell() != null) {
094: _testDialog.close();
095: }
096: close();
097: } else if (IDialogConstants.NO_ID == buttonId) {
098: handleFailure();
099: }
100: }
101:
102: /* (non-Javadoc)
103: * Method declared on Dialog.
104: */
105: protected Control createDialogArea(Composite parent) {
106: // top level composite
107: Composite parentComposite = (Composite) super
108: .createDialogArea(parent);
109:
110: // create a composite with standard margins and spacing
111: Composite composite = new Composite(parentComposite, SWT.NONE);
112: composite.setSize(SIZING_WIDTH, SWT.DEFAULT);
113: GridLayout layout = new GridLayout();
114: layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
115: layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
116: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
117: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
118: composite.setLayout(layout);
119: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
120:
121: createTestSelectionGroup(composite);
122: createCheckListGroup(composite);
123:
124: _queryLabel = new Label(composite, SWT.NONE);
125: _queryLabel
126: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
127:
128: initializeTest();
129: return composite;
130: }
131:
132: /*
133: * Group for selecting type of test.
134: */
135: private void createTestSelectionGroup(Composite parent) {
136: Group group = new Group(parent, SWT.SHADOW_NONE);
137: group.setText("Testing:");
138: group.setLayout(new GridLayout());
139: GridData data = new GridData(GridData.FILL_HORIZONTAL);
140: group.setLayoutData(data);
141:
142: for (int i = 0; i < _dialogTests.length; i++) {
143: Button radio = new Button(group, SWT.RADIO);
144: radio.setText(_dialogTests[i].label());
145: final int testID = _dialogTests[i].getID();
146: radio.addSelectionListener(new SelectionAdapter() {
147: public void widgetSelected(SelectionEvent e) {
148: TEST_TYPE = testID;
149: initializeTest();
150: _yesButton.setEnabled(true);
151: }
152: });
153: if (TEST_TYPE == _dialogTests[i].getID()) {
154: radio.setSelection(true);
155: }
156: }
157: }
158:
159: /*
160: * Initializes the checklist with empty checks.
161: */
162: private void createCheckListGroup(Composite parent) {
163: Group group = new Group(parent, SWT.SHADOW_NONE);
164: group.setText("Verify that:");
165: group.setLayout(new GridLayout());
166: GridData data = new GridData(GridData.FILL_HORIZONTAL);
167: group.setLayoutData(data);
168:
169: int checkListSize = 0;
170: for (int i = 0; i < _dialogTests.length; i++) {
171: int size = _dialogTests[i].checkListTexts().size();
172: if (size > checkListSize) {
173: checkListSize = size;
174: }
175: }
176: _checkList = new Button[checkListSize];
177: SelectionAdapter selectionAdapter = new SelectionAdapter() {
178: public void widgetSelected(SelectionEvent e) {
179: checkYesEnable();
180: }
181: };
182: for (int i = 0; i < checkListSize; i++) {
183: _checkList[i] = new Button(group, SWT.CHECK);
184: _checkList[i].addSelectionListener(selectionAdapter);
185: data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
186: data.grabExcessHorizontalSpace = true;
187: _checkList[i].setLayoutData(data);
188: }
189: }
190:
191: /*
192: * Disables the yes button if any of the items in the checklist
193: * are unchecked. Enables the yes button otherwise.
194: */
195: private void checkYesEnable() {
196: boolean enable = true;
197: for (int i = 0; i < _checkList.length; i++) {
198: if (!_checkList[i].getSelection()) {
199: enable = false;
200: }
201: }
202: _yesButton.setEnabled(enable);
203: }
204:
205: /*
206: * Initializes the checklist, banner texts, and query label
207: */
208: private void initializeTest() {
209: IDialogTestPass test = _dialogTests[TEST_TYPE];
210: setTitle(test.title());
211: setMessage(test.description());
212: Iterator iterator = test.checkListTexts().iterator();
213: for (int i = 0; i < _checkList.length; i++) {
214: if (iterator.hasNext()) {
215: _checkList[i].setText(iterator.next().toString());
216: _checkList[i].setVisible(true);
217: _checkList[i].update();
218: } else {
219: _checkList[i].setVisible(false);
220: _checkList[i].update();
221: }
222: _checkList[i].setSelection(true);
223: }
224: _queryLabel.setText(test.queryText());
225: }
226:
227: public String getFailureText() {
228: return _failureText;
229: }
230:
231: /*
232: * Can't open the verification dialog without a specified
233: * test dialog, this simply returns a failure and prevents
234: * opening. Should use open(Dialog) instead.
235: *
236: */
237: public int open() {
238: _failureText = "Testing dialog is required, use VerifyDialog::open(Dialog)";
239: return IDialogConstants.NO_ID;
240: }
241:
242: /*
243: * Opens the verification dialog to test the specified dialog.
244: */
245: public int open(Dialog testDialog) {
246: if (getShell() == null) {
247: create();
248: }
249: getShell().setLocation(0, 0);
250: getShell().setSize(
251: Math.max(SIZING_WIDTH, getShell().getSize().x),
252: getShell().getSize().y);
253: _testDialog = testDialog;
254: if (_testDialog.getShell() == null) {
255: _testDialog.create();
256: }
257: _testDialogSize = _testDialog.getShell().getSize();
258: openNewTestDialog();
259:
260: return super .open();
261: }
262:
263: /*
264: * Opens the dialog to be verified.
265: */
266: private void openNewTestDialog() {
267: if (_testDialog.getShell() == null) {
268: _testDialog.create();
269: }
270: _testDialog.setBlockOnOpen(false);
271: _testDialog.getShell().setLocation(getShell().getSize().x + 1,
272: 0);
273: _testDialog.getShell().setSize(_testDialogSize);
274: _testDialog.getShell().addShellListener(new ShellAdapter() {
275: public void shellClosed(ShellEvent e) {
276: e.doit = false;
277: }
278:
279: });
280: _testDialog.open();
281: }
282:
283: /*
284: * The test dialog failed, open the failure dialog.
285: */
286: private void handleFailure() {
287: IDialogTestPass test = _dialogTests[TEST_TYPE];
288: StringBuffer text = new StringBuffer();
289: String label = test.label();
290: label = label.substring(0, label.indexOf("&"))
291: + label.substring(label.indexOf("&") + 1);
292: text.append(label).append(" failed on the ").append(
293: SWT.getPlatform()).append(" platform:\n");
294:
295: String failureMessages[] = test.failureTexts();
296: for (int i = 0; i < test.checkListTexts().size(); i++) {
297: if (!_checkList[i].getSelection()) {
298: text.append("- ").append(failureMessages[i]).append(
299: "\n");
300: }
301: }
302: FailureDialog dialog = new FailureDialog(getShell());
303: dialog.create();
304: dialog.setText(text.toString());
305: if (dialog.open() == IDialogConstants.OK_ID) {
306: _failureText = dialog.toString();
307: setReturnCode(IDialogConstants.NO_ID);
308: if (_testDialog.getShell() != null) {
309: _testDialog.close();
310: }
311: close();
312: }
313: }
314:
315: /*
316: * In case the shell was closed by a means other than
317: * the NO button.
318: */
319: protected void handleShellCloseEvent() {
320: handleFailure();
321: }
322: }
|