KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.jface.internal.databinding.internal.viewers;
12
13 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
14 import org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue;
15 import org.eclipse.jface.internal.databinding.provisional.viewers.ViewersProperties;
16 import org.eclipse.jface.util.Assert;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.StructuredViewer;
23
24 /**
25  * @since 3.2
26  *
27  */

28 public class StructuredViewerObservableValue extends AbstractObservableValue {
29
30     private final StructuredViewer viewer;
31
32     private final String JavaDoc attribute;
33
34     private boolean updating = false;
35
36     private Object JavaDoc currentSelection;
37
38     /**
39      * @param viewer
40      * @param attribute
41      */

42     public StructuredViewerObservableValue(StructuredViewer viewer,
43             String JavaDoc attribute) {
44         this.viewer = viewer;
45         this.attribute = attribute;
46         this.currentSelection = doGetValue();
47         if (attribute.equals(ViewersProperties.SINGLE_SELECTION)) {
48             viewer.addSelectionChangedListener(new ISelectionChangedListener() {
49                 public void selectionChanged(SelectionChangedEvent event) {
50                     if (!updating) {
51                         Object JavaDoc oldSelection = currentSelection;
52                         currentSelection = doGetValue();
53                         fireValueChange(Diffs.createValueDiff(oldSelection,
54                                 currentSelection));
55                     }
56                 }
57             });
58         } else {
59             throw new IllegalArgumentException JavaDoc(
60                     "Attribute name not valid: " + attribute); //$NON-NLS-1$
61
}
62     }
63
64     public void setValue(final Object JavaDoc value) {
65         try {
66             updating = true;
67             if (attribute.equals(ViewersProperties.SINGLE_SELECTION)) {
68                 Object JavaDoc oldSelection = currentSelection;
69                 viewer.setSelection(value == null ? StructuredSelection.EMPTY
70                         : new StructuredSelection(value));
71                 currentSelection = doGetValue();
72                 fireValueChange(Diffs.createValueDiff(oldSelection,
73                         currentSelection));
74             }
75         } finally {
76             updating = false;
77         }
78     }
79
80     public Object JavaDoc doGetValue() {
81         if (attribute.equals(ViewersProperties.SINGLE_SELECTION)) {
82             ISelection selection = viewer.getSelection();
83             if (selection instanceof IStructuredSelection) {
84                 IStructuredSelection sel = (IStructuredSelection) selection;
85                 return sel.getFirstElement();
86             }
87         }
88         return null;
89     }
90
91     public Object JavaDoc getValueType() {
92         Assert.isTrue(attribute.equals(ViewersProperties.SINGLE_SELECTION),
93                 "unexpected attribute: " + attribute); //$NON-NLS-1$
94
return Object JavaDoc.class;
95     }
96
97 }
98
Popular Tags