1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.ui.wizards; 34 35 import org.eclipse.jface.dialogs.IDialogConstants; 36 import org.eclipse.jface.dialogs.IDialogSettings; 37 import org.eclipse.jface.dialogs.TitleAreaDialog; 38 import org.eclipse.jface.wizard.IWizardPage; 39 import org.eclipse.jface.wizard.WizardDialog; 40 import org.eclipse.jface.wizard.WizardPage; 41 import org.eclipse.swt.SWT; 42 import org.eclipse.swt.graphics.Image; 43 import org.eclipse.swt.widgets.Button; 44 import org.eclipse.swt.widgets.Composite; 45 import org.eclipse.swt.widgets.Control; 46 import org.eclipse.swt.widgets.Display; 47 import org.eclipse.swt.widgets.Shell; 48 49 import org.openjdk.jmc.ui.misc.DisplayToolkit; 50 51 /** 52 * Wizard that wraps an {@link IWizardPage} so it looks like an ordinary {@link TitleAreaDialog}. 53 * Benefits of using a wizard page instead of deriving from {@link TitleAreaDialog} is that the page 54 * can be reused in an ordinary wizard. 55 * <p> 56 * This class also takes care of window title, shell image, placement. 57 * <p> 58 * If the {@link IWizardPage} implements {@link IPerformFinishable} the method 59 * {@link IPerformFinishable#performFinish()} is called when the user presses OK. 60 */ 61 public class OnePageWizardDialog extends SizeConstrainedWizardDialog { 62 private Image m_image; 63 private boolean m_setFinishButtonAsOK = true; 64 private boolean m_hideCancelButton; 65 66 /** 67 * Creates a {@link WizardDialog} with only one {@link WizardPage} 68 * 69 * @param shell 70 * the shell 71 * @param page 72 * the {@link WizardPage} 73 * @param image 74 * the image to use for the window 75 * @param title 76 * the window title 77 */ 78 public OnePageWizardDialog(Shell shell, IWizardPage page) { 79 super(shell, new OnePageWizard(page)); 80 } 81 82 public OnePageWizardDialog(Shell shell, IWizardPage page, Image image) { 83 super(shell, new OnePageWizard(page)); 84 m_image = image; 85 } 86 87 public void setDialogSettings(IDialogSettings settings) { 88 getOnePageWizard().setDialogSettings(settings); 89 } 90 91 public IDialogSettings getDialogSettings() { 92 return getOnePageWizard().getDialogSettings(); 93 } 94 95 public IWizardPage getPage() { 96 return getOnePageWizard().getPage(); 97 } 98 99 private OnePageWizard getOnePageWizard() { 100 return (OnePageWizard) getWizard(); 101 } 102 103 public void setFinishButtonLabelAsOK(boolean setFinishButtonAsOK) { 104 m_setFinishButtonAsOK = setFinishButtonAsOK; 105 } 106 107 public void setHideCancelButton(boolean hideCancelButton) { 108 m_hideCancelButton = hideCancelButton; 109 } 110 111 @Override 112 protected Control createContents(Composite parent) { 113 Control control = super.createContents(parent); 114 if (m_image != null) { 115 getShell().setImage(m_image); 116 } 117 118 updateShellSize(); 119 DisplayToolkit.placeDialogInCenter(getParentShell(), getShell()); 120 if (m_setFinishButtonAsOK) { 121 getButton(IDialogConstants.FINISH_ID).setText(IDialogConstants.OK_LABEL); 122 } 123 if (m_hideCancelButton) { 124 Button showButton; 125 Button hideButton; 126 if (parent.getDisplay().getDismissalAlignment() == SWT.RIGHT) { 127 // Linux 128 showButton = getButton(IDialogConstants.FINISH_ID); 129 hideButton = getButton(IDialogConstants.CANCEL_ID); 130 } else { 131 // Windows 132 showButton = getButton(IDialogConstants.CANCEL_ID); 133 hideButton = getButton(IDialogConstants.FINISH_ID); 134 } 135 hideButton.setVisible(false); 136 showButton.setText(IDialogConstants.OK_LABEL); 137 showButton.setFocus(); 138 } 139 140 return control; 141 } 142 143 public boolean performFinish() { 144 return getWizard().performFinish(); 145 } 146 147 public static int open(IWizardPage wp, int width, int height) { 148 OnePageWizardDialog d = new OnePageWizardDialog(Display.getCurrent().getActiveShell(), wp); 149 d.setWidthConstraint(width, width); 150 d.setHeightConstraint(height, height); 151 return d.open(); 152 } 153 }