org.apache.wicket.ResourceReference类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中org.apache.wicket.ResourceReference类的一些代码示例,展示了ResourceReference类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ResourceReference类的具体详情如下:
包路径:org.apache.wicket.ResourceReference
类名称:ResourceReference

ResourceReference介绍

[英]ResourceReference is essentially a reference to an actual resource which is shared through the Application. A ResourceReference has a name and a scope (within which the name must be unique). It may also have a locale or style. The locale and/or style do not need to be set on a resource reference because those values will automatically be determined based on the context in which the resource is being used. For example, if a ResourceReference is attached to an Image component, when the locale for the page switches, the Image component will notice this and automatically change the locale for the referenced resource as appropriate. It's for this reason that you don't typically have to use the constructor overloads taking a Locale or style (these details are essentially internal and so the framework uses setLocale/setStyle internally so you don't have to worry about it).

Package resources (resources which can be pulled from the classpath) do not have to be pre-registered. For custom situations though, resources may be added to the Application when the Application is constructed using Application#getSharedResources() followed by SharedResources#add(Class,String,Locale,String,Resource), SharedResources#add(String,Locale,Resource)or SharedResources#add(String,Resource).

If a component has its own shared resource which should not be added to the application construction logic in this way, it can lazy-initialize the resource by overriding the #newResource() method. In this method, the component should supply logic that creates the shared resource. By default the #newResource() method tries to resolve to a package resource.
[中]ResourceReference本质上是对通过应用程序共享的实际资源的引用。ResourceReference有一个名称和一个范围(在该范围内名称必须是唯一的)。它也可能有一个地区或风格。不需要在资源引用上设置区域设置和/或样式,因为这些值将根据资源使用的上下文自动确定。例如,如果ResourceReference附加到图像组件,当页面的区域设置切换时,图像组件将注意到这一点,并根据需要自动更改所引用资源的区域设置。正是由于这个原因,您通常不必使用采用区域设置或样式的构造函数重载(这些细节基本上是内部的,因此框架在内部使用setLocale/setStyle,因此您不必担心)。
包资源(可以从类路径提取的资源)不必预先注册。但是,对于自定义情况,当使用应用程序#getSharedResources()然后是SharedResources#add(类、字符串、区域设置、字符串、资源)、SharedResources#add(字符串、区域设置、资源)或SharedResources#add(字符串、资源)构建应用程序时,可以向应用程序添加资源。
如果组件有自己的共享资源,而不应该以这种方式添加到应用程序构造逻辑中,那么它可以通过重写#newResource()方法来延迟初始化资源。在这种方法中,组件应该提供创建共享资源的逻辑。默认情况下,#newResource()方法尝试解析为包资源。

代码示例

代码示例来源:origin: org.geoserver.web/web-core

/**
 * Returns the resource reference to a replacement favicon for the header contribution,
 * or null if there is no favicon replacement
 * @return
 */
public ResourceReference getFavicon() {
  if( scope != null && faviconFilename != null) {
    return new ResourceReference(scope, faviconFilename);
  }
  
  return null;
}

代码示例来源:origin: org.onehippo.cms7/hippo-cms-yui

public String getSharedResourceKey() {
  return reference.getSharedResourceKey();
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * Creates new package resource reference.
 * 
 * @param app
 * @param scope
 * @param name
 * @return created resource reference
 */
protected ResourceReference createPackageResourceReference(Application app, Class<?> scope,
  String name)
{
  ResourceReference resourceReference = new ResourceReference(scope, name);
  resourceReference.bind(app);
  return resourceReference;
}

代码示例来源:origin: org.wicketstuff/yui

/**
 * Construct.
 * 
 * @param reference
 */
public ImageResourceInfo(ResourceReference reference)
{
  this(PackageResource.get(reference.getScope(), reference.getName()));
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * Factory method for lazy initialization of shared resources.
 * 
 * @return The resource
 */
protected Resource newResource()
{
  PackageResource packageResource = PackageResource.get(getScope(), getName(), getLocale(),
      getStyle());
  if (packageResource != null)
  {
    locale = packageResource.getLocale();
  }
  else
  {
    throw new IllegalArgumentException("package resource [scope=" + getScope() + ",name=" +
        getName() + ",locale=" + getLocale() + "style=" + getStyle() + "] not found");
  }
  return packageResource;
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

requestParameters.setResourceKey(resourceReference.getSharedResourceKey());
String name = resourceReference.getName();
if (getApplication().getResourceSettings().getAddLastModifiedTimeToResourceReferenceUrl() &&
  !Strings.isEmpty(name) && !name.endsWith("/")) // test for / because it could be a
  Time time = resourceReference.lastModifiedTime();
  if (time != null)

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * @return the shared resource key for this resource reference.
 */
public final String getSharedResourceKey()
{
  Application application = Application.get();
  if (useSessionLocale)
  {
    if (!Objects.equal(locale, Session.get().getLocale()))
    {
      setLocale(Session.get().getLocale());
    }
  }
  if (useSessionStyle)
  {
    if (!Objects.equal(style, Session.get().getStyle()))
    {
      setStyle(Session.get().getStyle());
    }
  }
  bind(application);
  return application.getSharedResources().resourceKey(getScope(), name, locale, style);
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * Binds this resource if it is shared
 */
public final void bind()
{
  // If we have a resource reference
  if (resourceReference != null)
  {
    // Bind the reference to the application
    resourceReference.bind(component.getApplication());
    // Then dereference the resource
    resource = resourceReference.getResource();
    if (resource instanceof PackageResource)
    {
      resourceReference.setLocale(((PackageResource)resource).getLocale());
    }
  }
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * @return the shared resource key for this resource reference.
 */
public final String getSharedResourceKey()
{
  Application application = Application.get();
  bind(application);
  return application.getSharedResources().resourceKey(getScope(), name, locale, style);
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

resource = sharedResources.get(getScope(), name, locale, style, true);
  resource = newResource();
  if (resource == null)
    resource = sharedResources.get(getScope(), name, locale, style, false);
    if (resource == null)
      PackageResource packageResource = PackageResource.get(getScope(), name);
  sharedResources.add(getScope(), name, locale, style, resource);

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * @see java.lang.Object#equals(java.lang.Object)
 */
public boolean equals(Object obj)
{
  if (obj instanceof ResourceReference)
  {
    ResourceReference that = (ResourceReference)obj;
    return Objects.equal(getScope().getName(), that.getScope().getName()) &&
        Objects.equal(name, that.name) && Objects.equal(locale, that.locale) &&
        Objects.equal(style, that.style);
  }
  return false;
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
   * @see org.apache.wicket.markup.html.link.Link#getURL()
   */
  protected final CharSequence getURL()
  {
    if (resourceReference != null)
    {
      // TODO post 1.2: should we have support for locale changes when the
      // resource reference (or resource??) is set manually..
      // We should get a new resource reference for the current locale
      // then
      // that points to the same resource but with another locale if it
      // exists.
      // something like
      // SharedResource.getResourceReferenceForLocale(resourceReference);

      resourceReference.bind(getApplication());
      return getRequestCycle().urlFor(resourceReference, resourceParameters);
    }
    return urlFor(IResourceListener.INTERFACE);
  }
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

@Override
public boolean acceptReference(ResourceReference ref)
{
  if (ref instanceof AbstractResourceDependentResourceReference)
  {
    return ResourceType.CSS.equals(((AbstractResourceDependentResourceReference)ref).getResourceType());
  }
  if (!Strings.isEmpty(ref.getName()) && ref.getName().endsWith(".css"))
  {
    return true;
  }
  return false;
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * Factory method for lazy initialization of shared resources.
 * 
 * @return The resource
 */
protected Resource newResource()
{
  PackageResource packageResource = PackageResource.get(getScope(), getName(), getLocale(),
    getStyle());
  if (packageResource != null)
  {
    locale = packageResource.getLocale();
  }
  else
  {
    throw new IllegalArgumentException("package resource [scope=" + getScope() + ",name=" +
      getName() + ",locale=" + getLocale() + "style=" + getStyle() + "] not found");
  }
  return packageResource;
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * Returns a URL that references a shared resource through the provided resource reference.
 * 
 * @param resourceReference
 *            The resource reference where a url must be generated for.
 * @param parameters
 *            The parameters to pass to the resource.
 * @return The url for the shared resource
 */
public final CharSequence urlFor(final ResourceReference resourceReference, ValueMap parameters)
{
  RequestParameters requestParameters = new RequestParameters();
  requestParameters.setResourceKey(resourceReference.getSharedResourceKey());
  if (getApplication().getResourceSettings().getAddLastModifiedTimeToResourceReferenceUrl() &&
    !Strings.isEmpty(resourceReference.getName()))
  {
    Time time = resourceReference.lastModifiedTime();
    if (time != null)
    {
      if (parameters == null)
      {
        parameters = new ValueMap();
        parameters.put("wicket:lm", new Long(time.getMilliseconds()));
      }
    }
  }
  requestParameters.setParameters(parameters);
  return encodeUrlFor(new SharedResourceRequestTarget(requestParameters));
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * Binds this resource if it is shared
 */
public final void bind()
{
  // If we have a resource reference
  if (resourceReference != null)
  {
    // Bind the reference to the application
    resourceReference.bind(component.getApplication());
    // Then dereference the resource
    resource = resourceReference.getResource();
    if (resource instanceof PackageResource)
    {
      resourceReference.setLocale(((PackageResource)resource).getLocale());
    }
  }
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * Performs the tree initialization. Adds header contribution for the stylesheet.
 */
private void init()
{
  ResourceReference css = getCSS();
  if (css != null)
  {
    add(CSSPackageResource.getHeaderContribution(css.getScope(), css.getName()));
  }
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

resource = sharedResources.get(getScope(), name, locale, style, true);
if ((resource != null) && (resource instanceof PackageResource))
  resource = newResource();
  if (resource == null)
    resource = sharedResources.get(getScope(), name, locale, style, false);
    if (resource == null)
      resource = PackageResource.get(getScope(), name);
  sharedResources.add(getScope(), name, locale, style, resource);

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj)
{
  if (obj instanceof ResourceReference)
  {
    ResourceReference that = (ResourceReference)obj;
    return Objects.equal(getScope().getName(), that.getScope().getName()) &&
      Objects.equal(name, that.name) && Objects.equal(locale, that.locale) &&
      Objects.equal(style, that.style);
  }
  return false;
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
   * @see org.apache.wicket.markup.html.link.Link#getURL()
   */
  @Override
  protected final CharSequence getURL()
  {
    if (resourceReference != null)
    {
      // TODO post 1.2: should we have support for locale changes when the
      // resource reference (or resource??) is set manually..
      // We should get a new resource reference for the current locale
      // then
      // that points to the same resource but with another locale if it
      // exists.
      // something like
      // SharedResource.getResourceReferenceForLocale(resourceReference);

      resourceReference.bind(getApplication());
      return getRequestCycle().urlFor(resourceReference, resourceParameters);
    }
    return urlFor(IResourceListener.INTERFACE);
  }
}

相关文章