KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
22 import org.objectweb.jac.core.Wrappee;
23 import org.objectweb.jac.core.rtti.ClassItem;
24 import org.objectweb.jac.core.rtti.CollectionItem;
25
26 /**
27  * This is an abstract representation of a combo box. */

28
29 public class ComboBoxModel extends LessAbstractListModel
30     implements ObjectChooserModel
31 {
32     static Logger logger = Logger.getLogger("gui.combobox");
33
34     /**
35      * The constructor for an independent combobox model. */

36     public ComboBoxModel() {
37         super();
38     }
39
40     /**
41      * The constructor for a combobox model that is linked to a
42      * collection (values will be consistent).
43      *
44      * @param collection the substance collection
45      * @param substance the object that holds the collection's value */

46     public ComboBoxModel(CollectionItem collection, Object JavaDoc substance) {
47         super(collection,substance);
48     }
49
50     /**
51      * Adds an object in the combo box.
52      *
53      * @param object the new object
54      * @param label the associated label
55      */

56     public void addObject(Object JavaDoc object, String JavaDoc label) {
57         logger.debug("addChoice("+object+" -> "+label+")");
58         String JavaDoc key = label;
59         int i=2;
60         while (rows.contains(key)) {
61             key = label+"<"+(i++)+">";
62         }
63         super.addObject(object,key);
64     }
65
66     int selectedIndex = -1;
67     Object JavaDoc selectedObject = null;
68     Object JavaDoc selectedObjectString = null;
69
70     /**
71      * Returns the currently selected object of the combo (same as
72      * <code>getSelectedObject</code>). */

73     public Object JavaDoc getSelectedItem() {
74         return selectedObjectString;
75     }
76     /**
77      * Sets the selected object by it's name.
78      * @param object name of the object to select (should be a String)
79      * @see #setSelectedObject(Object)
80      */

81     public void setSelectedItem(Object JavaDoc object) {
82         logger.debug(this+".setSelectedItem("+object+")");
83         //logger.debug("rows = "+rows);
84
//logger.debug("objects = "+objects);
85
selectedIndex = rows.indexOf(object);
86         selectedObjectString = object;
87         if (selectedIndex!=-1) {
88             selectedObject = objects.get(selectedIndex);
89         } else {
90             if (type!=null && Wrappee.class.isAssignableFrom(type.getActualClass()))
91                 throw new RuntimeException JavaDoc("ComboBoxModel: no such element '"+object+"'");
92             // <HACK> we should transform the string (object) into the correct type
93
selectedObject = object;
94             // </HACK>
95
}
96         logger.debug(" selectedIndex="+selectedIndex);
97         logger.debug(" selectedObject="+selectedObject);
98         logger.debug(" selectedObjectString="+selectedObjectString);
99         fireContentsChanged(this,-1,-1);
100     }
101
102     /**
103      * Sets the selected object
104      * @param object the object to select
105      * @see #setSelectedItem(Object)
106      */

107     public void setSelectedObject(Object JavaDoc object) {
108         logger.debug(this+".setSelectedObject("+object+")");
109         //logger.debug("rows = "+rows);
110
//logger.debug("objects = "+objects);
111
selectedIndex = objects.indexOf(object);
112         selectedObject = object;
113         if (selectedIndex!=-1)
114             selectedObjectString = rows.get(selectedIndex);
115         else
116             selectedObjectString = GuiAC.toString(object);
117         logger.debug(" selectedIndex="+selectedIndex);
118         logger.debug(" selectedObject="+selectedObject);
119         logger.debug(" selectedObjectString="+selectedObjectString);
120         fireContentsChanged(this,-1,-1);
121     }
122
123     /**
124      * Returns the currently selected object of the combo (same as
125      * <code>getSelectedItem</code>). */

126     public Object JavaDoc getSelectedObject() {
127         return selectedObject;
128     }
129
130     // ObjectChooserModel interface
131

132     ClassItem type;
133     public void setType(ClassItem type) {
134         this.type = type;
135     }
136     public ClassItem getType() {
137         return type;
138     }
139 }
140
Popular Tags