1 17 18 package org.objectweb.jac.aspects.gui.web; 19 20 import java.awt.Dimension ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.Arrays ; 25 import java.util.Hashtable ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import org.apache.log4j.Logger; 29 import org.objectweb.jac.aspects.cache.MethodCache; 30 import org.objectweb.jac.aspects.gui.*; 31 import org.objectweb.jac.aspects.gui.web.html.*; 32 import org.objectweb.jac.aspects.timestamp.Timestamps; 33 import org.objectweb.jac.core.rtti.FieldItem; 34 import org.objectweb.jac.core.rtti.MethodItem; 35 import org.objectweb.jac.util.Images; 36 import org.objectweb.jac.util.ObjectArray; 37 import org.objectweb.jac.util.Strings; 38 39 public abstract class AbstractView implements View { 40 static Logger logger = Logger.getLogger("web"); 41 static Logger loggerClose = Logger.getLogger("gui.close"); 42 static Logger loggerDisplay = Logger.getLogger("display"); 43 static Logger loggerContext = Logger.getLogger("display.context"); 44 45 protected String label; 46 protected DisplayContext context; 47 protected Length width; 48 protected Length height; 49 ViewFactory factory; 50 51 String style; 53 54 Object [] parameters; 55 String type; 56 57 58 protected boolean isCellViewer = false; 59 protected int row; 60 protected int column; 61 protected View table; 62 63 public AbstractView() { 64 } 65 66 public AbstractView(ViewFactory factory, DisplayContext context) { 67 this.factory = factory; 68 this.context = context; 69 } 70 71 Border viewBorder; 72 73 77 public Border getViewBorder() { 78 return viewBorder; 79 } 80 81 85 public void setViewBorder(Border v) { 86 this.viewBorder = v; 87 } 88 89 MethodItem message; 90 91 95 public MethodItem getMessage() { 96 return message; 97 } 98 99 103 public void setMessage(MethodItem v) { 104 this.message = v; 105 } 106 107 protected String description; 108 109 113 public String getDescription() { 114 return description; 115 } 116 117 121 public void setDescription(String v) { 122 this.description = v; 123 } 124 125 protected View parentView; 126 127 131 public View getParentView() { 132 return parentView; 133 } 134 135 139 public void setParentView(View v) { 140 this.parentView = v; 141 } 142 143 public View getRootView() { 144 if (parentView==null) 145 return this; 146 return parentView.getRootView(); 147 } 148 149 public boolean isDescendantOf(View ancestor) { 150 if (this==ancestor) 151 return true; 152 else if (parentView==null) 153 return false; 154 else 155 return parentView.isDescendantOf(ancestor); 156 } 157 158 public void setContext(DisplayContext context) { 159 loggerContext.debug("setContext on "+this); 160 this.context = context; 161 } 162 163 public DisplayContext getContext() { 164 return context; 165 } 166 167 public void setFactory(ViewFactory factory) { 168 this.factory = factory; 169 } 170 171 public ViewFactory getFactory() { 172 return factory; 173 } 174 175 public void setLabel(String label) { 176 this.label = label; 177 } 178 179 public String getLabel() { 180 return label; 181 } 182 183 public void setSize(Length width, Length height) { 184 this.width = width; 185 this.height = height; 186 } 187 188 public void setType(String type) { 189 this.type = type; 190 } 191 192 public String getType() { 193 return type; 194 } 195 196 public void setStyle(String style) { 197 this.style = style; 198 } 199 200 public String getStyle() { 201 return style; 202 } 203 204 public void setParameters(Object [] parameters) { 205 this.parameters = parameters; 206 } 207 208 public Object [] getParameters() { 209 return parameters; 210 } 211 212 public void setFocus(FieldItem field, Object option) { 213 } 214 215 public void close(boolean validate) { 216 loggerClose.debug("closing "+this); 217 closed = true; 218 ((WebDisplay)context.getDisplay()).unregisterView(this); 219 } 220 221 boolean closed = false; 222 223 public boolean isClosed() { 224 return closed; 225 } 226 227 public boolean equalsView(ViewIdentity view) { 228 return 229 ( ( type!=null && 230 type.equals(view.getType()) ) 231 || (type==null && view.getType()==null ) ) 232 && ( ( parameters!=null && 233 Arrays.equals(parameters,view.getParameters()) ) 234 || (parameters==null && view.getParameters()==null) ); 235 } 236 237 public boolean equalsView(String type, Object [] parameters) { 238 return this.type.equals(type) 239 && Arrays.equals(this.parameters,parameters); 240 } 241 242 245 protected boolean isInForm() { 246 return true; 247 } 248 249 256 protected Composite eventURL(String text, String event, String params) { 257 JacRequest request = WebDisplay.getRequest(); 258 String parameters = "event="+event+"&source="+getId()+params; 259 if (request.isIEUserAgent()) { 260 logger.debug("user-agent: "+request.getUserAgent()); 262 Link link = new Link( 263 ((WebDisplay)context.getDisplay()).getServletName()+ 264 "?"+parameters, 265 text); 266 link.attribute("onclick","return commitForm(this,'"+parameters+"')"); 267 return link; 268 } else { 269 if (isInForm()) { 270 Button button = 271 new Button("submit","eventAndAction",parameters); 272 button.add(text); 273 return button; 274 } else { 275 return new Link( 276 ((WebDisplay)context.getDisplay()).getServletName()+ 277 "?event="+event+"&source="+getId()+params, 278 text); 279 } 280 } 281 } 282 283 291 protected void showButton(PrintWriter out, String icon, String label, String event) { 292 JacRequest request = WebDisplay.getRequest(); 293 if (request.isIEUserAgent()) { 294 out.println( 295 "<table class=\"method\"><td>"+ 296 (icon!=null?iconElement(ResourceManager.getResource(icon),"").toString():"")+ 297 eventURL(label,event,"").toString()+ 298 "</td></table>"); 299 } else { 300 out.println( 301 eventURL(label,event,"") 302 .add(0,(icon!=null?iconElement(ResourceManager.getResource(icon),"").toString():"")) 303 .cssClass("method") 304 .toString()); 305 } 306 } 307 308 public String getOpenBorder() { 309 String s = ""; 310 if (viewBorder==null) 311 return s; 312 s += "<div class=\"BORDER_"+Border.i2aStyle(viewBorder.getStyle())+"\">"; 313 if (viewBorder.hasTitle()) { 314 s += "<div class=\"label\">"+viewBorder.getTitle()+"</div>"; 315 } 316 return s; 317 } 318 319 public String getCloseBorder() { 320 if (viewBorder==null) 321 return ""; 322 return "</div>"; 323 } 324 325 protected String getBaseURL() { 326 return ((WebDisplay)context.getDisplay()).getServletName(); 327 } 328 329 333 protected String eventURL(String event) { 334 String base = getBaseURL()+ 335 "?event="+event+"&source="+getId(); 336 if (isCellViewer) 337 return base+"&tableEventSource="+getId(table)+ 338 "&row="+row+"&col="+column; 339 else 340 return base; 341 } 342 343 349 protected Element iconElement(String icon, String alt) { 350 return iconElement(icon,alt,true); 351 } 352 353 static MethodCache iconCache = new MethodCache(null); 354 355 362 protected Element iconElement(String icon, String alt, boolean showAlt) { 363 if (Strings.isEmpty(icon)) { 364 return new Text(showAlt?alt:""); 365 } else { 366 Dimension size = null; 367 File file = new File (icon); 368 ObjectArray args = new ObjectArray(new Object []{file}); 369 MethodCache.Entry entry = iconCache.getEntry(args,null); 370 if (entry!=null) { 371 size = (Dimension )entry.value; 372 } else { 373 try { 374 size = Images.getImageFileSize(file); 375 logger.debug("size of "+icon+": "+size.width+"x"+size.height); 376 } catch (Exception e) { 377 logger.warn("Could not determine size of icon "+icon,e); 378 } 379 } 380 if (size==null) { 381 logger.warn("Could not determine size of icon "+icon); 382 } else { 383 iconCache.putEntry(args,size); 384 } 385 return new Image("resources/"+icon,alt,size).cssClass("icon"); 386 } 387 } 388 389 protected String getId() { 390 return ((WebDisplay)context.getDisplay()).registerView(this); 391 } 392 393 protected String getId(View view) { 394 return ((WebDisplay)context.getDisplay()).registerView(view); 395 } 396 397 399 400 String styleSheet = "resources/org/objectweb/jac/aspects/gui/web/style.css"; 401 String styleSheetIE = "resources/org/objectweb/jac/aspects/gui/web/ie.css"; 402 String styleSheetKonqueror = "resources/org/objectweb/jac/aspects/gui/web/konqueror.css"; 403 404 String javascript = "resources/org/objectweb/jac/aspects/gui/web/script.js"; 405 406 public void setStyleSheet(String styleSheet) { 407 this.styleSheet = styleSheet; 408 } 409 410 414 protected void genPage(PrintWriter out) throws IOException { 415 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">"); 416 out.println("<html>"); 417 out.println(" <head>"); 418 out.println(" <title>"+label+"</title>"); 419 out.println(" <meta name=\"Author\" content=\"JAC web-gui server\">" ); 420 out.println(" <script type=\"text/javascript\" SRC=\""+javascript+"\"></script>"); 421 genStyleSheets(out,context.getCustomizedView()); 422 out.println(" </head>"); 423 out.println(" <body>"); 424 genBody(out); 425 out.println(" </body>"); 426 out.println("</html>"); 427 } 428 429 protected void genStyleSheets(PrintWriter out, CustomizedView customized) { 430 out.println(" <link rel=\"stylesheet\" type=\"text/css\" "+ 431 "href=\""+styleSheet+"\" title=\"JAC\">"); 432 JacRequest request = WebDisplay.getRequest(); 433 if (request.isIEUserAgent()) { 434 out.println(" <link rel=\"stylesheet\" type=\"text/css\" "+ 435 "href=\""+styleSheetIE+"\">"); 436 } 437 if (request.userAgentMatch("Konqueror")) { 438 out.println(" <link rel=\"stylesheet\" type=\"text/css\" "+ 439 "href=\""+styleSheetKonqueror+"\">"); 440 } 441 442 out.println(" <style type=\"text/css\">"); 443 Iterator it; 444 if (customized!=null) { 445 it = customized.getCustomizedGUI().getStyleSheetURLs().iterator(); 446 while (it.hasNext()) { 447 String url = (String )it.next(); 448 out.println(" @import \""+url+"\";"); 449 } 450 } 451 it = GuiAC.getStyleSheetURLs().iterator(); 452 while (it.hasNext()) { 453 String url = (String )it.next(); 454 out.println(" @import \""+url+"\";"); 455 } 456 out.println(" </style>"); 457 } 458 459 463 protected void genBody(PrintWriter out) throws IOException { 464 out.println(" <p>Empty page</p>"); 465 } 466 467 469 public void setTable(View table) { 470 this.table = table; 471 } 472 473 public void setRow(int row) { 474 this.row = row; 475 } 476 477 public void setColumn(int column) { 478 this.column = column; 479 } 480 481 Hashtable attributes = new Hashtable (); 482 public void setAttribute(String name, String value) { 483 attributes.put(name,value); 484 } 485 protected void printAttributes(PrintWriter out) { 486 Iterator it = attributes.entrySet().iterator(); 487 while (it.hasNext()) { 488 Map.Entry entry = (Map.Entry )it.next(); 489 out.write(" "+entry.getKey()+"=\""+entry.getValue()+"\""); 490 } 491 } 492 493 protected void openForm(PrintWriter out) { 494 out.println(" <form action=\""+ 495 ((WebDisplay)context.getDisplay()).getServletName()+"\" "+ 496 "method=\"post\" accept-charset=\""+GuiAC.getEncoding()+"\" "+ 497 "enctype=\"multipart/form-data\">"); 498 } 499 500 protected void closeForm(PrintWriter out) { 501 out.println(" </form>"); 502 } 503 504 protected void showFormButtons(PrintWriter out, boolean dialog) { 505 out.println(" <div class=\"actions\">"); 506 out.println(" <input type=\"hidden\" name=\"source\" value=\""+getId()+"\">"); 507 if (dialog) { 508 showButton(out,null,GuiAC.getLabelOK(),"onOK"); 509 showButton(out,null,GuiAC.getLabelCancel(),"onCancel"); 510 } else { 511 showButton(out,null,GuiAC.getLabelClose(),"onOK"); 512 } 513 if (context.hasEnabledEditor()) { 514 showButton(out,null,"Refresh","onRefresh"); 515 } 516 out.println(" </div>"); 517 } 518 519 protected void showFormButtons(PrintWriter out) { 520 showFormButtons(out,true); 521 } 522 523 protected void genEventAndActionButton(PrintWriter out, String event) { 524 showButton(out,null,"Refresh",event); 525 } 526 } 527 | Popular Tags |