- 浏览: 185503 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
java-007:
alanland 写道一楼还有个眉头没脑的提问你的理解能力真T ...
神奇的Grails:自关联的树状Domain一行代码获取所有子节点 -
xpjava:
请问大侠,如何显示?这个树形结构?
神奇的Grails:自关联的树状Domain一行代码获取所有子节点 -
谷赛玉:
我也遇到int和long不匹配的问题了,呵呵
iBatis访问MS Access数据库(*.mdb)时的若干注意事项 -
alanland:
一楼还有个眉头没脑的提问
神奇的Grails:自关联的树状Domain一行代码获取所有子节点 -
alanland:
涨知识了,还学到一个 flatten
神奇的Grails:自关联的树状Domain一行代码获取所有子节点
MVP For GWT 系列资料转载二:An MVP-compatible EnumListBox for GWT
- 博客分类:
- GWT
源文转自:An MVP-compatible EnumListBox for GWT
A frequent request on the GWT and gwt-presenter forums is for a ListBox that implements HasValue like a TextBox. I recently needed one myself, and thought it would be especially cool if I could use it with a Java enum type like this:
public static enum Frequency {DAILY, WEEKLY, MONTHLY}; private ConstantsWithLookup enumLabels = GWT.create(EnumLabels.class); private EnumListBox<Frequency> freqBox; freqBox = new EnumListBox<Frequency>(Frequency.class, enumLabels);
In keeping with MVP philosophy, the presenter’s display interface only needs the HasValue type to get and set the selected value as well as add a ValueChangeHandler to respond to a new selection. Here as some relevant excerpts from a presenter that uses the EnumListBox:
public interface Display extends WidgetDisplay { HasValue<Frequency> getFrequency(); } ... protected void onFirstRequest() { ... display.getFrequency().addValueChangeHandler(new ValueChangeHandler<Frequency>() { @Override public void onValueChange(ValueChangeEvent<Frequency> event) { // Do something with the newly selected event.getValue() ... } }); }
Here’s a straightforward implementation of an EnumListBox that implements HasValue. Thanks to the gwt-ent project for the original idea for this。
package com.roa.app.client.ui.widget; import com.google.gwt.event.dom.client.ChangeEvent; import com.google.gwt.event.dom.client.ChangeHandler; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.i18n.client.ConstantsWithLookup; import com.google.gwt.user.client.ui.HasValue; import com.google.gwt.user.client.ui.ListBox; import com.roa.common.client.util.EnumTranslator; public class EnumListBox<T extends Enum<T>> extends ListBox implements HasValue<T> { private final Class<T> clazzOfEnum; private boolean valueChangeHandlerInitialized; public EnumListBox(final Class<T> clazzOfEnum, final ConstantsWithLookup constants) { if (clazzOfEnum == null) throw new IllegalArgumentException("Enum class cannot be null"); this.clazzOfEnum = clazzOfEnum; EnumTranslator enumTranslator = new EnumTranslator(constants); T[] values = clazzOfEnum.getEnumConstants(); for (T value : values) { // this.addItem(constant.toString(), constant.name()); this.addItem(enumTranslator.getText(value), value.name()); } } public T getSelectedValue() { if (getSelectedIndex() >= 0) { String name = getValue(getSelectedIndex()); T[] values = clazzOfEnum.getEnumConstants(); for (T value : values) { if (value.name().equals(name)) return value; } } return null; } public void setSelectedValue(T value) { T[] values = clazzOfEnum.getEnumConstants(); for (int i = 0; i < values.length; i++) { if (values[i] == value) { this.setSelectedIndex(i); return; } } throw new IllegalArgumentException("No index found for value " + value.toString()); } /* * Methods to implement HasValue */ @Override public T getValue() { return this.getSelectedValue(); } @Override public void setValue(T value) { this.setValue(value, false); } @Override public void setValue(T value, boolean fireEvents) { T oldValue = getValue(); this.setSelectedValue(value); if (fireEvents) { ValueChangeEvent.fireIfNotEqual(this, oldValue, value); } } @Override public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) { // Initialization code if (!valueChangeHandlerInitialized) { valueChangeHandlerInitialized = true; super.addChangeHandler(new ChangeHandler() { public void onChange(ChangeEvent event) { ValueChangeEvent.fire(EnumListBox.this, getValue()); } }); } return addHandler(handler, ValueChangeEvent.getType()); } }
There’s really not much to it, just a little weirdness that always comes with generics. Notice that the constructor uses an EnumTranslator to populate the labels in the ListBox. This allows you to use a standard GWT ConstantsWithLookup inteface to supply localized text for the enum values instead of the constant names. ConstantsWithLookup is just like Constants, but with the important ability to find a value dynamically without invoking a method corresponding to the property name. Unfortunately, you still have to define a method for each value of the enum in your ConstantsWithLookup class, even though it’s never used directly. Here’s a sample interface:
public interface EnumLabels extends ConstantsWithLookup { // Enums String com_mypackage_MyClass_Frequency_DAILY(); String com_mypackage_MyClass_Frequency_WEEKLY(); String com_mypackage_MyClass_Frequency_MONTHLY(); String com_mypackage_MyClass_Frequency_QUARTERLY(); String com_mypackage_MyClass_Frequency_YEARLY();
And the corresponding default properties file EnumLabels.properties:
com_mypackage_MyClass_Frequency_DAILY=daily com_mypackage_MyClass_Frequency_WEEKLY=weekly com_mypackage_MyClass_Frequency_MONTHLY=monthly com_mypackage_MyClass_Frequency_QUARTERLY=quarterly com_mypackage_MyClass_Frequency_YEARLY=yearly
And finally, here’s my EnumTranslator:
package com.roa.common.client.util; import com.google.gwt.i18n.client.ConstantsWithLookup; /** * Does a properties file lookup to get text associated with an enum value * Property keys use the full class name with all dots and dollars * converted to underscores. Keys are case-sensitive and GWT requires a * method in the interface that extends ConstantsWithLookup, even though * the method is never called. * * Example: * String my_package_class_Frequency_DAILY(); * * In the corresponding properties file: * my_package_class_Frequency_DAILY=daily * * @author David Chandler */ public class EnumTranslator { private ConstantsWithLookup constants; public EnumTranslator(ConstantsWithLookup constants) { this.constants = constants; } public String getText(Enum e) { String lookupKey = e.getClass().getName() + "." + e.name(); lookupKey = lookupKey.replace(".", "_"); lookupKey = lookupKey.replace("$", "_"); return constants.getString(lookupKey); } }
This EnumListBox is a fairly hard-wired kind of ListBox. In the near future, I anticipate refactoring along these lines:
Add a constructor that takes any java.util.List, not just an Enum.
Create an interface HasSelectedValue that extends HasValue by adding a populateAllSelections() method. This would allow the available selections to come from the presenter through the Display interface and is thus even better for MVP. Versions of the new method could also take a java.util.List or Enum and would replace the constructor.
Ditto for a HasSelectedValues interface to deal with multiple-select type ListBoxes.
Stay tuned.
发表评论
-
GWT + Maven开发常见问题及处理(持续更新中)
2010-03-27 01:55 19101.总是提示“The project 'XXX' does n ... -
关于GWT-Presenter的简介
2010-03-07 22:53 2237原文出自GWT-Presenter项目的创建者对该项目的简要介 ... -
GWT-Presenter发布1.1.1
2010-03-07 21:38 1083一直关注GWT-Presenter的进展情况,最近该项目发布了 ... -
MVP For GWT 系列资料转载十:Loading a default view on startup with gwt-presenter
2010-03-07 20:57 941源文转自:Loading a default view on ... -
MVP For GWT 系列资料转载九:Base presenter and view classes for gwt-presenter
2010-03-07 20:23 829源文转自:Base presenter and view cl ... -
MVP For GWT 系列资料转载八:Swapping DIVs technique for navigation with gwt-presenter
2010-03-07 20:18 859源文转自:Swapping DIVs technique fo ... -
MVP For GWT 系列资料转载七:How to show a “Loading…” pop-up in your GWT app
2010-03-07 20:14 725源文转自:How to show a “Loading…” p ... -
MVP For GWT 系列资料转载六:Unit testing with JDO PersistenceManager injected via Guice
2010-03-07 15:34 811源文转自:Unit testing with JDO Pers ... -
MVP For GWT 系列资料转载五:More on unit testing with an injected JDO PersistenceManager
2010-03-07 15:30 977源文转自:More on unit testing with ... -
MVP For GWT 系列资料转载四:Writing common test data services for gwt-dispatch with Guic
2010-03-07 15:23 977源文转自:Writing common test data s ... -
MVP For GWT 系列资料转载三:How to reduce startup time with gwt-presenter
2010-03-07 15:18 895源文转自:How to reduce startup time ... -
MVP For GWT 系列资料转载一:Quick note on using gwt-presenter’s ActionException
2010-03-07 15:07 1028源文转自:Quick note on using gwt-pr ... -
GWT MVP模式的UML图
2010-03-07 14:34 1920转自:http://www.nieleyde.org/Skyw ... -
透过GXT 的 Mail 示例,学习GXT 的 MVC结构
2009-11-21 15:59 4558基于GWT 的 GXT UI框架 (即EXT-GW ... -
GWT也能jQuery?没有想不到
2009-04-19 23:14 2065今天看到有人用jQuery实现Skype式的按钮效果,突然想到 ...
相关推荐
NULL 博文链接:https://mydownload.iteye.com/blog/1157105
GWT整合Spring时需要这个包,在官网上没有最新的jar包,这是自己用jar命令生成的,并测试可以使用。
gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2gxt-2.1.1-gwt2
火狐浏览器插件,gwt-dev-plugin 最新下载使用。本人已经测试可用。gwt框架
原地址如下 http://google-web-toolkit.googlecode.com/files/gwt-dev-plugin-1.26-rc1.xpi
mvn install:install-file -D groupId=org.zenika.widget -D artifactId=gwt-datepicker -D version=r29 -D packaging=jar -D file=C:\gwt-datepicker-r29.jar 命令参数详解: mvn install:install-file: maven 安装...
mvn install:install-file -D groupId=org.zenika.widget -D artifactId=gwt-datepicker -D version=r29 -D packaging=jar -D file=C:\gwt-datepicker-r29.jar 命令参数详解: mvn install:install-file: maven 安装...
怀念Gaël gwt-test-utils是用于应用程序的Java测试框架。 它提供了一种简单的方法来为GWT客户端代码编写快速的Java测试,而无需GWTTestCase或任何servlet容器实例! 这意味着您可以不受限制地使用任何Java工具:...
现在,该插件被认为是legacy GWT maven plugin (又名mojo GWT maven插件),而新插件被认为是new generation GWT maven plugin (又名tbroyer GWT maven插件)。 仍然支持旧版maven插件,但强烈建议将新插件用于新...
gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-...
gwt代码调试插件gwt-dev-plugin-x86.msi,针对ie浏览器使用
log4j-gwt使GWT客户端可以使用log4j Logger,并受益于log4j的灵活性和便捷的方法。 使用log4j-gwt,记录的消息被路由到GWT的内部记录API。
gwt-maven-plugin 该插件旨在通过提供两种特定的打包方式: gwt-lib和gwt-app ,使使用Maven构建GWT项目更加容易。 基本用法 将插件添加到您的POM并启用扩展: < groupId>net.ltgt.gwt.maven</ groupId> ...
NULL 博文链接:https://gavin-chen.iteye.com/blog/351206
gwt-270-骨架GWT v2.7.0 骨架 mvn archetype:generate \ -DarchetypeGroupId=org.codehaus.mojo \ -DarchetypeArtifactId=gwt-maven-plugin \ -DarchetypeVersion=2.7.0属性配置: groupId: ... gwt270exampleversion: ...
fwefwefwGWT工具GWT工具GWT工具GWT工具GWT工具GWT工具
cd gwt-bike-shop 使用 Jetty 构建和运行 mvn clean install mvn jetty:run 现在你可以去: http://localhost:8080/app/不要忘记 '/app' 您的应用程序现在正常运行(非开发模式)。 您可以使用以下说明来设置和...
gwt-rpc-序列化 重用 gwt-storage 和 gwt-rpc 序列化技术在客户端序列化对象的概念证明
一个非常出色的gwt实例,基于gwt-windows-1.5.2实例,是非常好的gwt经典学习实例