KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.jdt.internal.debug.ui;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24
25 import org.eclipse.core.runtime.IStatus;
26
27 /**
28  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
29  * The status message must be passed over as StatusInfo object and can be
30  * an error, warning or ok. The OK button is enabled or disabled depending
31  * on the status.
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     private boolean fStatusLineAboveButtons= true;
41     
42     /**
43      * Creates an instane of a status dialog.
44      */

45     public StatusDialog(Shell parent) {
46         super(parent);
47         fLastStatus= new StatusInfo();
48     }
49     
50     /**
51      * Specifies whether status line appears to the left of the buttons (default)
52      * or above them.
53      *
54      * @param aboveButtons if <code>true</code> status line is placed above buttons; if
55      * <code>false</code> to the right
56      */

57     public void setStatusLineAboveButtons(boolean aboveButtons) {
58         fStatusLineAboveButtons= aboveButtons;
59     }
60     
61     /**
62      * Update the dialog's status line to reflect the given status.
63      * It is save to call this method before the dialog has been opened.
64      */

65     protected void updateStatus(IStatus status) {
66         fLastStatus= status;
67         if (fStatusLine != null && !fStatusLine.isDisposed()) {
68             updateButtonsEnableState(status);
69             fStatusLine.setErrorStatus(status);
70         }
71     }
72     
73     /**
74      * Returns the last status.
75      */

76     public IStatus getStatus() {
77         return fLastStatus;
78     }
79
80     /**
81      * Updates the status of the ok button to reflect the given status.
82      * Subclasses may override this method to update additional buttons.
83      * @param status the status.
84      */

85     protected void updateButtonsEnableState(IStatus status) {
86         if (fOkButton != null && !fOkButton.isDisposed())
87             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
88     }
89     
90     /*
91      * @see Window#create(Shell)
92      */

93     protected void configureShell(Shell shell) {
94         super.configureShell(shell);
95         if (fTitle != null)
96             shell.setText(fTitle);
97     }
98
99     /*
100      * @see Window#create()
101      */

102     public void create() {
103         super.create();
104         if (fLastStatus != null) {
105             // policy: dialogs are not allowed to come up with an error message
106
if (fLastStatus.matches(IStatus.ERROR)) {
107                 StatusInfo status= new StatusInfo();
108                 status.setError(""); //$NON-NLS-1$
109
fLastStatus= status;
110             }
111             updateStatus(fLastStatus);
112         }
113     }
114
115     /*
116      * @see Dialog#createButtonsForButtonBar(Composite)
117      */

118     protected void createButtonsForButtonBar(Composite parent) {
119         fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
120         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
121     }
122     
123     /*
124      * @see Dialog#createButtonBar(Composite)
125      */

126     protected Control createButtonBar(Composite parent) {
127         Composite composite= new Composite(parent, SWT.NULL);
128         GridLayout layout= new GridLayout();
129     
130         if (fStatusLineAboveButtons) {
131             layout.numColumns= 1;
132         } else {
133             layout.numColumns= 2;
134         }
135         
136         layout.marginHeight= 0;
137         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
138         composite.setLayout(layout);
139         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
140         
141         fStatusLine= new MessageLine(composite);
142         fStatusLine.setAlignment(SWT.LEFT);
143         fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
144         fStatusLine.setErrorStatus(null);
145         applyDialogFont(composite);
146         super.createButtonBar(composite);
147         return composite;
148     }
149     
150     /**
151      * Sets the title for this dialog.
152      * @param title the title.
153      */

154     public void setTitle(String JavaDoc title) {
155         fTitle= title != null ? title : ""; //$NON-NLS-1$
156
Shell shell= getShell();
157         if ((shell != null) && !shell.isDisposed())
158             shell.setText(fTitle);
159     }
160
161     /**
162      * Sets the image for this dialog.
163      * @param image the image.
164      */

165     public void setImage(Image image) {
166         fImage= image;
167         Shell shell= getShell();
168         if ((shell != null) && !shell.isDisposed())
169             shell.setImage(fImage);
170     }
171     
172 }
173
Popular Tags