多个“主题”的ClientBundle

| 我们有一个Web应用程序,每个主要客户都需要一个不同的主题。原始开发人员通过查看javascript中的URL并添加样式表来覆盖默认主题来实现此目的。 一个问题是该网站的默认外观为几秒钟,然后突然切换为正确的主题。另一个是,这似乎浪费了很多带宽/时间。 我当前的想法是使用默认的外观来创建\“ default \” ClientBundle,以扩展该接口,并使用@ImageResouce等各种注释并指向不同的位置,使用客户端的图像覆盖每个条目(根据需要) 。 有没有人有这样做的经验?我预见的一个问题是无法使用uibinder样式标签,因为它们静态指向特定的资源包。 有任何想法吗?
已邀请:
覆盖捆绑 是的你可以。 我已经使用ClientBundles进行了覆盖,并且工作正常。您必须做的一件事就是也继承属性的类型。例如:
BigBundle {
  Nestedundle otherBundle();
  ImageResource otherImage();
  Styles css();
}
然后,您必须以这种方式继承:
OtherBigBundle extends BigBundle {
  OtherNestedBundle otherBundle(); // if you want to change it
  ImageResource otherImage(); // of you want to change it
  OtherStyles css(); // of you want to change it
}
OtherNestedBundle extends NestedBundle
OtherStyles extends Styles
至少使用css's:如果将属性声明为不使用子接口,则它们将为相同的CSS类名生成样式,并且将所有样式混合在一起。因此,使用子接口声明覆盖样式:) 灵活的UIBinder 如果使用
UiField(provided=true)
注释,则可以从捆绑软件外部进行设置。这样,您首先要设置捆绑包,然后调用uibindler。假定已创建资源,它将使用资源字段。 延迟绑定 您可以使用GWT.runAsync仅加载正确的捆绑软件。 一些例子 ui.xml
<ui:with field=\'res\' type=\'your.package.TheBundle\'/>
对应的班级
@UiField(provided=true) TheBundle bundle;

