001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.dialogfields;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.graphics.GC;
014: import org.eclipse.swt.graphics.Image;
015: import org.eclipse.swt.widgets.Button;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Control;
018: import org.eclipse.swt.widgets.Label;
019: import org.eclipse.swt.widgets.Text;
020:
021: import org.eclipse.swt.layout.GridData;
022:
023: import org.eclipse.jface.resource.JFaceResources;
024:
025: /**
026: * Dialog field containing a label, text control, status label and a button control.
027: * The status label can be either a image or text label, and can be usd to give
028: * additional information about the current element chosen.
029: */
030: public class StringButtonStatusDialogField extends
031: StringButtonDialogField {
032:
033: private Label fStatusLabelControl;
034: private Object fStatus; // String or ImageDescriptor
035:
036: private String fWidthHintString;
037: private int fWidthHint;
038:
039: public StringButtonStatusDialogField(IStringButtonAdapter adapter) {
040: super (adapter);
041: fStatus = null;
042: fWidthHintString = null;
043: fWidthHint = -1;
044: }
045:
046: // ------ set status
047:
048: /**
049: * Sets the status string.
050: */
051: public void setStatus(String status) {
052: if (isOkToUse(fStatusLabelControl)) {
053: fStatusLabelControl.setText(status);
054: }
055: fStatus = status;
056: }
057:
058: /**
059: * Sets the status image.
060: * Caller is responsible to dispose image
061: */
062: public void setStatus(Image image) {
063: if (isOkToUse(fStatusLabelControl)) {
064: if (image == null) {
065: fStatusLabelControl.setImage(null);
066: } else {
067: fStatusLabelControl.setImage(image);
068: }
069: }
070: fStatus = image;
071: }
072:
073: /**
074: * Sets the staus string hint of the status label.
075: * The string is used to calculate the size of the status label.
076: */
077: public void setStatusWidthHint(String widthHintString) {
078: fWidthHintString = widthHintString;
079: fWidthHint = -1;
080: }
081:
082: /**
083: * Sets the width hint of the status label.
084: */
085: public void setStatusWidthHint(int widthHint) {
086: fWidthHint = widthHint;
087: fWidthHintString = null;
088: }
089:
090: // ------- layout helpers
091:
092: /*
093: * @see DialogField#doFillIntoGrid
094: */
095: public Control[] doFillIntoGrid(Composite parent, int nColumns) {
096: assertEnoughColumns(nColumns);
097:
098: Label label = getLabelControl(parent);
099: label.setLayoutData(gridDataForLabel(1));
100: Text text = getTextControl(parent);
101: text.setLayoutData(gridDataForText(nColumns - 3));
102: Label status = getStatusLabelControl(parent);
103: status.setLayoutData(gridDataForStatusLabel(parent, 1));
104: Button button = getChangeControl(parent);
105: button.setLayoutData(gridDataForButton(button, 1));
106:
107: return new Control[] { label, text, status, button };
108: }
109:
110: /*
111: * @see DialogField#getNumberOfControls
112: */
113: public int getNumberOfControls() {
114: return 4;
115: }
116:
117: protected GridData gridDataForStatusLabel(Control aControl, int span) {
118: GridData gd = new GridData();
119: gd.horizontalAlignment = GridData.BEGINNING;
120: gd.grabExcessHorizontalSpace = false;
121: gd.horizontalIndent = 0;
122: if (fWidthHintString != null) {
123: GC gc = new GC(aControl);
124: gc.setFont(JFaceResources.getDialogFont());
125: gd.widthHint = gc.textExtent(fWidthHintString).x;
126: gc.dispose();
127: } else if (fWidthHint != -1) {
128: gd.widthHint = fWidthHint;
129: } else {
130: gd.widthHint = SWT.DEFAULT;
131: }
132: return gd;
133: }
134:
135: // ------- ui creation
136:
137: /**
138: * Creates or returns the created status label widget.
139: * @param parent The parent composite or <code>null</code> when the widget has
140: * already been created.
141: */
142: public Label getStatusLabelControl(Composite parent) {
143: if (fStatusLabelControl == null) {
144: assertCompositeNotNull(parent);
145: fStatusLabelControl = new Label(parent, SWT.LEFT);
146: fStatusLabelControl.setFont(parent.getFont());
147: fStatusLabelControl.setEnabled(isEnabled());
148: if (fStatus instanceof Image) {
149: fStatusLabelControl.setImage((Image) fStatus);
150: } else if (fStatus instanceof String) {
151: fStatusLabelControl.setText((String) fStatus);
152: } else {
153: // must be null
154: }
155: }
156: return fStatusLabelControl;
157: }
158:
159: // ------ enable / disable management
160:
161: /*
162: * @see DialogField#updateEnableState
163: */
164: protected void updateEnableState() {
165: super .updateEnableState();
166: if (isOkToUse(fStatusLabelControl)) {
167: fStatusLabelControl.setEnabled(isEnabled());
168: }
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
173: */
174: public void refresh() {
175: super .refresh();
176: if (fStatus instanceof String) {
177: setStatus((String) fStatus);
178: } else {
179: setStatus((Image) fStatus);
180: }
181: }
182: }
|