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, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 GNU Lesser General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public License 016 along with this program; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 018 019 package org.objectweb.jac.aspects.gui.swing; 020 021 import java.awt.Color; 022 import java.util.Arrays; 023 import javax.swing.BorderFactory; 024 import javax.swing.BoxLayout; 025 import javax.swing.JPanel; 026 import javax.swing.border.TitledBorder; 027 import org.apache.log4j.Logger; 028 import org.objectweb.jac.aspects.gui.Border; 029 import org.objectweb.jac.aspects.gui.DialogView; 030 import org.objectweb.jac.aspects.gui.DisplayContext; 031 import org.objectweb.jac.aspects.gui.GuiAC; 032 import org.objectweb.jac.aspects.gui.InvokeEvent; 033 import org.objectweb.jac.aspects.gui.InvokeThread; 034 import org.objectweb.jac.aspects.gui.View; 035 import org.objectweb.jac.aspects.gui.ViewFactory; 036 import org.objectweb.jac.aspects.gui.ViewIdentity; 037 import org.objectweb.jac.aspects.session.SessionAC; 038 import org.objectweb.jac.core.Collaboration; 039 import org.objectweb.jac.core.rtti.ClassRepository; 040 import org.objectweb.jac.core.rtti.FieldItem; 041 import org.objectweb.jac.core.rtti.MethodItem; 042 import org.objectweb.jac.util.Strings; 043 044 public abstract class AbstractView extends JPanel implements View { 045 static Logger loggerEvents = Logger.getLogger("gui.events"); 046 static Logger loggerClose = Logger.getLogger("gui.close"); 047 static Logger loggerContext = Logger.getLogger("display-context"); 048 static Logger loggerDnd = Logger.getLogger("gui.dnd"); 049 050 String label; 051 DisplayContext context; 052 int width; 053 int height; 054 ViewFactory factory; 055 056 Object[] parameters; 057 String type; 058 059 public AbstractView() { 060 setLayout(new BoxLayout(this,BoxLayout.X_AXIS)); 061 } 062 063 public AbstractView(ViewFactory factory, DisplayContext context) { 064 this.factory = factory; 065 this.context = context; 066 } 067 068 MethodItem message; 069 070 /** 071 * Get the value of message. 072 * @return value of message. 073 */ 074 public MethodItem getMessage() { 075 return message; 076 } 077 078 /** 079 * Set the value of message. 080 * @param v Value to assign to message. 081 */ 082 public void setMessage(MethodItem v) { 083 this.message = v; 084 } 085 086 String description; 087 088 /** 089 * Get the value of description. 090 * @return value of description. 091 */ 092 public String getDescription() { 093 return description; 094 } 095 096 /** 097 * Set the value of description. 098 * @param v Value to assign to description. 099 */ 100 public void setDescription(String v) { 101 this.description = v; 102 } 103 104 View parentView; 105 106 /** 107 * Get the value of parentView. 108 * @return value of parentView. 109 */ 110 public View getParentView() { 111 return parentView; 112 } 113 114 /** 115 * Set the value of parentView. 116 * @param v Value to assign to parentView. 117 */ 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 public void setContext(DisplayContext context) { 138 loggerContext.debug("setContext on "+this); 139 this.context = context; 140 } 141 142 public DisplayContext getContext() { 143 return context; 144 } 145 146 // style used to change display (css for web) 147 String style; 148 149 public void setStyle(String style) { 150 this.style = style; 151 } 152 153 public String getStyle() { 154 return style; 155 } 156 157 public void setFactory(ViewFactory factory) { 158 this.factory = factory; 159 } 160 161 public ViewFactory getFactory() { 162 return factory; 163 } 164 165 public void setLabel(String label) { 166 this.label = label; 167 } 168 169 public String getLabel() { 170 return label; 171 } 172 173 public void setWidth(int width) { 174 this.width = width; 175 } 176 177 public void setHeight(int height) { 178 this.height = height; 179 } 180 181 public void setType(String type) { 182 this.type = type; 183 } 184 185 public String getType() { 186 return type; 187 } 188 189 public void setParameters(Object[] parameters) { 190 this.parameters = parameters; 191 } 192 193 public Object[] getParameters() { 194 return parameters; 195 } 196 197 public boolean equalsView(ViewIdentity view) { 198 return 199 ( ( type!=null && 200 type.equals(view.getType()) ) 201 || (type==null && view.getType()==null ) ) 202 && ( ( parameters!=null && 203 Arrays.equals(parameters,view.getParameters()) ) 204 || (parameters==null && view.getParameters()==null) ); 205 } 206 207 public boolean equalsView(String type, Object[] parameters) { 208 return this.type.equals(type) 209 && Arrays.equals(this.parameters,parameters); 210 } 211 212 protected boolean closed = false; 213 214 public void close(boolean validate) { 215 closed = true; 216 parameters = null; 217 } 218 219 public boolean isClosed() { 220 return closed; 221 } 222 223 Border viewBorder; 224 225 /** 226 * Get the value of viewBorder. 227 * @return value of viewBorder. 228 */ 229 public Border getViewBorder() { 230 return viewBorder; 231 } 232 233 /** 234 * Set the value of viewBorder. 235 * @param border Value to assign to viewBorder. 236 */ 237 public void setViewBorder(Border border) { 238 javax.swing.border.Border swingBorder; 239 this.viewBorder = border; 240 switch(border.getStyle()) { 241 case Border.LINE: 242 swingBorder=BorderFactory.createLineBorder(Color.black); 243 break; 244 case Border.ETCHED: 245 swingBorder=BorderFactory.createEtchedBorder(); 246 break; 247 case Border.RAISED: 248 swingBorder=BorderFactory.createRaisedBevelBorder(); 249 break; 250 case Border.LOWERED: 251 swingBorder=BorderFactory.createLoweredBevelBorder(); 252 break; 253 default: 254 swingBorder=BorderFactory.createLineBorder(Color.black); 255 } 256 if(border.hasTitle()) { 257 swingBorder=BorderFactory.createTitledBorder( 258 swingBorder,border.getTitle()); 259 switch(border.getAlignment()) { 260 case Border.RIGHT: 261 ((TitledBorder)swingBorder).setTitleJustification( 262 TitledBorder.RIGHT); 263 break; 264 case Border.CENTER: 265 ((TitledBorder)swingBorder).setTitleJustification( 266 TitledBorder.CENTER); 267 } 268 } 269 this.setBorder(swingBorder); 270 } 271 272 public String toString() { 273 return Strings.hex(this); 274 } 275 276 void setContext() { 277 Collaboration.get().addAttribute( 278 GuiAC.DISPLAY_CONTEXT,context); 279 Collaboration.get().addAttribute( 280 SessionAC.SESSION_ID,GuiAC.getLocalSessionID()); 281 } 282 283 public void setFocus(FieldItem field, Object option) { 284 } 285 286 /** 287 * Invoke a method with correct attributes 288 * (DISPLAY_CONTEXT,SESSION_ID and all attributes stored by the 289 * current window) with InvokeThread 290 * 291 * @param substance the object on which to invoke the method 292 * @param methodName the method to invoke 293 * @param parameters parameters to pass to the method 294 */ 295 protected void invokeInContext(Object substance,String methodName, 296 Object[] parameters) 297 { 298 MethodItem method = ClassRepository.get().getClass(substance) 299 .getMethod(methodName); 300 String[] names = new String[2]; 301 Object[] values = new Object[2]; 302 names[0] = GuiAC.DISPLAY_CONTEXT; 303 values[0] = context; 304 names[1] = SessionAC.SESSION_ID; 305 values[1] = GuiAC.getLocalSessionID(); 306 //names[2] = "Gui.askForParameters"; 307 //values[2] = method; 308 Object window = context.getWindow(); 309 310 if (window instanceof DialogView) { 311 ((DialogView)window).restoreContext(); 312 } else if (window instanceof ObjectViewDialog) { 313 ((ObjectViewDialog)window).restoreContext(); 314 } 315 InvokeThread.run(new InvokeEvent(this,substance, method, parameters), 316 null, null, names, values); 317 } 318 }