1 11 package org.eclipse.jdt.internal.ui.wizards.dialogfields; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.SelectionEvent; 15 import org.eclipse.swt.events.SelectionListener; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.widgets.Button; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Control; 20 21 import org.eclipse.jdt.internal.ui.util.SWTUtil; 22 23 26 public class SelectionButtonDialogField extends DialogField { 27 28 private Button fButton; 29 private boolean fIsSelected; 30 private DialogField[] fAttachedDialogFields; 31 private int fButtonStyle; 32 33 37 public SelectionButtonDialogField(int buttonStyle) { 38 super(); 39 fIsSelected= false; 40 fAttachedDialogFields= null; 41 fButtonStyle= buttonStyle; 42 } 43 44 48 public void attachDialogField(DialogField dialogField) { 49 attachDialogFields(new DialogField[] { dialogField }); 50 } 51 52 56 public void attachDialogFields(DialogField[] dialogFields) { 57 fAttachedDialogFields= dialogFields; 58 for (int i= 0; i < dialogFields.length; i++) { 59 dialogFields[i].setEnabled(fIsSelected); 60 } 61 } 62 63 66 public boolean isAttached(DialogField editor) { 67 if (fAttachedDialogFields != null) { 68 for (int i=0; i < fAttachedDialogFields.length; i++) { 69 if (fAttachedDialogFields[i] == editor) { 70 return true; 71 } 72 } 73 } 74 return false; 75 } 76 77 79 82 public Control[] doFillIntoGrid(Composite parent, int nColumns) { 83 assertEnoughColumns(nColumns); 84 85 Button button= getSelectionButton(parent); 86 GridData gd= new GridData(); 87 gd.horizontalSpan= nColumns; 88 gd.horizontalAlignment= GridData.FILL; 89 if (fButtonStyle == SWT.PUSH) { 90 gd.widthHint = SWTUtil.getButtonWidthHint(button); 91 } 92 93 button.setLayoutData(gd); 94 95 return new Control[] { button }; 96 } 97 98 101 public int getNumberOfControls() { 102 return 1; 103 } 104 105 107 112 public Button getSelectionButton(Composite group) { 113 if (fButton == null) { 114 assertCompositeNotNull(group); 115 116 fButton= new Button(group, fButtonStyle); 117 fButton.setFont(group.getFont()); 118 fButton.setText(fLabelText); 119 fButton.setEnabled(isEnabled()); 120 fButton.setSelection(fIsSelected); 121 fButton.addSelectionListener(new SelectionListener() { 122 public void widgetDefaultSelected(SelectionEvent e) { 123 doWidgetSelected(e); 124 } 125 public void widgetSelected(SelectionEvent e) { 126 doWidgetSelected(e); 127 } 128 }); 129 } 130 return fButton; 131 } 132 133 private void doWidgetSelected(SelectionEvent e) { 134 if (isOkToUse(fButton)) { 135 changeValue(fButton.getSelection()); 136 } 137 } 138 139 private void changeValue(boolean newState) { 140 if (fIsSelected != newState) { 141 fIsSelected= newState; 142 if (fAttachedDialogFields != null) { 143 boolean focusSet= false; 144 for (int i= 0; i < fAttachedDialogFields.length; i++) { 145 fAttachedDialogFields[i].setEnabled(fIsSelected); 146 if (fIsSelected && !focusSet) { 147 focusSet= fAttachedDialogFields[i].setFocus(); 148 } 149 } 150 } 151 dialogFieldChanged(); 152 } else if (fButtonStyle == SWT.PUSH) { 153 dialogFieldChanged(); 154 } 155 } 156 157 160 public void setLabelText(String labeltext) { 161 fLabelText= labeltext; 162 if (isOkToUse(fButton)) { 163 fButton.setText(labeltext); 164 } 165 } 166 167 168 170 173 public boolean isSelected() { 174 return fIsSelected; 175 } 176 177 180 public void setSelection(boolean selected) { 181 changeValue(selected); 182 if (isOkToUse(fButton)) { 183 fButton.setSelection(selected); 184 } 185 } 186 187 189 192 protected void updateEnableState() { 193 super.updateEnableState(); 194 if (isOkToUse(fButton)) { 195 fButton.setEnabled(isEnabled()); 196 } 197 } 198 199 202 public void refresh() { 203 super.refresh(); 204 if (isOkToUse(fButton)) { 205 fButton.setSelection(fIsSelected); 206 } 207 } 208 209 210 } 211 | Popular Tags |