KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataProvider


1 /*
2  * $Id: DataProvider.java,v 1.2 2005/02/27 00:24:55 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.dataset;
8
9 import java.util.concurrent.Executor JavaDoc;
10 import java.util.concurrent.Executors JavaDoc;
11 import org.jdesktop.dataset.provider.LoadTask;
12 import org.jdesktop.dataset.provider.SaveTask;
13 import org.jdesktop.dataset.provider.Task;
14
15 /**
16  * Provides a basic implementation of DataProvider that handles all of the
17  * threading issues normally associated with writing a DataProvider.
18  *
19  * @author rbair
20  */

21 public abstract class DataProvider {
22     /**
23      * thread pool from which to get threads to execute the loader.
24      */

25     private static final Executor JavaDoc EX = Executors.newCachedThreadPool();
26
27     public void load(DataTable[] tables) {
28         Task task = createLoadTask(tables);
29         runTask(task);
30     }
31     
32     public void load(DataTable t) {
33         load(new DataTable[]{t});
34     }
35     
36     public void save(DataTable t) {
37         save(new DataTable[]{t});
38     }
39     
40     public void save(DataTable[] tables) {
41         Task task = createSaveTask(tables);
42         runTask(task);
43     }
44     
45     /**
46      * Creates a task that saves data from an array of DataTables to the
47      * data store. All of these tables will be saved serially on the same
48      * background thread.
49      */

50     protected abstract SaveTask createSaveTask(DataTable[] tables);
51     
52     /**
53      * Creates a Task that loads data from the data store into one or more
54      * DataTables. All of these tables will be loaded serially using the same
55      * background thread.
56      */

57     protected abstract LoadTask createLoadTask(DataTable[] tables);
58     
59     /**
60      * Invoked by the <code>load</code> or <code>save</code> methods.
61      * This method will be called on the EventDispatch thread, and therefore
62      * must not block. This method is provided to allow concrete subclasses to
63      * provide a custom thread creation/scheduling implementation.
64      *
65      * @param runner
66      */

67     protected void runTask(Task runner) {
68 // Application.getInstance().getProgressManager().addProgressable(runner);
69
EX.execute(runner);
70     }
71 }
Popular Tags