diff options
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface')
4 files changed, 1584 insertions, 0 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/CustomTitleAreaDialog.java b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/CustomTitleAreaDialog.java new file mode 100644 index 000000000..009c69b5e --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/CustomTitleAreaDialog.java @@ -0,0 +1,374 @@ +/******************************************************************************* + * Copyright (c) 2011 Wind River Systems, Inc. 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: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tcf.te.ui.jface.dialogs; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.jface.dialogs.IMessageProvider; +import org.eclipse.jface.dialogs.TitleAreaDialog; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Layout; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.tcf.te.ui.swt.activator.UIPlugin; +import org.eclipse.ui.PlatformUI; + +/** + * Custom title area dialog implementation. + */ +public class CustomTitleAreaDialog extends TitleAreaDialog implements IMessageProvider { + protected static final int comboHistoryLength = 10; + private String contextHelpId = null; + + // The dialog settings storage + private IDialogSettings dialogSettings; + + private String message; + private int messageType; + private String errorMessage; + private String title; + + // The default message is shown to the user if no other message is set + private String defaultMessage; + private int defaultMessageType; + + /** + * Constructor. + * + * @param parent The parent shell used to view the dialog. + */ + public CustomTitleAreaDialog(Shell parent) { + this(parent, null); + } + + /** + * Constructor. + * + * @param parent The parent shell used to view the dialog, or <code>null</code>. + * @param contextHelpId The dialog context help id or <code>null</code>. + */ + public CustomTitleAreaDialog(Shell parent, String contextHelpId) { + super(parent); + initializeDialogSettings(); + setContextHelpId(contextHelpId); + } + + protected void setContextHelpId(String contextHelpId) { + this.contextHelpId = contextHelpId; + setHelpAvailable(contextHelpId != null); + } + + /** + * Initialize the dialog settings storage. + */ + protected void initializeDialogSettings() { + IDialogSettings settings = doGetDialogSettingsToInitialize(); + Assert.isNotNull(settings); + IDialogSettings section = settings.getSection(getDialogSettingsSectionName()); + if (section == null) { + section = settings.addNewSection(getDialogSettingsSectionName()); + } + setDialogSettings(section); + } + + /** + * Returns the dialog settings container to use and to initialize. This + * method is called from <code>initializeDialogSettings</code> and allows + * overriding the dialog settings container without changing the dialog + * settings structure. + * + * @return The dialog settings container to use. Must not be <code>null</code>. + */ + protected IDialogSettings doGetDialogSettingsToInitialize() { + return UIPlugin.getDefault().getDialogSettings(); + } + + /** + * Returns the section name to use for separating different persistent + * dialog settings from different dialogs. + * + * @return The section name used to store the persistent dialog settings within the plugins persistent + * dialog settings store. + */ + public String getDialogSettingsSectionName() { + return "CustomTitleAreaDialog"; //$NON-NLS-1$ + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.Dialog#create() + */ + @Override + public void create() { + super.create(); + + // If the dialog got set a message, make sure the message is really shown + // to the user from the beginning. + if (isMessageSet()) { + if (errorMessage != null) { + super.setErrorMessage(errorMessage); + } + else { + super.setMessage(message, messageType); + } + } else if (defaultMessage != null) { + // Default message set + super.setMessage(defaultMessage, defaultMessageType); + } + + // If the dialog got set a title, make sure the title is shown + if (title != null) { + super.setTitle(title); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Control createDialogArea(Composite parent) { + if (contextHelpId != null) { + PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, contextHelpId); + } + + // Let the super implementation create the dialog area control + Control control = super.createDialogArea(parent); + // But fix the layout data for the top control + if (control instanceof Composite) { + configureDialogAreaControl((Composite)control); + } + + return control; + } + + /** + * Configure the dialog top control. + * + * @param composite The dialog top control. Must not be <code>null</code>. + */ + protected void configureDialogAreaControl(Composite composite) { + Assert.isNotNull(composite); + Layout layout = composite.getLayout(); + if (layout == null || layout instanceof GridLayout) { + composite.setLayout(new GridLayout()); + } + } + + /** + * Returns the associated dialog settings storage. + * + * @return The dialog settings storage. + */ + public IDialogSettings getDialogSettings() { + // The dialog settings may not been initialized here. Initialize first in this case + // to be sure that we do have always the correct dialog settings. + if (dialogSettings == null) { + initializeDialogSettings(); + } + return dialogSettings; + } + + /** + * Sets the associated dialog settings storage. + * + * @return The dialog settings storage. + */ + public void setDialogSettings(IDialogSettings dialogSettings) { + this.dialogSettings = dialogSettings; + } + + /** + * Adds the given string to the given string array. + * + * @param history String array to add the given entry to it. + * @param newEntry The new entry to add. + * @return The updated string array containing the old array content plus the new entry. + */ + protected String[] addToHistory(String[] history, String newEntry) { + List<String> l = new ArrayList<String>(Arrays.asList(history)); + addToHistory(l, newEntry); + String[] r = new String[l.size()]; + l.toArray(r); + return r; + } + + /** + * Adds the given string to the given list. + * + * @param history List to add the given entry to it. + * @param newEntry The new entry to add. Must not be <code>null</code> + * + * @return The updated list containing the old list content plus the new entry. + */ + protected void addToHistory(List<String> history, String newEntry) { + Assert.isNotNull(newEntry); + + history.remove(newEntry); + history.add(0, newEntry); + // since only one new item was added, we can be over the limit + // by at most one item + if (history.size() > comboHistoryLength) { + history.remove(comboHistoryLength); + } + } + + /** + * Save current dialog widgets values. + * Called by <code>okPressed</code>. + */ + protected void saveWidgetValues() { + return; + } + + /** + * Restore previous dialog widgets values. + * Note: This method is not called automatically! You have + * to call this method at the appropriate time and place. + */ + protected void restoreWidgetValues() { + return; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.Dialog#okPressed() + */ + @Override + protected void okPressed() { + saveWidgetValues(); + super.okPressed(); + } + + /** + * Cleanup when dialog is closed. + */ + protected void dispose() { + dialogSettings = null; + message = null; + messageType = IMessageProvider.NONE; + errorMessage = null; + title = null; + defaultMessage = null; + defaultMessageType = IMessageProvider.NONE; + } + + /** + * Cleanup the Dialog and close it. + */ + @Override + public boolean close() { + dispose(); + return super.close(); + } + + /** + * Set the enabled state of the dialog button specified by the given id (@see <code>IDialogConstants</code>) + * to the given state. + * + * @param buttonId The button id for the button to change the enabled state for. + * @param enabled The new enabled state to set for the button. + */ + public void setButtonEnabled(int buttonId, boolean enabled) { + Button button = getButton(buttonId); + if (button != null) { + button.setEnabled(enabled); + } + } + + /** + * Sets the title for this dialog. + * + * @param title The title. + */ + public void setDialogTitle(String title) { + if (getShell() != null && !getShell().isDisposed()) { + getShell().setText(title); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.TitleAreaDialog#setTitle(java.lang.String) + */ + @Override + public void setTitle(String newTitle) { + title = newTitle; + super.setTitle(newTitle); + } + + /** + * Set the default message. The default message is shown within the + * dialogs message area if no other message is set. + * + * @param message The default message or <code>null</code>. + * @param type The default message type. See {@link IMessageProvider}. + */ + public void setDefaultMessage(String message, int type) { + defaultMessage = message; + defaultMessageType = type; + // Push the default message to the dialog if no other message is set + if (!isMessageSet() && getContents() != null) { + super.setMessage(defaultMessage, defaultMessageType); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.TitleAreaDialog#setMessage(java.lang.String, int) + */ + @Override + public void setMessage(String newMessage, int newType) { + // To be able to implement IMessageProvider, we have to remember the + // set message ourselfs. There is no access to these information by the + // base class. + message = newMessage; messageType = newType; + // Only pass on to super implementation if the control has been created yet + if (getContents() != null) { + super.setMessage(message != null ? message : defaultMessage, message != null ? messageType : defaultMessageType); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.TitleAreaDialog#setErrorMessage(java.lang.String) + */ + @Override + public void setErrorMessage(String newErrorMessage) { + // See setMessage(...) + errorMessage = newErrorMessage; + super.setErrorMessage(newErrorMessage); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.IMessageProvider#getMessage() + */ + @Override + public String getMessage() { + return errorMessage != null ? errorMessage : message; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.IMessageProvider#getMessageType() + */ + @Override + public int getMessageType() { + return errorMessage != null ? IMessageProvider.ERROR : messageType; + } + + /** + * Returns if or if not an message is set to the dialog. + * + * @return <code>True</code> if a message has been set, <code>false</code> otherwise. + */ + public boolean isMessageSet() { + return errorMessage != null || message != null; + } +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/CustomTrayDialog.java b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/CustomTrayDialog.java new file mode 100644 index 000000000..38e647197 --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/CustomTrayDialog.java @@ -0,0 +1,247 @@ +/******************************************************************************* + * Copyright (c) 2011 Wind River Systems, Inc. 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: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tcf.te.ui.jface.dialogs; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.jface.dialogs.TrayDialog; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Layout; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.tcf.te.ui.swt.activator.UIPlugin; +import org.eclipse.ui.PlatformUI; + + +/** + * Custom tray dialog implementation. + */ +public class CustomTrayDialog extends TrayDialog { + protected static final int comboHistoryLength = 10; + private String contextHelpId = null; + + // the dialog storage + private IDialogSettings dialogSettings; + + /** + * Constructor. + * + * @param shell The parent shell or <code>null</code>. + */ + public CustomTrayDialog(Shell shell) { + this(shell, null); + } + + /** + * Constructor. + * + * @param shell The parent shell or <code>null</code>. + * @param contextHelpId The dialog context help id or <code>null</code>. + */ + public CustomTrayDialog(Shell shell, String contextHelpId) { + super(shell); + initializeDialogSettings(); + setContextHelpId(contextHelpId); + } + + /** + * Configure the dialogs context help id. + * + * @param contextHelpId The context help id or <code>null</code>. + */ + protected void setContextHelpId(String contextHelpId) { + this.contextHelpId = contextHelpId; + setHelpAvailable(contextHelpId != null); + } + + /** + * Initialize the dialog settings storage. + */ + protected void initializeDialogSettings() { + IDialogSettings settings = doGetDialogSettingsToInitialize(); + Assert.isNotNull(settings); + IDialogSettings section = settings.getSection(getDialogSettingsSectionName()); + if (section == null) { + section = settings.addNewSection(getDialogSettingsSectionName()); + } + setDialogSettings(section); + } + + /** + * Returns the dialog settings container to use and to initialize. This + * method is called from <code>initializeDialogSettings</code> and allows + * overriding the dialog settings container without changing the dialog + * settings structure. + * + * @return The dialog settings container to use. Must not be <code>null</code>. + */ + protected IDialogSettings doGetDialogSettingsToInitialize() { + return UIPlugin.getDefault().getDialogSettings(); + } + + /** + * Returns the section name to use for separating different persistent + * dialog settings from different dialogs. + * + * @return The section name used to store the persistent dialog settings within the plugins persistent + * dialog settings store. + */ + public String getDialogSettingsSectionName() { + return "CustomTrayDialog"; //$NON-NLS-1$ + } + + /** + * Returns the associated dialog settings storage. + * + * @return The dialog settings storage. + */ + public IDialogSettings getDialogSettings() { + // The dialog settings may not been initialized here. Initialize first in this case + // to be sure that we do have always the correct dialog settings. + if (dialogSettings == null) { + initializeDialogSettings(); + } + return dialogSettings; + } + + /** + * Sets the associated dialog settings storage. + * + * @return The dialog settings storage. + */ + public void setDialogSettings(IDialogSettings dialogSettings) { + this.dialogSettings = dialogSettings; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Control createDialogArea(Composite parent) { + if (contextHelpId != null) { + PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, contextHelpId); + } + + // Let the super implementation create the dialog area control + Control control = super.createDialogArea(parent); + // But fix the layout data for the top control + if (control instanceof Composite) { + configureDialogAreaControl((Composite)control); + } + + return control; + } + + /** + * Configure the dialog top control. + * + * @param composite The dialog top control. Must not be <code>null</code>. + */ + protected void configureDialogAreaControl(Composite composite) { + Assert.isNotNull(composite); + Layout layout = composite.getLayout(); + if (layout == null || layout instanceof GridLayout) { + composite.setLayout(new GridLayout()); + } + } + + /** + * Adds the given string to the given string array. + * + * @param history String array to add the given entry to it. + * @param newEntry The new entry to add. + * @return The updated string array containing the old array content plus the new entry. + */ + protected String[] addToHistory(String[] history, String newEntry) { + List<String> l = new ArrayList<String>(Arrays.asList(history)); + addToHistory(l, newEntry); + String[] r = new String[l.size()]; + l.toArray(r); + return r; + } + + /** + * Adds the given string to the given list. + * + * @param history List to add the given entry to it. + * @param newEntry The new entry to add. Must not be <code>null</code> + * + * @return The updated list containing the old list content plus the new entry. + */ + protected void addToHistory(List<String> history, String newEntry) { + Assert.isNotNull(newEntry); + + history.remove(newEntry); + history.add(0, newEntry); + // since only one new item was added, we can be over the limit + // by at most one item + if (history.size() > comboHistoryLength) { + history.remove(comboHistoryLength); + } + } + + /** + * Save current dialog widgets values. + * Called by <code>okPressed</code>. + */ + protected void saveWidgetValues() { + return; + } + + /** + * Restore previous dialog widgets values. + * Note: This method is not called automatically! You have + * to call this method at the appropriate time and place. + */ + protected void restoreWidgetValues() { + return; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.Dialog#okPressed() + */ + @Override + protected void okPressed() { + saveWidgetValues(); + super.okPressed(); + } + + /** + * Cleanup when dialog is closed. + */ + protected void dispose() { + dialogSettings = null; + } + + /** + * Cleanup the Dialog and close it. + */ + @Override + public boolean close() { + dispose(); + return super.close(); + } + + /** + * Sets the title for this dialog. + * + * @param title The title. + */ + public void setDialogTitle(String title) { + if (getShell() != null && !getShell().isDisposed()) { + getShell().setText(title); + } + } +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/OptionalMessageDialog.java b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/OptionalMessageDialog.java new file mode 100644 index 000000000..ee98c6cd8 --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/dialogs/OptionalMessageDialog.java @@ -0,0 +1,674 @@ +/******************************************************************************* + * Copyright (c) 2011 Wind River Systems, Inc. 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: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tcf.te.ui.jface.dialogs; + +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.jface.dialogs.MessageDialogWithToggle; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Cursor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Link; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.ToolBar; +import org.eclipse.swt.widgets.ToolItem; +import org.eclipse.tcf.te.ui.swt.activator.UIPlugin; +import org.eclipse.tcf.te.ui.swt.nls.Messages; +import org.eclipse.ui.PlatformUI; + +/** + * Message dialog with "do not show again" and optional help button. The Dialog + * stores the selected button result automatically, when "do not show again" was + * selected. All stored values can be cleared in the Target Explorer preferences + * root page. + * <p> + * Additional information (e.g. last opening time stamp for license warning) can + * be stored using <code>set/getAdditionalDialogInfo()</code>, that should + * also be cleared with the states. + */ +public class OptionalMessageDialog extends MessageDialogWithToggle { + + // section name for the dialog settings stored by this dialog + private static final String DIALOG_ID = "OptionalMessageDialog"; //$NON-NLS-1$ + + // context help id for the dialog + private String contextHelpId; + // the key where the result is stored within the dialog settings section + private String key; + + /** + * Constructor. Message dialog with "do not show again" and optional help + * button. The dialog automatically stores the pressed button when "do not + * show again" was selected. The next time the dialogs <code>open()</code> + * method is called it returns the stored value without opening the dialog. + * When the cancel button was pressed, _NO_ value will be stored. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param image + * The window icon or <code>null</code> if default icon should + * be used. + * @param message + * The dialog message text. + * @param imageType + * The dialog image type (QUESTION, INFORMATION, WARNING, ERROR). + * @param buttonLabels + * The labels for buttons. + * @param defaultIndex + * The default button index. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + */ + public OptionalMessageDialog(Shell parentShell, String title, Image image, String message, int imageType, String[] buttonLabels, int defaultIndex, String key, String contextHelpId) { + + super(parentShell, + title, + image, + message, + imageType, + buttonLabels != null ? buttonLabels : new String [] { IDialogConstants.OK_LABEL }, + defaultIndex, + Messages.getString(DIALOG_ID + (imageType == QUESTION ? "_rememberMyDecision_label" : "_doNotShowAgain_label")), //$NON-NLS-1$ //$NON-NLS-2$ + false); + + this.contextHelpId = contextHelpId; + this.key = key == null || key.trim().length() == 0 ? null : key.trim(); + + if (contextHelpId != null) { + PlatformUI.getWorkbench().getHelpSystem().setHelp(parentShell, contextHelpId); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.Dialog#isResizable() + */ + @Override + protected boolean isResizable() { + return true; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.IconAndMessageDialog#createButtonBar(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Control createButtonBar(Composite parent) { + Composite composite = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginWidth = 0; + layout.marginHeight = 0; + layout.horizontalSpacing = 0; + composite.setLayout(layout); + GridData gd = new GridData(SWT.FILL, SWT.BOTTOM, true, false); + if (parent.getLayout() instanceof GridLayout) { + gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns; + } + composite.setLayoutData(gd); + composite.setFont(parent.getFont()); + + // create help control if needed + if (contextHelpId != null) { + Control helpControl = createHelpControl(composite); + ((GridData)helpControl.getLayoutData()).horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); + } + + Control buttonSection = super.createButtonBar(composite); + ((GridData)buttonSection.getLayoutData()).grabExcessHorizontalSpace = true; + return composite; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.MessageDialog#createButton(org.eclipse.swt.widgets.Composite, int, java.lang.String, boolean) + */ + @Override + protected Button createButton(Composite parent, int id, String label, boolean defaultButton) { + // Allow to re-adjust the button id's. Base implementation is matching + // the button labels against the well known labels defined by IDialogConstants. + // For labels not defined there, the implementation set id's starting with 256. + return super.createButton(parent, adjustButtonIdForLabel(id, label), label, defaultButton); + } + + /** + * Adjust the button id to use for the button with the given label. + * <p> + * <b>Note:</b>Base implementation is matching the button labels against the well known + * labels defined by {@link IDialogConstants}. For labels not defined there, the implementation + * set id's starting with 256. + * <p> + * The default implementation returns the button id unmodified. + * + * @param id The suggested button id. + * @param label The button label. + * @return The effective button id. + */ + protected int adjustButtonIdForLabel(int id, String label) { + return id; + } + + private Control createHelpControl(Composite parent) { + Image helpImage = JFaceResources.getImage(DLG_IMG_HELP); + if (helpImage != null) { + return createHelpImageButton(parent, helpImage); + } + return createHelpLink(parent); + } + + /* + * Creates a button with a help image. This is only used if there + * is an image available. + */ + private ToolBar createHelpImageButton(Composite parent, Image image) { + ToolBar toolBar = new ToolBar(parent, SWT.FLAT | SWT.NO_FOCUS); + ((GridLayout)parent.getLayout()).numColumns++; + toolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); + final Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND); + toolBar.setCursor(cursor); + toolBar.addDisposeListener(new DisposeListener() { + @Override + public void widgetDisposed(DisposeEvent e) { + cursor.dispose(); + } + }); + ToolItem item = new ToolItem(toolBar, SWT.NONE); + item.setImage(image); + item.setToolTipText(JFaceResources.getString("helpToolTip")); //$NON-NLS-1$ + item.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + helpPressed(); + } + }); + return toolBar; + } + + /* + * Creates a help link. This is used when there is no help image + * available. + */ + private Link createHelpLink(Composite parent) { + Link link = new Link(parent, SWT.WRAP | SWT.NO_FOCUS); + ((GridLayout)parent.getLayout()).numColumns++; + link.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); + link.setText("<a>" + IDialogConstants.HELP_LABEL + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$ + link.setToolTipText(IDialogConstants.HELP_LABEL); + link.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + helpPressed(); + } + }); + return link; + } + + /** + * Invoked if the help button is pressed. + */ + /* default */ void helpPressed() { + if (getShell() != null) { + Control c = getShell().getDisplay().getFocusControl(); + while (c != null) { + if (c.isListening(SWT.Help)) { + c.notifyListeners(SWT.Help, new Event()); + break; + } + c = c.getParent(); + } + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.MessageDialog#createCustomArea(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Control createCustomArea(Composite parent) { + if (contextHelpId != null) { + PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, contextHelpId); + } + Label label = new Label(parent, SWT.NULL); + label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + + return label; + } + + /** + * Opens the dialog only, if no dialog result is stored and this dialog + * should be displayed. If a dialog result is stored, this state will be + * returned without opening the dialog. When the dialog is closed and "do + * not show again" was selected, the result will be stored. + * + * @see org.eclipse.jface.window.Window#open() + */ + @Override + public int open() { + int result = getDialogResult(key); + if (result < 0) { + result = super.open(); + if (getToggleState() && result >= 0 && result != IDialogConstants.CANCEL_ID) { + setDialogResult(key, result); + } + } + return result; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.MessageDialogWithToggle#setToggleButton(org.eclipse.swt.widgets.Button) + */ + @Override + protected void setToggleButton(Button button) { + // if no key is given, no toggle button should be displayed + if (button != null && key != null) { + super.setToggleButton(button); + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.MessageDialogWithToggle#createToggleButton(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Button createToggleButton(Composite parent) { + // if no key is given, no toggle button should be created + if (key != null) { + return super.createToggleButton(parent); + } + return null; + } + /** + * Opens a question dialog with OK and CANCEL. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openOkCancelDialog(Shell parentShell, String title, String message, String key, String contextHelpId) { + return openOkCancelDialog(parentShell, title, message, null, key, contextHelpId); + } + + /** + * Opens a question dialog with OK and CANCEL. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param buttonLabel + * An string array listing the labels of the message dialog buttons. If <code>null</code>, the default + * labeling, typically "OK" for a single button message dialog, will be applied. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openOkCancelDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) { + if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }; + OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, QUESTION, buttonLabel, 0, key, contextHelpId); + return dialog.open(); + } + + /** + * Opens a question dialog with YES, NO and CANCEL. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openYesNoCancelDialog(Shell parentShell, String title, String message, String key, String contextHelpId) { + return openYesNoCancelDialog(parentShell, title, message, null, key, contextHelpId); + } + + /** + * Opens a question dialog with YES, NO and CANCEL. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param buttonLabel + * An string array listing the labels of the message dialog buttons. If <code>null</code>, the default + * labeling, typically "OK" for a single button message dialog, will be applied. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openYesNoCancelDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) { + if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }; + OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, QUESTION, buttonLabel, 0, key, contextHelpId); + return dialog.open(); + } + + /** + * Opens a question dialog with YES and NO. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openYesNoDialog(Shell parentShell, String title, String message, String key, String contextHelpId) { + return openYesNoDialog(parentShell, title, message, null, key, contextHelpId); + } + + /** + * Opens a question dialog with YES and NO. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param buttonLabel + * An string array listing the labels of the message dialog buttons. If <code>null</code>, the default + * labeling, typically "OK" for a single button message dialog, will be applied. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openYesNoDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) { + if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }; + OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, QUESTION, buttonLabel, 0, key, contextHelpId); + return dialog.open(); + } + + /** + * Opens a info dialog with OK. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openInformationDialog(Shell parentShell, String title, String message, String key, String contextHelpId) { + return openInformationDialog(parentShell, title, message, null, key, contextHelpId); + } + + /** + * Opens a info dialog with OK. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param buttonLabel + * An string array listing the labels of the message dialog buttons. If <code>null</code>, the default + * labeling, typically "OK" for a single button message dialog, will be applied. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openInformationDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) { + if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL }; + OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, INFORMATION, buttonLabel, 0, key, contextHelpId); + return dialog.open(); + } + + /** + * Opens a warning dialog with OK. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openWarningDialog(Shell parentShell, String title, String message, String key, String contextHelpId) { + return openWarningDialog(parentShell, title, message, null, key, contextHelpId); + } + + /** + * Opens a warning dialog with OK. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param buttonLabel + * An string array listing the labels of the message dialog buttons. If <code>null</code>, the default + * labeling, typically "OK" for a single button message dialog, will be applied. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openWarningDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) { + if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL }; + OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, WARNING, buttonLabel, 0, key, contextHelpId); + return dialog.open(); + } + + /** + * Opens a error dialog with OK. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openErrorDialog(Shell parentShell, String title, String message, String key, String contextHelpId) { + return openErrorDialog(parentShell, title, message, null, key, contextHelpId); + } + + /** + * Opens a error dialog with OK. + * + * @param parentShell + * The shell. + * @param title + * The title for the message dialog. + * @param message + * The dialog message text. + * @param buttonLabel + * An string array listing the labels of the message dialog buttons. If <code>null</code>, the default + * labeling, typically "OK" for a single button message dialog, will be applied. + * @param key + * The unique key for the stored result value (e.g. "<PluginName>.<ActionOrDialogName>"). + * @param contextHelpId + * The optional help context id. If <code>null</code>, no help + * button will be shown. + * @return The stored or selected result. + */ + public static int openErrorDialog(Shell parentShell, String title, String message, String[] buttonLabel, String key, String contextHelpId) { + if (buttonLabel == null) buttonLabel = new String[] { IDialogConstants.OK_LABEL }; + OptionalMessageDialog dialog = new OptionalMessageDialog(parentShell, title, null, message, ERROR, buttonLabel, 0, key, contextHelpId); + return dialog.open(); + } + + /* + * Get the dialog settings section or create it when it is not available. + */ + private static IDialogSettings getDialogSettings() { + IDialogSettings settings = UIPlugin.getDefault().getDialogSettings(); + settings = settings.getSection(DIALOG_ID); + if (settings == null) + settings = UIPlugin.getDefault().getDialogSettings().addNewSection(DIALOG_ID); + return settings; + } + + /** + * Get the stored result for this key. If the dialog should be opened, -1 + * will be returned. + * + * @param key + * The key for the stored result. + * @return The stored result or -1 of the dialog should be opened. + */ + public static int getDialogResult(String key) { + IDialogSettings settings = getDialogSettings(); + try { + return settings.getInt(key + ".result"); //$NON-NLS-1$ + } + catch (NumberFormatException e) { + } + return -1; + } + + /** + * Get the stored toggle state for this key. + * If no state is stored, <code>false</code> will be returned. + * + * @param key + * The key for the stored toggle state. + * @return The stored result or -1 of the dialog should be opened. + */ + public static boolean getDialogToggleState(String key) { + IDialogSettings settings = getDialogSettings(); + return settings.getBoolean(key + ".toggleState"); //$NON-NLS-1$ + } + + /** + * Get the stored info for this key. + * + * @param key + * The key for the stored info. + * @return The stored info or <code>null</code>. + */ + public static String getAdditionalDialogInfo(String key) { + IDialogSettings settings = getDialogSettings(); + return settings.get(key + ".additionalInfo"); //$NON-NLS-1$ + } + + /** + * Set the dialog result for this key. If the result is < 0, the string + * "PROMPT" will be stored. + * + * @param key + * The key to store the result. + * @param result + * The result that should be stored. + */ + public static void setDialogResult(String key, int result) { + IDialogSettings settings = getDialogSettings(); + if (result < 0) { + settings.put(key + ".result", PROMPT); //$NON-NLS-1$ + } + else { + settings.put(key + ".result", result); //$NON-NLS-1$ + } + } + + /** + * Set the dialog toggle state for this key. + * + * @param key + * The key to store the toggle state. + * @param result + * The toggle state that should be stored. + */ + public static void setDialogToggleState(String key, boolean state) { + IDialogSettings settings = getDialogSettings(); + settings.put(key + ".toggleState", state); //$NON-NLS-1$ + } + + /** + * Set additional info for this key. + * + * @param key + * The key to store the additional info. + * @param value + * The additional info that should be stored. + */ + public static void setAdditionalDialogInfo(String key, String value) { + IDialogSettings settings = getDialogSettings(); + settings.put(key + ".additionalInfo", value); //$NON-NLS-1$ + } + + /** + * Clears all stored information for this dialogs + */ + public static void clearAllRememberedStates() { + IDialogSettings settings = UIPlugin.getDefault().getDialogSettings(); + settings.addNewSection(DIALOG_ID); + } +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/images/AbstractImageDescriptor.java b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/images/AbstractImageDescriptor.java new file mode 100644 index 000000000..4d86a552b --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.ui.swt/src/org/eclipse/tcf/te/ui/jface/images/AbstractImageDescriptor.java @@ -0,0 +1,289 @@ +/******************************************************************************* + * Copyright (c) 2011 Wind River Systems, Inc. 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: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tcf.te.ui.jface.images; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.resource.CompositeImageDescriptor; +import org.eclipse.jface.resource.ImageRegistry; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.Point; + +/** + * Extended composite image descriptor. + * <p> + * The image descriptor implementation adds method for easily drawing overlay + * images on different positions on top of a base image. + */ +public abstract class AbstractImageDescriptor extends CompositeImageDescriptor { + // The parent image registry providing the images for drawing + private final ImageRegistry parentImageRegistry; + + // The image descriptor key + private String descriptorKey = null; + + /** + * Constructor. + * + * @param parent The parent image registry. Must not be <code>null</code>. + */ + public AbstractImageDescriptor(ImageRegistry parent) { + super(); + + Assert.isNotNull(parent); + parentImageRegistry = parent; + } + + /** + * Returns the parent image registry. + * + * @return The parent image registry instance. + */ + protected final ImageRegistry getParentImageRegistry() { + return parentImageRegistry; + } + + /** + * Set the image descriptor key. + * + * @param key The image descriptor key. Must not be <code>null</code>. + */ + protected final void setDecriptorKey(String key) { + Assert.isNotNull(key); + descriptorKey = key; + } + + /** + * Returns the image descriptor key. + * + * @return The image descriptor key, or <code>null</code> if not set. + */ + public final String getDecriptorKey() { + return descriptorKey; + } + + /** + * Draw the image, found under the specified key, centered within the + * rectangle given by width x height. + * + * @param key The image key. Must not be <code>null</code>. + * @param width The width of the rectangle to center the image in. + * @param height The height of the rectangle to center the image in. + */ + protected void drawCentered(String key, int width, int height) { + Assert.isNotNull(key); + drawCentered(parentImageRegistry.get(key), width, height); + } + + /** + * Draw the given image centered within the rectangle + * defined by the specified width x height. + * + * @param image The image. Must not be <code>null</code>. + * @param width The width of the rectangle to center the image in. + * @param height The height of the rectangle to center the image in. + */ + protected void drawCentered(Image image, int width, int height) { + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int x = StrictMath.max(0, (width - imageData.width + 1) / 2); + int y = StrictMath.max(0, (height - imageData.height + 1) / 2); + drawImage(imageData, x, y); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, centered right on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + * @param width The width of the base image. + * @param height The height of the base image. + */ + protected void drawCenterRight(String key, int width, int height) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int x = StrictMath.max(0, width - imageData.width); + int y = StrictMath.max(0, (height - imageData.height + 1) / 2); + drawImage(imageData, x, y); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, top left on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + */ + protected void drawTopLeft(String key) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + drawImage(imageData, 0, 0); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, top right on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + */ + protected void drawTopRight(String key) { + if (getSize() != null) { + Point size = getSize(); + drawTopRight(key, size.x, size.y); + } else { + // the default eclipse style guide recommendation is 16x16 + drawTopRight(key, 16, 16); + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, top right on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + * @param width The width of the base image. + * @param height The height of the base image. + */ + protected void drawTopRight(String key, int width, int height) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int x = StrictMath.max(0, width - imageData.width); + drawImage(imageData, x, 0); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, bottom center on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + * @param width The width of the base image. + * @param height The height of the base image. + */ + protected void drawBottomCenter(String key, int width, int height) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int x = StrictMath.max(0, (width - imageData.width + 1) / 2); + int y = StrictMath.max(0, height - imageData.height); + drawImage(imageData, x, y); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, bottom left on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + * @param width The width of the base image. + * @param height The height of the base image. + */ + protected void drawBottomLeft(String key, int width, int height) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int y = StrictMath.max(0, height - imageData.height); + drawImage(imageData, 0, y); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, center left on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + * @param width The width of the base image. + * @param height The height of the base image. + */ + protected void drawCenterLeft(String key, int width, int height) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int y = StrictMath.max(0, (height - imageData.height) / 2); + drawImage(imageData, 0, y); + } + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, bottom right on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + */ + protected void drawBottomRight(String key) { + if (getSize() != null) { + Point size = getSize(); + drawBottomRight(key, size.x, size.y); + } else { + // the default eclipse style guide recommendation is 16x16 + drawBottomRight(key, 16, 16); + } + } + + /** + * Draw the overlay image, found under the specified key in the parent + * image registry, bottom right on top of the base image. + * + * @param key The overlay image key. Must not be <code>null</code>. + * @param width The width of the base image. + * @param height The height of the base image. + */ + protected void drawBottomRight(String key, int width, int height) { + Image image = parentImageRegistry.get(key); + if (image != null) { + ImageData imageData = image.getImageData(); + if (imageData != null) { + int x = StrictMath.max(0, width - imageData.width); + int y = StrictMath.max(0, height - imageData.height); + drawImage(imageData, x, y); + } + } + } + + /** + * Returns the base image used for the combined image description. This + * method is called from <code>getTransparentPixel()</code> to query the + * transparent color of the palette. + * + * @return The base image or <code>null</code> if none. + */ + protected abstract Image getBaseImage(); + + /* (non-Javadoc) + * @see org.eclipse.jface.resource.CompositeImageDescriptor#getTransparentPixel() + */ + @Override + protected int getTransparentPixel() { + Image baseImage = getBaseImage(); + if (baseImage != null && baseImage.getImageData() != null) { + return baseImage.getImageData().transparentPixel; + } + return super.getTransparentPixel(); + } +} |