source: branches/MetisMQI/src/main/java/weka/gui/ensembleLibraryEditor/ModelList.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.9 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    ModelList.java
19 *    Copyright (C) 2006 Robert Jung
20 *
21 */
22
23package weka.gui.ensembleLibraryEditor;
24
25import weka.classifiers.EnsembleLibraryModel;
26import weka.classifiers.EnsembleLibraryModelComparator;
27
28import java.awt.Color;
29import java.awt.Component;
30import java.util.Arrays;
31import java.util.Collection;
32import java.util.Iterator;
33import java.util.SortedSet;
34import java.util.TreeSet;
35
36import javax.swing.AbstractListModel;
37import javax.swing.DefaultListCellRenderer;
38import javax.swing.JComponent;
39import javax.swing.JList;
40
41/**
42 * This class is basically a customization of the JList class to allow it
43 * to display LibraryModel objects.  It has two nested helper classes that
44 * respectively take care of rendering and modelling the List of models.
45 *
46 * @author Robert Jung (mrbobjung@gmail.com)
47 */
48public class ModelList 
49  extends JList {
50 
51  /** for serialization */
52  private static final long serialVersionUID = -421567241792939539L;
53
54  /**
55   * The constructor simply initializes the model and the renderer.
56   */
57  public ModelList() {
58    super();
59    this.setModel(new SortedListModel());
60    this.setCellRenderer(new ModelListRenderer());
61  }
62 
63  /**
64   * This nested helper class is responsible for rendering each Library
65   * Model object. 
66   */
67  public class ModelListRenderer 
68    extends DefaultListCellRenderer {
69   
70    /** for serialization */
71    private static final long serialVersionUID = -7061163240718897794L;
72
73    /**
74     * This is the only method necessary to overload.  All we have to
75     * do is print the String value of the model along with its index
76     * in the ModelList data structure.
77     *
78     * @param list              the JList
79     * @param value             the value
80     * @param index             the index of the value
81     * @param isSelected        if true the item is selected
82     * @param cellHasFocus      whether it has the focus
83     * @return                  the rendering component
84     */
85    public Component getListCellRendererComponent(JList list, Object value,
86        int index, boolean isSelected, boolean cellHasFocus) {
87     
88      Component modelComponent = null;
89     
90      if (value instanceof EnsembleLibraryModel) {
91       
92        EnsembleLibraryModel model = ((EnsembleLibraryModel) value);
93       
94        String modelString = index
95        + ": "
96        + model.getStringRepresentation().replaceAll(
97            "weka.classifiers.", "");
98       
99        modelComponent = super.getListCellRendererComponent(list,
100            modelString, index, isSelected, cellHasFocus);
101       
102        if (!model.getOptionsWereValid()) {
103          modelComponent.setBackground(Color.pink);
104        }
105       
106        ((JComponent) modelComponent).setToolTipText(model
107            .getDescriptionText());
108       
109      }
110     
111      return modelComponent;
112    }
113  }
114 
115  /**
116   *
117   * This is a helper class that creates a custom list model for the ModelList class.
118   * It basically ensures that all model entries are 1) unique - so that no duplicate
119   * entries can find their way in, and 2) sorted alphabetically.  It also numbers
120   * them.
121   * <p/>
122   * This nested class was adapted from code found in a freely available tutorial on
123   * sorting JList entries by John Zukowski - wait a sec, he's the guy who wrote the
124   * other tutorial I cited in the AddModelsPanel. wow, different web site even.
125   * This guy is really in to writing tutorials.  Anyway, it was very helpful, if
126   * you would like to know more about implementing swing MVC stuff.
127   * <p/>
128   * Anyway, John Zukowski's tutorial can be found at: <br/>
129   * <a href="http://www.jguru.com/faq/view.jsp?EID=15245" target="_blank">http://www.jguru.com/faq/view.jsp?EID=15245</a>
130   */
131  public class SortedListModel extends AbstractListModel {
132   
133    /** for serialization */
134    private static final long serialVersionUID = -8334675481243839371L;
135   
136    /** Define a SortedSet */
137    SortedSet m_Models;
138   
139    /**
140     * default constructor
141     */
142    public SortedListModel() {
143      // Create a TreeSet
144      // Store it in SortedSet variable
145      m_Models = new TreeSet(new EnsembleLibraryModelComparator());
146    }
147   
148    // ListModel methods
149    public int getSize() {
150      // Return the model size
151      return m_Models.size();
152    }
153   
154    public Object getElementAt(int index) {
155      // Return the appropriate element
156      return m_Models.toArray()[index];
157    }
158   
159    // Other methods
160    public void add(Object element) {
161      if (m_Models.add(element)) {
162        fireContentsChanged(this, 0, getSize());
163      }
164    }
165   
166    public void addAll(Object elements[]) {
167      Collection c = Arrays.asList(elements);
168      m_Models.addAll(c);
169      fireContentsChanged(this, 0, getSize());
170    }
171   
172    public void clear() {
173      m_Models.clear();
174      fireContentsChanged(this, 0, getSize());
175    }
176   
177    public boolean contains(Object element) {
178      return m_Models.contains(element);
179    }
180   
181    public Object firstElement() {
182      // Return the appropriate element
183      return m_Models.first();
184    }
185   
186    public Iterator iterator() {
187      return m_Models.iterator();
188    }
189   
190    public Object lastElement() {
191      // Return the appropriate element
192      return m_Models.last();
193    }
194   
195    public boolean removeElement(Object element) {
196      boolean removed = m_Models.remove(element);
197      if (removed) {
198        fireContentsChanged(this, 0, getSize());
199      }
200      return removed;
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.