1 19 20 package org.objectweb.jac.aspects.gui.web; 21 22 import java.io.PrintWriter ; 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 31 32 public class ObjectChooser extends AbstractFieldEditor 33 implements HTMLEditor, ChoiceListener, ReferenceEditor 34 { 35 static Logger logger = Logger.getLogger("gui.chooser"); 36 37 45 46 public ObjectChooser(Object 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 60 public ComboBoxModel getModel() { 61 return model; 62 } 63 64 66 public void setValue(Object value) { 67 super.setValue(value); 68 model.setSelectedObject(value); 69 } 70 71 public Object 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 public void genHTML(PrintWriter out) { 91 String selected = (String )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 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 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 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 } 153 context.getDisplay().refresh(); 154 } 155 } 156 } 157 158 | Popular Tags |