KickJava   Java API By Example, From Geeks To Geeks.

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


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.swt;
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.swt.SWTProperties;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.widgets.Table;
19
20 /**
21  * @since 1.0
22  *
23  */

24 public class TableObservableValue extends AbstractObservableValue {
25
26     private final Table table;
27
28     private boolean updating = false;
29
30     private int currentSelection;
31
32     /**
33      * @param table
34      * @param attribute
35      */

36     public TableObservableValue(Table table, String JavaDoc attribute) {
37         this.table = table;
38         currentSelection = table.getSelectionIndex();
39         if (attribute.equals(SWTProperties.SELECTION)) {
40             currentSelection = table.getSelectionIndex();
41             table.addSelectionListener(new SelectionListener() {
42                 public void widgetSelected(SelectionEvent e) {
43                     if (!updating) {
44                         int newSelection = TableObservableValue.this.table
45                                 .getSelectionIndex();
46                         fireValueChange(Diffs.createValueDiff(new Integer JavaDoc(
47                                 currentSelection), new Integer JavaDoc(newSelection)));
48                         currentSelection = newSelection;
49                     }
50                 }
51
52                 public void widgetDefaultSelected(SelectionEvent e) {
53                     widgetSelected(e);
54                 }
55             });
56         } else {
57             throw new IllegalArgumentException JavaDoc();
58         }
59     }
60
61     public void setValue(Object JavaDoc value) {
62         try {
63             updating = true;
64             int intValue = ((Integer JavaDoc) value).intValue();
65             table.setSelection(intValue);
66             currentSelection = intValue;
67         } finally {
68             updating = false;
69         }
70     }
71
72     public Object JavaDoc doGetValue() {
73         return new Integer JavaDoc(table.getSelectionIndex());
74     }
75
76     public Object JavaDoc getValueType() {
77         return Integer JavaDoc.class;
78     }
79
80 }
81
Popular Tags