KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > swt > ControlObservableValue


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Brad Reynolds - bug 164653
11  * Matt Carter - bug 170668
12  * Brad Reynolds - bug 170848
13  *******************************************************************************/

14 package org.eclipse.jface.internal.databinding.internal.swt;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.databinding.observable.Diffs;
20 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.widgets.Control;
24
25 /**
26  * @since 1.0
27  *
28  */

29 public class ControlObservableValue extends AbstractSWTObservableValue {
30
31     private final Control control;
32
33     private final String JavaDoc attribute;
34
35     private Object JavaDoc valueType;
36     
37     private static final Map JavaDoc SUPPORTED_ATTRIBUTES = new HashMap JavaDoc();
38     static {
39         SUPPORTED_ATTRIBUTES.put(SWTProperties.ENABLED, Boolean.TYPE);
40         SUPPORTED_ATTRIBUTES.put(SWTProperties.VISIBLE, Boolean.TYPE);
41         SUPPORTED_ATTRIBUTES.put(SWTProperties.TOOLTIP_TEXT, String JavaDoc.class);
42         SUPPORTED_ATTRIBUTES.put(SWTProperties.FOREGROUND, Color.class);
43         SUPPORTED_ATTRIBUTES.put(SWTProperties.BACKGROUND, Color.class);
44         SUPPORTED_ATTRIBUTES.put(SWTProperties.FONT, Font.class);
45     }
46     
47     /**
48      * @param control
49      * @param attribute
50      */

51     public ControlObservableValue(Control control, String JavaDoc attribute) {
52         super(control);
53         this.control = control;
54         this.attribute = attribute;
55         if (SUPPORTED_ATTRIBUTES.keySet().contains(attribute)) {
56             this.valueType = SUPPORTED_ATTRIBUTES.get(attribute);
57         } else {
58             throw new IllegalArgumentException JavaDoc();
59         }
60     }
61
62     public void doSetValue(Object JavaDoc value) {
63         Object JavaDoc oldValue = doGetValue();
64         if (attribute.equals(SWTProperties.ENABLED)) {
65             control.setEnabled(((Boolean JavaDoc) value).booleanValue());
66         } else if (attribute.equals(SWTProperties.VISIBLE)) {
67             control.setVisible(((Boolean JavaDoc) value).booleanValue());
68         } else if (attribute.equals(SWTProperties.TOOLTIP_TEXT)) {
69             control.setToolTipText((String JavaDoc) value);
70         } else if (attribute.equals(SWTProperties.FOREGROUND)) {
71             control.setForeground((Color) value);
72         } else if (attribute.equals(SWTProperties.BACKGROUND)) {
73             control.setBackground((Color) value);
74         } else if (attribute.equals(SWTProperties.FONT)) {
75             control.setFont((Font) value);
76         }
77         fireValueChange(Diffs.createValueDiff(oldValue, value));
78     }
79
80     public Object JavaDoc doGetValue() {
81         if (attribute.equals(SWTProperties.ENABLED)) {
82             return control.getEnabled() ? Boolean.TRUE : Boolean.FALSE;
83         }
84         if (attribute.equals(SWTProperties.VISIBLE)) {
85             return control.getVisible() ? Boolean.TRUE : Boolean.FALSE;
86         }
87         if (attribute.equals(SWTProperties.TOOLTIP_TEXT)) {
88             return control.getToolTipText();
89         }
90         if (attribute.equals(SWTProperties.FOREGROUND)) {
91             return control.getForeground();
92         }
93         if (attribute.equals(SWTProperties.BACKGROUND)) {
94             return control.getBackground();
95         }
96         if (attribute.equals(SWTProperties.FONT)) {
97             return control.getFont();
98         }
99         
100         return null;
101     }
102
103     public Object JavaDoc getValueType() {
104         return valueType;
105     }
106 }
107
Popular Tags