KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Ashley Cambrell - bug 198904
12  *******************************************************************************/

13 package org.eclipse.jface.internal.databinding.internal.swt;
14
15 import org.eclipse.core.databinding.observable.Diffs;
16 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Event;
20 import org.eclipse.swt.widgets.Listener;
21
22 /**
23  * @since 1.0
24  *
25  */

26 public class ButtonObservableValue extends AbstractSWTObservableValue {
27
28     private final Button button;
29
30     private boolean selectionValue;
31
32     private Listener updateListener = new Listener() {
33         public void handleEvent(Event event) {
34             boolean oldSelectionValue = selectionValue;
35             selectionValue = button.getSelection();
36             fireValueChange(Diffs.createValueDiff(oldSelectionValue ? Boolean.TRUE : Boolean.FALSE,
37                     selectionValue ? Boolean.TRUE : Boolean.FALSE));
38         }
39     };
40
41     /**
42      * @param button
43      */

44     public ButtonObservableValue(Button button) {
45         super(button);
46         this.button = button;
47         button.addListener(SWT.Selection, updateListener);
48         button.addListener(SWT.DefaultSelection, updateListener);
49     }
50
51     public void doSetValue(final Object JavaDoc value) {
52         boolean oldSelectionValue = selectionValue;
53         selectionValue = value == null ? false : ((Boolean JavaDoc) value)
54                 .booleanValue();
55         button.setSelection(selectionValue);
56         fireValueChange(Diffs.createValueDiff(oldSelectionValue ? Boolean.TRUE : Boolean.FALSE,
57                 selectionValue ? Boolean.TRUE : Boolean.FALSE));
58     }
59
60     public Object JavaDoc doGetValue() {
61         return button.getSelection() ? Boolean.TRUE : Boolean.FALSE;
62     }
63
64     public Object JavaDoc getValueType() {
65         return Boolean JavaDoc.class;
66     }
67
68     /*
69      * (non-Javadoc)
70      *
71      * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
72      */

73     public synchronized void dispose() {
74         super.dispose();
75
76         if (!button.isDisposed()) {
77             button.removeListener(SWT.Selection, updateListener);
78             button.removeListener(SWT.DefaultSelection, updateListener);
79         }
80     }
81
82 }
83
Popular Tags