KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>,
3                           Laurent Martelli <laurent@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

19
20 package org.objectweb.jac.aspects.gui.web;
21
22 import java.io.PrintWriter JavaDoc;
23 import org.apache.log4j.Logger;
24 import org.objectweb.jac.aspects.gui.*;
25 import org.objectweb.jac.core.Collaboration;
26 import org.objectweb.jac.core.rtti.FieldItem;
27
28 /**
29  * This is a special value editor that allows the user to choose a
30  * value within a set of object of a given type. */

31
32 public class ObjectChooser extends AbstractFieldEditor
33     implements HTMLEditor, ChoiceListener, ReferenceEditor
34 {
35     static Logger logger = Logger.getLogger("gui.chooser");
36
37     /**
38      * Constructs a new object chooser.
39      *
40      * @param substance
41      * @param reference the subtance reference that is affected by this
42      * chooser (can be null)
43      * @param isEditable should the user be allowed to enter a new value
44      */

45
46     public ObjectChooser(Object JavaDoc substance, FieldItem reference,
47                          ComboBoxModel model,
48                          boolean isEditable)
49     {
50         super(substance,reference);
51         this.model = model;
52         this.isEditable = isEditable;
53     }
54
55     ComboBoxModel model;
56     /**
57      * Gets the model containing the list of items the user can choose
58      * from.
59      */

60     public ComboBoxModel getModel() {
61         return model;
62     }
63
64     // FieldEditor interface
65

66     public void setValue(Object JavaDoc value) {
67         super.setValue(value);
68         model.setSelectedObject(value);
69     }
70
71     public Object JavaDoc getValue() {
72         logger.debug("selectedItem = "+model.getSelectedObject());
73         return model.getSelectedObject();
74     }
75
76     public void close(boolean validate) {
77         super.close(validate);
78         model.close();
79     }
80
81     boolean isEditable = false;
82     public boolean isEditable() {
83         return isEditable;
84     }
85     public void setEditable(boolean editable) {
86         this.isEditable = editable;
87     }
88
89     // HTMLEditor
90
public void genHTML(PrintWriter JavaDoc out) {
91         String JavaDoc selected = (String JavaDoc)model.getSelectedItem();
92         logger.debug("ObjectChooser(field="+field+
93                      ", selected="+selected+
94                      ", type="+model.getType()+")");
95
96         out.print("<select name=\""+label+"\"");
97         printAttributes(out);
98         out.println(">");
99
100         for (int i=0; i<model.getSize(); i++) {
101             String JavaDoc label = (String JavaDoc)model.getElementAt(i);
102             out.println("<option"+
103                         (label.equals(selected)?" selected":"")+
104                         " value=\""+label+"\""+
105                         ">"+
106                         label+"</option>");
107         }
108         out.println("</select>");
109
110         // display a "new" button
111
if (isEditable && model.getType()!=null &&
112             GuiAC.isCreatable(model.getType()))
113         {
114             showButton(out,"new_icon",GuiAC.getLabelNew(),"onCreateObject");
115         }
116     }
117
118     protected boolean doReadValue(Object JavaDoc parameter) {
119         if (parameter!=null) {
120             String JavaDoc string = (String JavaDoc)parameter;
121             model.setSelectedItem(string);
122             Object JavaDoc value = model.getSelectedObject();
123             super.setValue(value);
124             return true;
125         } else {
126             return false;
127         }
128     }
129
130     // ChoiceListener interface
131
public void onCreateObject() {
132         Thread JavaDoc createThread = new CreateThread();
133         createThread.start();
134     }
135
136     class CreateThread extends Thread JavaDoc {
137         Collaboration parentCollaboration;
138         public CreateThread() {
139             parentCollaboration = Collaboration.get();
140         }
141         public void run() {
142             Collaboration.set(new Collaboration(parentCollaboration));
143             Collaboration.get().addAttribute(GuiAC.AUTO_CREATION,"true");
144             Object JavaDoc instance =
145                 EventHandler.get().onCreateObject(context,model.getType(),substance,field);
146             if (instance!=null) {
147                 model.addObject(instance);
148                 value = instance;
149                 model.setSelectedObject(value);
150                 // do not use setValue(), because it would then fail to
151
// commit on close
152
}
153             context.getDisplay().refresh();
154         }
155     }
156 }
157
158
Popular Tags