001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.views.properties.tabbed.article.views;
011:
012: import org.eclipse.jface.resource.StringConverter;
013: import org.eclipse.jface.util.Assert;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.custom.CLabel;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.graphics.FontData;
021: import org.eclipse.swt.graphics.GC;
022: import org.eclipse.swt.graphics.Point;
023: import org.eclipse.swt.layout.FormAttachment;
024: import org.eclipse.swt.layout.FormData;
025: import org.eclipse.swt.widgets.Button;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.FontDialog;
028: import org.eclipse.swt.widgets.Shell;
029: import org.eclipse.swt.widgets.Text;
030: import org.eclipse.ui.IWorkbenchPart;
031: import org.eclipse.ui.PlatformUI;
032: import org.eclipse.ui.views.properties.IPropertySource;
033: import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
034: import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
035: import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
036:
037: /**
038: * The font section on the button tab.
039: *
040: * @author Anthony Hunter
041: */
042: public class FontSection extends AbstractPropertySection {
043:
044: Text fontText;
045:
046: private ButtonElement buttonElement;
047:
048: public void setInput(IWorkbenchPart part, ISelection selection) {
049: super .setInput(part, selection);
050: Assert.isTrue(selection instanceof IStructuredSelection);
051: Object input = ((IStructuredSelection) selection)
052: .getFirstElement();
053: Assert.isTrue(input instanceof ButtonElement);
054: this .buttonElement = (ButtonElement) input;
055: }
056:
057: /**
058: * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
059: * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
060: */
061: public void createControls(Composite parent,
062: TabbedPropertySheetPage tabbedPropertySheetPage) {
063: super .createControls(parent, tabbedPropertySheetPage);
064: Composite composite = getWidgetFactory()
065: .createFlatFormComposite(parent);
066: FormData data;
067:
068: Shell shell = new Shell();
069: GC gc = new GC(shell);
070: gc.setFont(shell.getFont());
071: Point point = gc.textExtent("");//$NON-NLS-1$
072: int buttonHeight = point.y + 5;
073: gc.dispose();
074: shell.dispose();
075:
076: CLabel fontLabel = getWidgetFactory().createCLabel(composite,
077: "Font:"); //$NON-NLS-1$
078: fontText = getWidgetFactory().createText(composite, ""); //$NON-NLS-1$
079: fontText.setEditable(false);
080: Button fontButton = getWidgetFactory().createButton(composite,
081: "Change...", SWT.PUSH); //$NON-NLS-1$
082: fontButton.addSelectionListener(new SelectionAdapter() {
083:
084: public void widgetSelected(SelectionEvent event) {
085: FontDialog ftDialog = new FontDialog(PlatformUI
086: .getWorkbench().getActiveWorkbenchWindow()
087: .getShell());
088:
089: FontData fontdata = buttonElement.getControl()
090: .getFont().getFontData()[0];
091: String value = fontdata.toString();
092:
093: if ((value != null) && (value.length() > 0)) {
094: ftDialog.setFontList(new FontData[] { new FontData(
095: value) });
096: }
097: FontData fData = ftDialog.open();
098:
099: if (fData != null) {
100: value = fData.toString();
101:
102: ButtonElementProperties properties = (ButtonElementProperties) buttonElement
103: .getAdapter(IPropertySource.class);
104: properties.setPropertyValue(
105: ButtonElementProperties.PROPERTY_FONT,
106: value);
107: fontText.setText(StringConverter.asString(fData));
108: }
109: }
110: });
111:
112: data = new FormData();
113: data.left = new FormAttachment(0, 0);
114: data.right = new FormAttachment(fontText,
115: -ITabbedPropertyConstants.HSPACE);
116: data.top = new FormAttachment(0, 0);
117: fontLabel.setLayoutData(data);
118:
119: data = new FormData();
120: data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
121: data.right = new FormAttachment(fontButton,
122: -ITabbedPropertyConstants.HSPACE);
123: data.top = new FormAttachment(0, 0);
124: fontText.setLayoutData(data);
125:
126: data = new FormData();
127: data.right = new FormAttachment(100, 0);
128: data.top = new FormAttachment(0, 0);
129: data.height = buttonHeight;
130: fontButton.setLayoutData(data);
131: }
132:
133: /*
134: * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
135: */
136: public void refresh() {
137: FontData fontdata = buttonElement.getControl().getFont()
138: .getFontData()[0];
139: fontText.setText(StringConverter.asString(fontdata));
140: }
141: }
|