1 19 20 package org.objectweb.jac.aspects.gui.swing; 21 22 import java.awt.Dimension ; 23 import java.awt.Insets ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.ItemEvent ; 27 import java.awt.event.ItemListener ; 28 import javax.swing.JButton ; 29 import javax.swing.JComboBox ; 30 import org.apache.log4j.Logger; 31 import org.objectweb.jac.aspects.gui.*; 32 import org.objectweb.jac.core.Collaboration; 33 import org.objectweb.jac.core.Wrappee; 34 import org.objectweb.jac.core.rtti.FieldItem; 35 36 39 40 public class ObjectChooser extends AbstractFieldEditor 41 implements ActionListener , ItemListener , ReferenceEditor 42 { 43 static Logger logger = Logger.getLogger("gui.chooser"); 44 45 JComboBox choice; 46 ComboBoxModel model; 47 JButton viewButton; 48 JButton newButton; 49 50 53 54 public ObjectChooser(Object substance, FieldItem reference, 55 ComboBoxModel model, boolean isEditable) 56 { 57 super(substance,reference); 58 59 this.model = model; 60 this.isEditable = isEditable; 61 choice = new JComboBox (model); 62 63 boolean isWrappee = model.getType()!=null && 64 Wrappee.class.isAssignableFrom(model.getType().getActualClass()); 65 choice.setEditable(isEditable && !isWrappee); 66 67 71 77 78 choice.getComponent(0).addFocusListener(this); 79 choice.addFocusListener(this); 80 81 84 85 add(choice); 86 choice.addItemListener(this); 88 89 Dimension minSize = choice.getPreferredSize(); 91 Dimension maxSize = choice.getMaximumSize(); 92 choice.setMaximumSize(new Dimension (maxSize.width,minSize.height)); 93 94 Boolean small_view = (Boolean ) Collaboration.get(). 95 getAttribute(GuiAC.SMALL_VIEW); 96 97 if ((small_view == null) || (!small_view.booleanValue())) { 98 if (isWrappee) { 99 viewButton = new JButton (ResourceManager.getIconResource("view_icon")); 100 viewButton.setToolTipText("View"); 101 viewButton.setActionCommand("open"); 102 viewButton.addActionListener(this); 103 viewButton.setMargin(new Insets (1,1,1,1)); 104 add(viewButton); 105 } 106 107 if (isWrappee && isEditable && model.getType()!=null && 108 GuiAC.isCreatable(model.getType())) { 109 newButton = new JButton (ResourceManager.getIconResource("new_icon")); 110 newButton.setToolTipText("New"); 111 newButton.setActionCommand("new"); 112 newButton.addActionListener(this); 113 newButton.setMargin(new Insets (1,1,1,1)); 114 add(newButton); 115 } 116 } 117 } 118 119 public void setFocus(FieldItem field, Object extraOption) {} 120 121 122 124 public void setValue(Object value) { 125 super.setValue(value); 126 model.setSelectedObject(value); 127 128 if (value==null && viewButton!=null) 129 viewButton.setEnabled(false); 130 } 131 132 public Object getValue() { 133 logger.debug("selectedItem = "+model.getSelectedObject()); 134 return model.getSelectedObject(); 135 } 136 137 public void close(boolean validate) { 138 super.close(validate); 139 model.close(); 140 } 141 142 151 public void actionPerformed(ActionEvent event) { 152 loggerEvents.debug("actionPerformed: "+event.getActionCommand()); 153 setContext(); 154 if (event.getActionCommand().equals("new")) { 155 Object instance = 156 EventHandler.get().onCreateObject( 157 context,model.getType(),substance,field); 158 if (instance!=null) { 159 requestFocus(); 160 model.addObject(instance); 161 model.setSelectedObject(instance); 162 } 163 } else if (event.getActionCommand().equals("open")) { 164 Object object = model.getSelectedObject(); 165 if (object == null) 166 return; 167 if (object!=null) { 168 EventHandler.get().onView(context,field,object,null,null); 169 } 170 } 171 } 172 173 176 public void requestFocus() { 177 choice.requestFocus(); 178 loggerFocus.debug("focusing "+choice.getClass().getName()); 179 } 180 181 boolean isEditable = false; 182 public boolean isEditable() { 183 return isEditable; 184 } 185 public void setEditable(boolean editable) { 186 this.isEditable = editable; 187 } 188 189 public ComboBoxModel getModel() { 190 return model; 191 } 192 193 195 public void itemStateChanged(ItemEvent event) { 196 loggerEvents.debug("itemStateChanged on "+this); 197 if (field!=null && isEmbedded) { 198 invokeInContext(this,"commit", new Object []{}); 199 } else { 200 loggerEvents.debug("ignoring item event"); 201 } 202 } 203 } 204 | Popular Tags |