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;
17 * An abstract base class for dialogs with a status bar and ok/cancel buttons.
18 * The status message must be passed over as StatusInfo object and can be an
19 * error, warning or ok. The OK button is enabled or disabled depending on the
22 public abstract class SelectionStatusDialog extends SelectionDialog {
24 private MessageLine fStatusLine;
26 private IStatus fLastStatus;
30 private boolean fStatusLineAboveButtons = false;
33 * Creates an instance of a <code>SelectionStatusDialog</code>.
35 public SelectionStatusDialog(Shell parent) {
40 * Controls whether status line appears to the left of the buttons (default)
44 * if <code>true</code> status line is placed above buttons; if
45 * <code>false</code> to the right
47 public void setStatusLineAboveButtons(boolean aboveButtons) {
48 fStatusLineAboveButtons = aboveButtons;
52 * Sets the image for this dialog.
57 public void setImage(Image image) {
62 * Returns the first element from the list of results. Returns
63 * <code>null</code> if no element has been selected.
65 * @return the first result element if one exists. Otherwise
66 * <code>null</code> is returned.
68 public Object getFirstResult() {
69 Object[] result = getResult();
70 if (result == null || result.length == 0)
76 * Sets a result element at the given position.
78 protected void setResult(int position, Object element) {
79 Object[] result = getResult();
80 result[position] = element;
81 setResult(Arrays.asList(result));
85 * Compute the result and return it.
87 protected abstract void computeResult();
89 protected void configureShell(Shell shell) {
90 super.configureShell(shell);
92 shell.setImage(fImage);
96 * Update the dialog's status line to reflect the given status. It is safe
97 * to call this method before the dialog has been opened.
99 protected void updateStatus(IStatus status) {
100 fLastStatus = status;
101 if (fStatusLine != null && !fStatusLine.isDisposed()) {
102 updateButtonsEnableState(status);
103 fStatusLine.setErrorStatus(status);
108 * Update the status of the ok button to reflect the given status.
109 * Subclasses may override this method to update additional buttons.
111 protected void updateButtonsEnableState(IStatus status) {
112 Button okButton = getOkButton();
113 if (okButton != null && !okButton.isDisposed())
114 okButton.setEnabled(!status.matches(IStatus.ERROR));
117 protected void okPressed() {
122 public void create() {
124 if (fLastStatus != null)
125 updateStatus(fLastStatus);
128 protected Control createButtonBar(Composite parent) {
129 Composite composite = new Composite(parent, SWT.NULL);
130 GridLayout layout = new GridLayout();
131 if (fStatusLineAboveButtons) {
132 layout.marginWidth = 5;
134 layout.numColumns = 2;
136 layout.marginHeight = 0;
137 layout.marginWidth = 0;
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);
146 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
147 gd.horizontalIndent = convertWidthInCharsToPixels(1);
148 fStatusLine.setLayoutData(gd);
150 super.createButtonBar(composite);