KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > swt > TableUpdater


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  *******************************************************************************/

11 package org.eclipse.jface.internal.databinding.provisional.swt;
12
13 import org.eclipse.core.databinding.observable.ChangeEvent;
14 import org.eclipse.core.databinding.observable.IChangeListener;
15 import org.eclipse.core.databinding.observable.IObservable;
16 import org.eclipse.core.databinding.observable.ObservableTracker;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.events.DisposeListener;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableItem;
25
26 /**
27  * NON-API - This class can be used to update a table with automatic dependency tracking.
28  * @since 1.1
29  *
30  */

31 public abstract class TableUpdater {
32
33     private class UpdateRunnable implements Runnable JavaDoc, IChangeListener,
34             DisposeListener {
35         private TableItem item;
36
37         private boolean dirty = false;
38
39         private IObservable[] dependencies = new IObservable[0];
40
41         UpdateRunnable(TableItem item) {
42             this.item = item;
43             item.addDisposeListener(this);
44         }
45
46         // Runnable implementation. This method runs at most once per repaint
47
// whenever the
48
// value gets marked as dirty.
49
public void run() {
50             if (theTable != null && !theTable.isDisposed() && item != null && !item.isDisposed()) {
51                 if (theTable.isVisible()) {
52                     int tableHeight = theTable.getClientArea().height;
53                     int numVisibleItems = tableHeight / theTable.getItemHeight();
54                     int indexOfItem = theTable.indexOf(item);
55                     int topIndex = theTable.getTopIndex();
56                     if (indexOfItem >= topIndex && indexOfItem <= topIndex+numVisibleItems) {
57                         updateIfNecessary();
58                         return;
59                     }
60                 }
61                 theTable.clear(theTable.indexOf(item));
62             }
63         }
64
65         private void updateIfNecessary() {
66             if (dirty) {
67                 dependencies = ObservableTracker.runAndMonitor(new Runnable JavaDoc() {
68                     public void run() {
69                         updateItem(item);
70                     }
71                 }, this, null);
72                 dirty = false;
73             }
74         }
75
76         // IChangeListener implementation (listening to the ComputedValue)
77
public void handleChange(ChangeEvent event) {
78             // Whenever this updator becomes dirty, schedule the run() method
79
makeDirty();
80         }
81
82         protected final void makeDirty() {
83             if (!dirty) {
84                 dirty = true;
85                 stopListening();
86                 SWTUtil.runOnce(theTable.getDisplay(), this);
87             }
88         }
89
90         private void stopListening() {
91             // Stop listening for dependency changes
92
for (int i = 0; i < dependencies.length; i++) {
93                 IObservable observable = dependencies[i];
94
95                 observable.removeChangeListener(this);
96             }
97         }
98
99         // DisposeListener implementation
100
public void widgetDisposed(DisposeEvent e) {
101             stopListening();
102             dependencies = null;
103             item = null;
104         }
105     }
106
107     private class PrivateInterface implements Listener, DisposeListener {
108
109         // Listener implementation
110
public void handleEvent(Event e) {
111             if (e.type == SWT.SetData) {
112                 UpdateRunnable runnable = (UpdateRunnable) e.item.getData();
113                 if (runnable == null) {
114                     runnable = new UpdateRunnable((TableItem) e.item);
115                     e.item.setData(runnable);
116                     runnable.makeDirty();
117                 } else {
118                     runnable.updateIfNecessary();
119                 }
120             }
121         }
122
123         // DisposeListener implementation
124
public void widgetDisposed(DisposeEvent e) {
125             TableUpdater.this.dispose();
126         }
127
128     }
129
130     private PrivateInterface privateInterface = new PrivateInterface();
131
132     private Table theTable;
133
134     /**
135      * Creates an updator for the given control.
136      *
137      * @param toUpdate
138      * table to update
139      */

140     public TableUpdater(Table toUpdate) {
141         Assert.isLegal((toUpdate.getStyle() & SWT.VIRTUAL) != 0, "TableUpdater requires virtual table"); //$NON-NLS-1$
142
theTable = toUpdate;
143
144         theTable.addDisposeListener(privateInterface);
145         theTable.addListener(SWT.SetData, privateInterface);
146     }
147
148     /**
149      * This is called automatically when the control is disposed. It may also be
150      * called explicitly to remove this updator from the control. Subclasses
151      * will normally extend this method to detach any listeners they attached in
152      * their constructor.
153      */

154     public void dispose() {
155         theTable.removeDisposeListener(privateInterface);
156         theTable.removeListener(SWT.SetData, privateInterface);
157
158     }
159
160     /**
161      * Updates the control. This method will be invoked once after the updator
162      * is created, and once before any repaint during which the control is
163      * visible and dirty.
164      *
165      * <p>
166      * Subclasses should overload this method to provide any code that changes
167      * the appearance of the widget.
168      * </p>
169      * @param item the item to update
170      */

171     protected abstract void updateItem(TableItem item);
172
173 }
174
Popular Tags