001 /* 002 Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, but 010 WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.aspects.gui; 020 021 import org.apache.log4j.Logger; 022 import org.objectweb.jac.core.Wrappee; 023 import org.objectweb.jac.core.rtti.ClassItem; 024 import org.objectweb.jac.core.rtti.CollectionItem; 025 026 /** 027 * This is an abstract representation of a combo box. */ 028 029 public class ComboBoxModel extends LessAbstractListModel 030 implements ObjectChooserModel 031 { 032 static Logger logger = Logger.getLogger("gui.combobox"); 033 034 /** 035 * The constructor for an independent combobox model. */ 036 public ComboBoxModel() { 037 super(); 038 } 039 040 /** 041 * The constructor for a combobox model that is linked to a 042 * collection (values will be consistent). 043 * 044 * @param collection the substance collection 045 * @param substance the object that holds the collection's value */ 046 public ComboBoxModel(CollectionItem collection, Object substance) { 047 super(collection,substance); 048 } 049 050 /** 051 * Adds an object in the combo box. 052 * 053 * @param object the new object 054 * @param label the associated label 055 */ 056 public void addObject(Object object, String label) { 057 logger.debug("addChoice("+object+" -> "+label+")"); 058 String key = label; 059 int i=2; 060 while (rows.contains(key)) { 061 key = label+"<"+(i++)+">"; 062 } 063 super.addObject(object,key); 064 } 065 066 int selectedIndex = -1; 067 Object selectedObject = null; 068 Object selectedObjectString = null; 069 070 /** 071 * Returns the currently selected object of the combo (same as 072 * <code>getSelectedObject</code>). */ 073 public Object getSelectedItem() { 074 return selectedObjectString; 075 } 076 /** 077 * Sets the selected object by it's name. 078 * @param object name of the object to select (should be a String) 079 * @see #setSelectedObject(Object) 080 */ 081 public void setSelectedItem(Object object) { 082 logger.debug(this+".setSelectedItem("+object+")"); 083 //logger.debug("rows = "+rows); 084 //logger.debug("objects = "+objects); 085 selectedIndex = rows.indexOf(object); 086 selectedObjectString = object; 087 if (selectedIndex!=-1) { 088 selectedObject = objects.get(selectedIndex); 089 } else { 090 if (type!=null && Wrappee.class.isAssignableFrom(type.getActualClass())) 091 throw new RuntimeException("ComboBoxModel: no such element '"+object+"'"); 092 // <HACK> we should transform the string (object) into the correct type 093 selectedObject = object; 094 // </HACK> 095 } 096 logger.debug(" selectedIndex="+selectedIndex); 097 logger.debug(" selectedObject="+selectedObject); 098 logger.debug(" selectedObjectString="+selectedObjectString); 099 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 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 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 }