KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > LessAbstractListModel


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25 import javax.swing.AbstractListModel JavaDoc;
26 import org.apache.log4j.Logger;
27 import org.objectweb.jac.core.rtti.CollectionItem;
28 import org.objectweb.jac.util.Stack;
29
30 /**
31  * Base class for ListModel and ComboBoxModel.
32  */

33 public abstract class LessAbstractListModel extends AbstractListModel JavaDoc
34     implements ObjectUpdate, CollectionModel, CollectionUpdate
35 {
36     static Logger logger = Logger.getLogger("gui.model");
37
38     CollectionItem collection=null;
39     Object JavaDoc substance;
40
41     List JavaDoc rows = new Vector JavaDoc();
42     List JavaDoc objects = new Vector JavaDoc();
43
44     Stack context = new Stack();
45
46     /**
47      * Construct a new abstract list model which is independent from
48      * any collection. */

49     public LessAbstractListModel() {
50         if (GuiAC.getGraphicContext()!=null)
51             context.addAll(GuiAC.getGraphicContext());
52     }
53
54     /**
55      * Construct a new abstract list model in which the values depend
56      * on a collection's values.
57      *
58      * @param collection the substance collection
59      * @param substance the object that holds the collection's value */

60     public LessAbstractListModel(CollectionItem collection, Object JavaDoc substance) {
61         if (GuiAC.getGraphicContext()!=null)
62             context.addAll(GuiAC.getGraphicContext());
63        
64         this.collection = collection;
65         this.substance = substance;
66
67         buildData();
68         Utils.registerCollection(substance,collection,this);
69     }
70
71     void buildData() {
72         Collection JavaDoc c = collection.getActualCollectionThroughAccessor(substance);
73         if (c!=null) {
74             logger.debug("buildData for "+substance+"."+collection.getName());
75             logger.debug("objects : " + new Vector JavaDoc(c));
76             Iterator JavaDoc i = c.iterator();
77             while (i.hasNext()) {
78                 Object JavaDoc obj = i.next();
79                 logger.debug("add "+obj);
80                 addObject(obj);
81             }
82         }
83     }
84
85     public CollectionItem getCollection() {
86         return collection;
87     }
88
89     String JavaDoc nullLabel;
90     /**
91      * Sets the default label to be used for a null reference.
92      * @param label label for null
93      */

94     public void setNullLabel(String JavaDoc label) {
95         this.nullLabel = label;
96     }
97
98     /**
99      * Adds an object in the list. Uses GuiAC.toString() to get a string
100      * representation of the object.
101      * @param object the object to add
102      * @see #addObject(Object,String)
103      * @see #setNullLabel(String)
104      */

105     public void addObject(Object JavaDoc object) {
106         addObject(object,
107                   object==null ?
108                       (nullLabel!=null ? nullLabel : GuiAC.getLabelNone())
109                     : GuiAC.toString(object,context));
110     }
111
112     /**
113      * Add an object in the list with a given label to be displayed.
114      * @param object the object to add
115      * @param label the label to be displayed for the object
116      * @see #addObject(Object)
117      */

118     public void addObject(Object JavaDoc object, String JavaDoc label) {
119         objects.add(object);
120         rows.add(label);
121         fireIntervalAdded(this,objects.size()-1,objects.size()-1);
122         Utils.registerObject(object,this);
123     }
124
125     /**
126      * Gets the list element count. */

127     public int getRowCount() {
128         return rows.size();
129     }
130
131     /**
132      * Returns the element at a given row. */

133     public Object JavaDoc getElementAt(int row) {
134         logger.debug("getElementAt("+row+") -> "+rows.get(row));
135         return rows.get(row);
136     }
137
138     /**
139      * Gets the list size (same as <code>getRowCount</code>). */

140     public int getSize() {
141         return getRowCount();
142     }
143
144     /**
145      * Gets the object at a given index. */

146     public Object JavaDoc getObject(int index) {
147         return objects.get(index);
148     }
149
150     public int indexOf(Object JavaDoc object) {
151         return objects.indexOf(object);
152     }
153
154     /**
155      * Tells if this cell is directly editable (always returns false
156      * for the moment). */

157
158     public boolean isCellEditable(int row, int column) {
159         return false;
160     }
161
162     // ObjectUpdate interface
163
public void objectUpdated(Object JavaDoc substance,Object JavaDoc param) {
164         int index = objects.indexOf(substance);
165         if (index!=-1) {
166             rows.set(index,GuiAC.toString(objects.get(index),context));
167             fireContentsChanged(this,index,index);
168         }
169     }
170
171     private int getMin(String JavaDoc row, Object JavaDoc obj)
172     {
173         int min = 0;
174         for (int i = 1; i < rows.size(); i++)
175         {
176             if ((((String JavaDoc) (((Vector JavaDoc) rows).elementAt(i)))
177                  .compareToIgnoreCase((String JavaDoc) (((Vector JavaDoc) rows).elementAt(min))))
178                 < 0)
179                 min = i;
180         }
181         return min;
182     }
183
184     /**
185      * sort the list alphabetically by label
186      */

187     public void sort()
188     {
189         Vector JavaDoc newRows = new Vector JavaDoc();
190         Vector JavaDoc newObjs = new Vector JavaDoc();
191       
192         Object JavaDoc obj = null;
193         String JavaDoc row = null;
194
195         while (rows.size() > 0)
196         {
197             int min = getMin(row, obj);
198             row = (String JavaDoc) ((Vector JavaDoc) rows).elementAt(min);
199             obj = ((Vector JavaDoc) objects).elementAt(min);
200             newRows.add(row);
201             newObjs.add(obj);
202             rows.remove(row);
203             objects.remove(obj);
204         }
205         rows = newRows;
206         objects = newObjs;
207     }
208
209
210     /**
211      * Unregister ourself as a view on all objects of the collection
212      */

213     protected void unregisterViews() {
214         logger.debug("TableModel.unRegisterViews "+objects.size());
215         Iterator JavaDoc i = objects.iterator();
216         while (i.hasNext()) {
217             Object JavaDoc object = i.next();
218             Utils.unregisterObject(object,this);
219         }
220     }
221
222     public void close() {
223         unregisterViews();
224         if(collection!=null) {
225             Utils.unregisterCollection(substance,collection,this);
226         }
227     }
228
229     // CollectionUpdate
230
public void onChange(Object JavaDoc substance, CollectionItem collection,
231                          Object JavaDoc value, Object JavaDoc param) {
232         unregisterViews();
233         int size = objects.size();
234         objects.clear();
235         rows.clear();
236         if (size>0)
237             fireIntervalRemoved(this,0,size-1);
238         buildData();
239         if (!objects.isEmpty())
240             fireIntervalAdded(this, 0, objects.size()-1);
241     }
242
243     public void onAdd(Object JavaDoc substance, CollectionItem collection,
244                       Object JavaDoc value, Object JavaDoc added, Object JavaDoc param) {
245         onChange(substance,collection,value,param);
246     }
247
248     public void onRemove(Object JavaDoc substance, CollectionItem collection,
249                          Object JavaDoc value, Object JavaDoc removed, Object JavaDoc param) {
250         onChange(substance,collection,value,param);
251     }
252
253
254 }
255
Popular Tags