LoadTask.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LoadTask.java,v 1.5 2005/10/10 17:01:18 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset.provider;
  22. import java.util.LinkedList;
  23. import javax.swing.Icon;
  24. import javax.swing.SwingUtilities;
  25. import org.jdesktop.dataset.DataTable;
  26. /**
  27.  *
  28.  * @author rbair
  29.  */
  30. public abstract class LoadTask extends AbstractTask {
  31.     /**
  32.      * This linked list contains "LoadItem" objects. As a specific unit of data
  33.      * is loaded (for instance, all of the data needed for a specific
  34.      * DataTable), a new LoadItem is created and placed at the tail of the
  35.      * loadQueue. The scheduleLoad method then executes and the current items in
  36.      * the queue are removed and passed to the loadData method. This queue is
  37.      * accessed from multiple threads (many threads put data into the queue, one
  38.      * thread removes items from the queue). Synchronization occurs on the
  39.      * loadQueue.
  40.      */
  41.     private LinkedList loadQueue = new LinkedList();
  42.     /**
  43.      * Object used for collescing multiple calls to scheduleLoad into one call
  44.      * to loadData.
  45.      */
  46.     private LoadNotifier loadNotifier = new LoadNotifier();
  47.     /**
  48.      * The DataTables that are being loaded.
  49.      */
  50.     private DataTable[] tables;
  51.     private boolean loadOnEDT = true;
  52.     public void setLoadOnEDT(boolean val) {
  53.         loadOnEDT = val;
  54.     }
  55.     
  56.     /**
  57.      * Creates a new LoadTask. The param is the array of DataTables to be
  58.      * loaded on this thread
  59.      */
  60.     public LoadTask(DataTable[] tables) {
  61.         this.tables = tables == null ? new DataTable[0] : tables;
  62.     }
  63.     /**
  64.      * @inheritDoc
  65.      */
  66.     public void run() {
  67.         setIndeterminate(true);
  68.         try {
  69.             //TODO If meta-data of any kind needs to be loaded, this is the place to do it
  70.             readData(tables);
  71.             scheduleLoad();
  72.             setProgress(getMaximum());
  73.         } catch (Exception e) {
  74.             final Throwable error = e;
  75.             e.printStackTrace();
  76.             setProgress(getMaximum());
  77.         }
  78.     }
  79.     /**
  80.      * Subclasses must implement this
  81.      * method to read the data from the data store and place it in a data
  82.      * structure which is <b>disconnected </b> from the DataTable. When
  83.      * increments of data are ready to be loaded into the model, this method
  84.      * should invoke <code>scheduleLoad</code>, which will cause
  85.      * <code>loadData</code> to be called on the event dispatch thread, where
  86.      * the model may be safely updated. Progress events may be fired from this
  87.      * method.
  88.      * <p>
  89.      * A final <code>scheduleLoad</code> will be called automatically at the
  90.      * conclusion of the loading process, so it is not technically necessary
  91.      * for this method to call <code>scheduleLoad</code> at all unless you
  92.      * want to support partial loads.
  93.      * 
  94.      * @see #scheduleLoad
  95.      * @throws Exception
  96.      *             if errors occur while reading data
  97.      */
  98.     protected abstract void readData(final DataTable[] tables) throws Exception;
  99.     /**
  100.      * Invoked internally once the <code>readData</code> method calls
  101.      * <code>scheduleLoad</code> to schedule the loading of an increment of
  102.      * data to the DataTable. This method is called on the event dispatch thread,
  103.      * therefore it is safe to mutate the model from this method. Subclasses
  104.      * must implement this method to load the current contents of the
  105.      * disconnected data structure into the DataTable. Note that because there
  106.      * is an unpredictable delay between the time <code>scheduleLoad</code> is
  107.      * called from the &quot;reader&quot; thread and <code>loadData</code>
  108.      * executes on the event dispatch thread, there may be more data available
  109.      * for loading than was available when <code>scheduleLoad</code> was
  110.      * invoked. All available data should be loaded from this method.
  111.      * <p>
  112.      * This method should fire an appropriate progress event to notify progress
  113.      * listeners when:
  114.      * <ul>
  115.      * <li>incremental load occurs(for determinate load operations)</li>
  116.      * <li>load completes</li>
  117.      * <li>exception occurs</li>
  118.      * </ul>
  119.      * </p>
  120.      * 
  121.      */
  122.     protected abstract void loadData(LoadItem[] items);
  123.     /**
  124.      * Invoked by the <code>readData</code> method from the &quot;reader&quot;
  125.      * thread to schedule a subsequent call to <code>loadData</code> on the
  126.      * event dispatch thread. If <code>readData</code> invokes
  127.      * <code>scheduleLoad</code> multiple times before <code>loadData</code>
  128.      * has the opportunity to execute on the event dispatch thread, those
  129.      * requests will be collapsed, resulting in only a single call to
  130.      * <code>loadData</code>.
  131.      * 
  132.      * @see #readData
  133.      * @see #loadData
  134.      */
  135.     protected void scheduleLoad(LoadItem item) {
  136.         synchronized (loadQueue) {
  137.             if (item != null) {
  138.                 loadQueue.addLast(item);
  139.             }
  140.             if (!loadNotifier.isPending()) {
  141.                 loadNotifier.setPending(true);
  142.                 if (loadOnEDT) {
  143.                     SwingUtilities.invokeLater(loadNotifier);
  144.                 } else {
  145.                     loadNotifier.run();
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     /**
  151.      * Same as <code>scheduleLoad(LoadItem)</code>, except that this method
  152.      * will simply schedule a load operation for any remaining items in the
  153.      * queue, but will not add any items to the queue.
  154.      */
  155.     protected void scheduleLoad() {
  156.         scheduleLoad(null);
  157.     }
  158.     /**
  159.      * @inheritDoc
  160.      */
  161.     public String getDescription() {
  162.         return "<html><h3>Loading data</h3></html>";
  163.     }
  164.     /**
  165.      * @inheritDoc
  166.      */
  167.     public Icon getIcon() {
  168.         return null;
  169.     }
  170.     /**
  171.      * @inheritDoc
  172.      */
  173.     public String getMessage() {
  174.         return "Loading item " + (getProgress() + 1) + " of " + getMaximum();
  175.     }
  176.     /**
  177.      * @inheritDoc
  178.      */
  179.     public boolean cancel() throws Exception {
  180.         return false;
  181.     }
  182.     /**
  183.      * Represents a Unit of data, ready to be loaded.
  184.      */
  185.     public static final class LoadItem<E> {
  186.         public DataTable table;
  187.         public E data;
  188.         public LoadItem(DataTable table, E data) {
  189.             this.table = table;
  190.             this.data = data;
  191.         }
  192.     }
  193.     /**
  194.      *
  195.      */
  196.     private class LoadNotifier implements Runnable {
  197.         private boolean pending = false;
  198.         LoadNotifier() {
  199.         }
  200.         public synchronized void setPending(boolean pending) {
  201.             this.pending = pending;
  202.         }
  203.         public synchronized boolean isPending() {
  204.             return pending;
  205.         }
  206.         public void run() {
  207.             synchronized (loadQueue) {
  208.                 if (loadQueue.size() > 0) {
  209.                     LoadItem[] items = (LoadItem[]) loadQueue
  210.                             .toArray(new LoadItem[loadQueue.size()]);
  211.                     loadQueue.clear();
  212.                     loadData(items);
  213.                 }
  214.                 setPending(false);
  215.             }
  216.         }
  217.     }
  218. }