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.swt.graphics.Font;
013: import org.eclipse.swt.graphics.FontData;
014: import org.eclipse.swt.graphics.Point;
015: import org.eclipse.swt.widgets.Button;
016: import org.eclipse.ui.views.properties.IPropertyDescriptor;
017: import org.eclipse.ui.views.properties.IPropertySource;
018: import org.eclipse.ui.views.properties.PropertyDescriptor;
019: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
020:
021: /**
022: * The property source provider for a button element.
023: *
024: * @author Anthony Hunter
025: */
026: public class ButtonElementProperties implements IPropertySource {
027:
028: final protected ButtonElement element;
029:
030: protected static final String PROPERTY_FONT = "font"; //$NON-NLS-1$
031:
032: protected static final String PROPERTY_SIZE = "size"; //$NON-NLS-1$
033:
034: protected static final String PROPERTY_TEXT = "text"; //$NON-NLS-1$
035:
036: private final Object PropertiesTable[][] = {
037: { PROPERTY_FONT,
038: new FontPropertyDescriptor(PROPERTY_FONT, "Font") },//$NON-NLS-1$
039: { PROPERTY_SIZE,
040: new PropertyDescriptor(PROPERTY_SIZE, "Size") },//$NON-NLS-1$
041: { PROPERTY_TEXT,
042: new TextPropertyDescriptor(PROPERTY_TEXT, "Text") }, //$NON-NLS-1$
043: };
044:
045: String strFont = "";//$NON-NLS-1$
046:
047: Point ptSize = null;
048:
049: String strText = "";//$NON-NLS-1$
050:
051: protected void firePropertyChanged(String propName, Object value) {
052: Button ctl = element.getControl();
053:
054: if (ctl == null) {
055: // the GUIView is probably hidden in this case
056: return;
057: }
058:
059: if (propName.equals(PROPERTY_FONT)) {
060: /**
061: * Font oldfont = ctl.getFont(); if (oldfont != null) {
062: * oldfont.dispose(); }
063: */
064: ctl.setFont(new Font(ctl.getDisplay(), new FontData(
065: (String) value)));
066: return;
067: }
068: if (propName.equals(PROPERTY_TEXT)) {
069: ctl.setText((String) value);
070: return;
071: }
072:
073: }
074:
075: protected void initProperties() {
076: Button ctl = element.getControl();
077:
078: if (ctl == null) {
079: // the GUIView is probably hidden in this case
080: return;
081: }
082:
083: strText = ctl.getText();
084: /**
085: * Font font = ctl.getFont(); if (font != null) { strFont =
086: * font.getFontData().toString(); }
087: */
088: ptSize = ctl.getSize();
089: }
090:
091: /**
092: * Creates a new ButtonElementProperties.
093: *
094: * @param element
095: * the element whose properties this instance represents
096: */
097: public ButtonElementProperties(ButtonElement element) {
098: super ();
099: this .element = element;
100: initProperties();
101: }
102:
103: /**
104: * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
105: */
106: public Object getEditableValue() {
107: return this ;
108: }
109:
110: /**
111: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
112: */
113: public IPropertyDescriptor[] getPropertyDescriptors() {
114: // Create the property vector.
115: IPropertyDescriptor[] propertyDescriptors = new IPropertyDescriptor[PropertiesTable.length];
116:
117: for (int i = 0; i < PropertiesTable.length; i++) {
118: // Add each property supported.
119:
120: PropertyDescriptor descriptor;
121:
122: descriptor = (PropertyDescriptor) PropertiesTable[i][1];
123: propertyDescriptors[i] = descriptor;
124: descriptor.setCategory("Basic");//$NON-NLS-1$
125: }
126:
127: // Return it.
128: return propertyDescriptors;
129:
130: }
131:
132: /**
133: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(Object)
134: */
135: public Object getPropertyValue(Object name) {
136: if (name.equals(PROPERTY_FONT))
137: return strFont;
138: if (name.equals(PROPERTY_SIZE))
139: return new SizePropertySource(element, ptSize);
140: if (name.equals(PROPERTY_TEXT))
141: return strText;
142:
143: return null;
144: }
145:
146: /**
147: * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(Object)
148: */
149: public boolean isPropertySet(Object id) {
150: return false;
151: }
152:
153: /**
154: * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(Object)
155: */
156: public void resetPropertyValue(Object id) {
157: //
158: }
159:
160: /**
161: * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(Object,
162: * Object)
163: */
164: public void setPropertyValue(Object name, Object value) {
165: firePropertyChanged((String) name, value);
166:
167: if (name.equals(PROPERTY_FONT)) {
168: strFont = (String) value;
169: return;
170: }
171: if (name.equals(PROPERTY_TEXT)) {
172: strText = (String) value;
173: return;
174: }
175: if (name.equals(PROPERTY_SIZE)) {
176: SizePropertySource sizeProp = (SizePropertySource) value;
177: ptSize = sizeProp.getValue();
178: }
179:
180: }
181:
182: /**
183: * Returns the mocha element.
184: *
185: * @return MochaElement
186: */
187: public ButtonElement getButtonElement() {
188: return element;
189: }
190:
191: }
|