1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.event.InvalidLowLevelEvent; 19 import org.wings.event.SInvalidLowLevelEventListener; 20 import org.wings.event.SRenderListener; 21 import org.wings.io.Device; 22 import org.wings.plaf.FrameCG; 23 import org.wings.resource.DynamicCodeResource; 24 import org.wings.resource.DynamicResource; 25 import org.wings.session.SessionManager; 26 import org.wings.style.StyleSheet; 27 import org.wings.util.ComponentVisitor; 28 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 36 48 public class SFrame 49 extends SRootContainer 50 implements PropertyChangeListener , LowLevelEventListener { 51 52 private final transient static Log log = LogFactory.getLog(SFrame.class); 53 54 57 protected String title; 58 59 62 protected List headers; 63 64 67 protected StyleSheet styleSheet; 73 protected String statusLine; 74 75 private RequestURL requestURL = null; 76 private String targetResource; 77 78 private HashMap dynamicResources; 79 80 private SComponent focusComponent = null; 82 85 private SButton backButton; 86 87 90 private long defaultBackButtonLastPressedTime; 91 92 95 private boolean noCaching = true; 96 97 102 private boolean fireInvalidLowLevelEvents = false; 103 104 107 public SFrame() { 108 getSession().addPropertyChangeListener("lookAndFeel", this); 109 getSession().addPropertyChangeListener("request.url", this); 110 } 111 112 117 public SFrame(String title) { 118 this(); 119 setTitle(title); 120 } 121 122 127 public void addDynamicResource(DynamicResource d) { 128 if (dynamicResources == null) { 129 dynamicResources = new HashMap (); 130 } 131 dynamicResources.put(d.getClass(), d); 132 } 133 134 140 public void removeDynamicResource(Class dynamicResourceClass) { 141 if (dynamicResources != null) { 142 dynamicResources.remove(dynamicResourceClass); 143 } 144 } 145 146 157 public DynamicResource getDynamicResource(Class c) { 158 if (dynamicResources == null) { 159 dynamicResources = new HashMap (); 160 } 161 return (DynamicResource) dynamicResources.get(c); 162 } 163 164 169 public SFrame getParentFrame() { 170 return this; 171 } 172 173 179 public String getEventEpoch() { 180 return getDynamicResource(DynamicCodeResource.class).getEpoch(); 181 } 182 183 186 public final void setRequestURL(RequestURL requestURL) { 187 this.requestURL = requestURL; 188 } 189 190 195 public final RequestURL getRequestURL() { 196 RequestURL result = null; 197 if (requestURL == null) { 199 requestURL = (RequestURL) SessionManager.getSession().getProperty("request.url"); 200 } 201 if (requestURL != null) { 202 result = (RequestURL) requestURL.clone(); 203 result.setResource(getTargetResource()); 204 } 205 return result; 206 } 207 208 211 public void setTargetResource(String targetResource) { 212 this.targetResource = targetResource; 213 } 214 215 220 public String getTargetResource() { 221 if (targetResource == null) { 222 targetResource = getDynamicResource(DynamicCodeResource.class).getId(); 223 } 224 return targetResource; 225 } 226 227 235 public void addHeader(Object m) { 236 if (!headers().contains(m)) 237 headers.add(m); 238 } 239 240 243 public void removeHeader(Object m) { 244 headers.remove(m); 245 } 246 247 252 public void clearHeaders() { 253 headers().clear(); 254 } 255 256 259 public List headers() { 260 if (headers == null) 261 headers = new ArrayList (2); 262 return headers; 263 } 264 265 270 public void setTitle(String title) { 271 this.title = title; 272 } 273 274 279 public String getTitle() { 280 return title; 281 } 282 283 public void setStatusLine(String s) { 284 statusLine = s; 285 } 286 287 293 public boolean isNoCaching() { 294 return noCaching; 295 } 296 297 public void write(Device s) throws IOException { 298 if (isNoCaching()) 299 reload(); super.write(s); 301 } 302 303 317 public void setNoCaching(boolean noCaching) { 318 this.noCaching = noCaching; 319 } 320 321 326 public void show() { 327 setVisible(true); 328 } 329 330 335 public void hide() { 336 setVisible(false); 337 } 338 339 344 public void setVisible(boolean b) { 345 if (b) { 346 getSession().addFrame(this); 347 } else { 348 getSession().removeFrame(this); 349 } 350 super.setVisible(b); 351 } 352 353 public void propertyChange(PropertyChangeEvent pe) { 354 if ("lookAndFeel".equals(pe.getPropertyName())) { 355 updateComponentTreeCG(getContentPane()); 356 } 357 if ("request.url".equals(pe.getPropertyName())) { 358 setRequestURL((RequestURL) pe.getNewValue()); 359 } 360 } 361 362 private void updateComponentTreeCG(SComponent c) { 363 c.updateCG(); 364 if (c instanceof SContainer) { 365 SComponent[] children = ((SContainer) c).getComponents(); 366 for (int i = 0; i < children.length; i++) { 367 updateComponentTreeCG(children[i]); 368 } 369 } 370 updateCG(); 371 } 372 373 public void setCG(FrameCG cg) { 374 super.setCG(cg); 375 } 376 377 public void invite(ComponentVisitor visitor) 378 throws Exception { 379 visitor.visit(this); 380 } 381 382 386 public void setFocus(SComponent c) { 387 focusComponent = c; 388 } 389 390 public SComponent getFocus() { 391 return focusComponent; 392 } 393 394 public void processLowLevelEvent(String name, String [] values) { 395 if (values.length == 1) { 396 String eventId = values[0]; 397 eventId = eventId.substring("focus_".length()); 398 SComponent component = (SComponent) getDispatcher().getLowLevelEventListener(eventId); 399 component.requestFocus(); 400 } 401 } 402 403 409 public final void addInvalidLowLevelEventListener(SInvalidLowLevelEventListener l) { 410 addEventListener(SInvalidLowLevelEventListener.class, l); 411 fireInvalidLowLevelEvents = true; 412 } 413 414 420 421 public final void removeDispatchListener(SInvalidLowLevelEventListener l) { 422 removeEventListener(SRenderListener.class, l); 423 } 424 425 432 public final void fireInvalidLowLevelEventListener(LowLevelEventListener source) { 433 if (fireInvalidLowLevelEvents) { 434 Object [] listeners = getListenerList(); 435 InvalidLowLevelEvent e = null; 436 for (int i = listeners.length - 2; i >= 0; i -= 2) { 437 if (listeners[i] == SInvalidLowLevelEventListener.class) { 438 if (e == null) 440 e = new InvalidLowLevelEvent(source); 441 ((SInvalidLowLevelEventListener) listeners[i + 1]).invalidLowLevelEvent(e); 442 } 443 } 444 } 445 fireDefaultBackButton(); 446 } 447 448 449 455 public SButton getBackButton() { 456 return backButton; 457 } 458 459 474 public void setBackButton(SButton defaultBackButton) { 475 this.backButton = defaultBackButton; 476 } 477 478 481 private void fireDefaultBackButton() { 482 final int TIME_TO_ASSUME_DOUBLECLICKS_MS = 750; 483 if (this.backButton != null) { 484 long currentTime = System.currentTimeMillis(); 485 if (currentTime - defaultBackButtonLastPressedTime > TIME_TO_ASSUME_DOUBLECLICKS_MS) { 486 backButton.processLowLevelEvent(null, new String []{"1"}); 488 } 489 defaultBackButtonLastPressedTime = currentTime; 490 } 491 } 492 493 public void fireIntermediateEvents() { 494 } 495 496 499 private boolean epochCheckEnabled = true; 500 501 504 public boolean isEpochCheckEnabled() { 505 return epochCheckEnabled; 506 } 507 508 511 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 512 this.epochCheckEnabled = epochCheckEnabled; 513 } 514 } 515 | Popular Tags |