widget.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 653 行 · 第 1/2 页

JAVA
653
字号
/* * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the *   Free SoftwareFoundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Sam */package com.caucho.widget;import com.caucho.lifecycle.Lifecycle;import com.caucho.util.L10N;import java.io.IOException;import java.util.AbstractMap;import java.util.Collections;import java.util.HashSet;import java.util.Map;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;/** * A Widget stores request specific state (S). */abstract public class Widget<S extends WidgetState>  extends AbstractMap<String, Widget>{  private static L10N L = new L10N( Widget.class );  static protected final Logger log =    Logger.getLogger( Widget.class.getName() );  private String _id;  private String _clientId;  private String _parameterName;  private String _modeParameterName;  private String _preferencePrefix;  private String _cssClass;  private WidgetPreferences _widgetPreferences;  private WidgetMode _widgetMode;  private HashSet<WidgetMode> _allowedWidgetModesSet;  private Widget _parent;  private Lifecycle _lifecycle = new Lifecycle();  private RendererCache _rendererCache = new RendererCache();  public Widget()  {  }  public Widget( String id )  {    setId( id );  }  public Widget( Widget parent )  {    setParent( parent );    parent.put( null, this );  }  public Widget( Widget parent, String id )  {    setParent( parent );    setId( id );    parent.put( getId(), this );  }  public void setParent( Widget parent )  {    _parent = parent;  }  public Widget getParent()  {    return _parent;  }  public void setId( String id )  {    _id = id;  }  public String getId()  {    return _id;  }  protected String calculateId( Widget child )  {    return "x" + size();  }  /**   * Default is a concatentation of all parent id's separated   * by `_'.   */  public void setClientId( String clientId )  {    _clientId = clientId;  }  public String getClientId()  {    if ( _clientId == null ) {      if ( _parent == null )        _clientId = getId();      else {        StringBuffer buf = new StringBuffer();        appendId( buf, _parent, '_' );        buf.append( getId() );        _clientId = buf.toString();      }    }    return _clientId;  }  private static void appendId( StringBuffer buf, Widget w, char sep )  {    if ( w == null )      return;    if ( w._parent != null ) {      appendId( buf, w._parent, sep );    }    if ( w.getId() != null ) {      buf.append( w.getId() );      buf.append( sep );    }  }  /**   * Default is a concatentation of all parent id's separated   * by `.'.   */  public void setParameterName( String parameterName )  {    _parameterName = parameterName;  }  public String getParameterName()  {    if ( _parameterName == null ) {      if ( _parent == null )        _parameterName = getId();      else {        StringBuffer buf = new StringBuffer();        appendId( buf, _parent, '.' );        buf.append( getId() );        _parameterName = buf.toString();      }    }    return _parameterName;  }  /**   * Set a css class for renders that use it, the default is   * to use the value of "<code>getId()</code> <code>shortClassName</code>"   * where <i>shortClassName</i> is the classname portion (no package) of the   * widget's class.   *   * If the passed string starts with "+", for example "+clear",   * then the string is appended to the current cssClass.   *   * If the passed string starts with "-", for example "-clear",   * then the string is removed from the current cssClass.   *   * For example, a TextWidget with id "phoneNumber" and clientId   * "form.phoneNumber" will have a default cssClass of   * "textWidget * phoneNumber".   */  public void setCssClass( String cssClass )  {    boolean add =  cssClass.charAt( 0 ) == '+';    boolean del =  cssClass.charAt( 0 ) == '-';    if ( add || del ) {      cssClass = cssClass.substring(1);      String current = getCssClass();      StringBuffer cssClassBuf = new StringBuffer();      if ( current != null && current.length() > 0 ) {        String[] split = current.split( "\\s*" );        for ( int i = 0; i < split.length; i++ ) {          String token = split[i];          if ( token.equals( cssClass ) )            continue;          cssClassBuf.append( token );        }      }      if ( add )        cssClassBuf.append( cssClass );      _cssClass = cssClassBuf.toString();    }    else {      _cssClass = cssClass;    }  }  /**   * Used for information purposes   */  public String getLogId()  {    StringBuffer buf = new StringBuffer();    String classname = getClass().getName();    classname = classname.substring( classname.lastIndexOf('.') + 1);    buf.append( classname );    buf.append('[');    appendLogId( buf, this );    if ( _parent == null )      buf.append('/');    buf.append(']');    return buf.toString();  }  private static void appendLogId( StringBuffer buf, Widget w )  {    if ( w._parent != null ) {      appendLogId( buf, w._parent );      buf.append( '/' );    }    if ( w.getId() != null )      buf.append( w.getId() );  }  /**   * Used by implementing classes to get a css class   * appropriate for this widget.   */  public String getCssClass()  {    if ( _cssClass == null ) {      StringBuffer buf = new StringBuffer();      String shortName = getClass().getName();      int i = shortName.lastIndexOf( '.' ) + 1;      for ( ; i < shortName.length(); i++ )        buf.append( shortName.charAt( i ) );      buf.append(' ');      buf.append( getId() );      _cssClass =  buf.toString();    }    return _cssClass;  }  /**   * Default is to the parameterName with a prefix of "_" and a suffix   * of "_".   */  public void setModeParameterName( String modeParameterName )  {    _modeParameterName = modeParameterName;  }  public String getModeParameterName()  {    if ( _modeParameterName == null )      _modeParameterName = "_" + getParameterName() + "_";    return _modeParameterName;  }  public WidgetPreferences createWidgetPreferences()  {    if ( _widgetPreferences == null ) {      _widgetPreferences = new WidgetPreferences();    }    return _widgetPreferences;  }  public void addPreference( WidgetPreference widgetPreference )  {    WidgetPreferences widgetPreferences = createWidgetPreferences();    widgetPreferences.addPreference( widgetPreference );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?