KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.PlatformUI;
22
23 /**
24  * A command handler to open a <code>MessageDialog</code> and return the
25  * result.
26  *
27  * @since 3.2
28  */

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

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

35     private static final String JavaDoc PARAM_ID_IMAGE_TYPE = "imageType"; //$NON-NLS-1$
36

37     private static final String JavaDoc PARAM_ID_DEFAULT_INDEX = "defaultIndex"; //$NON-NLS-1$
38

39     private static final String JavaDoc PARAM_ID_BUTTON_LABEL_PREFIX = "buttonLabel"; //$NON-NLS-1$
40

41     private static final int BUTTON_LABEL_COUNT = 4;
42     
43     private static final String JavaDoc PARAM_ID_CANCEL_RETURNS = "cancelReturns"; //$NON-NLS-1$
44

45     private static final int CANCEL_RETURN_CODE = -1;
46     
47     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
48
49         String JavaDoc title = event.getParameter(PARAM_ID_TITLE);
50         String JavaDoc message = event.getParameter(PARAM_ID_MESSAGE);
51
52         int imageType = MessageDialog.NONE;
53         if (event.getParameter(PARAM_ID_IMAGE_TYPE) != null) {
54             Integer JavaDoc imageTypeInteger = (Integer JavaDoc) event
55                     .getObjectParameterForExecution(PARAM_ID_IMAGE_TYPE);
56             imageType = imageTypeInteger.intValue();
57         }
58
59         int defaultValue = 0;
60         if (event.getParameter(PARAM_ID_DEFAULT_INDEX) != null) {
61             Integer JavaDoc defaultValueInteger = (Integer JavaDoc) event
62                     .getObjectParameterForExecution(PARAM_ID_DEFAULT_INDEX);
63             defaultValue = defaultValueInteger.intValue();
64         }
65
66         String JavaDoc[] buttonLabels = collectButtonLabels(event);
67
68         IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
69                 .getActiveWorkbenchWindow();
70         Shell shell = (activeWindow != null) ? activeWindow.getShell() : null;
71
72         MessageDialog dialog = new MessageDialog(shell, title, null, message,
73                 imageType, buttonLabels, defaultValue);
74         int returnCode = dialog.open();
75         
76         if (returnCode == CANCEL_RETURN_CODE) {
77             String JavaDoc cancelReturns = event.getParameter(PARAM_ID_CANCEL_RETURNS);
78             if (cancelReturns != null)
79                 return cancelReturns;
80             else
81                 throw new ExecutionException("dialog canceled"); //$NON-NLS-1$
82
}
83         
84         return buttonLabels[returnCode];
85     }
86
87     private String JavaDoc[] collectButtonLabels(ExecutionEvent event) {
88
89         ArrayList JavaDoc buttonLabelList = new ArrayList JavaDoc();
90
91         for (int i = 0; i < BUTTON_LABEL_COUNT; i++) {
92             String JavaDoc buttonLabelParamId = PARAM_ID_BUTTON_LABEL_PREFIX
93                     + Integer.toString(i);
94             String JavaDoc buttonLabel = event.getParameter(buttonLabelParamId);
95
96             if (buttonLabel == null) {
97                 break;
98             }
99
100             buttonLabelList.add(buttonLabel);
101         }
102
103         return (String JavaDoc[]) buttonLabelList.toArray(new String JavaDoc[buttonLabelList
104                 .size()]);
105     }
106
107 }
108
Popular Tags