KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > IndexSelector


1 /*
2   Copyright (C) 2003 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.web;
20
21 import org.objectweb.jac.core.rtti.CollectionItem;
22 import org.objectweb.jac.core.rtti.FieldItem;
23 import org.objectweb.jac.core.rtti.MethodItem;
24 import org.objectweb.jac.core.rtti.RttiAC;
25 import org.objectweb.jac.util.Strings;
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * A reference editor that uses the value of an index field to select
30  * objects.
31  */

32 public class IndexSelector extends AbstractFieldEditor
33     implements HTMLEditor
34 {
35     CollectionItem index;
36     FieldItem indexedField;
37     Object JavaDoc repository;
38     String JavaDoc key;
39     MethodItem indexNotFoundHandler;
40
41     public IndexSelector(Object JavaDoc substance, FieldItem field,
42                          CollectionItem index, Object JavaDoc repository,
43                          boolean allowCreation,
44                          MethodItem indexNotFoundHandler) {
45         super(substance,field);
46         this.index = index;
47         this.repository = repository;
48         indexedField = (FieldItem)index.getAttribute(RttiAC.INDEXED_FIELD);
49         this.indexNotFoundHandler = indexNotFoundHandler;
50         updateKey();
51     }
52
53     public void setValue(Object JavaDoc value) {
54         super.setValue(value);
55         updateKey();
56     }
57
58     protected void updateKey() {
59         if (indexedField!=null && value!=null) {
60             key = indexedField.getThroughAccessor(value).toString();
61         }
62     }
63
64     // HTMLEditor interface
65

66     public void genHTML(PrintWriter JavaDoc out) {
67         out.print("<input type=\"text\" name=\""+label+
68                   "\" size=\"12\" style=\"width:12ex\""+
69                   " value=\""+(key!=null?key:"")+"\"");
70         printAttributes(out);
71         out.println(">");
72     }
73
74     protected boolean doReadValue(Object JavaDoc parameter) {
75         key = (String JavaDoc)parameter;
76         if (Strings.isEmpty(key)) {
77             setValue(null);
78         } else {
79             Object JavaDoc value = index.getMap(repository,key);
80             if (value!=null) {
81                 setValue(value);
82             } else {
83                 if (indexNotFoundHandler!=null && field!=null) {
84                     value = indexNotFoundHandler.invokeStatic(
85                         new Object JavaDoc[] {field.getTypeItem(),key});
86                 }
87                 setValue(value);
88             }
89
90         }
91         return true;
92     }
93
94
95 }
96
97
Popular Tags