1 package net.sourceforge.phpdt.externaltools.internal.ui;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8 **********************************************************************/
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.jface.dialogs.Dialog;
12 import org.eclipse.jface.dialogs.IDialogConstants;
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;
23 * An abstract base class for dialogs with a status bar and ok/cancel buttons.
24 * The status message must be passed over as StatusInfo object and can be an
25 * error, warning or ok. The OK button is enabled or disabled depending on the
28 public abstract class StatusDialog extends Dialog {
30 private Button fOkButton;
32 private MessageLine fStatusLine;
34 private IStatus fLastStatus;
36 private String fTitle;
40 private boolean fStatusLineAboveButtons;
43 * Creates an instane of a status dialog.
45 public StatusDialog(Shell parent) {
47 fStatusLineAboveButtons = false;
48 fLastStatus = new StatusInfo();
52 * Specifies whether status line appears to the left of the buttons
53 * (default) or above them.
56 * if <code>true</code> status line is placed above buttons; if
57 * <code>false</code> to the right
59 public void setStatusLineAboveButtons(boolean aboveButtons) {
60 fStatusLineAboveButtons = aboveButtons;
64 * Update the dialog's status line to reflect the given status. It is save
65 * to call this method before the dialog has been opened.
67 protected void updateStatus(IStatus status) {
69 if (fStatusLine != null && !fStatusLine.isDisposed()) {
70 updateButtonsEnableState(status);
71 fStatusLine.setErrorStatus(status);
76 * Returns the last status.
78 public IStatus getStatus() {
83 * Updates the status of the ok button to reflect the given status.
84 * Subclasses may override this method to update additional buttons.
89 protected void updateButtonsEnableState(IStatus status) {
90 if (fOkButton != null && !fOkButton.isDisposed())
91 fOkButton.setEnabled(!status.matches(IStatus.ERROR));
95 * @see Window#create(Shell)
97 protected void configureShell(Shell shell) {
98 super.configureShell(shell);
100 shell.setText(fTitle);
104 * @see Window#create()
106 public void create() {
108 if (fLastStatus != null) {
109 // policy: dialogs are not allowed to come up with an error message
110 if (fLastStatus.matches(IStatus.ERROR)) {
111 StatusInfo status = new StatusInfo();
112 status.setError(""); //$NON-NLS-1$
113 fLastStatus = status;
115 updateStatus(fLastStatus);
120 * @see Dialog#createButtonsForButtonBar(Composite)
122 protected void createButtonsForButtonBar(Composite parent) {
123 fOkButton = createButton(parent, IDialogConstants.OK_ID,
124 IDialogConstants.OK_LABEL, true);
125 createButton(parent, IDialogConstants.CANCEL_ID,
126 IDialogConstants.CANCEL_LABEL, false);
130 * @see Dialog#createButtonBar(Composite)
132 protected Control createButtonBar(Composite parent) {
133 Composite composite = new Composite(parent, SWT.NULL);
134 GridLayout layout = new GridLayout();
135 layout.numColumns = 1;
136 layout.marginHeight = 0;
137 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
138 composite.setLayout(layout);
139 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
141 fStatusLine = new MessageLine(composite);
142 fStatusLine.setAlignment(SWT.LEFT);
143 fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
144 fStatusLine.setErrorStatus(null); //$NON-NLS-1$
146 super.createButtonBar(composite);
151 * Sets the title for this dialog.
156 public void setTitle(String title) {
157 fTitle = title != null ? title : ""; //$NON-NLS-1$
158 Shell shell = getShell();
159 if ((shell != null) && !shell.isDisposed())
160 shell.setText(fTitle);
164 * Sets the image for this dialog.
169 public void setImage(Image image) {
171 Shell shell = getShell();
172 if ((shell != null) && !shell.isDisposed())
173 shell.setImage(fImage);