KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > handlers > OpenInputDialogHandler


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.cheatsheets.handlers;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.jface.dialogs.InputDialog;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21
22 /**
23  * A command handler to open an <code>InputDialog</code> and return the
24  * result.
25  *
26  * @since 3.2
27  */

28 public class OpenInputDialogHandler extends AbstractHandler {
29
30     private static final String JavaDoc PARAM_ID_TITLE = "title"; //$NON-NLS-1$
31

32     private static final String JavaDoc PARAM_ID_MESSAGE = "message"; //$NON-NLS-1$
33

34     private static final String JavaDoc PARAM_ID_INITIAL_VALUE = "initialValue"; //$NON-NLS-1$
35

36     private static final String JavaDoc PARAM_ID_CANCEL_RETURNS = "cancelReturns"; //$NON-NLS-1$
37

38     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
39
40         String JavaDoc title = event.getParameter(PARAM_ID_TITLE);
41         String JavaDoc message = event.getParameter(PARAM_ID_MESSAGE);
42         String JavaDoc initialValue = event.getParameter(PARAM_ID_INITIAL_VALUE);
43
44         IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
45                 .getActiveWorkbenchWindow();
46         Shell shell = (activeWindow != null) ? activeWindow.getShell() : null;
47
48         InputDialog dialog = new InputDialog(shell, title, message,
49                 initialValue, null);
50         int returnCode = dialog.open();
51         
52         if (returnCode == Window.CANCEL) {
53             String JavaDoc cancelReturns = event.getParameter(PARAM_ID_CANCEL_RETURNS);
54             if (cancelReturns != null)
55                 return cancelReturns;
56             else
57                 throw new ExecutionException("dialog canceled"); //$NON-NLS-1$
58
}
59         
60         return dialog.getValue();
61     }
62
63 }
64
Popular Tags