KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > ProblemDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.dialogs;
12
13 import org.eclipse.core.runtime.IStatus;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.dialogs.ErrorDialog;
24
25 /**
26  * Overrides <code>ErrorDialog</code> to provide a dialog with
27  * the image that corresponds to the <code>IStatus</code>.
28  *
29  * This behavior should be implemented in the ErrorDialog itself,
30  * see: 1GJU7TK: ITPUI:WINNT - DCR: ErrorDialog should not always show the error icon
31  * The class can be removed when the above PR is fixed
32  *
33  * @see org.eclipse.core.runtime.IStatus
34  */

35 public class ProblemDialog extends ErrorDialog {
36
37     private Image fImage;
38     
39     /**
40      * Creates a problem dialog.
41      *
42      * @param parent the shell under which to create this dialog
43      * @param title the title to use for this dialog,
44      * or <code>null</code> to indicate that the default title should be used
45      * @param message the message to show in this dialog,
46      * or <code>null</code> to indicate that the error's message should be shown
47      * as the primary message
48      * @param image the image to be used
49      * @param status the error to show to the user
50      * @param displayMask the mask to use to filter the displaying of child items,
51      * as per <code>IStatus.matches</code>
52      * @see org.eclipse.core.runtime.IStatus#matches
53      */

54     protected ProblemDialog(Shell parent, String JavaDoc title, String JavaDoc message,
55         Image image, IStatus status, int displayMask)
56     {
57         super(parent, title, message, status, displayMask);
58         fImage= image;
59     }
60
61     /*
62      * Overrides method declared on Dialog.
63      */

64     protected Control createDialogArea(Composite parent) {
65         Composite composite= (Composite)super.createDialogArea(parent);
66         if (fImage == null)
67             return composite;
68
69         // find the label that contains the image
70
Control[] kids= composite.getChildren();
71         int childCount= kids.length;
72         Label label= null;
73         int i= 0;
74         while (i < childCount) {
75             if (kids[i] instanceof Label) {
76                 label= (Label)kids[i];
77                 if (label.getImage() != null)
78                     break;
79             }
80             i++;
81         }
82         if (i < childCount && label != null)
83             label.setImage(fImage);
84         applyDialogFont(composite);
85         return composite;
86     }
87
88     /**
89      * Opens a warning dialog to display the given warning. Use this method if the
90      * warning object being displayed does not contain child items, or if you
91      * wish to display all such items without filtering.
92      *
93      * @param parent the parent shell of the dialog, or <code>null</code> if none
94      * @param title the title to use for this dialog,
95      * or <code>null</code> to indicate that the default title should be used
96      * @param message the message to show in this dialog,
97      * or <code>null</code> to indicate that the error's message should be shown
98      * as the primary message
99      * @param status the error to show to the user
100      * @return the code of the button that was pressed that resulted in this dialog
101      * closing. This will be <code>Dialog.OK</code> if the OK button was
102      * pressed, or <code>Dialog.CANCEL</code> if this dialog's close window
103      * decoration or the ESC key was used.
104      */

105     public static int open(Shell parent, String JavaDoc title, String JavaDoc message, IStatus status) {
106         return open(parent, title, message, status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
107     }
108
109     /**
110      * Opens a dialog to display either an error or warning dialog. Use this method if the
111      * status being displayed contains child items <it>and</it> you wish to
112      * specify a mask which will be used to filter the displaying of these
113      * children. The error dialog will only be displayed if there is at
114      * least one child status matching the mask.
115      *
116      * @param parent the parent shell of the dialog, or <code>null</code> if none
117      * @param title the title to use for this dialog,
118      * or <code>null</code> to indicate that the default title should be used
119      * @param message the message to show in this dialog,
120      * or <code>null</code> to indicate that the error's message should be shown
121      * as the primary message
122      * @param status the error to show to the user
123      * @param displayMask the mask to use to filter the displaying of child items,
124      * as per <code>IStatus.matches</code>
125      * @return the code of the button that was pressed that resulted in this dialog
126      * closing. This will be <code>Dialog.OK</code> if the OK button was
127      * pressed, or <code>Dialog.CANCEL</code> if this dialog's close window
128      * decoration or the ESC key was used.
129      * @see org.eclipse.core.runtime.IStatus#matches
130      */

131     public static int open(Shell parent, String JavaDoc title, String JavaDoc message,
132         IStatus status, int displayMask)
133     {
134         Image image;
135         Display display= parent.getDisplay();
136         if (status == null || status.matches(IStatus.ERROR))
137             image= display.getSystemImage(SWT.ICON_ERROR);
138         else if (status.matches(IStatus.WARNING))
139             image= display.getSystemImage(SWT.ICON_WARNING);
140         else
141             image= display.getSystemImage(SWT.ICON_INFORMATION);
142             
143         ErrorDialog dialog= new ProblemDialog(parent, title, message, image, status, displayMask);
144         return dialog.open();
145     }
146     
147 }
148
Popular Tags