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 org.objectweb.jac.core.rtti.CollectionItem;
022    import org.objectweb.jac.core.rtti.FieldItem;
023    import org.objectweb.jac.core.rtti.MethodItem;
024    import org.objectweb.jac.core.rtti.RttiAC;
025    import org.objectweb.jac.util.Strings;
026    import java.io.PrintWriter;
027    
028    /**
029     * A reference editor that uses the value of an index field to select
030     * objects.
031     */
032    public class IndexSelector extends AbstractFieldEditor 
033        implements HTMLEditor
034    {
035        CollectionItem index;
036        FieldItem indexedField;
037        Object repository;
038        String key;
039        MethodItem indexNotFoundHandler;
040    
041        public IndexSelector(Object substance, FieldItem field, 
042                             CollectionItem index, Object repository, 
043                             boolean allowCreation,
044                             MethodItem indexNotFoundHandler) {
045            super(substance,field);
046            this.index = index;
047            this.repository = repository;
048            indexedField = (FieldItem)index.getAttribute(RttiAC.INDEXED_FIELD);
049            this.indexNotFoundHandler = indexNotFoundHandler;
050            updateKey();
051        }
052    
053        public void setValue(Object value) {
054            super.setValue(value);
055            updateKey();
056        }
057    
058        protected void updateKey() {
059            if (indexedField!=null && value!=null) {
060                key = indexedField.getThroughAccessor(value).toString();
061            }
062        }
063    
064        // HTMLEditor interface
065    
066        public void genHTML(PrintWriter out) {
067            out.print("<input type=\"text\" name=\""+label+
068                      "\" size=\"12\" style=\"width:12ex\""+
069                      " value=\""+(key!=null?key:"")+"\"");
070            printAttributes(out);
071            out.println(">");
072        }
073    
074        protected boolean doReadValue(Object parameter) {
075            key = (String)parameter;
076            if (Strings.isEmpty(key)) {
077                setValue(null);
078            } else {
079                Object value = index.getMap(repository,key);
080                if (value!=null) {
081                    setValue(value);
082                } else {
083                    if (indexNotFoundHandler!=null && field!=null) {
084                        value = indexNotFoundHandler.invokeStatic(
085                            new Object[] {field.getTypeItem(),key});
086                    }
087                    setValue(value);
088                }
089    
090            }
091            return true;
092        }
093    
094    
095    }
096