1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.jface.dialogs.IDialogConstants; 14 import org.eclipse.jface.resource.JFaceResources; 15 import org.eclipse.jface.resource.StringConverter; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.DisposeEvent; 19 import org.eclipse.swt.events.DisposeListener; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.graphics.Font; 23 import org.eclipse.swt.graphics.FontData; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.FontDialog; 29 import org.eclipse.swt.widgets.Label; 30 import org.eclipse.swt.widgets.Text; 31 32 35 public class FontFieldEditor extends FieldEditor { 36 37 41 private Button changeFontButton = null; 42 43 47 private String changeButtonText; 48 49 52 private String previewText; 53 54 57 private FontData[] chosenFont; 58 59 62 private Label valueControl; 63 64 67 private DefaultPreviewer previewer; 68 69 72 private static class DefaultPreviewer { 73 private Text text; 74 75 private String string; 76 77 private Font font; 78 79 84 public DefaultPreviewer(String s, Composite parent) { 85 string = s; 86 text = new Text(parent, SWT.READ_ONLY | SWT.BORDER); 87 text.addDisposeListener(new DisposeListener() { 88 public void widgetDisposed(DisposeEvent e) { 89 if (font != null) { 90 font.dispose(); 91 } 92 } 93 }); 94 if (string != null) { 95 text.setText(string); 96 } 97 } 98 99 102 public Control getControl() { 103 return text; 104 } 105 106 110 public void setFont(FontData[] fontData) { 111 if (font != null) { 112 font.dispose(); 113 } 114 font = new Font(text.getDisplay(), fontData); 115 text.setFont(font); 116 } 117 118 121 public int getPreferredExtent() { 122 return 40; 123 } 124 } 125 126 129 protected FontFieldEditor() { 130 } 131 132 141 public FontFieldEditor(String name, String labelText, 142 String previewAreaText, Composite parent) { 143 init(name, labelText); 144 previewText = previewAreaText; 145 changeButtonText = JFaceResources.getString("openChange"); createControl(parent); 147 148 } 149 150 157 public FontFieldEditor(String name, String labelText, Composite parent) { 158 this(name, labelText, null, parent); 159 160 } 161 162 165 protected void adjustForNumColumns(int numColumns) { 166 167 GridData data = new GridData(); 168 if (valueControl.getLayoutData() != null) { 169 data = (GridData) valueControl.getLayoutData(); 170 } 171 172 data.horizontalSpan = numColumns - getNumberOfControls() + 1; 173 valueControl.setLayoutData(data); 174 } 175 176 179 protected void applyFont() { 180 if (chosenFont != null && previewer != null) { 181 previewer.setFont(chosenFont); 182 } 183 } 184 185 188 protected void doFillIntoGrid(Composite parent, int numColumns) { 189 getLabelControl(parent); 190 191 valueControl = getValueControl(parent); 192 193 GridData gd = new GridData(GridData.FILL_HORIZONTAL 194 | GridData.GRAB_HORIZONTAL); 195 gd.horizontalSpan = numColumns - getNumberOfControls() + 1; 196 valueControl.setLayoutData(gd); 197 if (previewText != null) { 198 previewer = new DefaultPreviewer(previewText, parent); 199 gd = new GridData(GridData.FILL_HORIZONTAL); 200 gd.heightHint = previewer.getPreferredExtent(); 201 gd.widthHint = previewer.getPreferredExtent(); 202 previewer.getControl().setLayoutData(gd); 203 } 204 205 changeFontButton = getChangeControl(parent); 206 gd = new GridData(); 207 int widthHint = convertHorizontalDLUsToPixels(changeFontButton, 208 IDialogConstants.BUTTON_WIDTH); 209 gd.widthHint = Math.max(widthHint, changeFontButton.computeSize( 210 SWT.DEFAULT, SWT.DEFAULT, true).x); 211 changeFontButton.setLayoutData(gd); 212 213 } 214 215 218 protected void doLoad() { 219 if (changeFontButton == null) { 220 return; 221 } 222 updateFont(PreferenceConverter.getFontDataArray(getPreferenceStore(), 223 getPreferenceName())); 224 } 225 226 229 protected void doLoadDefault() { 230 if (changeFontButton == null) { 231 return; 232 } 233 updateFont(PreferenceConverter.getDefaultFontDataArray( 234 getPreferenceStore(), getPreferenceName())); 235 } 236 237 240 protected void doStore() { 241 if (chosenFont != null) { 242 PreferenceConverter.setValue(getPreferenceStore(), 243 getPreferenceName(), chosenFont); 244 } 245 } 246 247 253 protected Button getChangeControl(Composite parent) { 254 if (changeFontButton == null) { 255 changeFontButton = new Button(parent, SWT.PUSH); 256 if (changeButtonText != null) { 257 changeFontButton.setText(changeButtonText); 258 } 259 changeFontButton.addSelectionListener(new SelectionAdapter() { 260 public void widgetSelected(SelectionEvent event) { 261 FontDialog fontDialog = new FontDialog(changeFontButton 262 .getShell()); 263 if (chosenFont != null) { 264 fontDialog.setFontList(chosenFont); 265 } 266 FontData font = fontDialog.open(); 267 if (font != null) { 268 FontData[] oldFont = chosenFont; 269 if (oldFont == null) { 270 oldFont = JFaceResources.getDefaultFont() 271 .getFontData(); 272 } 273 setPresentsDefaultValue(false); 274 FontData[] newData = new FontData[1]; 275 newData[0] = font; 276 updateFont(newData); 277 fireValueChanged(VALUE, oldFont[0], font); 278 } 279 280 } 281 }); 282 changeFontButton.addDisposeListener(new DisposeListener() { 283 public void widgetDisposed(DisposeEvent event) { 284 changeFontButton = null; 285 } 286 }); 287 changeFontButton.setFont(parent.getFont()); 288 setButtonLayoutData(changeFontButton); 289 } else { 290 checkParent(changeFontButton, parent); 291 } 292 return changeFontButton; 293 } 294 295 298 public int getNumberOfControls() { 299 if (previewer == null) { 300 return 3; 301 } 302 303 return 4; 304 } 305 306 312 public int getPreferredPreviewHeight() { 313 if (previewer == null) { 314 return -1; 315 } 316 return previewer.getPreferredExtent(); 317 } 318 319 324 public Control getPreviewControl() { 325 if (previewer == null) { 326 return null; 327 } 328 329 return previewer.getControl(); 330 } 331 332 338 protected Label getValueControl(Composite parent) { 339 if (valueControl == null) { 340 valueControl = new Label(parent, SWT.LEFT); 341 valueControl.setFont(parent.getFont()); 342 valueControl.addDisposeListener(new DisposeListener() { 343 public void widgetDisposed(DisposeEvent event) { 344 valueControl = null; 345 } 346 }); 347 } else { 348 checkParent(valueControl, parent); 349 } 350 return valueControl; 351 } 352 353 358 public void setChangeButtonText(String text) { 359 Assert.isNotNull(text); 360 changeButtonText = text; 361 if (changeFontButton != null) { 362 changeFontButton.setText(text); 363 } 364 } 365 366 371 private void updateFont(FontData font[]) { 372 FontData[] bestFont = JFaceResources.getFontRegistry().filterData( 373 font, valueControl.getDisplay()); 374 375 if (bestFont == null) { 377 bestFont = getDefaultFontData(); 378 } 379 380 this.chosenFont = bestFont; 382 383 if (valueControl != null) { 384 valueControl.setText(StringConverter.asString(chosenFont[0])); 385 } 386 if (previewer != null) { 387 previewer.setFont(bestFont); 388 } 389 } 390 391 395 protected void setToDefault() { 396 FontData[] defaultFontData = PreferenceConverter 397 .getDefaultFontDataArray(getPreferenceStore(), 398 getPreferenceName()); 399 PreferenceConverter.setValue(getPreferenceStore(), getPreferenceName(), 400 defaultFontData); 401 } 402 403 407 private FontData[] getDefaultFontData() { 408 return valueControl.getDisplay().getSystemFont().getFontData(); 409 } 410 411 414 public void setEnabled(boolean enabled, Composite parent) { 415 super.setEnabled(enabled, parent); 416 getChangeControl(parent).setEnabled(enabled); 417 getValueControl(parent).setEnabled(enabled); 418 } 419 420 } 421 | Popular Tags |