KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > viewers > SelectionObservableValue


1 /*******************************************************************************
2  * Copyright (c) 2006 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  ******************************************************************************/

11
12 package org.eclipse.jface.internal.databinding.provisional.viewers;
13
14 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
15 import org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.StructuredViewer;
20
21 /**
22  * @since 1.0
23  *
24  */

25 public class SelectionObservableValue extends AbstractObservableValue {
26
27     private StructuredViewer viewer;
28
29     private Object JavaDoc currentSelection = null;
30
31     private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() {
32         public void selectionChanged(SelectionChangedEvent event) {
33             IStructuredSelection selection = (IStructuredSelection) event
34                     .getSelection();
35             Object JavaDoc oldSelection = currentSelection;
36             Object JavaDoc newSelection = selection.getFirstElement();
37             if (newSelection != oldSelection) {
38                 currentSelection = newSelection;
39                 fireValueChange(Diffs.createValueDiff(oldSelection, newSelection));
40             }
41         }
42     };
43
44     /**
45      * @param viewer
46      */

47     public SelectionObservableValue(StructuredViewer viewer) {
48         this.viewer = viewer;
49         viewer.addSelectionChangedListener(selectionChangedListener);
50         this.currentSelection = ((IStructuredSelection) viewer.getSelection())
51                 .getFirstElement();
52     }
53
54     protected Object JavaDoc doGetValue() {
55         return currentSelection;
56     }
57     
58     public void dispose() {
59         viewer.removeSelectionChangedListener(selectionChangedListener);
60         viewer = null;
61         currentSelection = null;
62         selectionChangedListener = null;
63         super.dispose();
64     }
65
66     public Object JavaDoc getValueType() {
67         return Object JavaDoc.class;
68     }
69
70 }
71
Popular Tags