1 17 18 package org.objectweb.jac.aspects.gui.swing; 19 20 import java.awt.Component ; 21 import java.awt.Insets ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.Arrays ; 25 import javax.swing.JButton ; 26 import javax.swing.JComponent ; 27 import javax.swing.JTable ; 28 import javax.swing.table.TableCellRenderer ; 29 import org.apache.log4j.Logger; 30 import org.objectweb.jac.aspects.gui.Border; 31 import org.objectweb.jac.aspects.gui.DisplayContext; 32 import org.objectweb.jac.aspects.gui.EventHandler; 33 import org.objectweb.jac.aspects.gui.GuiAC; 34 import org.objectweb.jac.aspects.gui.InvokeEvent; 35 import org.objectweb.jac.aspects.gui.Length; 36 import org.objectweb.jac.aspects.gui.MethodUpdate; 37 import org.objectweb.jac.aspects.gui.MethodView; 38 import org.objectweb.jac.aspects.gui.ResourceManager; 39 import org.objectweb.jac.aspects.gui.Utils; 40 import org.objectweb.jac.aspects.gui.View; 41 import org.objectweb.jac.aspects.gui.ViewFactory; 42 import org.objectweb.jac.aspects.gui.ViewIdentity; 43 import org.objectweb.jac.aspects.session.SessionAC; 44 import org.objectweb.jac.core.Collaboration; 45 import org.objectweb.jac.core.rtti.AbstractMethodItem; 46 import org.objectweb.jac.core.rtti.FieldItem; 47 import org.objectweb.jac.core.rtti.MethodItem; 48 import org.objectweb.jac.util.Strings; 49 50 public class SwingMethodView extends JButton 51 implements MethodView, ActionListener , TableCellRenderer , MethodUpdate 52 { 53 static Logger loggerEvents = Logger.getLogger("gui.events"); 54 55 DisplayContext context; 56 Length width; 57 Length height; 58 ViewFactory factory; 59 Object [] parameters; 60 String type; 61 62 Object substance; 63 AbstractMethodItem method; 64 MethodItem condition; 65 66 67 String style; 69 70 public void setStyle(String style) { 71 this.style = style; 72 } 73 74 public String getStyle() { 75 return style; 76 } 77 78 79 public SwingMethodView(Object substance, AbstractMethodItem method) { 80 this.substance = substance; 81 setMethod(method); 82 setMargin(new Insets (1,1,1,1)); 83 addActionListener(this); 84 } 85 86 String description; 87 88 92 public String getDescription() { 93 return description; 94 } 95 96 100 public void setDescription(String v) { 101 this.description = v; 102 } 103 104 View parentView; 105 106 110 public View getParentView() { 111 return parentView; 112 } 113 114 118 public void setParentView(View v) { 119 this.parentView = v; 120 } 121 122 public View getRootView() { 123 if (parentView==null) 124 return this; 125 return parentView.getRootView(); 126 } 127 128 public boolean isDescendantOf(View ancestor) { 129 if (this==ancestor) 130 return true; 131 else if (parentView==null) 132 return false; 133 else 134 return parentView.isDescendantOf(ancestor); 135 } 136 137 Border viewBorder; 138 139 143 public Border getViewBorder() { 144 return viewBorder; 145 } 146 147 151 public void setViewBorder(Border v) { 152 this.viewBorder = v; 153 } 154 155 MethodItem message; 156 157 161 public MethodItem getMessage() { 162 return message; 163 } 164 165 169 public void setMessage(MethodItem v) { 170 this.message = v; 171 } 172 173 public void setContext(DisplayContext context) { 174 this.context = context; 175 } 176 177 public DisplayContext getContext() { 178 return context; 179 } 180 181 public String getLabel() { 182 return getText(); 183 } 184 185 public void setLabel(String label) { 186 setText(label); 187 } 189 190 public String getText() { 191 if (method instanceof MethodItem && 192 ((MethodItem)method).isSetter() && getIcon()!=null) 193 return ""; 194 else 195 return super.getText(); 196 } 197 198 public void setIcon(String icon) { 199 setIcon(ResourceManager.getIcon(icon)); 200 } 201 202 boolean onlyIcon = false; 203 public void setOnlyIcon(boolean onlyIcon) { 204 this.onlyIcon = onlyIcon; 205 } 206 207 public void setSize(Length width, Length height) { 208 this.width = width; 209 this.height = height; 210 SwingUtils.setSize(this,width,height); 211 } 212 213 public void setMethod(AbstractMethodItem method) { 214 if (condition!=null) { 215 Utils.unregisterMethod(substance,condition,this); 216 } 217 this.method = method; 218 condition = GuiAC.getCondition(method); 219 if (condition!=null) { 220 setEnabled(GuiAC.isEnabled(method,substance)); 221 Utils.registerMethod(substance,condition,this); 222 } 223 } 224 225 public boolean equalsView(ViewIdentity view) { 226 return 227 ( ( type!=null && 228 type.equals(view.getType()) ) 229 || (type==null && view.getType()==null ) ) 230 && ( ( parameters!=null && 231 Arrays.equals(parameters,view.getParameters()) ) 232 || (parameters==null && view.getParameters()==null) ); 233 } 234 235 public boolean equalsView(String type, Object [] parameters) { 236 return this.type.equals(type) 237 && Arrays.equals(this.parameters,parameters); 238 } 239 240 public void setSubstance(Object substance) { 241 this.substance = substance; 242 } 243 244 public void close(boolean validate) { 245 closed = true; 246 } 247 248 boolean closed = false; 249 250 public boolean isClosed() { 251 return closed; 252 } 253 254 public ViewFactory getFactory() { 255 return factory; 256 } 257 258 public void setFactory(ViewFactory factory) { 259 this.factory = factory; 260 } 261 262 public void setType(String type) { 263 this.type = type; 264 } 265 266 public String getType() { 267 return type; 268 } 269 270 public void setParameters(Object [] parameters) { 271 this.parameters = parameters; 272 } 273 274 public Object [] getParameters() { 275 return parameters; 276 } 277 278 public void setFocus(FieldItem field, Object option) { 279 } 280 281 public String toString() { 282 return Strings.hex(this); 283 } 284 285 286 288 public void actionPerformed(ActionEvent action) { 289 loggerEvents.debug("action performed on MethodView "+method.getName()); 290 Collaboration collab = Collaboration.get(); 291 collab.addAttribute(GuiAC.DISPLAY_CONTEXT,context); 292 collab.addAttribute(SessionAC.SESSION_ID, 293 GuiAC.getLocalSessionID()); 294 EventHandler.get().onInvoke( 295 context,new InvokeEvent(this,substance,method)); 296 } 297 298 public Component getTableCellRendererComponent( 300 JTable table, Object value, 301 boolean isSelected, boolean hasFocus, 302 int row, int column) 303 { 304 JComponent component = this; 306 if (component!=null) { 307 component.setOpaque(true); } 309 setOpaque(true); 311 if (isSelected) { 312 if (component!=null) { 313 component.setForeground(table.getSelectionForeground()); 314 component.setBackground(table.getSelectionBackground()); 315 } 316 setForeground(table.getSelectionForeground()); 317 setBackground(table.getSelectionBackground()); 318 } else { 319 if (component!=null) { 320 component.setForeground(table.getForeground()); 321 component.setBackground(table.getBackground()); 322 } 323 setForeground(table.getForeground()); 324 setBackground(table.getBackground()); 325 } 326 if (component!=null) { 327 component.setFont(null); 328 } 329 setFont(null); 330 331 333 return this; 334 } 335 336 public void methodUpdated(Object substance, MethodItem method, Object param) { 338 setEnabled(GuiAC.isEnabled(this.method,this.substance)); 339 } 340 } 341 | Popular Tags |