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.Point;
013: import org.eclipse.swt.widgets.Control;
014: import org.eclipse.ui.views.properties.IPropertyDescriptor;
015: import org.eclipse.ui.views.properties.IPropertySource;
016: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
017:
018: /**
019: * Property source for the size properties.
020: *
021: * @author Anthony Hunter
022: */
023: public class SizePropertySource implements IPropertySource {
024:
025: protected ButtonElement element;
026:
027: /**
028: * The width.
029: */
030: public static String ID_WIDTH = "Width"; //$NON-NLS-1$
031:
032: /**
033: * The height.
034: */
035: public static String ID_HEIGHT = "Height"; //$NON-NLS-1$
036:
037: protected static IPropertyDescriptor[] descriptors;
038:
039: static {
040: descriptors = new IPropertyDescriptor[] {
041: new TextPropertyDescriptor(ID_WIDTH, "width"),//$NON-NLS-1$
042: new TextPropertyDescriptor(ID_HEIGHT, "height") //$NON-NLS-1$
043: };
044: }
045:
046: protected Point point = null;
047:
048: /**
049: * The constructor for SizePropertySource.
050: *
051: * @param m_element
052: * the button element.
053: * @param point
054: * the size of the button element.
055: */
056: public SizePropertySource(ButtonElement m_element, Point point) {
057: this .point = new Point(point.x, point.y);
058: element = m_element;
059: }
060:
061: /**
062: * Fire a property change event.
063: *
064: * @param propName
065: * the name of the property change.
066: */
067: protected void firePropertyChanged() {
068: Control ctl = element.getControl();
069:
070: if (ctl == null) {
071: // the GUIView is probably hidden in this case
072: return;
073: }
074: ctl.setSize(point);
075: }
076:
077: public Object getEditableValue() {
078: return this ;
079: }
080:
081: public IPropertyDescriptor[] getPropertyDescriptors() {
082: return descriptors;
083: }
084:
085: public Object getPropertyValue(Object propName) {
086: if (ID_WIDTH.equals(propName)) {
087: return new String(new Integer(point.x).toString());
088: }
089: if (ID_HEIGHT.equals(propName)) {
090: return new String(new Integer(point.y).toString());
091: }
092: return null;
093: }
094:
095: /**
096: * Retrieve the value of the size property.
097: *
098: * @return the value of the size property.
099: */
100: public Point getValue() {
101: return new Point(point.x, point.y);
102: }
103:
104: /**
105: * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(Object)
106: */
107: public boolean isPropertySet(Object propName) {
108: if (ID_WIDTH.equals(propName) || ID_HEIGHT.equals(propName))
109: return true;
110: return false;
111: }
112:
113: public void resetPropertyValue(Object propName) {
114: //
115: }
116:
117: public void setPropertyValue(Object propName, Object value) {
118: int newInt;
119: try {
120: newInt = Integer.parseInt((String) value);
121: } catch (NumberFormatException e) {
122: newInt = -1;
123: }
124:
125: if (newInt > 0) {
126: if (ID_WIDTH.equals(propName)) {
127: point.x = newInt;
128: } else if (ID_HEIGHT.equals(propName)) {
129: point.y = newInt;
130: }
131: }
132: firePropertyChanged();
133: }
134:
135: public String toString() {
136: return point.toString();
137: }
138:
139: }
|