📄 messagedialogwithtoggle.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.dialogs;import org.eclipse.jface.preference.IPreferenceStore;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Shell;/** * <p> * A message dialog which also allows the user to adjust a toggle setting. If a * preference store is provided and the user selects the toggle, then the user's * answer (yes/ok or no) will be persisted in the store. If no store is * provided, then this information can be queried after the dialog closes. * </p> * <p> * This type of dialog should be used whenever you want to user to be able to * avoid being prompted in the future. It is <strong>strongly </strong> * recommended that a cancel option be provided, so that the user has the option * of making the decision at a later point in time. The semantic for a cancel * button should be to cancel the operation (if it has not yet started), or stop * the operation (if it has already started). * </p> * <p> * It is the responsibility of the developer to provide a mechanism for the user * to change this preference at some later point in time (e.g., through a * preference page). * </p> * * @since 3.0 */public class MessageDialogWithToggle extends MessageDialog { /** * The value of the preference when the user has asked that the answer to * the question always be "okay" or "yes". */ public static final String ALWAYS = "always"; //$NON-NLS-1$ /** * The value of the preference when the user has asked that the answer to * the question always be "no". */ public static final String NEVER = "never"; //$NON-NLS-1$ /** * The value of the preference when the user wishes to prompted for an * answer every time the question is to be asked. */ public static final String PROMPT = "prompt"; //$NON-NLS-1$ /** * Convenience method to open a standard error dialog. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param title * the dialog's title, or <code>null</code> if none * @param message * the message * @param toggleMessage * the message for the toggle control, or <code>null</code> for * the default message * @param toggleState * the initial state for the toggle * @param store * the IPreference store in which the user's preference should be * persisted; <code>null</code> if you don't want it persisted * automatically. * @param key * the key to use when persisting the user's preference; * <code>null</code> if you don't want it persisted. * @return the dialog, after being closed by the user, which the client can * only call <code>getReturnCode()</code> or * <code>getToggleState()</code> */ public static MessageDialogWithToggle openError(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) { MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, title, null, // accept the default window icon message, ERROR, new String[] { IDialogConstants.OK_LABEL }, 0, // ok // is // the // default toggleMessage, toggleState); dialog.prefStore = store; dialog.prefKey = key; dialog.open(); return dialog; } /** * Convenience method to open a standard information dialog. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param title * the dialog's title, or <code>null</code> if none * @param message * the message * @param toggleMessage * the message for the toggle control, or <code>null</code> for * the default message * @param toggleState * the initial state for the toggle * @param store * the IPreference store in which the user's preference should be * persisted; <code>null</code> if you don't want it persisted * automatically. * @param key * the key to use when persisting the user's preference; * <code>null</code> if you don't want it persisted. * * @return the dialog, after being closed by the user, which the client can * only call <code>getReturnCode()</code> or * <code>getToggleState()</code> */ public static MessageDialogWithToggle openInformation(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) { MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, title, null, // accept the default window icon message, INFORMATION, new String[] { IDialogConstants.OK_LABEL }, 0, // ok is the // default toggleMessage, toggleState); dialog.prefStore = store; dialog.prefKey = key; dialog.open(); return dialog; } /** * Convenience method to open a simple confirm (OK/Cancel) dialog. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param title * the dialog's title, or <code>null</code> if none * @param message * the message * @param toggleMessage * the message for the toggle control, or <code>null</code> for * the default message * @param toggleState * the initial state for the toggle * @param store * the IPreference store in which the user's preference should be * persisted; <code>null</code> if you don't want it persisted * automatically. * @param key * the key to use when persisting the user's preference; * <code>null</code> if you don't want it persisted. * @return the dialog, after being closed by the user, which the client can * only call <code>getReturnCode()</code> or * <code>getToggleState()</code> */ public static MessageDialogWithToggle openOkCancelConfirm(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) { MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, title, null, // accept the default window icon message, QUESTION, new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }, 0, // OK is the default toggleMessage, toggleState); dialog.prefStore = store; dialog.prefKey = key; dialog.open(); return dialog; } /** * Convenience method to open a standard warning dialog. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param title * the dialog's title, or <code>null</code> if none * @param message * the message * @param toggleMessage * the message for the toggle control, or <code>null</code> for * the default message * @param toggleState * the initial state for the toggle * @param store * the IPreference store in which the user's preference should be * persisted; <code>null</code> if you don't want it persisted * automatically. * @param key * the key to use when persisting the user's preference; * <code>null</code> if you don't want it persisted. * @return the dialog, after being closed by the user, which the client can * only call <code>getReturnCode()</code> or * <code>getToggleState()</code> */ public static MessageDialogWithToggle openWarning(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) { MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, title, null, // accept the default window icon message, WARNING, new String[] { IDialogConstants.OK_LABEL }, 0, // ok is the default toggleMessage, toggleState); dialog.prefStore = store; dialog.prefKey = key; dialog.open(); return dialog; } /** * Convenience method to open a simple question Yes/No/Cancel dialog. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param title * the dialog's title, or <code>null</code> if none * @param message * the message * @param toggleMessage * the message for the toggle control, or <code>null</code> for * the default message * @param toggleState * the initial state for the toggle * @param store * the IPreference store in which the user's preference should be * persisted; <code>null</code> if you don't want it persisted * automatically. * @param key * the key to use when persisting the user's preference; * <code>null</code> if you don't want it persisted. * @return the dialog, after being closed by the user, which the client can * only call <code>getReturnCode()</code> or * <code>getToggleState()</code> */ public static MessageDialogWithToggle openYesNoCancelQuestion(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) { MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, title, null, // accept the default window icon message, QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 0, // YES is the // default toggleMessage, toggleState); dialog.prefStore = store; dialog.prefKey = key; dialog.open(); return dialog; } /** * Convenience method to open a simple Yes/No question dialog. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param title * the dialog's title, or <code>null</code> if none * @param message * the message * @param toggleMessage * the message for the toggle control, or <code>null</code> for * the default message * @param toggleState * the initial state for the toggle * @param store * the IPreference store in which the user's preference should be * persisted; <code>null</code> if you don't want it persisted * automatically. * @param key * the key to use when persisting the user's preference; * <code>null</code> if you don't want it persisted. * * @return the dialog, after being closed by the user, which the client can * only call <code>getReturnCode()</code> or * <code>getToggleState()</code> */ public static MessageDialogWithToggle openYesNoQuestion(Shell parent, String title, String message, String toggleMessage, boolean toggleState, IPreferenceStore store, String key) { MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, title, null, // accept the default window icon message, QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, // yes is the default toggleMessage, toggleState); dialog.prefStore = store; dialog.prefKey = key; dialog.open(); return dialog; } /** * The key at which the toggle state should be stored within the * preferences. This value may be <code>null</code>, which indicates that * no preference should be updated automatically. It is then the * responsibility of the user of this API to use the information from the * toggle. Note: a <code>prefStore</code> is also needed. */ private String prefKey = null; /** * The preference store which will be affected by the toggle button. This * value may be <code>null</code>, which indicates that no preference * should be updated automatically. It is then the responsibility of the * user of this API to use the information from the toggle. Note: a * <code>prefKey</code> is also needed. */ private IPreferenceStore prefStore = null; /** * The toggle button (widget). This value is <code>null</code> until the * dialog is created. */ private Button toggleButton = null; /** * The message displayed to the user, with the toggle button. This is the * text besides the toggle. If it is <code>null</code>, this means that * the default text for the toggle should be used. */ private String toggleMessage; /** * The initial selected state of the toggle. */ private boolean toggleState; /** * Creates a message dialog with a toggle. See the superclass constructor * for info on the other parameters. * * @param parentShell * the parent shell * @param dialogTitle * the dialog title, or <code>null</code> if none
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -