1 15 package org.apache.tapestry; 16 17 import java.util.EventListener ; 18 import java.util.Locale ; 19 20 import javax.swing.event.EventListenerList ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.hivemind.ApplicationRuntimeException; 25 import org.apache.tapestry.event.ChangeObserver; 26 import org.apache.tapestry.event.PageBeginRenderListener; 27 import org.apache.tapestry.event.PageDetachListener; 28 import org.apache.tapestry.event.PageEndRenderListener; 29 import org.apache.tapestry.event.PageEvent; 30 import org.apache.tapestry.event.PageAttachListener; 31 import org.apache.tapestry.event.PageRenderListener; 32 import org.apache.tapestry.event.PageValidateListener; 33 import org.apache.tapestry.util.StringSplitter; 34 35 41 42 public abstract class AbstractPage extends BaseComponent implements IPage 43 { 44 private static final Log LOG = LogFactory.getLog(AbstractPage.class); 45 46 50 51 private ChangeObserver _changeObserver; 52 53 56 57 private IEngine _engine; 58 59 63 64 private Object _visit; 65 66 71 72 private String _pageName; 73 74 77 78 private IRequestCycle _requestCycle; 79 80 83 84 private Locale _locale; 85 86 94 95 private EventListenerList _listenerList; 96 97 103 private String _outputEncoding; 104 105 111 112 public AbstractPage() 113 { 114 initialize(); 115 } 116 117 129 130 public void detach() 131 { 132 Tapestry.addMethodInvocation(Tapestry.ABSTRACTPAGE_DETACH_METHOD_ID); 133 134 137 _changeObserver = null; 138 139 firePageDetached(); 140 141 initialize(); 142 143 _engine = null; 144 _visit = null; 145 _requestCycle = null; 146 } 147 148 159 160 protected void initialize() 161 { 162 } 164 165 public IEngine getEngine() 166 { 167 return _engine; 168 } 169 170 public ChangeObserver getChangeObserver() 171 { 172 return _changeObserver; 173 } 174 175 178 179 public String getExtendedId() 180 { 181 return _pageName; 182 } 183 184 187 188 public String getIdPath() 189 { 190 return null; 191 } 192 193 197 198 public Locale getLocale() 199 { 200 return _locale; 201 } 202 203 public void setLocale(Locale value) 204 { 205 if (_locale != null) 206 throw new ApplicationRuntimeException(Tapestry 207 .getMessage("AbstractPage.attempt-to-change-locale")); 208 209 _locale = value; 210 } 211 212 public IComponent getNestedComponent(String path) 213 { 214 StringSplitter splitter; 215 IComponent current; 216 String [] elements; 217 int i; 218 219 if (path == null) 220 return this; 221 222 splitter = new StringSplitter('.'); 223 current = this; 224 225 elements = splitter.splitToArray(path); 226 for (i = 0; i < elements.length; i++) 227 { 228 current = current.getComponent(elements[i]); 229 } 230 231 return current; 232 233 } 234 235 241 242 public void attach(IEngine engine, IRequestCycle cycle) 243 { 244 if (_engine != null) 245 LOG.error(this + " attach(" + engine + "), but engine = " + _engine); 246 247 _engine = engine; 248 _requestCycle = cycle; 249 250 firePageAttached(); 251 } 252 253 262 263 public void renderPage(IMarkupWriter writer, IRequestCycle cycle) 264 { 265 try 266 { 267 firePageBeginRender(); 268 269 beginResponse(writer, cycle); 270 271 if (!cycle.isRewinding()) 272 cycle.commitPageChanges(); 273 274 render(writer, cycle); 275 } 276 finally 277 { 278 firePageEndRender(); 279 } 280 } 281 282 public void setChangeObserver(ChangeObserver value) 283 { 284 _changeObserver = value; 285 } 286 287 288 289 public void setPageName(String pageName) 290 { 291 if (_pageName != null) 292 throw new ApplicationRuntimeException(Tapestry 293 .getMessage("AbstractPage.attempt-to-change-name")); 294 295 _pageName = pageName; 296 } 297 298 301 302 public void validate(IRequestCycle cycle) 303 { 304 Tapestry.addMethodInvocation(Tapestry.ABSTRACTPAGE_VALIDATE_METHOD_ID); 305 306 firePageValidate(); 307 } 308 309 314 315 public void beginResponse(IMarkupWriter writer, IRequestCycle cycle) 316 { 317 } 318 319 public IRequestCycle getRequestCycle() 320 { 321 return _requestCycle; 322 } 323 324 327 328 public Object getVisit() 329 { 330 if (_visit == null) 331 _visit = _engine.getVisit(_requestCycle); 332 333 return _visit; 334 } 335 336 341 342 public Object getGlobal() 343 { 344 return _engine.getGlobal(); 345 } 346 347 public void addPageDetachListener(PageDetachListener listener) 348 { 349 addListener(PageDetachListener.class, listener); 350 } 351 352 private void addListener(Class listenerClass, EventListener listener) 353 { 354 if (_listenerList == null) 355 _listenerList = new EventListenerList (); 356 357 _listenerList.add(listenerClass, listener); 358 } 359 360 363 364 private void removeListener(Class listenerClass, EventListener listener) 365 { 366 if (_listenerList != null) 367 _listenerList.remove(listenerClass, listener); 368 } 369 370 public void addPageRenderListener(PageRenderListener listener) 371 { 372 addPageBeginRenderListener(listener); 373 addPageEndRenderListener(listener); 374 } 375 376 377 public void addPageBeginRenderListener(PageBeginRenderListener listener) 378 { 379 addListener(PageBeginRenderListener.class, listener); 380 } 381 382 383 public void addPageEndRenderListener(PageEndRenderListener listener) 384 { 385 addListener(PageEndRenderListener.class, listener); 386 } 387 388 389 public void removePageBeginRenderListener(PageBeginRenderListener listener) 390 { 391 removeListener(PageBeginRenderListener.class, listener); 392 } 393 394 395 public void removePageEndRenderListener(PageEndRenderListener listener) 396 { 397 removeListener(PageEndRenderListener.class, listener); 398 } 399 400 403 404 protected void firePageAttached() 405 { 406 if (_listenerList == null) 407 return; 408 409 PageEvent event = null; 410 Object [] listeners = _listenerList.getListenerList(); 411 412 for (int i = 0; i < listeners.length; i += 2) 413 { 414 if (listeners[i] == PageAttachListener.class) 415 { 416 PageAttachListener l = (PageAttachListener) listeners[i + 1]; 417 418 if (event == null) 419 event = new PageEvent(this, _requestCycle); 420 421 l.pageAttached(event); 422 } 423 } 424 } 425 426 429 430 protected void firePageDetached() 431 { 432 if (_listenerList == null) 433 return; 434 435 PageEvent event = null; 436 Object [] listeners = _listenerList.getListenerList(); 437 438 for (int i = 0; i < listeners.length; i += 2) 439 { 440 if (listeners[i] == PageDetachListener.class) 441 { 442 PageDetachListener l = (PageDetachListener) listeners[i + 1]; 443 444 if (event == null) 445 event = new PageEvent(this, _requestCycle); 446 447 l.pageDetached(event); 448 } 449 } 450 } 451 452 455 456 protected void firePageBeginRender() 457 { 458 if (_listenerList == null) 459 return; 460 461 PageEvent event = null; 462 Object [] listeners = _listenerList.getListenerList(); 463 464 for (int i = 0; i < listeners.length; i += 2) 465 { 466 if (listeners[i] == PageBeginRenderListener.class) 467 { 468 PageBeginRenderListener l = (PageBeginRenderListener) listeners[i + 1]; 469 470 if (event == null) 471 event = new PageEvent(this, _requestCycle); 472 473 l.pageBeginRender(event); 474 } 475 } 476 } 477 478 481 482 protected void firePageEndRender() 483 { 484 if (_listenerList == null) 485 return; 486 487 PageEvent event = null; 488 Object [] listeners = _listenerList.getListenerList(); 489 490 for (int i = 0; i < listeners.length; i += 2) 491 { 492 if (listeners[i] == PageEndRenderListener.class) 493 { 494 PageEndRenderListener l = (PageEndRenderListener) listeners[i + 1]; 495 496 if (event == null) 497 event = new PageEvent(this, _requestCycle); 498 499 l.pageEndRender(event); 500 } 501 } 502 } 503 504 507 508 public void removePageDetachListener(PageDetachListener listener) 509 { 510 removeListener(PageDetachListener.class, listener); 511 } 512 513 public void removePageRenderListener(PageRenderListener listener) 514 { 515 removePageBeginRenderListener(listener); 516 removePageEndRenderListener(listener); 517 } 518 519 520 521 public void beginPageRender() 522 { 523 firePageBeginRender(); 524 } 525 526 527 528 public void endPageRender() 529 { 530 firePageEndRender(); 531 } 532 533 534 535 public String getPageName() 536 { 537 return _pageName; 538 } 539 540 public void addPageValidateListener(PageValidateListener listener) 541 { 542 addListener(PageValidateListener.class, listener); 543 } 544 545 public void removePageValidateListener(PageValidateListener listener) 546 { 547 removeListener(PageValidateListener.class, listener); 548 } 549 550 551 public void addPageAttachListener(PageAttachListener listener) 552 { 553 addListener(PageAttachListener.class, listener); 554 } 555 556 557 public void removePageAttachListener(PageAttachListener listener) 558 { 559 removeListener(PageAttachListener.class, listener); 560 } 561 562 protected void firePageValidate() 563 { 564 if (_listenerList == null) 565 return; 566 567 PageEvent event = null; 568 Object [] listeners = _listenerList.getListenerList(); 569 570 for (int i = 0; i < listeners.length; i += 2) 571 { 572 if (listeners[i] == PageValidateListener.class) 573 { 574 PageValidateListener l = (PageValidateListener) listeners[i + 1]; 575 576 if (event == null) 577 event = new PageEvent(this, _requestCycle); 578 579 l.pageValidate(event); 580 } 581 } 582 } 583 584 590 protected String getOutputEncoding() 591 { 592 if (_outputEncoding == null) 593 _outputEncoding = getEngine().getOutputEncoding(); 594 595 return _outputEncoding; 596 } 597 } | Popular Tags |