1 11 package org.eclipse.pde.ui.templates; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.SelectionAdapter; 15 import org.eclipse.swt.events.SelectionEvent; 16 import org.eclipse.swt.events.SelectionListener; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.layout.GridLayout; 19 import org.eclipse.swt.widgets.Button; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 23 30 public class ChoiceOption extends TemplateOption { 31 private String [][] choices; 32 private Control labelControl; 33 private Button[] buttons; 34 private boolean blockListener; 35 36 51 public ChoiceOption(BaseOptionTemplateSection section, String name, 52 String label, String [][] choices) { 53 super(section, name, label); 54 this.choices = choices; 55 } 56 57 60 public void createControl(Composite parent, int span) { 61 Composite container = createComposite(parent, span); 62 fill(container, span); 63 GridLayout layout = new GridLayout(); 64 layout.marginWidth = layout.marginHeight = 0; 65 container.setLayout(layout); 66 labelControl = createLabel(container, span); 67 labelControl.setEnabled(isEnabled()); 68 fill(labelControl, span); 69 70 buttons = new Button[choices.length]; 71 72 SelectionListener listener = new SelectionAdapter() { 73 public void widgetSelected(SelectionEvent e) { 74 Button b = (Button) e.widget; 75 if (blockListener) 76 return; 77 if (b.getSelection()) { 78 ChoiceOption.super.setValue(b.getData().toString()); 79 getSection().validateOptions(ChoiceOption.this); 80 } 81 } 82 }; 83 84 for (int i = 0; i < choices.length; i++) { 85 String [] choice = choices[i]; 86 Button button = createRadioButton(parent, span, choice); 87 buttons[i] = button; 88 button.addSelectionListener(listener); 89 button.setEnabled(isEnabled()); 90 } 91 if (getChoice() != null) 92 selectChoice(getChoice()); 93 } 94 99 public String getChoice() { 100 return getValue() != null ? getValue().toString() : null; 101 } 102 103 110 public void setValue(Object value) { 111 super.setValue(value); 112 if (buttons != null && value != null) { 113 selectChoice(value.toString()); 114 } 115 } 116 120 public void setEnabled(boolean enabled) { 121 super.setEnabled(enabled); 122 if (labelControl != null) { 123 labelControl.setEnabled(enabled); 124 for (int i = 0; i < buttons.length; i++) { 125 buttons[i].setEnabled(isEnabled()); 126 } 127 } 128 } 129 130 private GridData fill(Control control, int span) { 131 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 132 gd.horizontalSpan = span; 133 control.setLayoutData(gd); 134 return gd; 135 } 136 137 private Composite createComposite(Composite parent, int span) { 138 Composite composite = new Composite(parent, SWT.NULL); 139 fill(composite, span); 140 return composite; 141 } 142 143 private Button createRadioButton(Composite parent, int span, String [] choice) { 144 Button button = new Button(parent, SWT.RADIO); 145 button.setData(choice[0]); 146 button.setText(choice[1]); 147 GridData gd = fill(button, span); 148 gd.horizontalIndent = 10; 149 return button; 150 } 151 152 private void selectChoice(String choice) { 153 blockListener = true; 154 for (int i = 0; i < buttons.length; i++) { 155 Button button = buttons[i]; 156 String bname = button.getData().toString(); 157 if (bname.equals(choice)) { 158 button.setSelection(true); 159 } else { 160 button.setSelection(false); 161 } 162 } 163 blockListener = false; 164 } 165 } 166 | Popular Tags |