1 13 package org.eclipse.jface.preference; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.jface.util.IPropertyChangeListener; 21 import org.eclipse.jface.util.PropertyChangeEvent; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 28 36 public abstract class FieldEditorPreferencePage extends PreferencePage 37 implements IPropertyChangeListener { 38 39 43 public static final int FLAT = 0; 44 45 49 public static final int GRID = 1; 50 51 55 protected static final int VERTICAL_SPACING = 10; 56 57 61 protected static final int MARGIN_WIDTH = 0; 62 63 67 protected static final int MARGIN_HEIGHT = 0; 68 69 72 private List fields = null; 73 74 77 private int style; 78 79 83 private FieldEditor invalidFieldEditor = null; 84 85 88 private Composite fieldEditorParent; 89 90 93 public FieldEditorPreferencePage() { 94 this(FLAT); 95 } 96 97 103 protected FieldEditorPreferencePage(int style) { 104 super(); 105 this.style = style; 106 } 107 108 115 protected FieldEditorPreferencePage(String title, int style) { 116 super(title); 117 this.style = style; 118 } 119 120 129 protected FieldEditorPreferencePage(String title, ImageDescriptor image, 130 int style) { 131 super(title, image); 132 this.style = style; 133 } 134 135 140 protected void addField(FieldEditor editor) { 141 if (fields == null) { 142 fields = new ArrayList (); 143 } 144 fields.add(editor); 145 } 146 147 151 protected void adjustGridLayout() { 152 int numColumns = calcNumberOfColumns(); 153 ((GridLayout) fieldEditorParent.getLayout()).numColumns = numColumns; 154 if (fields != null) { 155 for (int i = 0; i < fields.size(); i++) { 156 FieldEditor fieldEditor = (FieldEditor) fields.get(i); 157 fieldEditor.adjustForNumColumns(numColumns); 158 } 159 } 160 } 161 162 165 protected void applyFont() { 166 if (fields != null) { 167 Iterator e = fields.iterator(); 168 while (e.hasNext()) { 169 FieldEditor pe = (FieldEditor) e.next(); 170 pe.applyFont(); 171 } 172 } 173 } 174 175 180 private int calcNumberOfColumns() { 181 int result = 0; 182 if (fields != null) { 183 Iterator e = fields.iterator(); 184 while (e.hasNext()) { 185 FieldEditor pe = (FieldEditor) e.next(); 186 result = Math.max(result, pe.getNumberOfControls()); 187 } 188 } 189 return result; 190 } 191 192 196 protected void checkState() { 197 boolean valid = true; 198 invalidFieldEditor = null; 199 if (fields != null) { 202 int size = fields.size(); 203 for (int i = 0; i < size; i++) { 204 FieldEditor editor = (FieldEditor) fields.get(i); 205 valid = valid && editor.isValid(); 206 if (!valid) { 207 invalidFieldEditor = editor; 208 break; 209 } 210 } 211 } 212 setValid(valid); 213 } 214 215 218 protected Control createContents(Composite parent) { 219 fieldEditorParent = new Composite(parent, SWT.NULL); 220 GridLayout layout = new GridLayout(); 221 layout.numColumns = 1; 222 layout.marginHeight = 0; 223 layout.marginWidth = 0; 224 fieldEditorParent.setLayout(layout); 225 fieldEditorParent.setFont(parent.getFont()); 226 227 createFieldEditors(); 228 229 if (style == GRID) { 230 adjustGridLayout(); 231 } 232 233 initialize(); 234 checkState(); 235 return fieldEditorParent; 236 } 237 238 253 protected abstract void createFieldEditors(); 254 255 261 public void dispose() { 262 super.dispose(); 263 if (fields != null) { 264 Iterator e = fields.iterator(); 265 while (e.hasNext()) { 266 FieldEditor pe = (FieldEditor) e.next(); 267 pe.setPage(null); 268 pe.setPropertyChangeListener(null); 269 pe.setPreferenceStore(null); 270 } 271 } 272 } 273 274 285 protected Composite getFieldEditorParent() { 286 if (style == FLAT) { 287 Composite parent = new Composite(fieldEditorParent, SWT.NULL); 289 parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 290 return parent; 291 } 292 return fieldEditorParent; 294 } 295 296 299 protected void initialize() { 300 if (fields != null) { 301 Iterator e = fields.iterator(); 302 while (e.hasNext()) { 303 FieldEditor pe = (FieldEditor) e.next(); 304 pe.setPage(this); 305 pe.setPropertyChangeListener(this); 306 pe.setPreferenceStore(getPreferenceStore()); 307 pe.load(); 308 } 309 } 310 } 311 312 316 protected void performDefaults() { 317 if (fields != null) { 318 Iterator e = fields.iterator(); 319 while (e.hasNext()) { 320 FieldEditor pe = (FieldEditor) e.next(); 321 pe.loadDefault(); 322 } 323 } 324 checkState(); 326 super.performDefaults(); 327 } 328 329 338 public boolean performOk() { 339 if (fields != null) { 340 Iterator e = fields.iterator(); 341 while (e.hasNext()) { 342 FieldEditor pe = (FieldEditor) e.next(); 343 pe.store(); 344 pe.setPresentsDefaultValue(false); 345 } 346 } 347 return true; 348 } 349 350 355 public void propertyChange(PropertyChangeEvent event) { 356 357 if (event.getProperty().equals(FieldEditor.IS_VALID)) { 358 boolean newValue = ((Boolean ) event.getNewValue()).booleanValue(); 359 if (newValue) { 362 checkState(); 363 } else { 364 invalidFieldEditor = (FieldEditor) event.getSource(); 365 setValid(newValue); 366 } 367 } 368 } 369 370 373 public void setVisible(boolean visible) { 374 super.setVisible(visible); 375 if (visible && invalidFieldEditor != null) { 376 invalidFieldEditor.setFocus(); 377 } 378 } 379 } 380 | Popular Tags |