001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
011: * font should be activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.dialogs;
013:
014: import java.util.Arrays;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.jface.dialogs.IDialogConstants;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.graphics.Font;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.layout.GridData;
022: import org.eclipse.swt.layout.GridLayout;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.ui.internal.MessageLine;
028:
029: /**
030: * An abstract base class for dialogs with a status bar and ok/cancel buttons.
031: * The status message must be passed over as StatusInfo object and can be
032: * an error, warning or ok. The OK button is enabled or disabled depending
033: * on the status.
034: *
035: * @since 2.0
036: */
037: public abstract class SelectionStatusDialog extends SelectionDialog {
038:
039: private MessageLine fStatusLine;
040:
041: private IStatus fLastStatus;
042:
043: private Image fImage;
044:
045: private boolean fStatusLineAboveButtons = false;
046:
047: /**
048: * Creates an instance of a <code>SelectionStatusDialog</code>.
049: * @param parent
050: */
051: public SelectionStatusDialog(Shell parent) {
052: super (parent);
053: }
054:
055: /**
056: * Controls whether status line appears to the left of the buttons (default)
057: * or above them.
058: *
059: * @param aboveButtons if <code>true</code> status line is placed above buttons; if
060: * <code>false</code> to the right
061: */
062: public void setStatusLineAboveButtons(boolean aboveButtons) {
063: fStatusLineAboveButtons = aboveButtons;
064: }
065:
066: /**
067: * Sets the image for this dialog.
068: * @param image the image.
069: */
070: public void setImage(Image image) {
071: fImage = image;
072: }
073:
074: /**
075: * Returns the first element from the list of results. Returns <code>null</code>
076: * if no element has been selected.
077: *
078: * @return the first result element if one exists. Otherwise <code>null</code> is
079: * returned.
080: */
081: public Object getFirstResult() {
082: Object[] result = getResult();
083: if (result == null || result.length == 0) {
084: return null;
085: }
086: return result[0];
087: }
088:
089: /**
090: * Sets a result element at the given position.
091: * @param position
092: * @param element
093: */
094: protected void setResult(int position, Object element) {
095: Object[] result = getResult();
096: result[position] = element;
097: setResult(Arrays.asList(result));
098: }
099:
100: /**
101: * Compute the result and return it.
102: */
103: protected abstract void computeResult();
104:
105: /*
106: * @see Window#configureShell(shell)
107: */
108: protected void configureShell(Shell shell) {
109: super .configureShell(shell);
110: if (fImage != null) {
111: shell.setImage(fImage);
112: }
113: }
114:
115: /**
116: * Update the dialog's status line to reflect the given status. It is safe to call
117: * this method before the dialog has been opened.
118: * @param status
119: */
120: protected void updateStatus(IStatus status) {
121: fLastStatus = status;
122: if (fStatusLine != null && !fStatusLine.isDisposed()) {
123: updateButtonsEnableState(status);
124: fStatusLine.setErrorStatus(status);
125: }
126: }
127:
128: /**
129: * Update the status of the ok button to reflect the given status. Subclasses
130: * may override this method to update additional buttons.
131: * @param status
132: */
133: protected void updateButtonsEnableState(IStatus status) {
134: Button okButton = getOkButton();
135: if (okButton != null && !okButton.isDisposed()) {
136: okButton.setEnabled(!status.matches(IStatus.ERROR));
137: }
138: }
139:
140: /*
141: * @see Dialog#okPressed()
142: */
143: protected void okPressed() {
144: computeResult();
145: super .okPressed();
146: }
147:
148: /*
149: * @see Window#create()
150: */
151: public void create() {
152: super .create();
153: if (fLastStatus != null) {
154: updateStatus(fLastStatus);
155: }
156: }
157:
158: /*
159: * @see Dialog#createButtonBar(Composite)
160: */
161: protected Control createButtonBar(Composite parent) {
162: Font font = parent.getFont();
163: Composite composite = new Composite(parent, SWT.NULL);
164: GridLayout layout = new GridLayout();
165: if (!fStatusLineAboveButtons) {
166: layout.numColumns = 2;
167: }
168: layout.marginHeight = 0;
169: layout.marginLeft = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
170: layout.marginWidth = 0;
171: composite.setLayout(layout);
172: composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
173: composite.setFont(font);
174:
175: if (!fStatusLineAboveButtons && isHelpAvailable()) {
176: createHelpControl(composite);
177: }
178: fStatusLine = new MessageLine(composite);
179: fStatusLine.setAlignment(SWT.LEFT);
180: GridData statusData = new GridData(GridData.FILL_HORIZONTAL);
181: fStatusLine.setErrorStatus(null);
182: fStatusLine.setFont(font);
183: if (fStatusLineAboveButtons && isHelpAvailable()) {
184: statusData.horizontalSpan = 2;
185: createHelpControl(composite);
186: }
187: fStatusLine.setLayoutData(statusData);
188:
189: /*
190: * Create the rest of the button bar, but tell it not to
191: * create a help button (we've already created it).
192: */
193: boolean helpAvailable = isHelpAvailable();
194: setHelpAvailable(false);
195: super.createButtonBar(composite);
196: setHelpAvailable(helpAvailable);
197: return composite;
198: }
199:
200: }
|