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.ui.examples.jobs.actions;
011:
012: import org.eclipse.core.runtime.OperationCanceledException;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.jface.dialogs.IDialogConstants;
015: import org.eclipse.jface.dialogs.IInputValidator;
016: import org.eclipse.jface.dialogs.InputDialog;
017: import org.eclipse.jface.dialogs.MessageDialog;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.window.Window;
020: import org.eclipse.ui.IWorkbenchWindow;
021: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
022: import org.eclipse.ui.examples.jobs.TestJob;
023:
024: /**
025: * Test action that creates a number of fake jobs, and then waits until they complete.
026: */
027: public class CreateJobsAction implements IWorkbenchWindowActionDelegate {
028: static final long DELAY = 100;
029:
030: private IWorkbenchWindow window;
031:
032: private long askForDuration() {
033: InputDialog dialog = new InputDialog(
034: window.getShell(),
035: "How long?", "Enter the number of milliseconds per job", "1000", new IInputValidator() { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
036: public String isValid(String newText) {
037: try {
038: Long.parseLong(newText);
039: } catch (NumberFormatException e) {
040: return "Not a number"; //$NON-NLS-1$
041: }
042: return null;
043: }
044: });
045: if (dialog.open() == Window.CANCEL)
046: throw new OperationCanceledException();
047: return Long.parseLong(dialog.getValue());
048: }
049:
050: private boolean askForExclusive() {
051: MessageDialog dialog = new MessageDialog(
052: window.getShell(),
053: "Likes to be left alone?", //$NON-NLS-1$
054: null,
055: "Press yes if the jobs should be run one at a time, and no otherwise", //$NON-NLS-1$
056: MessageDialog.QUESTION, new String[] {
057: IDialogConstants.YES_LABEL,
058: IDialogConstants.NO_LABEL }, 1 // no is the default
059: );
060: return dialog.open() == 0;
061: }
062:
063: private boolean askForFailure() {
064: MessageDialog dialog = new MessageDialog(window.getShell(),
065: "Born to fail?", //$NON-NLS-1$
066: null, "Should the jobs return an error status?", //$NON-NLS-1$
067: MessageDialog.QUESTION, new String[] {
068: IDialogConstants.YES_LABEL,
069: IDialogConstants.NO_LABEL }, 1 // no is the default
070: );
071: return dialog.open() == 0;
072: }
073:
074: private int askForJobCount() {
075: InputDialog dialog = new InputDialog(
076: window.getShell(),
077: "How much work?", "Enter the number of jobs to run", "100", new IInputValidator() { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
078: public String isValid(String newText) {
079: try {
080: Integer.parseInt(newText);
081: } catch (NumberFormatException e) {
082: return "Not a number"; //$NON-NLS-1$
083: }
084: return null;
085: }
086: });
087: if (dialog.open() == Window.CANCEL)
088: throw new OperationCanceledException();
089: return Integer.parseInt(dialog.getValue());
090: }
091:
092: public void dispose() {
093: //do nothing
094: }
095:
096: public void init(IWorkbenchWindow window) {
097: this .window = window;
098: }
099:
100: public void run(IAction action) {
101: int jobCount = askForJobCount();
102: long duration = askForDuration();
103: boolean exclusive = askForExclusive();
104: boolean failure = askForFailure();
105: for (int i = 0; i < jobCount; i++) {
106: new TestJob(duration, exclusive, failure, false, false, 0)
107: .schedule(DELAY);
108: }
109: }
110:
111: public void selectionChanged(IAction action, ISelection selection) {
112: //do nothing
113: }
114: }
|