Package jsynoptic.plugins.jfreechart

Source Code of jsynoptic.plugins.jfreechart.CategoryClassifier

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*
* $Id: CategoryClassifier.java,v 1.6 2007/11/08 14:39:07 ogor Exp $
*
* Changes
* -------
* 06-Nov-2003 : Initial version (NB);
*
*/
package jsynoptic.plugins.jfreechart;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import simtools.ui.GenericMapper;
import simtools.ui.MapperListener;
import simtools.ui.TextMapper;
import simtools.util.ListenerManager;


public class CategoryClassifier implements Serializable, MapperListener {

  static final long serialVersionUID = 4459343875031474989L;
 
  public List categories; // of String
  protected TextMapper mapper;
  protected transient ListenerManager listeners = new ListenerManager();

  public void addListener(CategoryClassifierListener l) {
    listeners.add(l);
  }
 
  public void removeListener(CategoryClassifierListener l) {
    listeners.remove(l);
  }
 
  protected void notifyListeners() {
      synchronized(listeners) {
        int n = listeners.size(); // only one call outside loop
        for (int i=0; i<n; ++i) {
            CategoryClassifierListener ccl = (CategoryClassifierListener)listeners.get(i);
            if (ccl!=null) ccl.classifierModified(this);
        }
      }
  }
 
  /**
   * @return Returns the mapper.
   */
  public TextMapper getMapper() {
    return mapper;
  }
 
  /**
   * @param mapper The mapper to set.
   */
  public void setMapper(TextMapper mapper) {
    if (this.mapper!=null) this.mapper.removeListener(this);
    this.mapper = mapper;
    if (this.mapper!=null) this.mapper.addListener(this);
    mappingChanged(mapper);
    notifyListeners();
  }
 
  /**
   * @param name
   * @param index
   */
  public CategoryClassifier(String name) {
    this(new TextMapper(name));
  }

  /**
   * @param name
   * @param index
   */
  public CategoryClassifier(TextMapper mapper) {
    this.mapper = mapper;
    mapper.addListener(this);
    mappingChanged(mapper); // just so categories are defined, even if empty
  }

  /* (non-Javadoc)
   * @see simtools.ui.MapperListener#mappingChanged(simtools.ui.GenericMapper)
   */
  public void mappingChanged(GenericMapper mapper) {
   
    if (mapper==null) {
      categories = new Vector();
      return;
    }
   
    // this is handy because it is called each time the mapping changed
    // => use the occasion to re-create the categories
   
    // Make sure we have unique categories by using a Set
    HashSet set = new HashSet();

    // Add all the single mappings
    for (Iterator it = mapper.getMap().entrySet().iterator(); it.hasNext();)  {
      set.add(((Map.Entry)it.next()).getValue());
    }

    // Add all the interval mappings
    for (Iterator it = mapper.getIntervals().iterator(); it.hasNext();)  {
      set.add(((TextMapper.Interval)it.next()).value);
    }

    // Now sort strings
    Object[] a = set.toArray();
    Arrays.sort(a);
 
    // and store them in order
    categories = Arrays.asList(a);

    notifyListeners();
  }
 
}
TOP

Related Classes of jsynoptic.plugins.jfreechart.CategoryClassifier

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.