private void createTheThing() {
  this.bundle = factory.createBundle();
  MyUiBindler binder = GWT.create(MyUiBindler.class);
  this.panel = binder.createAndBindUi(this);
  ...
}
一些捆绑接口
interface TheBundle extends ClientBundle {
  @ImageResource(\"default.png\")
  ImageResource image1();

  @Source(\"default.css\")
  TheCss css();
}

interface Theme1Bundle extends TheBundle {
  @ImageResource(\"one.png\")
  ImageResource image1(); // type: imageresource is ok

  @Source(\"one.css\")
  OneCss css(); // type: OneCss => use other compiled css class-names

  interface OneCss extends TheCss { // inner-interface, just for fun
     // don\'t need to declare each String method
  }
}
如果您不覆盖某些内容就可以了 捆绑工厂的选项 1)总共
if (...) {
  return GWT.create(TheBundle.class);
} else if (...) {
  return GWT.create(Theme1Bundle.class);
}
2)runAsync(只需加载所需的部分...但是在执行初始部分之后)
if (...) {
   GWT.runAsync(new RunAsyncCallback() {
      public void onSuccess() {
        return GWT.create(TheBundle.class);
      }
      // please program the onFailure method
   });
} else if (...) {
   GWT.runAsync(new RunAsyncCallback() {
      public void onSuccess() {
        return GWT.create(Theme1Bundle.class);
      }
      // please program the onFailure method
   });
}
3)使用延迟绑定和生成器在基于注释的包(例如ѭ10)的编译时自动生成工厂 这个例子来自现实世界。我使用DynamicEntryPointWidgetFactory(简称DEPWidgetFactory)基于标识符字符串创建窗口小部件。每个小部件都是一个应用程序屏幕,每个主菜单项都有必须创建的widgetName。 在您的情况下,id将是要创建的主题。 重要提示:如果使用runAsync,则不能像在前面的示例代码中那样在创建UI之前就创建resourcebundle。您必须询问主题,主题准备好(在回调中)将其传递给小部件构造函数,小部件可以将其分配给其字段。 工厂界面:
public interface DynamicEntryPointWidgetFactory
{
   public void buildWidget(String widgetName, AsyncCallback<Widget> callback);
}
小部件生成的注释:
@Target(ElementType.TYPE)
public @interface EntryPointWidget 
{
    /**
     * The name wich will be used to identify this widget.
     */
    String value();
}
模块配置: 它说:Factory的实现将使用此类生成(另一个选项是使用replace-with,但在我们的情况下,我们没有针对每个语言环境或浏览器的预定义选项,而是更动态的东西)。
<generate-with class=\"com.dia.nexdia.services.gwt.rebind.entrypoint.DynamicEntryPointFactoryGenerator\">
  <when-type-assignable class=\"com.dia.nexdia.services.gwt.client.entrypoint.DynamicEntryPointWidgetFactory\" />
</generate-with>
生成器:
public class DynamicEntryPointFactoryGenerator extends Generator {
    @Override
    public String generate(TreeLogger logger, GeneratorContext context,
            String typeName) throws UnableToCompleteException {
        PrintWriter pw = context.tryCreate(logger,
                \"x.services.gwt.client.entrypoint\",
                \"DynamicEntryPointWidgetFactoryImpl\");

        if (pw != null) {
            // write package, imports, whatever
            pw.append(\"package x.services.gwt.client.entrypoint;\");
            pw.append(\"import x.services.gwt.client.entrypoint.DynamicEntryPointWidgetFactory;\");
            pw.append(\"import com.google.gwt.core.client.GWT;\");
            pw.append(\"import com.google.gwt.core.client.RunAsyncCallback;\");
            pw.append(\"import com.google.gwt.user.client.rpc.AsyncCallback;\");
            pw.append(\"import com.google.gwt.user.client.ui.Widget;\");

            // the class
            pw.append(\"public class DynamicEntryPointWidgetFactoryImpl implements DynamicEntryPointWidgetFactory {\");

            // buildWidget method
            pw.append(\"   public void buildWidget(String widgetName, final AsyncCallback<Widget> callback) {\");

            // iterates over all the classes to find those with EntryPointWidget annotation
            TypeOracle oracle = context.getTypeOracle();
            JPackage[] packages = oracle.getPackages();
            for (JPackage pack : packages) 
            {
                JClassType[] classes = pack.getTypes();
                for (JClassType classtype : classes) 
                {
                    EntryPointWidget annotation = classtype.getAnnotation(EntryPointWidget.class);
                    if (annotation != null) 
                    {
                        String fullName = classtype.getQualifiedSourceName();
                        logger.log(TreeLogger.INFO, \"Entry-point widget found: \" + fullName);

                        pw.append(\"if (\\\"\" + annotation.value() + \"\\\".equals(widgetName)) {\");
                        pw.append(\"   GWT.runAsync(\" + fullName + \".class, new RunAsyncCallback() {\");
                        pw.append(\"      public void onFailure(Throwable t) {\");
                        pw.append(\"         callback.onFailure(t);\");
                        pw.append(\"      }\");
                        pw.append(\"      public void onSuccess() {\");
                        pw.append(\"         callback.onSuccess(new \" + fullName + \"());\");
                        pw.append(\"      }\");
                        pw.append(\"   });\");
                        pw.append(\"   return;\");
                        pw.append(\"}\");
                    }
                }
            }
            pw.append(\"callback.onFailure(new IllegalArgumentException(\\\"Widget \'\\\" + widgetName + \\\"\' not recognized.\\\"));\");

            pw.append(\"   }\");
            pw.append(\"}\");

            context.commit(logger, pw);         
        }

        // return the name of the generated class
        return \"x.services.gwt.client.entrypoint.DynamicEntryPointWidgetFactoryImpl\";
    }

要回复问题请先登录注册