KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > 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.pde.internal.ui.parts;
12
13 import org.eclipse.core.runtime.IStatus;
14 //import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
15
import org.eclipse.jface.dialogs.*;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.layout.*;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.ui.internal.MessageLine;
21 import org.eclipse.jface.dialogs.Dialog;
22
23 /**
24  * An abstract base class for dialogs with a status bar and ok/cancel buttons.
25  * The status message must be passed over as StatusInfo object and can be
26  * an error, warning or ok. The OK button is enabled or disabled depending
27  * on the status.
28  */

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

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

52     /*public void setStatusLineAboveButtons(boolean aboveButtons) {
53         fStatusLineAboveButtons= aboveButtons;
54     }*/

55     
56     /**
57      * Update the dialog's status line to reflect the given status.
58      * It is save to call this method before the dialog has been opened.
59      */

60     protected void updateStatus(IStatus status) {
61         fLastStatus= status;
62         if (fStatusLine != null && !fStatusLine.isDisposed()) {
63             updateButtonsEnableState(status);
64             fStatusLine.setErrorStatus(status);
65         }
66     }
67     
68     /**
69      * Returns the last status.
70      */

71     public IStatus getStatus() {
72         return fLastStatus;
73     }
74
75     /**
76      * Updates the status of the ok button to reflect the given status.
77      * Subclasses may override this method to update additional buttons.
78      * @param status the status.
79      */

80     protected void updateButtonsEnableState(IStatus status) {
81         if (fOkButton != null && !fOkButton.isDisposed())
82             fOkButton.setEnabled(!status.matches(IStatus.ERROR));
83     }
84     
85     /*
86      * @see Window#create(Shell)
87      */

88     protected void configureShell(Shell shell) {
89         super.configureShell(shell);
90         if (fTitle != null)
91             shell.setText(fTitle);
92     }
93
94     /*
95      * @see Window#create()
96      */

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

113     protected void createButtonsForButtonBar(Composite parent) {
114         fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
115         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
116     }
117     
118     /*
119      * @see Dialog#createButtonBar(Composite)
120      */

121     protected Control createButtonBar(Composite parent) {
122         Composite composite= new Composite(parent, SWT.NULL);
123         GridLayout layout= new GridLayout();
124         layout.numColumns= 1;
125         layout.marginHeight= 0;
126         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
127         composite.setLayout(layout);
128         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
129         
130         fStatusLine= new MessageLine(composite);
131         fStatusLine.setAlignment(SWT.LEFT);
132         fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
133         fStatusLine.setErrorStatus(null); //$NON-NLS-1$
134

135         super.createButtonBar(composite);
136         return composite;
137     }
138     
139     /**
140      * Sets the title for this dialog.
141      * @param title the title.
142      */

143     public void setTitle(String JavaDoc title) {
144         fTitle= title != null ? title : ""; //$NON-NLS-1$
145
Shell shell= getShell();
146         if ((shell != null) && !shell.isDisposed())
147             shell.setText(fTitle);
148     }
149
150     /**
151      * Sets the image for this dialog.
152      * @param image the image.
153      */

154     public void setImage(Image image) {
155         fImage= image;
156         Shell shell= getShell();
157         if ((shell != null) && !shell.isDisposed())
158             shell.setImage(fImage);
159     }
160     
161 }
Popular Tags