KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
22 import org.apache.log4j.Logger;
23 import org.objectweb.jac.aspects.gui.CollectionModel;
24 import org.objectweb.jac.aspects.gui.DisplayContext;
25 import org.objectweb.jac.aspects.gui.FieldEditor;
26 import org.objectweb.jac.aspects.gui.GuiAC;
27 import org.objectweb.jac.aspects.gui.ListModel;
28 import org.objectweb.jac.aspects.gui.ViewFactory;
29 import org.objectweb.jac.core.Collaboration;
30 import org.objectweb.jac.core.rtti.ClassItem;
31 import org.objectweb.jac.core.rtti.CollectionItem;
32 import org.objectweb.jac.core.rtti.FieldItem;
33 import org.objectweb.jac.core.rtti.MethodItem;
34 import org.objectweb.jac.core.rtti.RttiAC;
35 import org.objectweb.jac.util.Strings;
36
37 /**
38  * A collection editor that uses the value of an index field to select
39  * objects.
40  */

41 public class IndicesSelector extends AbstractCollection
42     implements FieldEditor, HTMLEditor
43 {
44     static Logger logger = Logger.getLogger("gui.editor");
45
46     CollectionItem index;
47     Object JavaDoc repository;
48     FieldItem indexedField;
49     MethodItem indexNotFoundHandler;
50     String JavaDoc indices;
51     ClassItem componentType;
52     ClassItem type;
53
54     public IndicesSelector(ViewFactory factory, DisplayContext context,
55                            CollectionItem collection, Object JavaDoc substance,
56                            CollectionModel model,
57                            org.objectweb.jac.aspects.gui.CollectionItemView itemView) {
58         super(factory,context,collection,substance,model,itemView);
59         componentType = collection.getComponentType();
60         this.index = (CollectionItem)
61             componentType.getAttribute(GuiAC.INDEXED_FIELD_SELECTOR);
62         this.repository = GuiAC.getRepository(componentType);
63         indexedField = (FieldItem)index.getAttribute(RttiAC.INDEXED_FIELD);
64         this.indexNotFoundHandler = (MethodItem)
65             componentType.getAttribute(GuiAC.INDEX_NOT_FOUND_HANDLER);
66         indices = objectsToString();
67     }
68
69     public void setEditedType(ClassItem type) {
70         this.type = type;
71     }
72
73     public void sort() {
74     }
75
76     public void updateModel(Object JavaDoc substance) {
77         if (model!=null)
78             model.close();
79         model = new ListModel(collection,substance);
80         indices = objectsToString();
81     }
82
83     public void commit() {
84         logger.debug(this+": "+collection.getName()+
85                      "'s value changed: ");
86         collection.clear(substance);
87         String JavaDoc[] keys = Strings.split(indices," ");
88         for (int i=0; i<keys.length; i++) {
89             if (Strings.isEmpty(keys[i].trim()))
90                 continue;
91             Object JavaDoc value = index.getMap(repository,keys[i]);
92             if (value==null) {
93                 if (indexNotFoundHandler!=null) {
94                     value = indexNotFoundHandler.invokeStatic(
95                         new Object JavaDoc[] {componentType,keys[i]});
96                 }
97             }
98             if (value!=null) {
99                 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 JavaDoc getValue() {
110         return null;
111     }
112
113     public void setEmbedded(boolean embedded) {
114     }
115
116     public void onSetFocus(Object JavaDoc param) {
117     }
118
119     // HTMLEditor interface
120

121     public String JavaDoc objectsToString() {
122         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc parameter) {
140         indices = (String JavaDoc)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
154
Popular Tags