001 /* 002 Copyright (C) 2003 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.web; 020 021 import java.io.PrintWriter; 022 import org.apache.log4j.Logger; 023 import org.objectweb.jac.aspects.gui.CollectionModel; 024 import org.objectweb.jac.aspects.gui.DisplayContext; 025 import org.objectweb.jac.aspects.gui.FieldEditor; 026 import org.objectweb.jac.aspects.gui.GuiAC; 027 import org.objectweb.jac.aspects.gui.ListModel; 028 import org.objectweb.jac.aspects.gui.ViewFactory; 029 import org.objectweb.jac.core.Collaboration; 030 import org.objectweb.jac.core.rtti.ClassItem; 031 import org.objectweb.jac.core.rtti.CollectionItem; 032 import org.objectweb.jac.core.rtti.FieldItem; 033 import org.objectweb.jac.core.rtti.MethodItem; 034 import org.objectweb.jac.core.rtti.RttiAC; 035 import org.objectweb.jac.util.Strings; 036 037 /** 038 * A collection editor that uses the value of an index field to select 039 * objects. 040 */ 041 public class IndicesSelector extends AbstractCollection 042 implements FieldEditor, HTMLEditor 043 { 044 static Logger logger = Logger.getLogger("gui.editor"); 045 046 CollectionItem index; 047 Object repository; 048 FieldItem indexedField; 049 MethodItem indexNotFoundHandler; 050 String indices; 051 ClassItem componentType; 052 ClassItem type; 053 054 public IndicesSelector(ViewFactory factory, DisplayContext context, 055 CollectionItem collection, Object substance, 056 CollectionModel model, 057 org.objectweb.jac.aspects.gui.CollectionItemView itemView) { 058 super(factory,context,collection,substance,model,itemView); 059 componentType = collection.getComponentType(); 060 this.index = (CollectionItem) 061 componentType.getAttribute(GuiAC.INDEXED_FIELD_SELECTOR); 062 this.repository = GuiAC.getRepository(componentType); 063 indexedField = (FieldItem)index.getAttribute(RttiAC.INDEXED_FIELD); 064 this.indexNotFoundHandler = (MethodItem) 065 componentType.getAttribute(GuiAC.INDEX_NOT_FOUND_HANDLER); 066 indices = objectsToString(); 067 } 068 069 public void setEditedType(ClassItem type) { 070 this.type = type; 071 } 072 073 public void sort() { 074 } 075 076 public void updateModel(Object substance) { 077 if (model!=null) 078 model.close(); 079 model = new ListModel(collection,substance); 080 indices = objectsToString(); 081 } 082 083 public void commit() { 084 logger.debug(this+": "+collection.getName()+ 085 "'s value changed: "); 086 collection.clear(substance); 087 String[] keys = Strings.split(indices," "); 088 for (int i=0; i<keys.length; i++) { 089 if (Strings.isEmpty(keys[i].trim())) 090 continue; 091 Object value = index.getMap(repository,keys[i]); 092 if (value==null) { 093 if (indexNotFoundHandler!=null) { 094 value = indexNotFoundHandler.invokeStatic( 095 new Object[] {componentType,keys[i]}); 096 } 097 } 098 if (value!=null) { 099 collection.addThroughAdder(substance,value); 100 } else { 101 logger.warn("No such "+collection.getComponentType()+ 102 " with "+indexedField.getName()+"="+keys[i]); 103 } 104 } 105 } 106 107 // FieldEditor interface 108 109 public Object getValue() { 110 return null; 111 } 112 113 public void setEmbedded(boolean embedded) { 114 } 115 116 public void onSetFocus(Object param) { 117 } 118 119 // HTMLEditor interface 120 121 public String objectsToString() { 122 StringBuffer res = new StringBuffer(); 123 for (int i=0; i<model.getRowCount(); i++) { 124 if (i!=0) 125 res.append(" "); 126 res.append(indexedField.getThroughAccessor(model.getObject(i)).toString()); 127 } 128 return res.toString(); 129 } 130 131 public void genHTML(PrintWriter out) { 132 out.print("<input type=\"text\" name=\""+label+ 133 "\" size=\"20\" style=\"width:20ex\""+ 134 " value=\""+indices+"\""); 135 printAttributes(out); 136 out.println(">"); 137 } 138 139 public boolean readValue(Object parameter) { 140 indices = (String)parameter; 141 /* 142 key = (String)parameter; 143 if (Strings.isEmpty(key)) 144 setValue(null); 145 else 146 setValue(index.getMap(repository,key)); 147 */ 148 return true; 149 } 150 151 152 } 153