KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > StatusDialog


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

11 package org.eclipse.debug.internal.ui.actions;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.internal.MessageLine;
25
26 /**
27  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
28  * The status message must be passed over as StatusInfo object and can be
29  * an error, warning or ok. The OK button is enabled or disabled depending
30  * on the status.
31  * Copied from org.eclipse.jdt.internal.ui.StatusDialog
32  */

33 public abstract class StatusDialog extends Dialog {
34
35     private Button fOkButton;
36     private MessageLine fStatusLine;
37     private IStatus fLastStatus;
38     private String JavaDoc fTitle;
39     private Image fImage;
40
41     /**
42      * Creates an instane of a status dialog.
43      */

44     public StatusDialog(Shell parent) {
45         super(parent);
46         fLastStatus = new StatusInfo();
47     }
48
49     /**
50      * Update the dialog's status line to reflect the given status.
51      * It is save to call this method before the dialog has been opened.
52      */

53     protected void updateStatus(IStatus status) {
54         fLastStatus = status;
55         if (fStatusLine != null && !fStatusLine.isDisposed()) {
56             updateButtonsEnableState(status);
57             fStatusLine.setErrorStatus(status);
58         }
59     }
60
61     /**
62      * Returns the last status.
63      */

64     public IStatus getStatus() {
65         return fLastStatus;
66     }
67
68     /**
69      * Updates the status of the ok button to reflect the given status.
70      * Subclasses may override this method to update additional buttons.
71      * @param status the status.
72      */

73     protected void updateButtonsEnableState(IStatus status) {
74         if (fOkButton != null && !fOkButton.isDisposed())
75             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
76     }
77
78     /*
79      * @see Window#create(Shell)
80      */

81     protected void configureShell(Shell shell) {
82         super.configureShell(shell);
83         if (fTitle != null)
84             shell.setText(fTitle);
85     }
86
87     /*
88      * @see Window#create()
89      */

90     public void create() {
91         super.create();
92         if (fLastStatus != null) {
93             // policy: dialogs are not allowed to come up with an error message
94
if (fLastStatus.matches(IStatus.ERROR)) {
95                 StatusInfo status = new StatusInfo();
96                 status.setError(""); //$NON-NLS-1$
97
fLastStatus = status;
98             }
99             updateStatus(fLastStatus);
100         }
101     }
102
103     /*
104      * @see Dialog#createButtonsForButtonBar(Composite)
105      */

106     protected void createButtonsForButtonBar(Composite parent) {
107         fOkButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
108         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
109     }
110
111     /*
112      * @see Dialog#createButtonBar(Composite)
113      */

114     protected Control createButtonBar(Composite parent) {
115         Composite composite = new Composite(parent, SWT.NULL);
116         GridLayout layout = new GridLayout();
117         layout.numColumns = 1;
118         layout.marginHeight = 0;
119         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
120         composite.setLayout(layout);
121         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
122
123         fStatusLine = new MessageLine(composite);
124         fStatusLine.setAlignment(SWT.LEFT);
125         fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126         fStatusLine.setErrorStatus(null); //$NON-NLS-1$
127
applyDialogFont(composite);
128         super.createButtonBar(composite);
129         return composite;
130     }
131
132     /**
133      * Sets the title for this dialog.
134      * @param title the title.
135      */

136     public void setTitle(String JavaDoc title) {
137         fTitle = title != null ? title : ""; //$NON-NLS-1$
138
Shell shell = getShell();
139         if ((shell != null) && !shell.isDisposed())
140             shell.setText(fTitle);
141     }
142
143     /**
144      * Sets the image for this dialog.
145      * @param image the image.
146      */

147     public void setImage(Image image) {
148         fImage = image;
149         Shell shell = getShell();
150         if ((shell != null) && !shell.isDisposed())
151             shell.setImage(fImage);
152     }
153 }
154
Popular Tags