fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[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
26  * control. The status label can be either a image or text label, and can be usd
27  * to give additional information about the current element chosen.
28  */
29 public class StringButtonStatusDialogField extends StringButtonDialogField {
30
31         private Label fStatusLabelControl;
32
33         private Object fStatus; // String or ImageDescriptor
34
35         private String fWidthHintString;
36
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 status) {
52                 if (isOkToUse(fStatusLabelControl)) {
53                         fStatusLabelControl.setText(status);
54                 }
55                 fStatus = status;
56         }
57
58         /**
59          * Sets the status image. Caller is responsible to dispose image
60          */
61         public void setStatus(Image image) {
62                 if (isOkToUse(fStatusLabelControl)) {
63                         if (image == null) {
64                                 fStatusLabelControl.setImage(null);
65                         } else {
66                                 fStatusLabelControl.setImage(image);
67                         }
68                 }
69                 fStatus = image;
70         }
71
72         /**
73          * Sets the staus string hint of the status label. The string is used to
74          * calculate the size of the status label.
75          */
76         public void setStatusWidthHint(String widthHintString) {
77                 fWidthHintString = widthHintString;
78                 fWidthHint = -1;
79         }
80
81         /**
82          * Sets the width hint of the status label.
83          */
84         public void setStatusWidthHint(int widthHint) {
85                 fWidthHint = widthHint;
86                 fWidthHintString = null;
87         }
88
89         // ------- layout helpers
90
91         /*
92          * @see DialogField#doFillIntoGrid
93          */
94         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
95                 assertEnoughColumns(nColumns);
96
97                 Label label = getLabelControl(parent);
98                 label.setLayoutData(gridDataForLabel(1));
99                 Text text = getTextControl(parent);
100                 text.setLayoutData(gridDataForText(nColumns - 3));
101                 Label status = getStatusLabelControl(parent);
102                 status.setLayoutData(gridDataForStatusLabel(parent, 1));
103                 Button button = getChangeControl(parent);
104                 button.setLayoutData(gridDataForButton(button, 1));
105
106                 return new Control[] { label, text, status, button };
107         }
108
109         /*
110          * @see DialogField#getNumberOfControls
111          */
112         public int getNumberOfControls() {
113                 return 4;
114         }
115
116         protected GridData gridDataForStatusLabel(Control aControl, int span) {
117                 GridData gd = new GridData();
118                 gd.horizontalAlignment = GridData.BEGINNING;
119                 gd.grabExcessHorizontalSpace = false;
120                 gd.horizontalIndent = 0;
121                 if (fWidthHintString != null) {
122                         GC gc = new GC(aControl);
123                         gc.setFont(JFaceResources.getDialogFont());
124                         gd.widthHint = gc.textExtent(fWidthHintString).x;
125                         gc.dispose();
126                 } else if (fWidthHint != -1) {
127                         gd.widthHint = fWidthHint;
128                 } else {
129                         gd.widthHint = SWT.DEFAULT;
130                 }
131                 return gd;
132         }
133
134         // ------- ui creation
135
136         /**
137          * Creates or returns the created status label widget.
138          * 
139          * @param parent
140          *            The parent composite or <code>null</code> when the widget
141          *            has already been created.
142          */
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);
153                         } else {
154                                 // must be null
155                         }
156                 }
157                 return fStatusLabelControl;
158         }
159
160         // ------ enable / disable management
161
162         /*
163          * @see DialogField#updateEnableState
164          */
165         protected void updateEnableState() {
166                 super.updateEnableState();
167                 if (isOkToUse(fStatusLabelControl)) {
168                         fStatusLabelControl.setEnabled(isEnabled());
169                 }
170         }
171 }