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.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.Group; 24 import org.eclipse.swt.widgets.Label; 25 26 27 30 public class SelectionButtonDialogFieldGroup extends DialogField { 31 32 private Composite fButtonComposite; 33 34 private Button[] fButtons; 35 private String [] fButtonNames; 36 private boolean[] fButtonsSelected; 37 private boolean[] fButtonsEnabled; 38 39 private int fGroupBorderStyle; 40 private int fGroupNumberOfColumns; 41 private int fButtonsStyle; 42 43 46 public SelectionButtonDialogFieldGroup(int buttonsStyle, String [] buttonNames, int nColumns) { 47 this(buttonsStyle, buttonNames, nColumns, SWT.NONE); 48 } 49 50 51 56 public SelectionButtonDialogFieldGroup(int buttonsStyle, String [] buttonNames, int nColumns, int borderStyle) { 57 super(); 58 59 Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK || buttonsStyle == SWT.TOGGLE); 60 fButtonNames= buttonNames; 61 fButtonsStyle= buttonsStyle; 62 63 int nButtons= buttonNames.length; 64 fButtonsSelected= new boolean[nButtons]; 65 fButtonsEnabled= new boolean[nButtons]; 66 for (int i= 0; i < nButtons; i++) { 67 fButtonsSelected[i]= false; 68 fButtonsEnabled[i]= true; 69 } 70 if (buttonsStyle == SWT.RADIO) { 71 fButtonsSelected[0]= true; 72 } 73 74 fGroupBorderStyle= borderStyle; 75 fGroupNumberOfColumns= (nColumns <= 0) ? nButtons : nColumns; 76 77 78 79 } 80 81 83 86 public Control[] doFillIntoGrid(Composite parent, int nColumns) { 87 assertEnoughColumns(nColumns); 88 89 if (fGroupBorderStyle == SWT.NONE) { 90 Label label= getLabelControl(parent); 91 label.setLayoutData(gridDataForLabel(1)); 92 93 Composite buttonsgroup= getSelectionButtonsGroup(parent); 94 GridData gd= new GridData(); 95 gd.horizontalSpan= nColumns - 1; 96 buttonsgroup.setLayoutData(gd); 97 98 return new Control[] { label, buttonsgroup }; 99 } else { 100 Composite buttonsgroup= getSelectionButtonsGroup(parent); 101 GridData gd= new GridData(); 102 gd.horizontalSpan= nColumns; 103 buttonsgroup.setLayoutData(gd); 104 105 return new Control[] { buttonsgroup }; 106 } 107 } 108 109 112 public int getNumberOfControls() { 113 return (fGroupBorderStyle == SWT.NONE) ? 2 : 1; 114 } 115 116 118 private Button createSelectionButton(int index, Composite group, SelectionListener listener) { 119 Button button= new Button(group, fButtonsStyle | SWT.LEFT); 120 button.setFont(group.getFont()); 121 button.setText(fButtonNames[index]); 122 button.setEnabled(isEnabled() && fButtonsEnabled[index]); 123 button.setSelection(fButtonsSelected[index]); 124 button.addSelectionListener(listener); 125 button.setLayoutData(new GridData()); 126 return button; 127 } 128 129 134 public Composite getSelectionButtonsGroup(Composite parent) { 135 if (fButtonComposite == null) { 136 assertCompositeNotNull(parent); 137 138 GridLayout layout= new GridLayout(); 139 layout.makeColumnsEqualWidth= true; 140 layout.numColumns= fGroupNumberOfColumns; 141 142 if (fGroupBorderStyle != SWT.NONE) { 143 Group group= new Group(parent, fGroupBorderStyle); 144 group.setFont(parent.getFont()); 145 if (fLabelText != null && fLabelText.length() > 0) { 146 group.setText(fLabelText); 147 } 148 fButtonComposite= group; 149 } else { 150 fButtonComposite= new Composite(parent, SWT.NONE); 151 fButtonComposite.setFont(parent.getFont()); 152 layout.marginHeight= 0; 153 layout.marginWidth= 0; 154 } 155 156 fButtonComposite.setLayout(layout); 157 158 SelectionListener listener= new SelectionListener() { 159 public void widgetDefaultSelected(SelectionEvent e) { 160 doWidgetSelected(e); 161 } 162 public void widgetSelected(SelectionEvent e) { 163 doWidgetSelected(e); 164 } 165 }; 166 int nButtons= fButtonNames.length; 167 fButtons= new Button[nButtons]; 168 for (int i= 0; i < nButtons; i++) { 169 fButtons[i]= createSelectionButton(i, fButtonComposite, listener); 170 } 171 int nRows= nButtons / fGroupNumberOfColumns; 172 int nFillElements= nRows * fGroupNumberOfColumns - nButtons; 173 for (int i= 0; i < nFillElements; i++) { 174 createEmptySpace(fButtonComposite); 175 } 176 } 177 return fButtonComposite; 178 } 179 180 183 public Button getSelectionButton(int index) { 184 if (index >= 0 && index < fButtons.length) { 185 return fButtons[index]; 186 } 187 return null; 188 } 189 190 private void doWidgetSelected(SelectionEvent e) { 191 Button button= (Button)e.widget; 192 for (int i= 0; i < fButtons.length; i++) { 193 if (fButtons[i] == button) { 194 fButtonsSelected[i]= button.getSelection(); 195 dialogFieldChanged(); 196 return; 197 } 198 } 199 } 200 201 203 207 public boolean isSelected(int index) { 208 if (index >= 0 && index < fButtonsSelected.length) { 209 return fButtonsSelected[index]; 210 } 211 return false; 212 } 213 214 217 public void setSelection(int index, boolean selected) { 218 if (index >= 0 && index < fButtonsSelected.length) { 219 if (fButtonsSelected[index] != selected) { 220 fButtonsSelected[index]= selected; 221 if (fButtons != null) { 222 Button button= fButtons[index]; 223 if (isOkToUse(button)) { 224 button.setSelection(selected); 225 } 226 } 227 } 228 } 229 } 230 231 233 protected void updateEnableState() { 234 super.updateEnableState(); 235 if (fButtons != null) { 236 boolean enabled= isEnabled(); 237 for (int i= 0; i < fButtons.length; i++) { 238 Button button= fButtons[i]; 239 if (isOkToUse(button)) { 240 button.setEnabled(enabled && fButtonsEnabled[i]); 241 } 242 } 243 } 244 } 245 246 249 public void enableSelectionButton(int index, boolean enable) { 250 if (index >= 0 && index < fButtonsEnabled.length) { 251 fButtonsEnabled[index]= enable; 252 if (fButtons != null) { 253 Button button= fButtons[index]; 254 if (isOkToUse(button)) { 255 button.setEnabled(isEnabled() && enable); 256 } 257 } 258 } 259 } 260 261 262 265 public void refresh() { 266 super.refresh(); 267 for (int i= 0; i < fButtons.length; i++) { 268 Button button= fButtons[i]; 269 if (isOkToUse(button)) { 270 button.setSelection(fButtonsSelected[i]); 271 } 272 } 273 } 274 275 } 276 | Popular Tags |