KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > viewers > SelectionProviderSingleSelectionObservableValue


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 137877
11  * Brad Reynolds - bug 164653
12  * Brad Reynolds - bug 147515
13  * Ashley Cambrell - bug 198906
14  *******************************************************************************/

15
16 package org.eclipse.jface.internal.databinding.internal.viewers;
17
18 import org.eclipse.core.databinding.observable.Diffs;
19 import org.eclipse.core.databinding.observable.Realm;
20 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
21 import org.eclipse.jface.util.Util;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.SelectionChangedEvent;
27 import org.eclipse.jface.viewers.StructuredSelection;
28
29 /**
30  * Observes single selection of an <code>ISelectionProvider</code>.
31  *
32  * @since 1.1
33  */

34 public class SelectionProviderSingleSelectionObservableValue extends
35         AbstractObservableValue {
36
37     private final ISelectionProvider selectionProvider;
38
39     private boolean updating = false;
40
41     private Object JavaDoc currentSelection;
42
43     private ISelectionChangedListener selectionChangedListener;
44
45     /**
46      * Constructs a new instance associated with the provided
47      * <code>selectionProvider</code>. In order to initialize itself properly
48      * the constructor invokes {@link #doGetValue()}. This could be dangerous
49      * for subclasses, see {@link #doGetValue()} for an explanation.
50      *
51      * @param realm
52      *
53      * @param selectionProvider
54      * @see #doGetValue()
55      */

56     public SelectionProviderSingleSelectionObservableValue(Realm realm,
57             ISelectionProvider selectionProvider) {
58         super(realm);
59         if (selectionProvider == null) {
60             throw new IllegalArgumentException JavaDoc(
61                     "The 'selectionProvider' parameter is null."); //$NON-NLS-1$
62
}
63
64         this.selectionProvider = selectionProvider;
65         this.currentSelection = doGetValue();
66
67         selectionChangedListener = new ISelectionChangedListener() {
68             public void selectionChanged(SelectionChangedEvent event) {
69                 if (!updating) {
70                     Object JavaDoc oldSelection = currentSelection;
71                     currentSelection = doGetValue();
72                     fireValueChange(Diffs.createValueDiff(oldSelection,
73                             currentSelection));
74                 }
75             }
76         };
77         selectionProvider.addSelectionChangedListener(selectionChangedListener);
78     }
79
80     /**
81      * Sets the selection to the provided <code>value</code>. Value change
82      * events are fired after selection is set in the selection provider.
83      *
84      * @param value
85      * object to set as selected, <code>null</code> if wanting to
86      * remove selection
87      */

88     public void doSetValue(final Object JavaDoc value) {
89         try {
90             updating = true;
91
92             Object JavaDoc oldSelection = currentSelection;
93             selectionProvider
94                     .setSelection(value == null ? StructuredSelection.EMPTY
95                             : new StructuredSelection(value));
96             currentSelection = doGetValue();
97             if (!Util.equals(oldSelection, currentSelection)) {
98                 fireValueChange(Diffs.createValueDiff(oldSelection,
99                         currentSelection));
100             }
101         } finally {
102             updating = false;
103         }
104     }
105
106     /**
107      * Retrieves the current selection.
108      * <p>
109      * If a subclass overrides this method it must not depend upon the subclass
110      * to have been fully initialized before this method is invoked.
111      * <code>doGetValue()</code> is invoked by the
112      * {@link #SelectionProviderSingleSelectionObservableValue(Realm, ISelectionProvider) constructor}
113      * which means the subclass's constructor will not have fully executed
114      * before this method is invoked.
115      * </p>
116      *
117      * @return selection will be an instance of
118      * <code>IStructuredSelection</code> if a selection exists,
119      * <code>null</code> if no selection
120      * @see #SelectionProviderSingleSelectionObservableValue(Realm,
121      * ISelectionProvider)
122      */

123     protected Object JavaDoc doGetValue() {
124         ISelection selection = selectionProvider.getSelection();
125         if (selection instanceof IStructuredSelection) {
126             IStructuredSelection sel = (IStructuredSelection) selection;
127             return sel.getFirstElement();
128         }
129
130         return null;
131     }
132
133     public Object JavaDoc getValueType() {
134         return null;
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
141      */

142     public synchronized void dispose() {
143         selectionProvider
144                 .removeSelectionChangedListener(selectionChangedListener);
145         super.dispose();
146     }
147 }
148
Popular Tags