1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.dialogfields;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.layout.GridData;
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.Label;
21 import org.eclipse.swt.widgets.Text;
23 import org.eclipse.jface.resource.JFaceResources;
26 * Dialog field containing a label, text control, status label and a button control.
27 * The status label can be either a image or text label, and can be usd to give
28 * additional information about the current element chosen.
30 public class StringButtonStatusDialogField extends StringButtonDialogField {
32 private Label fStatusLabelControl;
33 private Object fStatus; // String or ImageDescriptor
35 private String fWidthHintString;
36 private int fWidthHint;
38 public StringButtonStatusDialogField(IStringButtonAdapter adapter) {
41 fWidthHintString= null;
48 * Sets the status string.
50 public void setStatus(String status) {
51 if (isOkToUse(fStatusLabelControl)) {
52 fStatusLabelControl.setText(status);
58 * Sets the status image.
59 * Caller is responsible to dispose image
61 public void setStatus(Image image) {
62 if (isOkToUse(fStatusLabelControl)) {
64 fStatusLabelControl.setImage(null);
66 fStatusLabelControl.setImage(image);
73 * Sets the staus string hint of the status label.
74 * The string is used to calculate the size of the status label.
76 public void setStatusWidthHint(String widthHintString) {
77 fWidthHintString= widthHintString;
82 * Sets the width hint of the status label.
84 public void setStatusWidthHint(int widthHint) {
85 fWidthHint= widthHint;
86 fWidthHintString= null;
89 // ------- layout helpers
92 * @see DialogField#doFillIntoGrid
95 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
96 assertEnoughColumns(nColumns);
98 Label label= getLabelControl(parent);
99 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));
107 return new Control[] { label, text, status, button };
111 * @see DialogField#getNumberOfControls
114 public int getNumberOfControls() {
118 protected GridData gridDataForStatusLabel(Control aControl, int span) {
119 GridData gd= new GridData();
120 gd.horizontalAlignment= GridData.BEGINNING;
121 gd.grabExcessHorizontalSpace= false;
122 gd.horizontalIndent= 0;
123 if (fWidthHintString != null) {
124 GC gc= new GC(aControl);
125 gc.setFont(JFaceResources.getDialogFont());
126 gd.widthHint= gc.textExtent(fWidthHintString).x;
128 } else if (fWidthHint != -1) {
129 gd.widthHint= fWidthHint;
131 gd.widthHint= SWT.DEFAULT;
136 // ------- ui creation
139 * Creates or returns the created status label widget.
140 * @param parent The parent composite or <code>null</code> when the widget has
141 * already been created.
143 public Label getStatusLabelControl(Composite parent) {
144 if (fStatusLabelControl == null) {
145 assertCompositeNotNull(parent);
146 fStatusLabelControl= new Label(parent, SWT.LEFT);
147 fStatusLabelControl.setFont(parent.getFont());
148 fStatusLabelControl.setEnabled(isEnabled());
149 if (fStatus instanceof Image) {
150 fStatusLabelControl.setImage((Image)fStatus);
151 } else if (fStatus instanceof String) {
152 fStatusLabelControl.setText((String)fStatus);
157 return fStatusLabelControl;
160 // ------ enable / disable management
163 * @see DialogField#updateEnableState
166 protected void updateEnableState() {
167 super.updateEnableState();
168 if (isOkToUse(fStatusLabelControl)) {
169 fStatusLabelControl.setEnabled(isEnabled());
174 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
177 public void refresh() {
179 if (fStatus instanceof String) {
180 setStatus((String) fStatus);
182 setStatus((Image) fStatus);