1 package net.sourceforge.phpdt.internal.ui.dialogs;
3 import java.util.Arrays;
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.graphics.Image;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.ui.dialogs.SelectionDialog;
18 * An abstract base class for dialogs with a status bar and ok/cancel buttons.
19 * The status message must be passed over as StatusInfo object and can be
20 * an error, warning or ok. The OK button is enabled or disabled depending
23 public abstract class SelectionStatusDialog extends SelectionDialog {
25 private MessageLine fStatusLine;
26 private IStatus fLastStatus;
28 private boolean fStatusLineAboveButtons= false;
31 * Creates an instance of a <code>SelectionStatusDialog</code>.
33 public SelectionStatusDialog(Shell parent) {
38 * Controls whether status line appears to the left of the buttons (default)
41 * @param aboveButtons if <code>true</code> status line is placed above buttons; if
42 * <code>false</code> to the right
44 public void setStatusLineAboveButtons(boolean aboveButtons) {
45 fStatusLineAboveButtons= aboveButtons;
49 * Sets the image for this dialog.
50 * @param image the image.
52 public void setImage(Image image) {
57 * Returns the first element from the list of results. Returns <code>null</code>
58 * if no element has been selected.
60 * @return the first result element if one exists. Otherwise <code>null</code> is
63 public Object getFirstResult() {
64 Object[] result= getResult();
65 if (result == null || result.length == 0)
71 * Sets a result element at the given position.
73 protected void setResult(int position, Object element) {
74 Object[] result= getResult();
75 result[position]= element;
76 setResult(Arrays.asList(result));
80 * Compute the result and return it.
82 protected abstract void computeResult();
84 protected void configureShell(Shell shell) {
85 super.configureShell(shell);
87 shell.setImage(fImage);
91 * Update the dialog's status line to reflect the given status. It is safe to call
92 * this method before the dialog has been opened.
94 protected void updateStatus(IStatus status) {
96 if (fStatusLine != null && !fStatusLine.isDisposed()) {
97 updateButtonsEnableState(status);
98 fStatusLine.setErrorStatus(status);
103 * Update the status of the ok button to reflect the given status. Subclasses
104 * may override this method to update additional buttons.
106 protected void updateButtonsEnableState(IStatus status) {
107 Button okButton= getOkButton();
108 if (okButton != null && !okButton.isDisposed())
109 okButton.setEnabled(!status.matches(IStatus.ERROR));
112 protected void okPressed() {
117 public void create() {
119 if (fLastStatus != null)
120 updateStatus(fLastStatus);
123 protected Control createButtonBar(Composite parent) {
124 Composite composite= new Composite(parent, SWT.NULL);
125 GridLayout layout= new GridLayout();
126 if (fStatusLineAboveButtons) {
127 layout.marginWidth= 5;
129 layout.numColumns= 2;
131 layout.marginHeight= 0; layout.marginWidth= 0;
132 composite.setLayout(layout);
133 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
135 fStatusLine= new MessageLine(composite);
136 fStatusLine.setAlignment(SWT.LEFT);
137 fStatusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
138 fStatusLine.setErrorStatus(null);
140 GridData gd= new GridData(GridData.FILL_HORIZONTAL);
141 gd.horizontalIndent= convertWidthInCharsToPixels(1);
142 fStatusLine.setLayoutData(gd);
144 super.createButtonBar(composite);