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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: HighlighterPipeline.java,v 1.9 2005/10/13 08:59:53 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.decorator;
  22. import java.awt.Component;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import javax.swing.BoundedRangeModel;
  27. import javax.swing.JTable;
  28. import javax.swing.event.ChangeEvent;
  29. import javax.swing.event.ChangeListener;
  30. import javax.swing.event.EventListenerList;
  31. /**
  32.  * A class which manages the lists of highlighters.
  33.  *
  34.  * @see Highlighter
  35.  *
  36.  * @author Ramesh Gupta
  37.  * @author Jeanette Winzenburg
  38.  * 
  39.  */
  40. public class HighlighterPipeline {
  41.     protected transient ChangeEvent changeEvent = null;
  42.     protected EventListenerList listenerList = new EventListenerList();
  43.     protected List<Highlighter> highlighters;
  44.     private final static Highlighter nullHighlighter = new Highlighter(null, null);
  45.     private ChangeListener highlighterChangeListener;
  46.     public HighlighterPipeline() {
  47.         highlighters = new ArrayList<Highlighter>();
  48.     }
  49.     
  50.     /**
  51.      * 
  52.      * @param inList the array of highlighters to initially add to this.
  53.      * @throws NullPointerException if array is null of array contains null values.
  54.      */
  55.     public HighlighterPipeline(Highlighter[] inList) {
  56.         this();
  57.         for (int i = 0; i < inList.length; i++) {
  58.             addHighlighter(inList[i]);
  59.         }
  60.     }
  61.     /**
  62.      * Appends a highlighter to the pipeline.
  63.      *
  64.      * @param hl highlighter to add
  65.       * @throws NullPointerException if highlighter is null.
  66.     */
  67.     public void addHighlighter(Highlighter hl) {
  68.         addHighlighter(hl, false);
  69.     }
  70.     /**
  71.      * Adds a highlighter to the pipeline.
  72.      *
  73.      * PENDING: Duplicate inserts?
  74.      * 
  75.      * @param hl highlighter to add
  76.      * @param prepend prepend the highlighter if true; false will append
  77.      * @throws NullPointerException if highlighter is null.
  78.      */
  79.     public void addHighlighter(Highlighter hl, boolean prepend) {
  80.         if (prepend) {
  81.             highlighters.add(0, hl);
  82.         } else {
  83.             highlighters.add(highlighters.size(), hl);
  84.         }
  85.         hl.addChangeListener(getHighlighterChangeListener());
  86.         fireStateChanged();
  87.     }
  88.     private ChangeListener getHighlighterChangeListener() {
  89.         if (highlighterChangeListener == null) {
  90.             highlighterChangeListener = new ChangeListener() {
  91.                 public void stateChanged(ChangeEvent e) {
  92.                     fireStateChanged();
  93.                     
  94.                 }
  95.                 
  96.             };
  97.         }
  98.         return highlighterChangeListener;
  99.     }
  100.     /**
  101.      * Removes a highlighter from the pipeline.
  102.      *
  103.      *  
  104.      * @param hl highlighter to remove
  105.      */
  106.     public void removeHighlighter(Highlighter hl) {
  107.         boolean success = highlighters.remove(hl);
  108.         if (success) {
  109.             // PENDING: duplicates?
  110.             hl.removeChangeListener(getHighlighterChangeListener());
  111.             fireStateChanged();
  112.         }
  113.         // should log if this didn't succeed. Maybe
  114.     }
  115.     public Highlighter[] getHighlighters() {
  116.         return (Highlighter[])highlighters.toArray(new Highlighter[highlighters.size()]);
  117.     }
  118.     /**
  119.      * Applies all the highlighters to the components.
  120.      * 
  121.      * @throws NullPointerException if either stamp or adapter is null.
  122.      */
  123.     public Component apply(Component stamp, ComponentAdapter adapter) {
  124.         //JW
  125.         // table renderers have different state memory as renderers
  126.         // without the null they don't unstamp!
  127.         // but... null has adversory effect on JXList f.i. - selection
  128.         // color is changed
  129.         // 
  130.         if (adapter.getComponent() instanceof JTable) {
  131.         /** @todo optimize the following bug fix */
  132.             stamp = nullHighlighter.highlight(stamp, adapter);      // fixed bug from M1
  133.         }
  134.         for (Iterator<Highlighter> iter = highlighters.iterator(); iter.hasNext();) {
  135.             stamp = iter.next().highlight(stamp, adapter);
  136.             
  137.         }
  138.         return stamp;
  139.     }
  140.     /**
  141.      * Adds a <code>ChangeListener</code>.  The change listeners are run each
  142.      * time any one of the Bounded Range model properties changes.
  143.      *
  144.      * @param l the ChangeListener to add
  145.      * @see #removeChangeListener
  146.      * @see BoundedRangeModel#addChangeListener
  147.      */
  148.     public void addChangeListener(ChangeListener l) {
  149.         listenerList.add(ChangeListener.class, l);
  150.     }
  151.     
  152.     /**
  153.      * Removes a <code>ChangeListener</code>.
  154.      *
  155.      * @param l the <code>ChangeListener</code> to remove
  156.      * @see #addChangeListener
  157.      * @see BoundedRangeModel#removeChangeListener
  158.      */
  159.     public void removeChangeListener(ChangeListener l) {
  160.         listenerList.remove(ChangeListener.class, l);
  161.     }
  162.     /**
  163.      * Returns an array of all the change listeners
  164.      * registered on this <code>DefaultBoundedRangeModel</code>.
  165.      *
  166.      * @return all of this model's <code>ChangeListener</code>s 
  167.      *         or an empty
  168.      *         array if no change listeners are currently registered
  169.      *
  170.      * @see #addChangeListener
  171.      * @see #removeChangeListener
  172.      *
  173.      * @since 1.4
  174.      */
  175.     public ChangeListener[] getChangeListeners() {
  176.         return (ChangeListener[])listenerList.getListeners(
  177.                 ChangeListener.class);
  178.     }
  179.     /** 
  180.      * Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method.
  181.      * 
  182.      * @see #setRangeProperties
  183.      * @see EventListenerList
  184.      */
  185.     protected void fireStateChanged() 
  186.     {
  187.         Object[] listeners = listenerList.getListenerList();
  188.         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
  189.             if (listeners[i] == ChangeListener.class) {
  190.                 if (changeEvent == null) {
  191.                     changeEvent = new ChangeEvent(this);
  192.                 }
  193.                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  194.             }          
  195.         }
  196.     }   
  197. }