1 11 package org.eclipse.jdt.internal.ui.wizards.dialogfields; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Display; 20 import org.eclipse.swt.widgets.Label; 21 22 23 32 public class DialogField { 33 34 private Label fLabel; 35 protected String fLabelText; 36 37 private IDialogFieldListener fDialogFieldListener; 38 39 private boolean fEnabled; 40 41 public DialogField() { 42 fEnabled= true; 43 fLabel= null; 44 fLabelText= ""; } 46 47 50 public void setLabelText(String labeltext) { 51 fLabelText= labeltext; 52 if (isOkToUse(fLabel)) { 53 fLabel.setText(labeltext); 54 } 55 } 56 57 59 62 public final void setDialogFieldListener(IDialogFieldListener listener) { 63 fDialogFieldListener= listener; 64 } 65 66 69 public void dialogFieldChanged() { 70 if (fDialogFieldListener != null) { 71 fDialogFieldListener.dialogFieldChanged(this); 72 } 73 } 74 75 77 82 public boolean setFocus() { 83 return false; 84 } 85 86 89 public void postSetFocusOnDialogField(Display display) { 90 if (display != null) { 91 display.asyncExec( 92 new Runnable () { 93 public void run() { 94 setFocus(); 95 } 96 } 97 ); 98 } 99 } 100 101 103 110 public Control[] doFillIntoGrid(Composite parent, int nColumns) { 111 assertEnoughColumns(nColumns); 112 113 Label label= getLabelControl(parent); 114 label.setLayoutData(gridDataForLabel(nColumns)); 115 116 return new Control[] { label }; 117 } 118 119 123 public int getNumberOfControls() { 124 return 1; 125 } 126 127 protected static GridData gridDataForLabel(int span) { 128 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 129 gd.horizontalSpan= span; 130 return gd; 131 } 132 133 135 140 public Label getLabelControl(Composite parent) { 141 if (fLabel == null) { 142 assertCompositeNotNull(parent); 143 144 fLabel= new Label(parent, SWT.LEFT | SWT.WRAP); 145 fLabel.setFont(parent.getFont()); 146 fLabel.setEnabled(fEnabled); 147 if (fLabelText != null && !"".equals(fLabelText)) { fLabel.setText(fLabelText); 149 } else { 150 fLabel.setText("."); fLabel.setVisible(false); 153 } 154 } 155 return fLabel; 156 } 157 158 162 public static Control createEmptySpace(Composite parent) { 163 return createEmptySpace(parent, 1); 164 } 165 166 172 public static Control createEmptySpace(Composite parent, int span) { 173 Label label= new Label(parent, SWT.LEFT); 174 GridData gd= new GridData(); 175 gd.horizontalAlignment= GridData.BEGINNING; 176 gd.grabExcessHorizontalSpace= false; 177 gd.horizontalSpan= span; 178 gd.horizontalIndent= 0; 179 gd.widthHint= 0; 180 gd.heightHint= 0; 181 label.setLayoutData(gd); 182 return label; 183 } 184 185 188 protected final boolean isOkToUse(Control control) { 189 return (control != null) && (Display.getCurrent() != null) && !control.isDisposed(); 190 } 191 192 194 197 public final void setEnabled(boolean enabled) { 198 if (enabled != fEnabled) { 199 fEnabled= enabled; 200 updateEnableState(); 201 } 202 } 203 204 208 protected void updateEnableState() { 209 if (fLabel != null) { 210 fLabel.setEnabled(fEnabled); 211 } 212 } 213 214 218 public void refresh() { 219 updateEnableState(); 220 } 221 222 225 public final boolean isEnabled() { 226 return fEnabled; 227 } 228 229 protected final void assertCompositeNotNull(Composite comp) { 230 Assert.isNotNull(comp, "uncreated control requested with composite null"); } 232 233 protected final void assertEnoughColumns(int nColumns) { 234 Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); } 236 237 238 239 240 } 241 | Popular Tags |