]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/dialogfields/StringButtonStatusDialogField.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / dialogfields / StringButtonStatusDialogField.java
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
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.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;
22
23 import org.eclipse.jface.resource.JFaceResources;
24
25 /**
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.
29  */
30 public class StringButtonStatusDialogField extends StringButtonDialogField {
31
32         private Label fStatusLabelControl;
33         private Object fStatus;  // String or ImageDescriptor
34
35         private String fWidthHintString;
36         private int fWidthHint;
37
38         public StringButtonStatusDialogField(IStringButtonAdapter adapter) {
39                 super(adapter);
40                 fStatus= null;
41                 fWidthHintString= null;
42                 fWidthHint= -1;
43         }
44
45         // ------ set status
46
47         /**
48          * Sets the status string.
49          */
50         public void setStatus(String status) {
51                 if (isOkToUse(fStatusLabelControl)) {
52                         fStatusLabelControl.setText(status);
53                 }
54                 fStatus= status;
55         }
56
57         /**
58          * Sets the status image.
59          * 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.
74          * The string is used to 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         @Override
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         @Override
114         public int getNumberOfControls() {
115                 return 4;
116         }
117
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;
127                         gc.dispose();
128                 } else if (fWidthHint != -1) {
129                         gd.widthHint= fWidthHint;
130                 } else {
131                         gd.widthHint= SWT.DEFAULT;
132                 }
133                 return gd;
134         }
135
136         // ------- ui creation
137
138         /**
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.
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         @Override
166         protected void updateEnableState() {
167                 super.updateEnableState();
168                 if (isOkToUse(fStatusLabelControl)) {
169                         fStatusLabelControl.setEnabled(isEnabled());
170                 }
171         }
172
173         /* (non-Javadoc)
174          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
175          */
176         @Override
177         public void refresh() {
178                 super.refresh();
179                 if (fStatus instanceof String) {
180                         setStatus((String) fStatus);
181                 } else {
182                         setStatus((Image) fStatus);
183                 }
184         }
185 }