1 11 package org.eclipse.pde.ui.templates; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.events.SelectionAdapter; 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.Label; 23 24 30 public class RadioChoiceOption extends AbstractChoiceOption { 31 32 private Button[] fButtons; 33 private Label fLabel; 34 35 52 public RadioChoiceOption(BaseOptionTemplateSection section, String name, 53 String label, String [][] choices) { 54 super(section, name, label, choices); 55 Assert.isTrue(choices.length == 2); 56 } 57 58 private Button createRadioButton(Composite parent, int span, String [] choice) { 59 Button button = new Button(parent, SWT.RADIO); 60 button.setData(choice[0]); 61 button.setText(choice[1]); 62 GridData gd = fill(button, span); 63 gd.horizontalIndent = 10; 64 return button; 65 } 66 67 68 public void createControl(Composite parent, int span) { 69 70 fLabel = createLabel(parent, 1); 71 fLabel.setEnabled(isEnabled()); 72 fill(fLabel, span); 73 74 Composite radioComp = createComposite(parent, span); 75 GridData gd = fill(radioComp, span); 76 gd.horizontalIndent = 10; 77 GridLayout layout = new GridLayout(fChoices.length, true); 78 layout.marginWidth = layout.marginHeight = 0; 79 radioComp.setLayout(layout); 80 81 fButtons = new Button[fChoices.length]; 82 83 SelectionListener listener = new SelectionAdapter() { 84 public void widgetSelected(SelectionEvent e) { 85 Button b = (Button) e.widget; 86 if (isBlocked()) 87 return; 88 if (b.getSelection()) { 89 setValue(b.getData().toString()); 90 getSection().validateOptions(RadioChoiceOption.this); 91 } 92 } 93 }; 94 95 for (int i = 0; i < fChoices.length; i++) { 96 fButtons[i] = createRadioButton(radioComp, 1, fChoices[i]); 97 fButtons[i].addSelectionListener(listener); 98 fButtons[i].setEnabled(isEnabled()); 99 } 100 101 if (getChoice() != null) 102 selectChoice(getChoice()); 103 } 104 105 protected void setOptionValue(Object value) { 106 if (fButtons != null && value != null) { 107 selectChoice(value.toString()); 108 } 109 } 110 111 protected void setOptionEnabled(boolean enabled) { 112 if (fLabel != null) { 113 fLabel.setEnabled(enabled); 114 for (int i = 0; i < fButtons.length; i++) { 115 fButtons[i].setEnabled(enabled); 116 } 117 } 118 } 119 120 protected void selectOptionChoice(String choice) { 121 for (int i = 0; i < fButtons.length; i++) { 122 Button button = fButtons[i]; 123 String bname = button.getData().toString(); 124 if (bname.equals(choice)) { 125 button.setSelection(true); 126 } else { 127 button.setSelection(false); 128 } 129 } 130 } 131 } 132 | Popular Tags |