Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / StringButtonStatusDialogField.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
13 import org.eclipse.jface.resource.JFaceResources;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Text;
23
24 /**
25  * Dialog field containing a label, text control, status label and a button control.
26  * The status label can be either a image or text label, and can be usd to give
27  * additional information about the current element chosen.
28  */
29 public class StringButtonStatusDialogField extends StringButtonDialogField {
30                 
31         private Label fStatusLabelControl;
32         private Object fStatus;  // String or ImageDescriptor
33         
34         private String fWidthHintString;
35         private int fWidthHint; 
36         
37         public StringButtonStatusDialogField(IStringButtonAdapter adapter) {
38                 super(adapter);
39                 fStatus= null;
40                 fWidthHintString= null;
41                 fWidthHint= -1;
42         }
43         
44         // ------ set status
45
46         /**
47          * Sets the status string.
48          */     
49         public void setStatus(String status) {
50                 if (isOkToUse(fStatusLabelControl)) {
51                         fStatusLabelControl.setText(status);
52                 }
53                 fStatus= status;                
54         }
55         
56         /**
57          * Sets the status image.
58          * Caller is responsible to dispose image
59          */
60         public void setStatus(Image image) {
61                 if (isOkToUse(fStatusLabelControl)) {
62                         if (image == null) {
63                                 fStatusLabelControl.setImage(null);
64                         } else {
65                                 fStatusLabelControl.setImage(image);
66                         }
67                 }
68                 fStatus= image;         
69         }       
70
71         /**
72          * Sets the staus string hint of the status label.
73          * The string is used to calculate the size of the status label.
74          */             
75         public void setStatusWidthHint(String widthHintString) {
76                 fWidthHintString= widthHintString;
77                 fWidthHint= -1;
78         }
79         
80         /**
81          * Sets the width hint of the status label.
82          */
83         public void setStatusWidthHint(int widthHint) {
84                 fWidthHint= widthHint;
85                 fWidthHintString= null;
86         }
87         
88         // ------- layout helpers       
89
90         /*
91          * @see DialogField#doFillIntoGrid
92          */             
93         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
94                 assertEnoughColumns(nColumns);
95                 
96                 Label label= getLabelControl(parent);
97                 label.setLayoutData(gridDataForLabel(1));
98                 Text text= getTextControl(parent);
99                 text.setLayoutData(gridDataForText(nColumns - 3));
100                 Label status= getStatusLabelControl(parent);
101                 status.setLayoutData(gridDataForStatusLabel(parent, 1));
102                 Button button= getChangeControl(parent);
103                 button.setLayoutData(gridDataForButton(button, 1));
104                 
105                 return new Control[] { label, text, status, button };
106         }
107         
108         /*
109          * @see DialogField#getNumberOfControls
110          */
111         public int getNumberOfControls() {
112                 return 4;       
113         }
114         
115         protected GridData gridDataForStatusLabel(Control aControl, int span) {
116                 GridData gd= new GridData();
117                 gd.horizontalAlignment= GridData.BEGINNING;
118                 gd.grabExcessHorizontalSpace= false;
119                 gd.horizontalIndent= 0;
120                 if (fWidthHintString != null) {
121                         GC gc= new GC(aControl);
122                         gc.setFont(JFaceResources.getDialogFont());
123                         gd.widthHint= gc.textExtent(fWidthHintString).x;
124                         gc.dispose();
125                 } else if (fWidthHint != -1) {
126                         gd.widthHint= fWidthHint;
127                 } else {
128                         gd.widthHint= SWT.DEFAULT;
129                 }               
130                 return gd;
131         }
132         
133         // ------- ui creation  
134
135         /**
136          * Creates or returns the created status label widget.
137          * @param parent The parent composite or <code>null</code> when the widget has
138          * already been created.
139          */                     
140         public Label getStatusLabelControl(Composite parent) {
141                 if (fStatusLabelControl == null) {
142                         assertCompositeNotNull(parent);                 
143                         fStatusLabelControl= new Label(parent, SWT.LEFT);
144                         fStatusLabelControl.setFont(parent.getFont());
145                         fStatusLabelControl.setEnabled(isEnabled());
146                         if (fStatus instanceof Image) {
147                                 fStatusLabelControl.setImage((Image)fStatus);
148                         } else if (fStatus instanceof String) {
149                                 fStatusLabelControl.setText((String)fStatus);
150                         } else {
151                                 // must be null
152                         }
153                 }
154                 return fStatusLabelControl;
155         }
156         
157         // ------ enable / disable management
158
159         /*
160          * @see DialogField#updateEnableState
161          */     
162         protected void updateEnableState() {
163                 super.updateEnableState();
164                 if (isOkToUse(fStatusLabelControl)) {
165                         fStatusLabelControl.setEnabled(isEnabled());
166                 }
167         }       
168 }