1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.Color ; 22 import java.util.Arrays ; 23 import javax.swing.BorderFactory ; 24 import javax.swing.BoxLayout ; 25 import javax.swing.JPanel ; 26 import javax.swing.border.TitledBorder ; 27 import org.apache.log4j.Logger; 28 import org.objectweb.jac.aspects.gui.Border; 29 import org.objectweb.jac.aspects.gui.DialogView; 30 import org.objectweb.jac.aspects.gui.DisplayContext; 31 import org.objectweb.jac.aspects.gui.GuiAC; 32 import org.objectweb.jac.aspects.gui.InvokeEvent; 33 import org.objectweb.jac.aspects.gui.InvokeThread; 34 import org.objectweb.jac.aspects.gui.Length; 35 import org.objectweb.jac.aspects.gui.View; 36 import org.objectweb.jac.aspects.gui.ViewFactory; 37 import org.objectweb.jac.aspects.gui.ViewIdentity; 38 import org.objectweb.jac.aspects.session.SessionAC; 39 import org.objectweb.jac.core.Collaboration; 40 import org.objectweb.jac.core.rtti.ClassRepository; 41 import org.objectweb.jac.core.rtti.FieldItem; 42 import org.objectweb.jac.core.rtti.MethodItem; 43 import org.objectweb.jac.util.Strings; 44 45 public abstract class AbstractView extends JPanel implements View { 46 static Logger loggerEvents = Logger.getLogger("gui.events"); 47 static Logger loggerClose = Logger.getLogger("gui.close"); 48 static Logger loggerContext = Logger.getLogger("display-context"); 49 static Logger loggerDnd = Logger.getLogger("gui.dnd"); 50 51 String label; 52 DisplayContext context; 53 protected Length width; 54 protected Length height; 55 ViewFactory factory; 56 57 Object [] parameters; 58 String type; 59 60 public AbstractView() { 61 setLayout(new BoxLayout (this,BoxLayout.X_AXIS)); 62 } 63 64 public AbstractView(ViewFactory factory, DisplayContext context) { 65 this.factory = factory; 66 this.context = context; 67 } 68 69 MethodItem message; 70 71 75 public MethodItem getMessage() { 76 return message; 77 } 78 79 83 public void setMessage(MethodItem v) { 84 this.message = v; 85 } 86 87 String description; 88 89 93 public String getDescription() { 94 return description; 95 } 96 97 101 public void setDescription(String v) { 102 this.description = v; 103 } 104 105 View parentView; 106 107 111 public View getParentView() { 112 return parentView; 113 } 114 115 119 public void setParentView(View v) { 120 this.parentView = v; 121 } 122 123 public View getRootView() { 124 if (parentView==null) 125 return this; 126 return parentView.getRootView(); 127 } 128 129 public boolean isDescendantOf(View ancestor) { 130 if (this==ancestor) 131 return true; 132 else if (parentView==null) 133 return false; 134 else 135 return parentView.isDescendantOf(ancestor); 136 } 137 138 public void setContext(DisplayContext context) { 139 loggerContext.debug("setContext on "+this); 140 this.context = context; 141 } 142 143 public DisplayContext getContext() { 144 return context; 145 } 146 147 String style; 149 150 public void setStyle(String style) { 151 this.style = style; 152 } 153 154 public String getStyle() { 155 return style; 156 } 157 158 public void setFactory(ViewFactory factory) { 159 this.factory = factory; 160 } 161 162 public ViewFactory getFactory() { 163 return factory; 164 } 165 166 public void setLabel(String label) { 167 this.label = label; 168 } 169 170 public String getLabel() { 171 return label; 172 } 173 174 public void setSize(Length width, Length height) { 175 this.width = width; 176 this.height = height; 177 SwingUtils.setSize(this,width,height); 178 } 179 180 public void setType(String type) { 181 this.type = type; 182 } 183 184 public String getType() { 185 return type; 186 } 187 188 public void setParameters(Object [] parameters) { 189 this.parameters = parameters; 190 } 191 192 public Object [] getParameters() { 193 return parameters; 194 } 195 196 public boolean equalsView(ViewIdentity view) { 197 return 198 ( ( type!=null && 199 type.equals(view.getType()) ) 200 || (type==null && view.getType()==null ) ) 201 && ( ( parameters!=null && 202 Arrays.equals(parameters,view.getParameters()) ) 203 || (parameters==null && view.getParameters()==null) ); 204 } 205 206 public boolean equalsView(String type, Object [] parameters) { 207 return this.type.equals(type) 208 && Arrays.equals(this.parameters,parameters); 209 } 210 211 protected boolean closed = false; 212 213 public void close(boolean validate) { 214 closed = true; 215 parameters = null; 216 } 217 218 public boolean isClosed() { 219 return closed; 220 } 221 222 Border viewBorder; 223 224 228 public Border getViewBorder() { 229 return viewBorder; 230 } 231 232 236 public void setViewBorder(Border border) { 237 javax.swing.border.Border swingBorder; 238 this.viewBorder = border; 239 switch(border.getStyle()) { 240 case Border.LINE: 241 swingBorder=BorderFactory.createLineBorder(Color.black); 242 break; 243 case Border.ETCHED: 244 swingBorder=BorderFactory.createEtchedBorder(); 245 break; 246 case Border.RAISED: 247 swingBorder=BorderFactory.createRaisedBevelBorder(); 248 break; 249 case Border.LOWERED: 250 swingBorder=BorderFactory.createLoweredBevelBorder(); 251 break; 252 default: 253 swingBorder=BorderFactory.createLineBorder(Color.black); 254 } 255 if(border.hasTitle()) { 256 swingBorder=BorderFactory.createTitledBorder( 257 swingBorder,border.getTitle()); 258 switch(border.getAlignment()) { 259 case Border.RIGHT: 260 ((TitledBorder )swingBorder).setTitleJustification( 261 TitledBorder.RIGHT); 262 break; 263 case Border.CENTER: 264 ((TitledBorder )swingBorder).setTitleJustification( 265 TitledBorder.CENTER); 266 } 267 } 268 this.setBorder(swingBorder); 269 } 270 271 public String toString() { 272 return Strings.hex(this); 273 } 274 275 void setContext() { 276 Collaboration.get().addAttribute( 277 GuiAC.DISPLAY_CONTEXT,context); 278 Collaboration.get().addAttribute( 279 SessionAC.SESSION_ID,GuiAC.getLocalSessionID()); 280 } 281 282 public void setFocus(FieldItem field, Object option) { 283 } 284 285 294 protected void invokeInContext(Object substance,String methodName, 295 Object [] parameters) 296 { 297 MethodItem method = ClassRepository.get().getClass(substance) 298 .getMethod(methodName); 299 String [] names = new String [2]; 300 Object [] values = new Object [2]; 301 names[0] = GuiAC.DISPLAY_CONTEXT; 302 values[0] = context; 303 names[1] = SessionAC.SESSION_ID; 304 values[1] = GuiAC.getLocalSessionID(); 305 Object window = context.getWindow(); 308 309 if (window instanceof DialogView) { 310 ((DialogView)window).restoreContext(); 311 } else if (window instanceof ObjectViewDialog) { 312 ((ObjectViewDialog)window).restoreContext(); 313 } 314 InvokeThread.run(new InvokeEvent(this,substance, method, parameters), 315 null, null, names, values); 316 } 317 } 318 | Popular Tags |