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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: MessageSourceSupport.java,v 1.3 2005/10/13 08:59:59 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 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.swingx.event;
  22. import javax.swing.event.EventListenerList;
  23. import java.util.logging.Level;
  24. /**
  25.  * A helper class which is an implementation of a message source and a progress
  26.  * source. This class manages the listener list and has convenience methods to
  27.  * fire MessageEvents and ProgressEvents.
  28.  * 
  29.  * @see MessageEvent
  30.  * @see MessageSource
  31.  * @see ProgressEvent
  32.  * @see ProgressSource
  33.  * @author Mark Davidson
  34.  */
  35. public class MessageSourceSupport implements MessageSource, ProgressSource {
  36.     private EventListenerList listeners;
  37.     private Object source;
  38.     /**
  39.      * Creates an MessageSource instance using the source object as the source
  40.      * of events.
  41.      */
  42.     public MessageSourceSupport(Object source) {
  43.         if (source == null) {
  44.             throw new IllegalArgumentException("source Object cannot be null");
  45.         }
  46.         this.source = source;
  47.         this.listeners = new EventListenerList();
  48.     }
  49.     public void addMessageListener(MessageListener l) {
  50.         if (l != null) {
  51.             listeners.add(MessageListener.class, l);
  52.         }
  53.     }
  54.     public void removeMessageListener(MessageListener l) {
  55.         if (l != null) {
  56.             listeners.remove(MessageListener.class, l);
  57.         }
  58.     }
  59.     public MessageListener[] getMessageListeners() {
  60.         return (MessageListener[]) listeners
  61.                 .getListeners(MessageListener.class);
  62.     }
  63.     // Perhaps the ProgressListener interface should be defined
  64.     public void addProgressListener(ProgressListener l) {
  65.         if (l != null) {
  66.             listeners.add(ProgressListener.class, l);
  67.         }
  68.     }
  69.     public void removeProgressListener(ProgressListener l) {
  70.         if (l != null) {
  71.             listeners.remove(ProgressListener.class, l);
  72.         }
  73.     }
  74.     public ProgressListener[] getProgressListeners() {
  75.         return (ProgressListener[]) listeners
  76.                 .getListeners(ProgressListener.class);
  77.     }
  78.     /**
  79.      * Indicates that a long operation is staring. For a determinite progress
  80.      * operation, the minimum value should be less than the maximum value. For
  81.      * inderminate operations, set minimum equal to maximum.
  82.      * 
  83.      * @param minimum the minimum value of the progress operation
  84.      * @param maximum the maximum value of the progress operation
  85.      */
  86.     public void fireProgressStarted(int minimum, int maximum) {
  87.         fireProgressStarted(new ProgressEvent(source, minimum, maximum));
  88.     }
  89.     /**
  90.      * Indicates that an increment of progress has occured.
  91.      * 
  92.      * @param progress total value of the progress operation. This value should
  93.      *        be between the minimum and maximum values
  94.      */
  95.     public void fireProgressIncremented(int progress) {
  96.         fireProgressIncremented(new ProgressEvent(source, progress));
  97.     }
  98.     /**
  99.      * Indicates that a progress operation has completed
  100.      */
  101.     public void fireProgressEnded() {
  102.         fireProgressEnded(new ProgressEvent(source));
  103.     }
  104.     /**
  105.      * Create a SEVERE MessageEvent which represents an Exception.
  106.      * <p>
  107.      * <b>NOTE: This will create an event which is by default at Level.SEVERE</b>
  108.      * 
  109.      * @see java.util.logging.Level
  110.      */
  111.     public void fireException(Throwable t) {
  112.         fireMessage(new MessageEvent(source, t, Level.SEVERE));
  113.     }
  114.     /**
  115.      * Send a Level.INFO, non-timestamped message to the list of listeners.
  116.      * 
  117.      * @param message a string of text to send
  118.      */
  119.     public void fireMessage(String message) {
  120.         fireMessage(message, Level.INFO);
  121.     }
  122.     /**
  123.      * Send a non-timestamped message to the list of listeners.
  124.      * 
  125.      * @param value the contents of the message
  126.      * @param level the level of message.
  127.      */
  128.     public void fireMessage(Object value, Level level) {
  129.         fireMessage(value, level, 0L);
  130.     }
  131.     /**
  132.      * Send a message to the list of listeners with a timestamp.
  133.      * 
  134.      * @param value the contents of the message
  135.      * @param level the level of message
  136.      * @param when timestamp of when this event occured
  137.      */
  138.     public void fireMessage(Object value, Level level, long when) {
  139.         fireMessage(new MessageEvent(source, value, level, when));
  140.     }
  141.     /**
  142.      * Send the MessageEvent to the list of listeners.
  143.      * 
  144.      * @param evt a non-null MessageEvent.
  145.      */
  146.     public void fireMessage(MessageEvent evt) {
  147.         if (evt == null) {
  148.             throw new IllegalArgumentException("the event should not be null");
  149.         }
  150.         MessageListener[] ls = getMessageListeners();
  151.         for (int i = 0; i < ls.length; i++) {
  152.             ls[i].message(evt);
  153.         }
  154.     }
  155.     /**
  156.      * Send the ProgessEvent to the list of listeners.
  157.      * 
  158.      * @param evt a non-null ProgressEvent
  159.      */
  160.     public void fireProgressIncremented(ProgressEvent evt) {
  161.         if (evt == null) {
  162.             throw new IllegalArgumentException("the event should not be null");
  163.         }
  164.         ProgressListener[] ls = getProgressListeners();
  165.         for (int i = 0; i < ls.length; i++) {
  166.             ls[i].progressIncremented(evt);
  167.         }
  168.     }
  169.     /**
  170.      * Send the ProgessEvent to the list of listeners.
  171.      * 
  172.      * @param evt a non-null ProgressEvent
  173.      */
  174.     public void fireProgressStarted(ProgressEvent evt) {
  175.         if (evt == null) {
  176.             throw new IllegalArgumentException("the event should not be null");
  177.         }
  178.         ProgressListener[] ls = getProgressListeners();
  179.         for (int i = 0; i < ls.length; i++) {
  180.             ls[i].progressStarted(evt);
  181.         }
  182.     }
  183.     /**
  184.      * Send the ProgessEvent to the list of listeners.
  185.      * 
  186.      * @param evt a non-null ProgressEvent
  187.      */
  188.     public void fireProgressEnded(ProgressEvent evt) {
  189.         if (evt == null) {
  190.             throw new IllegalArgumentException("the event should not be null");
  191.         }
  192.         ProgressListener[] ls = getProgressListeners();
  193.         for (int i = 0; i < ls.length; i++) {
  194.             ls[i].progressEnded(evt);
  195.         }
  196.     }
  197. }