1 19 20 package za.org.coefficient.html; 21 22 import za.org.coefficient.core.Constants; 23 import za.org.coefficient.core.Project; 24 import za.org.coefficient.interfaces.CoefficientContext; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.io.Serializable ; 29 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.TreeMap ; 34 35 import javax.naming.Context ; 36 import javax.naming.NamingException ; 37 38 import za.org.coefficient.util.common.InvokerFactory; 39 40 55 public class Page implements Serializable { 56 58 private static final String JAVASCRIPT = 59 "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" HREF=\"images?image=/calendar-win2k-cold-1.css\" title=\"win2k-cold-1\" />\n <script type=\"text/javascript\" SRC=\"images?image=/calendar.js\"></script>\n <script type=\"text/javascript\" SRC=\"images?image=/calendar-en.js\"></script>\n <script type=\"text/javascript\" SRC=\"images?image=/calendar-setup.js\"></script>\n <script language=\"Javascript\">\n<!--\nfunction render(t) {\nif (document.getElementById(t).style.display == \"none\") {\ndocument.getElementById(t).style.display=\"block\"\n} else {\ndocument.getElementById(t).style.display=\"none\"\n}\n}\n//-->\n</script>"; 60 private static final String SIDE_WRAP_1 = 61 "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><br><a style=\"color:blue;cursor:hand;FONT-FAMILY: Verdana;FONT-SIZE: 14px;\" name=\""; 62 private static final String SIDE_WRAP_2 = "\" onClick=\"render('"; 63 private static final String SIDE_WRAP_3 = 64 "')\">+/-</a></td></tr></table><table id=\""; 65 private static final String SIDE_WRAP_4 = 66 "\" style=\"display:block\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" ><tr><td width=\"100%\">"; 67 private static final String SIDE_WRAP_5 = "</td></tr></table>"; 68 private static final String NAV_BLOCK_1 = 69 "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" >\n<tr>\n<td class=\"navBlock\">\n"; 70 private static final String NAV_BLOCK_2 = "\n</td>\n</tr>\n<tr><td>"; 72 private static final String NAV_BLOCK_3 = "\n<br></td></tr></table>"; 73 74 76 private CoefficientContext context = null; 77 private ArrayList errors; 78 private Project project = null; 79 80 81 private String theme = null; 82 private TreeMap centerContent = new TreeMap (); 83 private HashMap centerModulePositions = new HashMap (); 84 private TreeMap eastContent = new TreeMap (); 85 private HashMap eastModulePositions = new HashMap (); 86 private TreeMap northContent = new TreeMap (); 87 private HashMap northModulePositions = new HashMap (); 88 private TreeMap southContent = new TreeMap (); 89 private HashMap southModulePositions = new HashMap (); 90 private TreeMap westContent = new TreeMap (); 91 private HashMap westModulePositions = new HashMap (); 92 private int centerContentMax = Integer.MAX_VALUE; 93 private int eastContentMax = Integer.MAX_VALUE; 94 private int northContentMax = Integer.MAX_VALUE; 95 private int southContentMax = Integer.MAX_VALUE; 96 private int westContentMax = Integer.MAX_VALUE; 97 98 101 105 public Page() { 106 errors = new ArrayList (); 107 } 108 109 public Page(CoefficientContext ctx) { 110 errors = new ArrayList (); 111 context = ctx; 112 theme = determineTheme(); 113 project = ctx.getProject(); 114 } 115 116 118 public Project getProject() { 119 return project; 120 } 121 122 public String getCenter() throws Exception { 123 StringBuffer sb = new StringBuffer (""); 124 126 Object [] params = { this }; 127 sb.append("<td width=\""); 128 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCenterPercentage", params)); 129 sb.append("\" valign=\"top\" >"); 130 sb.append(getErrors()); 131 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCenter", params)); 132 sb.append("</td>"); 133 134 return sb.toString(); 135 } 136 137 public void setCenterContent(String html, Integer position, String moduleName) { 138 if (position == null) { 139 position = new Integer (centerContentMax); 140 centerContentMax--; 141 } 142 this.centerContent.put(position, html); 143 this.centerModulePositions.put(moduleName, position); 144 } 145 146 public String getCenterContent() { 147 return getCenterContent(false); 148 } 149 150 public String getCenterContent(boolean includeNavBlock) { 151 StringBuffer sb = new StringBuffer (); 152 for (Iterator it = centerContent.keySet().iterator(); it.hasNext();) { 153 Integer key = (Integer ) it.next(); 154 String moduleName = null; 155 for(Iterator itr = centerModulePositions.keySet().iterator(); 156 itr.hasNext();){ 157 String key2 = (String )itr.next(); 158 Integer value = (Integer )centerModulePositions.get(key2); 159 if(value.equals(key)) { 160 moduleName = key2; 161 break; 162 } 163 } 164 String content = (String ) centerContent.get(key); 165 if (includeNavBlock && !content.trim().equals("") 166 && moduleName != null && !"".equals(moduleName.trim())) { 167 sb.append(NAV_BLOCK_1); 168 sb.append(moduleName); 169 sb.append(NAV_BLOCK_2); 170 sb.append(content); 171 sb.append(NAV_BLOCK_3); 172 } else { 173 sb.append(content); 174 } 175 } 176 177 return sb.toString(); 178 } 179 180 public String getEast() throws Exception { 181 StringBuffer sb = new StringBuffer (""); 182 Object [] params = { this }; 183 190 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getEast", params)); 191 194 195 return sb.toString(); 196 } 197 198 public void setEastContent(String html, Integer position, String moduleName) { 199 if (position == null) { 200 position = new Integer (eastContentMax); 201 eastContentMax--; 202 } 203 this.eastContent.put(position, html); 204 this.eastModulePositions.put(moduleName, position); 205 } 206 207 public String getEastContent() { 208 return getEastContent(true); 209 } 210 211 public String getEastContent(boolean includeNavBlock) { 212 StringBuffer sb = new StringBuffer (); 213 for (Iterator it = eastContent.keySet().iterator(); it.hasNext();) { 214 Integer key = (Integer ) it.next(); 215 String moduleName = null; 216 for(Iterator itr = eastModulePositions.keySet().iterator(); 217 itr.hasNext();){ 218 String key2 = (String )itr.next(); 219 Integer value = (Integer )eastModulePositions.get(key2); 220 if(value.equals(key)) { 221 moduleName = key2; 222 break; 223 } 224 } 225 String content = (String ) eastContent.get(key); 226 if (includeNavBlock && !content.trim().equals("") 227 && moduleName != null && !"".equals(moduleName.trim())) { 228 sb.append(NAV_BLOCK_1); 229 sb.append(moduleName); 230 sb.append(NAV_BLOCK_2); 231 sb.append(content); 232 sb.append(NAV_BLOCK_3); 233 } else { 234 sb.append(content); 235 } 236 } 237 238 return sb.toString(); 239 } 240 241 public void setError(String html) { 242 this.errors.add("<font color=\"red\">" + html + "</font>"); 243 } 244 245 public String getErrors() { 246 StringBuffer sb = new StringBuffer (""); 247 for (Iterator it = errors.iterator(); it.hasNext();) { 248 String error = (String ) it.next(); 249 sb.append("<li>" + error + "</li><br>"); 250 } 251 252 return sb.toString(); 253 } 254 255 public String getFooter() throws Exception { 256 StringBuffer sb = new StringBuffer (""); 257 Object [] params = { this }; 258 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getFooter", params)); 259 sb.append("</body>\n</html>\n"); 260 261 return sb.toString(); 262 } 263 264 public String getHeader() throws Exception { 265 StringBuffer sb = new StringBuffer (""); 266 Object [] params = { this }; 267 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getHeader", params)); 268 sb.append(JAVASCRIPT); 269 270 return sb.toString(); 271 } 272 273 public void setModuleContent(String html, String name) throws Exception { 274 this.setModuleContent(html, name, null); 275 } 276 277 public void setModuleContent(String html, String name, String utilityTheme) throws Exception { 278 Object [] params = { this, name, html }; 279 if (utilityTheme==null) { 280 InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "format", params); 281 } 282 else 283 { 284 try { 285 Context initialContext = za.org.coefficient.util.common.ContextUtil.getContext(); 286 if (initialContext.lookup(Constants.JNDI_CONTEXT + "/" 287 + "utilityThemes" + "/"+utilityTheme)!=null) { 288 InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(utilityTheme, "format", params); 289 theme = utilityTheme; 290 } 291 } catch (NamingException ne) { 292 ne.printStackTrace(); 293 } 294 } 295 } 296 297 public String getNorth() throws Exception { 298 Object [] params = { this }; 299 StringBuffer sb = new StringBuffer (""); 300 sb.append("<table width=\""); 301 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getTableWidth", params)); 302 sb.append("\" border=\"0\" cellspacing=\""); 303 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCellSpacing", params)); 304 sb.append("\""); 305 sb.append(" cellpadding=\""); 306 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCellPadding", params)); 307 sb.append("\" bgcolor=\""); 308 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getNorthBackGround", params)); 309 sb.append("\">"); 310 sb.append("<tr><td>"); 311 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getNorth", params)); 312 313 return sb.toString(); 314 } 315 316 public void setNorthContent(String html, Integer position, String moduleName) { 317 if (position == null) { 318 position = new Integer (northContentMax); 319 northContentMax--; 320 } 321 this.northContent.put(position, html); 322 this.northModulePositions.put(moduleName, position); 323 } 324 325 public String getNorthContent() { 326 return getNorthContent(false); 327 } 328 329 public String getNorthContent(boolean includeNavBlock) { 330 StringBuffer sb = new StringBuffer (); 331 for (Iterator it = northContent.keySet().iterator(); it.hasNext();) { 332 Integer key = (Integer ) it.next(); 333 String moduleName = null; 334 for(Iterator itr = northModulePositions.keySet().iterator(); 335 itr.hasNext();){ 336 String key2 = (String )itr.next(); 337 Integer value = (Integer )northModulePositions.get(key2); 338 if(value.equals(key)) { 339 moduleName = key2; 340 break; 341 } 342 } 343 String content = (String ) northContent.get(key); 344 if (includeNavBlock && !content.trim().equals("") 345 && moduleName != null && !"".equals(moduleName.trim())) { 346 sb.append(NAV_BLOCK_1); 347 sb.append(moduleName); 348 sb.append(NAV_BLOCK_2); 349 sb.append(content); 350 sb.append(NAV_BLOCK_3); 351 } else { 352 sb.append(content); 353 } 354 } 355 return sb.toString(); 356 } 357 358 public String getSouth() throws Exception { 359 Object [] params = { this }; 360 StringBuffer sb = new StringBuffer (""); 361 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getSouth", params)); 362 363 return sb.toString(); 364 } 365 366 public void setSouthContent(String html, Integer position, String moduleName) { 367 if (position == null) { 368 position = new Integer (southContentMax); 369 southContentMax--; 370 } 371 this.southContent.put(position, html); 372 this.southModulePositions.put(moduleName, position); 373 } 374 375 public String getSouthContent() { 376 return getSouthContent(false); 377 } 378 379 public String getSouthContent(boolean includeNavBlock) { 380 StringBuffer sb = new StringBuffer (); 381 for (Iterator it = southContent.keySet().iterator(); it.hasNext();) { 382 Integer key = (Integer ) it.next(); 383 String moduleName = null; 384 for(Iterator itr = southModulePositions.keySet().iterator(); 385 itr.hasNext();){ 386 String key2 = (String )itr.next(); 387 Integer value = (Integer )southModulePositions.get(key2); 388 if(value.equals(key)) { 389 moduleName = key2; 390 break; 391 } 392 } 393 String content = (String ) southContent.get(key); 394 if (includeNavBlock && !content.trim().equals("") 395 && moduleName != null && !"".equals(moduleName.trim())) { 396 sb.append(NAV_BLOCK_1); 397 sb.append(moduleName); 398 sb.append(NAV_BLOCK_2); 399 sb.append(content); 400 sb.append(NAV_BLOCK_3); 401 } else { 402 sb.append(content); 403 } 404 } 405 return sb.toString(); 406 } 407 408 public String getThemeWelcomeContent() throws Exception { 409 return (String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getWelcomePageContent", null); 410 } 411 412 public String getWest() throws Exception { 413 StringBuffer sb = new StringBuffer (""); 414 sb.append("<!--here-->\n<tr bgcolor=\"#FFFFFF\">"); 415 Object [] params = { this }; 416 sb.append((String )InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getWest", params)); 417 return sb.toString(); 418 } 419 420 public void setWestContent(String html, Integer position, String moduleName) { 421 if (position == null) { 422 position = new Integer (westContentMax); 423 westContentMax--; 424 } 425 this.westContent.put(position, html); 426 this.westModulePositions.put(moduleName, position); 427 } 428 429 public String getWestContent() { 430 return getWestContent(true); 431 } 432 433 public String getWestContent(boolean includeNavBlock) { 434 StringBuffer sb = new StringBuffer (); 435 for (Iterator it = westContent.keySet().iterator(); it.hasNext();) { 436 Integer key = (Integer ) it.next(); 437 String moduleName = null; 438 for(Iterator itr = westModulePositions.keySet().iterator(); 439 itr.hasNext();){ 440 String key2 = (String )itr.next(); 441 Integer value = (Integer )westModulePositions.get(key2); 442 if(value.equals(key)) { 443 moduleName = key2; 444 break; 445 } 446 } 447 String content = (String ) westContent.get(key); 448 if (includeNavBlock && !content.trim().equals("") 449 && moduleName != null && !"".equals(moduleName.trim())) { 450 sb.append(NAV_BLOCK_1); 451 sb.append(moduleName); 452 sb.append(NAV_BLOCK_2); 453 sb.append(content); 454 sb.append(NAV_BLOCK_3); 455 } else { 456 sb.append(content); 457 } 458 } 459 return sb.toString(); 460 } 461 462 public String displayFootMessage() { 463 StringBuffer sb = new StringBuffer (""); 464 sb.append("<font class=\"pn-sub\">\n${config:" + Constants.CFG_FOOT1 465 + "}<br>\n</font>\n"); 466 467 return sb.toString(); 468 } 469 470 public long process(PrintWriter out) throws IOException , Exception { 471 long time = -System.currentTimeMillis(); 472 473 if ( theme != null && theme.indexOf("PrinterUtil") == -1 ) { 477 out.println(getHeader()); 478 out.println(getNorth()); 479 out.println(getWest()); 480 out.println(getCenter()); 481 out.println(getEast()); 482 out.println(getSouth()); 483 out.println(getFooter()); 484 } 485 else { 486 out.println(getCenterContent()); 487 } 488 489 time += System.currentTimeMillis(); 490 491 return time; 492 } 493 494 504 public String clearModuleContent(String moduleName) { 505 Integer position = (Integer )centerModulePositions.get(moduleName); 507 if(position != null) { 508 centerModulePositions.remove(moduleName); 509 return (String )centerContent.remove(position); 510 } 511 position = (Integer )eastModulePositions.get(moduleName); 512 if(position != null) { 513 eastModulePositions.remove(moduleName); 514 return (String )eastContent.remove(position); 515 } 516 position = (Integer )westModulePositions.get(moduleName); 517 if(position != null) { 518 westModulePositions.remove(moduleName); 519 return (String )westContent.remove(position); 520 } 521 position = (Integer )northModulePositions.get(moduleName); 522 if(position != null) { 523 northModulePositions.remove(moduleName); 524 return (String )northContent.remove(position); 525 } 526 position = (Integer )southModulePositions.get(moduleName); 527 if(position != null) { 528 southModulePositions.remove(moduleName); 529 return (String )southContent.remove(position); 530 } 531 return null; 532 } 533 534 public String getPositionForModule(String moduleName) { 535 Object [] params = {moduleName}; 536 try { 537 return (String )InvokerFactory.getRemoteInvoker() 538 .invokeMethodOnTheme(theme, "getPositionForModule", params); 539 } catch(Exception e) { 540 return Theme.NULL_POSITION; 541 } 542 } 543 544 private String determineTheme() { 545 if ( context == null ) { 546 return ThemeSupport.getCurrentThemeLocalName(); 547 } 548 else { 549 String t = context.getTheme(); 550 if ( t == null || t.equals(ThemeSupport.getCurrentThemeLocalName()) ) { 551 if ( context.getProject() != null ) { 552 String projectTheme = context.getProject().getThemeName(); 553 if ( projectTheme != null && !projectTheme.trim().equals("") ) 554 return projectTheme; 555 else 556 return ThemeSupport.getCurrentThemeLocalName(); 557 } 558 else { 559 return ThemeSupport.getCurrentThemeLocalName(); 560 } 561 } 562 else { 563 return t; 564 } 565 } 566 } 567 } 568 | Popular Tags |