[Title] Added preferece page link support, message dialog.
authorgyeongseok.seo <gyeongseok.seo@samsung.com>
Wed, 5 Dec 2012 10:15:28 +0000 (19:15 +0900)
committergyeongseok.seo <gyeongseok.seo@samsung.com>
Wed, 5 Dec 2012 10:15:28 +0000 (19:15 +0900)
[Desc.] link preference page. and error, info, confirm, warning, question dialog support.
[Issue] #7570

Change-Id: I8968e7e5b0d19b66099c532f5485f2fc4aa9a4b8

org.tizen.common/src/org/tizen/common/util/PreferenceLinkMessageDialog.java [new file with mode: 0644]

diff --git a/org.tizen.common/src/org/tizen/common/util/PreferenceLinkMessageDialog.java b/org.tizen.common/src/org/tizen/common/util/PreferenceLinkMessageDialog.java
new file mode 100644 (file)
index 0000000..ef98501
--- /dev/null
@@ -0,0 +1,406 @@
+/*
+*  Common
+*
+* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: 
+* Kangho Kim <kh5325.kim@samsung.com>
+* Gyeongseok Seo <gyeongseok.seo@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+* Contributors:
+* - S-Core Co., Ltd
+*
+*/
+package org.tizen.common.util;
+
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.preference.PreferenceDialog;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.accessibility.AccessibleAdapter;
+import org.eclipse.swt.accessibility.AccessibleEvent;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Link;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.dialogs.PreferencesUtil;
+
+/**
+ * PreferenceLinkMessageDialog<br>
+ * 
+ * This class extends {@link MessageDialog}, so It has all MessageDialog's function.
+ * In Addition, Support of preference link operation.<br>
+ * This class's {@link #linkMessage} is important. if linkMessage is <code>null</code> then,
+ * this class's all dialog is same to MessageDialog. even if {@link #preferencePageId} is not null.
+ * 
+ * @author Gyeongseok Seo {@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ *
+ */
+public class PreferenceLinkMessageDialog extends MessageDialog {
+
+       /**
+        * Link Message. when dialog is open then this message is include link.<br>
+        * link is connected to preferencePage, using {@link #preferencePageId}
+        */
+       protected String linkMessage;
+
+       /**
+        * preferencePage's ID. if value is <code>null</code>, do nothing.<br>
+        */
+       protected String preferencePageId;
+
+       /**
+        * Create a message dialog. Note that the dialog will have no visual
+        * representation (no widgets) until it is told to open.
+        * <p>
+        * The labels of the buttons to appear in the button bar are supplied in
+        * this constructor as an array. The <code>open</code> method will return
+        * the index of the label in this array corresponding to the button that was
+        * pressed to close the dialog.
+        * </p>
+        * <p>
+        * <strong>Note:</strong> If the dialog was dismissed without pressing
+        * a button (ESC key, close box, etc.) then {@link SWT#DEFAULT} is returned.
+        * Note that the <code>open</code> method blocks.
+        * </p>
+        *
+        * @param parentShell
+        *            the parent shell
+        * @param dialogTitle
+        *            the dialog title, or <code>null</code> if none
+        * @param dialogTitleImage
+        *            the dialog title image, or <code>null</code> if none
+        * @param dialogMessage
+        *            the dialog message
+        * @param dialogImageType
+        *            one of the following values:
+        *            <ul>
+        *            <li><code>MessageDialog.NONE</code> for a dialog with no
+        *            image</li>
+        *            <li><code>MessageDialog.ERROR</code> for a dialog with an
+        *            error image</li>
+        *            <li><code>MessageDialog.INFORMATION</code> for a dialog
+        *            with an information image</li>
+        *            <li><code>MessageDialog.QUESTION </code> for a dialog with a
+        *            question image</li>
+        *            <li><code>MessageDialog.WARNING</code> for a dialog with a
+        *            warning image</li>
+        *            </ul>
+        * @param dialogButtonLabels
+        *            an array of labels for the buttons in the button bar
+        * @param defaultIndex
+        *            the index in the button label array of the default button
+        * @param linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        */
+       public PreferenceLinkMessageDialog(Shell parentShell, String dialogTitle,
+                       Image dialogTitleImage, String dialogMessage, int dialogImageType,
+                       String[] dialogButtonLabels, int defaultIndex, String linkMessage, String preferencePageId) {
+               super(parentShell, dialogTitle, dialogTitleImage, dialogMessage,
+                               dialogImageType, dialogButtonLabels, defaultIndex);
+               this.linkMessage = linkMessage;
+               this.preferencePageId = preferencePageId;
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.dialogs.IconAndMessageDialog#createMessageArea(org.eclipse.swt.widgets.Composite)
+        */
+       @Override
+       protected Control createMessageArea(Composite composite) {
+               // create composite
+               // create image
+               Image image = getImage();
+               if (image != null) {
+                       imageLabel = new Label(composite, SWT.NULL);
+                       image.setBackground(imageLabel.getBackground());
+                       imageLabel.setImage(image);
+                       addAccessibleListeners(imageLabel, image);
+                       GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING)
+                                       .applyTo(imageLabel);
+               }
+               // create message
+               if ( StringUtil.isEmpty( linkMessage ) ) {
+                       // link not included
+                       if ( !StringUtil.isEmpty( message ) ) {
+                               messageLabel = new Label(composite, getMessageLabelStyle());
+                               messageLabel.setText(message);
+                               GridDataFactory
+                                       .fillDefaults()
+                                       .align(SWT.FILL, SWT.BEGINNING)
+                                       .grab(true, false)
+                                       .hint(
+                                               convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
+                                               SWT.DEFAULT).applyTo(messageLabel);
+                       }
+               } else {
+                       // link included then create link
+                       String fullMessage = StringUtil.EMPTY_STRING;
+                       if ( !StringUtil.isEmpty( message ) ) {
+                               fullMessage = message;
+                       }
+                       fullMessage += " <a>"+linkMessage+"</a>";
+
+                       Link linkLabel = new Link( composite, getMessageLabelStyle() );
+                       linkLabel.setText( fullMessage );
+                       GridDataFactory
+                               .fillDefaults()
+                               .align(SWT.FILL, SWT.BEGINNING)
+                               .grab(true, false)
+                               .hint(
+                                       convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
+                                       SWT.DEFAULT).applyTo(linkLabel);
+                       linkLabel.addSelectionListener( new SelectionListener() {
+                               @Override
+                               public void widgetSelected(SelectionEvent e) {
+                                       String preferencePageId = getPreferencePageId();
+                                       if ( !StringUtil.isEmpty( preferencePageId ) ) {
+                                               PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn( getShell(), preferencePageId, new String[] { preferencePageId }, null );
+                                               dialog.open();
+                                       }
+                               }
+
+                               @Override
+                               public void widgetDefaultSelected(SelectionEvent e) {
+                               }
+                       });
+               }
+
+               return composite;
+       }
+
+       public static String[] getButtonLabels(int kind) {
+               String[] dialogButtonLabels;
+               switch (kind) {
+               case ERROR:
+               case INFORMATION:
+               case WARNING: {
+                       dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL };
+                       break;
+               }
+               case CONFIRM: {
+                       dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL,
+                                       IDialogConstants.CANCEL_LABEL };
+                       break;
+               }
+               case QUESTION: {
+                       dialogButtonLabels = new String[] { IDialogConstants.YES_LABEL,
+                                       IDialogConstants.NO_LABEL };
+                       break;
+               }
+               case QUESTION_WITH_CANCEL: {
+                       dialogButtonLabels = new String[] { IDialogConstants.YES_LABEL,
+                                       IDialogConstants.NO_LABEL,
+                                       IDialogConstants.CANCEL_LABEL };
+                       break;
+               }
+               default: {
+                       throw new IllegalArgumentException(
+                                       "Illegal value for kind in MessageDialog.open()"); //$NON-NLS-1$
+               }
+               }
+               return dialogButtonLabels;
+       }
+
+       /**
+        * Add an accessible listener to the label if it can be inferred from the
+        * image.
+        * 
+        * @param label
+        * @param image
+        */
+       private void addAccessibleListeners(Label label, final Image image) {
+               label.getAccessible().addAccessibleListener(new AccessibleAdapter() {
+                       public void getName(AccessibleEvent event) {
+                               final String accessibleMessage = getAccessibleMessageFor(image);
+                               if (accessibleMessage == null) {
+                                       return;
+                               }
+                               event.result = accessibleMessage;
+                       }
+               });
+       }
+
+       private String getAccessibleMessageFor(Image image) {
+               if (image.equals(getErrorImage())) {
+                       return JFaceResources.getString("error");//$NON-NLS-1$
+               }
+
+               if (image.equals(getWarningImage())) {
+                       return JFaceResources.getString("warning");//$NON-NLS-1$
+               }
+
+               if (image.equals(getInfoImage())) {
+                       return JFaceResources.getString("info");//$NON-NLS-1$
+               }
+
+               if (image.equals(getQuestionImage())) {
+                       return JFaceResources.getString("question"); //$NON-NLS-1$
+               }
+
+               return null;
+       }
+
+       /**
+        * get preferencePage id, it's setting constructor.<br>
+        * used link creation time.
+        * 
+        * @Method Name  : getPreferencePageId
+        * @return preferencePage id
+        */
+       protected String getPreferencePageId() {
+               return this.preferencePageId;
+       }
+
+       /**
+        * Convenience method to open a simple dialog as specified by the
+        * <code>kind</code> flag.
+        * 
+        * @param kind
+        *            the kind of dialog to open, one of {@link MessageDialog#ERROR},
+        *            {@link MessageDialog#INFORMATION}, {@link MessageDialog#QUESTION}, {@link MessageDialog#WARNING},
+        *            {@link MessageDialog#CONFIRM}, or {@link MessageDialog#QUESTION_WITH_CANCEL}.
+        * @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 style
+        *            {@link SWT#NONE} for a default dialog, or {@link SWT#SHEET} for
+        *            a dialog with sheet behavior
+        * @param linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        * @return <code>true</code> if the user presses the OK or Yes button,
+        *         <code>false</code> otherwise
+        * @since 3.5
+        */
+       public static boolean open(int kind, Shell parent, String title,
+                       String message, int style, String linkMessage, String preferencePageId) {
+               PreferenceLinkMessageDialog dialog = new PreferenceLinkMessageDialog(parent, title, null, message,
+                               kind, getButtonLabels(kind), 0, linkMessage, preferencePageId);
+               style &= SWT.SHEET;
+               dialog.setShellStyle(dialog.getShellStyle() | style);
+               return dialog.open() == 0;
+       }
+
+       /**
+        * 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 linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        * @return <code>true</code> if the user presses the OK button,
+        *         <code>false</code> otherwise
+        */
+       public static boolean openConfirm(Shell parent, String title, String message,
+                       String linkMessage, String preferencePageId) {
+               return open(CONFIRM, parent, title, message, SWT.NONE, linkMessage, preferencePageId);
+       }
+
+       /**
+        * 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 linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        */
+       public static void openError(Shell parent, String title, String message,
+                       String linkMessage, String preferencePageId) {
+               open(ERROR, parent, title, message, SWT.NONE, linkMessage, preferencePageId);
+       }
+
+       /**
+        * 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 linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        */
+       public static void openInformation(Shell parent, String title, String message,
+                       String linkMessage, String preferencePageId) {
+               open(INFORMATION, parent, title, message, SWT.NONE, linkMessage, preferencePageId);
+       }
+
+       /**
+        * 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 linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        * @return <code>true</code> if the user presses the Yes button,
+        *         <code>false</code> otherwise
+        */
+       public static boolean openQuestion(Shell parent, String title, String message,
+                       String linkMessage, String preferencePageId) {
+               return open(QUESTION, parent, title, message, SWT.NONE, linkMessage, preferencePageId);
+       }
+
+       /**
+        * 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 linkMessage
+        *            the dialog's link message, or <code>null</code> if none
+        * @param preferencePageId
+        *            linkMessage's linked preferencePageId, not used if linkMessage is null or empty
+        */
+       public static void openWarning(Shell parent, String title, String message,
+                       String linkMessage, String preferencePageId) {
+               open(WARNING, parent, title, message, SWT.NONE, linkMessage, preferencePageId);
+       }
+}