1 11 package org.eclipse.jdt.internal.junit.wizards; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.SelectionEvent; 17 import org.eclipse.swt.events.SelectionListener; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Button; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Display; 24 import org.eclipse.swt.widgets.Group; 25 import org.eclipse.swt.widgets.Label; 26 27 28 import org.eclipse.jdt.internal.junit.util.LayoutUtil; 29 30 34 public class MethodStubsSelectionButtonGroup { 35 36 private Label fLabel; 37 protected String fLabelText; 38 39 private SelectionButtonGroupListener fGroupListener; 40 41 private boolean fEnabled; 42 43 private Composite fButtonComposite; 44 45 private Button[] fButtons; 46 private String [] fButtonNames; 47 private boolean[] fButtonsSelected; 48 private boolean[] fButtonsEnabled; 49 50 private int fGroupBorderStyle; 51 private int fGroupNumberOfColumns; 52 private int fButtonsStyle; 53 54 55 public interface SelectionButtonGroupListener { 56 60 void groupChanged(MethodStubsSelectionButtonGroup field); 61 } 62 63 66 public MethodStubsSelectionButtonGroup(int buttonsStyle, String [] buttonNames, int nColumns) { 67 this(buttonsStyle, buttonNames, nColumns, SWT.NONE); 68 } 69 70 75 public MethodStubsSelectionButtonGroup(int buttonsStyle, String [] buttonNames, int nColumns, int borderStyle) { 76 fEnabled= true; 77 fLabel= null; 78 fLabelText= ""; 80 Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK || buttonsStyle == SWT.TOGGLE); 81 fButtonNames= buttonNames; 82 83 int nButtons= buttonNames.length; 84 fButtonsSelected= new boolean[nButtons]; 85 fButtonsEnabled= new boolean[nButtons]; 86 for (int i= 0; i < nButtons; i++) { 87 fButtonsSelected[i]= false; 88 fButtonsEnabled[i]= true; 89 } 90 91 if (buttonsStyle == SWT.RADIO) { 92 fButtonsSelected[0]= true; 93 } 94 95 fGroupBorderStyle= borderStyle; 96 fGroupNumberOfColumns= (nColumns <= 0) ? nButtons : nColumns; 97 98 fButtonsStyle= buttonsStyle; 99 } 100 101 104 public Control[] doFillIntoGrid(Composite parent, int nColumns) { 105 assertEnoughColumns(nColumns); 106 107 if (fGroupBorderStyle == SWT.NONE) { 108 Label label= getLabelControl(parent); 109 label.setLayoutData(gridDataForLabel(1)); 110 111 Composite buttonsgroup= getSelectionButtonsGroup(parent); 112 GridData gd= new GridData(); 113 gd.horizontalSpan= nColumns - 1; 114 buttonsgroup.setLayoutData(gd); 115 return new Control[] { label, buttonsgroup }; 116 } else { 117 Composite buttonsgroup= getSelectionButtonsGroup(parent); 118 GridData gd= new GridData(); 119 gd.horizontalSpan= nColumns; 120 buttonsgroup.setLayoutData(gd); 121 return new Control[] { buttonsgroup }; 122 } 123 } 124 125 128 public int getNumberOfControls() { 129 return (fGroupBorderStyle == SWT.NONE) ? 2 : 1; 130 } 131 132 private Button createSelectionButton(int index, Composite group, SelectionListener listener) { 133 Button button= new Button(group, fButtonsStyle | SWT.LEFT); 134 button.setFont(group.getFont()); 135 button.setText(fButtonNames[index]); 136 button.setEnabled(isEnabled() && isEnabled(index)); 137 button.setSelection(isSelected(index)); 138 button.addSelectionListener(listener); 139 return button; 140 } 141 142 143 148 public Composite getSelectionButtonsGroup(Composite parent) { 149 if (fButtonComposite == null) { 150 assertCompositeNotNull(parent); 151 152 GridLayout layout= new GridLayout(); 153 layout.numColumns= fGroupNumberOfColumns; 155 156 if (fGroupBorderStyle != SWT.NONE) { 157 Group group= new Group(parent, fGroupBorderStyle); 158 if (fLabelText != null && fLabelText.length() > 0) { 159 group.setText(fLabelText); 160 } 161 fButtonComposite= group; 162 } else { 163 fButtonComposite= new Composite(parent, SWT.NULL); 164 layout.marginHeight= 0; 165 layout.marginWidth= 0; 166 } 167 fButtonComposite.setLayout(layout); 168 169 SelectionListener listener= new SelectionListener() { 170 public void widgetDefaultSelected(SelectionEvent e) { 171 doWidgetSelected(e); 172 } 173 public void widgetSelected(SelectionEvent e) { 174 doWidgetSelected(e); 175 } 176 }; 177 int nButtons= fButtonNames.length; 178 fButtons= new Button[nButtons]; 179 180 for (int i= 0; i < nButtons; i++) { 181 fButtons[i]= createSelectionButton(i, fButtonComposite, listener); 182 } 183 int nRows= nButtons / fGroupNumberOfColumns; 184 int nFillElements= nRows * fGroupNumberOfColumns - nButtons; 185 for (int i= 0; i < nFillElements; i++) { 186 createEmptySpace(fButtonComposite); 187 } 188 setSelectionGroupListener(new SelectionButtonGroupListener() { 189 public void groupChanged(MethodStubsSelectionButtonGroup field) { 190 field.setEnabled(1, isEnabled() && field.isSelected(0)); 191 } 192 }); 193 } 194 return fButtonComposite; 195 } 196 197 200 public Button getSelectionButton(int index) { 201 if (index >= 0 && index < fButtons.length) { 202 return fButtons[index]; 203 } 204 return null; 205 } 206 207 private void doWidgetSelected(SelectionEvent e) { 208 Button button= (Button)e.widget; 209 for (int i= 0; i < fButtons.length; i++) { 210 if (fButtons[i] == button) { 211 fButtonsSelected[i]= button.getSelection(); 212 dialogFieldChanged(); 213 return; 214 } 215 } 216 } 217 218 222 public boolean isSelected(int index) { 223 if (index >= 0 && index < fButtonsSelected.length) { 224 return fButtonsSelected[index] && fButtonsEnabled[index]; 225 } 226 return false; 227 } 228 229 232 public void setSelection(int index, boolean selected) { 233 if (index >= 0 && index < fButtonsSelected.length) { 234 if (fButtonsSelected[index] != selected) { 235 fButtonsSelected[index]= selected; 236 if (fButtons != null) { 237 Button button= fButtons[index]; 238 if (isOkToUse(button) && button.isEnabled()) { 239 button.setSelection(selected); 240 } 241 } 242 } 243 } 244 } 245 246 250 public boolean isEnabled(int index) { 251 if (index >= 0 && index < fButtonsEnabled.length) { 252 return fButtonsEnabled[index]; 253 } 254 return false; 255 } 256 257 260 public void setEnabled(int index, boolean enabled) { 261 if (index >= 0 && index < fButtonsEnabled.length) { 262 if (fButtonsEnabled[index] != enabled) { 263 fButtonsEnabled[index]= enabled; 264 if (fButtons != null) { 265 Button button= fButtons[index]; 266 if (isOkToUse(button)) { 267 button.setEnabled(enabled); 268 if (!enabled) { 269 button.setSelection(false); 270 } else { 271 button.setSelection(fButtonsSelected[index]); 272 } 273 } 274 } 275 } 276 } 277 } 278 279 protected void updateEnableState() { 280 if (fLabel != null) { 281 fLabel.setEnabled(fEnabled); 282 } 283 if (fButtons != null) { 284 boolean enabled= isEnabled(); 285 for (int i= 0; i < fButtons.length; i++) { 286 Button button= fButtons[i]; 287 if (isOkToUse(button)) { 288 button.setEnabled(enabled && fButtonsEnabled[i]); 289 } 290 } 291 } 292 } 293 294 297 public void setLabelText(String labeltext) { 298 fLabelText= labeltext; 299 } 300 301 304 public final void setSelectionGroupListener(SelectionButtonGroupListener listener) { 305 fGroupListener= listener; 306 } 307 308 311 public void dialogFieldChanged() { 312 if (fGroupListener != null) { 313 fGroupListener.groupChanged(this); 314 } 315 } 316 317 322 public boolean setFocus() { 323 return false; 324 } 325 326 329 public void postSetFocusOnDialogField(Display display) { 330 if (display != null) { 331 display.asyncExec( 332 new Runnable () { 333 public void run() { 334 setFocus(); 335 } 336 } 337 ); 338 } 339 } 340 341 protected static GridData gridDataForLabel(int span) { 342 GridData gd= new GridData(); 343 gd.horizontalSpan= span; 344 return gd; 345 } 346 347 352 public Label getLabelControl(Composite parent) { 353 if (fLabel == null) { 354 assertCompositeNotNull(parent); 355 356 fLabel= new Label(parent, SWT.LEFT | SWT.WRAP); 357 fLabel.setFont(parent.getFont()); 358 fLabel.setEnabled(fEnabled); 359 if (fLabelText != null && !"".equals(fLabelText)) { fLabel.setText(fLabelText); 361 } else { 362 fLabel.setText("."); fLabel.setVisible(false); 365 } 366 } 367 return fLabel; 368 } 369 370 374 public static Control createEmptySpace(Composite parent) { 375 return createEmptySpace(parent, 1); 376 } 377 378 384 public static Control createEmptySpace(Composite parent, int span) { 385 return LayoutUtil.createEmptySpace(parent, span); 386 } 387 388 391 protected final boolean isOkToUse(Control control) { 392 return (control != null) && !(control.isDisposed()); 393 } 394 395 397 400 public final void setEnabled(boolean enabled) { 401 if (enabled != fEnabled) { 402 fEnabled= enabled; 403 updateEnableState(); 404 } 405 } 406 407 410 public final boolean isEnabled() { 411 return fEnabled; 412 } 413 414 protected final void assertCompositeNotNull(Composite comp) { 415 Assert.isNotNull(comp, "uncreated control requested with composite null"); } 417 418 protected final void assertEnoughColumns(int nColumns) { 419 Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); } 421 } 422 | Popular Tags |