1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.DisposeEvent; 15 import org.eclipse.swt.events.DisposeListener; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.widgets.Button; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Label; 22 23 26 public class BooleanFieldEditor extends FieldEditor { 27 28 33 public static final int DEFAULT = 0; 34 35 40 public static final int SEPARATE_LABEL = 1; 41 42 46 private int style; 47 48 51 private boolean wasSelected; 52 53 56 private Button checkBox = null; 57 58 61 protected BooleanFieldEditor() { 62 } 63 64 75 public BooleanFieldEditor(String name, String labelText, int style, 76 Composite parent) { 77 init(name, labelText); 78 this.style = style; 79 createControl(parent); 80 } 81 82 89 public BooleanFieldEditor(String name, String label, Composite parent) { 90 this(name, label, DEFAULT, parent); 91 } 92 93 96 protected void adjustForNumColumns(int numColumns) { 97 if (style == SEPARATE_LABEL) { 98 numColumns--; 99 } 100 ((GridData) checkBox.getLayoutData()).horizontalSpan = numColumns; 101 } 102 103 106 protected void doFillIntoGrid(Composite parent, int numColumns) { 107 String text = getLabelText(); 108 switch (style) { 109 case SEPARATE_LABEL: 110 getLabelControl(parent); 111 numColumns--; 112 text = null; 113 default: 114 checkBox = getChangeControl(parent); 115 GridData gd = new GridData(); 116 gd.horizontalSpan = numColumns; 117 checkBox.setLayoutData(gd); 118 if (text != null) { 119 checkBox.setText(text); 120 } 121 } 122 } 123 124 129 protected void doLoad() { 130 if (checkBox != null) { 131 boolean value = getPreferenceStore() 132 .getBoolean(getPreferenceName()); 133 checkBox.setSelection(value); 134 wasSelected = value; 135 } 136 } 137 138 143 protected void doLoadDefault() { 144 if (checkBox != null) { 145 boolean value = getPreferenceStore().getDefaultBoolean( 146 getPreferenceName()); 147 checkBox.setSelection(value); 148 wasSelected = value; 149 } 150 } 151 152 155 protected void doStore() { 156 getPreferenceStore().setValue(getPreferenceName(), 157 checkBox.getSelection()); 158 } 159 160 165 public boolean getBooleanValue() { 166 return checkBox.getSelection(); 167 } 168 169 175 protected Button getChangeControl(Composite parent) { 176 if (checkBox == null) { 177 checkBox = new Button(parent, SWT.CHECK | SWT.LEFT); 178 checkBox.setFont(parent.getFont()); 179 checkBox.addSelectionListener(new SelectionAdapter() { 180 public void widgetSelected(SelectionEvent e) { 181 boolean isSelected = checkBox.getSelection(); 182 valueChanged(wasSelected, isSelected); 183 wasSelected = isSelected; 184 } 185 }); 186 checkBox.addDisposeListener(new DisposeListener() { 187 public void widgetDisposed(DisposeEvent event) { 188 checkBox = null; 189 } 190 }); 191 } else { 192 checkParent(checkBox, parent); 193 } 194 return checkBox; 195 } 196 197 200 public int getNumberOfControls() { 201 switch (style) { 202 case SEPARATE_LABEL: 203 return 2; 204 default: 205 return 1; 206 } 207 } 208 209 212 public void setFocus() { 213 if (checkBox != null) { 214 checkBox.setFocus(); 215 } 216 } 217 218 221 public void setLabelText(String text) { 222 super.setLabelText(text); 223 Label label = getLabelControl(); 224 if (label == null && checkBox != null) { 225 checkBox.setText(text); 226 } 227 } 228 229 237 protected void valueChanged(boolean oldValue, boolean newValue) { 238 setPresentsDefaultValue(false); 239 if (oldValue != newValue) { 240 fireStateChanged(VALUE, oldValue, newValue); 241 } 242 } 243 244 247 public void setEnabled(boolean enabled, Composite parent) { 248 if (style == SEPARATE_LABEL) { 250 super.setEnabled(enabled, parent); 251 } 252 getChangeControl(parent).setEnabled(enabled); 253 } 254 255 } 256 | Popular Tags |