KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > StringButtonStatusDialogField


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.dialogfields;
12
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.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Text;
21
22 import org.eclipse.swt.layout.GridData;
23
24 import org.eclipse.jface.resource.JFaceResources;
25
26 /**
27  * Dialog field containing a label, text control, status label and a button control.
28  * The status label can be either a image or text label, and can be usd to give
29  * additional information about the current element chosen.
30  */

31 public class StringButtonStatusDialogField extends StringButtonDialogField {
32         
33     private Label fStatusLabelControl;
34     private Object JavaDoc fStatus; // String or ImageDescriptor
35

36     private String JavaDoc fWidthHintString;
37     private int fWidthHint;
38     
39     public StringButtonStatusDialogField(IStringButtonAdapter adapter) {
40         super(adapter);
41         fStatus= null;
42         fWidthHintString= null;
43         fWidthHint= -1;
44     }
45     
46     // ------ set status
47

48     /**
49      * Sets the status string.
50      */

51     public void setStatus(String JavaDoc status) {
52         if (isOkToUse(fStatusLabelControl)) {
53             fStatusLabelControl.setText(status);
54         }
55         fStatus= status;
56     }
57     
58     /**
59      * Sets the status image.
60      * Caller is responsible to dispose image
61      */

62     public void setStatus(Image image) {
63         if (isOkToUse(fStatusLabelControl)) {
64             if (image == null) {
65                 fStatusLabelControl.setImage(null);
66             } else {
67                 fStatusLabelControl.setImage(image);
68             }
69         }
70         fStatus= image;
71     }
72
73     /**
74      * Sets the staus string hint of the status label.
75      * The string is used to calculate the size of the status label.
76      */

77     public void setStatusWidthHint(String JavaDoc widthHintString) {
78         fWidthHintString= widthHintString;
79         fWidthHint= -1;
80     }
81     
82     /**
83      * Sets the width hint of the status label.
84      */

85     public void setStatusWidthHint(int widthHint) {
86         fWidthHint= widthHint;
87         fWidthHintString= null;
88     }
89     
90     // ------- layout helpers
91

92     /*
93      * @see DialogField#doFillIntoGrid
94      */

95     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
96         assertEnoughColumns(nColumns);
97         
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));
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 JavaDoc) {
151                 fStatusLabelControl.setText((String JavaDoc)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 JavaDoc) {
177             setStatus((String JavaDoc) fStatus);
178         } else {
179             setStatus((Image) fStatus);
180         }
181     }
182 }
183
Popular Tags