001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>, 
003                              Laurent Martelli <laurent@aopsys.com>
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful, but
011      WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013      Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public
016      License along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
018      USA */
019    
020    package org.objectweb.jac.aspects.gui.web;
021    
022    import java.io.PrintWriter;
023    import org.apache.log4j.Logger;
024    import org.objectweb.jac.aspects.gui.*;
025    import org.objectweb.jac.core.Collaboration;
026    import org.objectweb.jac.core.rtti.FieldItem;
027    
028    /**
029     * This is a special value editor that allows the user to choose a
030     * value within a set of object of a given type. */
031    
032    public class ObjectChooser extends AbstractFieldEditor
033        implements HTMLEditor, ChoiceListener, ReferenceEditor
034    {
035        static Logger logger = Logger.getLogger("gui.chooser");
036    
037        /**
038         * Constructs a new object chooser.
039         *
040         * @param substance
041         * @param reference the subtance reference that is affected by this
042         * chooser (can be null) 
043         * @param isEditable should the user be allowed to enter a new value
044         */
045    
046        public ObjectChooser(Object substance, FieldItem reference, 
047                             ComboBoxModel model,
048                             boolean isEditable) 
049        {
050            super(substance,reference);
051            this.model = model;
052            this.isEditable = isEditable;
053        }
054    
055        ComboBoxModel model;
056        /**
057         * Gets the model containing the list of items the user can choose
058         * from.
059         */
060        public ComboBoxModel getModel() {
061            return model;
062        }
063    
064        // FieldEditor interface
065    
066        public void setValue(Object value) {
067            super.setValue(value);
068            model.setSelectedObject(value);
069        }
070    
071        public Object getValue() {
072            logger.debug("selectedItem = "+model.getSelectedObject());
073            return model.getSelectedObject();
074        }
075    
076        public void close(boolean validate) {
077            super.close(validate);
078            model.close();
079        }
080    
081        boolean isEditable = false;
082        public boolean isEditable() {
083            return isEditable;
084        }
085        public void setEditable(boolean editable) {
086            this.isEditable = editable;
087        }
088    
089        // HTMLEditor
090        public void genHTML(PrintWriter out) {
091            String selected = (String)model.getSelectedItem();
092            logger.debug("ObjectChooser(field="+field+
093                         ", selected="+selected+
094                         ", type="+model.getType()+")");
095    
096            out.print("<select name=\""+label+"\"");
097            printAttributes(out);
098            out.println(">");
099    
100            for (int i=0; i<model.getSize(); i++) {
101                String label = (String)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 parameter) {
119            if (parameter!=null) {
120                String string = (String)parameter;
121                model.setSelectedItem(string);
122                Object 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 createThread = new CreateThread();
133            createThread.start();
134        }
135    
136        class CreateThread extends Thread {
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 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