1 11 package org.eclipse.ui.internal.cheatsheets.handlers; 12 13 import java.util.ArrayList ; 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 29 public class OpenMessageDialogHandler extends AbstractHandler { 30 31 private static final String PARAM_ID_TITLE = "title"; 33 private static final String PARAM_ID_MESSAGE = "message"; 35 private static final String PARAM_ID_IMAGE_TYPE = "imageType"; 37 private static final String PARAM_ID_DEFAULT_INDEX = "defaultIndex"; 39 private static final String PARAM_ID_BUTTON_LABEL_PREFIX = "buttonLabel"; 41 private static final int BUTTON_LABEL_COUNT = 4; 42 43 private static final String PARAM_ID_CANCEL_RETURNS = "cancelReturns"; 45 private static final int CANCEL_RETURN_CODE = -1; 46 47 public Object execute(ExecutionEvent event) throws ExecutionException { 48 49 String title = event.getParameter(PARAM_ID_TITLE); 50 String message = event.getParameter(PARAM_ID_MESSAGE); 51 52 int imageType = MessageDialog.NONE; 53 if (event.getParameter(PARAM_ID_IMAGE_TYPE) != null) { 54 Integer imageTypeInteger = (Integer ) 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 defaultValueInteger = (Integer ) event 62 .getObjectParameterForExecution(PARAM_ID_DEFAULT_INDEX); 63 defaultValue = defaultValueInteger.intValue(); 64 } 65 66 String [] 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 cancelReturns = event.getParameter(PARAM_ID_CANCEL_RETURNS); 78 if (cancelReturns != null) 79 return cancelReturns; 80 else 81 throw new ExecutionException("dialog canceled"); } 83 84 return buttonLabels[returnCode]; 85 } 86 87 private String [] collectButtonLabels(ExecutionEvent event) { 88 89 ArrayList buttonLabelList = new ArrayList (); 90 91 for (int i = 0; i < BUTTON_LABEL_COUNT; i++) { 92 String buttonLabelParamId = PARAM_ID_BUTTON_LABEL_PREFIX 93 + Integer.toString(i); 94 String buttonLabel = event.getParameter(buttonLabelParamId); 95 96 if (buttonLabel == null) { 97 break; 98 } 99 100 buttonLabelList.add(buttonLabel); 101 } 102 103 return (String []) buttonLabelList.toArray(new String [buttonLabelList 104 .size()]); 105 } 106 107 } 108 | Popular Tags |