- 浏览: 188776 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
java-007:
alanland 写道一楼还有个眉头没脑的提问你的理解能力真T ...
神奇的Grails:自关联的树状Domain一行代码获取所有子节点 -
xpjava:
请问大侠,如何显示?这个树形结构?
神奇的Grails:自关联的树状Domain一行代码获取所有子节点 -
谷赛玉:
我也遇到int和long不匹配的问题了,呵呵
iBatis访问MS Access数据库(*.mdb)时的若干注意事项 -
alanland:
一楼还有个眉头没脑的提问
神奇的Grails:自关联的树状Domain一行代码获取所有子节点 -
alanland:
涨知识了,还学到一个 flatten
神奇的Grails:自关联的树状Domain一行代码获取所有子节点
源文转自: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 19441.总是提示“The project 'XXX' does n ... -
关于GWT-Presenter的简介
2010-03-07 22:53 2260原文出自GWT-Presenter项目的创建者对该项目的简要介 ... -
GWT-Presenter发布1.1.1
2010-03-07 21:38 1110一直关注GWT-Presenter的进展情况,最近该项目发布了 ... -
MVP For GWT 系列资料转载十:Loading a default view on startup with gwt-presenter
2010-03-07 20:57 974源文转自:Loading a default view on ... -
MVP For GWT 系列资料转载九:Base presenter and view classes for gwt-presenter
2010-03-07 20:23 851源文转自:Base presenter and view cl ... -
MVP For GWT 系列资料转载八:Swapping DIVs technique for navigation with gwt-presenter
2010-03-07 20:18 880源文转自:Swapping DIVs technique fo ... -
MVP For GWT 系列资料转载七:How to show a “Loading…” pop-up in your GWT app
2010-03-07 20:14 755源文转自:How to show a “Loading…” p ... -
MVP For GWT 系列资料转载六:Unit testing with JDO PersistenceManager injected via Guice
2010-03-07 15:34 829源文转自:Unit testing with JDO Pers ... -
MVP For GWT 系列资料转载五:More on unit testing with an injected JDO PersistenceManager
2010-03-07 15:30 999源文转自:More on unit testing with ... -
MVP For GWT 系列资料转载四:Writing common test data services for gwt-dispatch with Guic
2010-03-07 15:23 1002源文转自:Writing common test data s ... -
MVP For GWT 系列资料转载三:How to reduce startup time with gwt-presenter
2010-03-07 15:18 921源文转自:How to reduce startup time ... -
MVP For GWT 系列资料转载一:Quick note on using gwt-presenter’s ActionException
2010-03-07 15:07 1057源文转自:Quick note on using gwt-pr ... -
GWT MVP模式的UML图
2010-03-07 14:34 1941转自:http://www.nieleyde.org/Skyw ... -
透过GXT 的 Mail 示例,学习GXT 的 MVC结构
2009-11-21 15:59 4591基于GWT 的 GXT UI框架 (即EXT-GW ... -
GWT也能jQuery?没有想不到
2009-04-19 23:14 2093今天看到有人用jQuery实现Skype式的按钮效果,突然想到 ...
相关推荐
5. **异步通信(Ajax)**:GWT内置了异步通信机制,通过GWT的RequestBuilder或GWT-RPC(Remote Procedure Call)实现与服务器的无缝交互,实现页面的无刷新更新。 6. **本地化支持**:GWT支持多语言环境,开发者...
gwt-maven-plugin 该插件旨在通过提供两种特定的打包方式: gwt-lib和gwt-app ,使使用Maven构建GWT项目更加容易。 基本用法 将插件添加到您的POM并启用扩展: < groupId>net.ltgt.gwt.maven</ groupId> ...
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框架
现在,该插件被认为是legacy GWT maven plugin (又名mojo GWT maven插件),而新插件被认为是new generation GWT maven plugin (又名tbroyer GWT maven插件)。 仍然支持旧版maven插件,但强烈建议将新插件用于新...
原地址如下 http://google-web-toolkit.googlecode.com/files/gwt-dev-plugin-1.26-rc1.xpi
GWT-Ext-Tree 是一个基于 Google Web Toolkit (GWT) 的组件库,它扩展了 GWT 的功能,提供了一套强大的、可定制的树形控件。GWT 是一个用于构建富互联网应用程序(RIA)的 Java 开发框架,允许开发者使用 Java 语言...
怀念Gaël gwt-test-utils是用于应用程序的Java测试框架。 它提供了一种简单的方法来为GWT客户端代码编写快速的Java测试,而无需GWTTestCase或任何servlet容器实例! 这意味着您可以不受限制地使用任何Java工具:...
这个压缩包包含的资源是关于Gwt-Ext的基础、中级和进阶学习资料,适合想要深入理解和应用Gwt-Ext的开发者。 在"基础篇"中,你将学习到以下知识点: 1. **GWT概述**:Google Web Toolkit是一个用于构建高性能、跨...
标题中的"gwt-dev-plugin-x86-对ie浏览器使用"指的是一个特定的GWT开发插件,适用于x86架构的机器,并且是专为Internet Explorer(IE)浏览器设计的。在GWT的早期版本中,为了实现Java到JavaScript的编译和在浏览器...
2. **GWT MVP模式**:Model-View-Presenter模式是一种常见的GWT应用架构,有助于分离视图、模型和逻辑,提升代码可维护性。 3. **GWT RAPID-UI**:GWT的Rapid-UI技术,如Cell Widgets,使开发者能快速构建动态表格...
2. **cypal.studio.for.gwt插件安装**:将`cypal.studio.for.gwt-1.0.zip`文件解压至Eclipse的`plugins`目录中。 3. **GWT路径设置**:在Eclipse中设置GWT插件路径。具体步骤如下: - 打开Eclipse,进入相应的设置...
"Log4j-GWT" 是一个专为Google Web Toolkit (GWT) 设计的日志框架,它让GWT应用能够利用Apache Log4j的强大功能。Log4j是一个广泛使用的Java日志库,以其灵活性和可配置性而闻名。通过Log4j-GWT,开发人员可以在GWT...
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-Servlet是GWT框架的一部分,主要负责处理服务器端的交互。`gwt-servlet-2.3.0.jar`是GWT 2.3.0版本的Servlet库,这个库包含了运行GWT应用所需的服务器端组件。 在GWT的应用程序中,客户端部分通常由JavaScript...
GWT-RPC使用高效的二进制编码,这使得数据传输更快速且节省带宽。在GWT-RPC中,开发者定义服务接口和服务实现,GWT编译器会自动生成对应的JavaScript代码,处理这些远程调用的细节。 **GWT-Storage序列化** GWT-...
这个实例“gwt-windows-1.5.2”是GWT的一个早期版本,它提供了一个在Windows环境下开发GWT应用的示例。GWT 1.5.2发布于2008年,虽然现在已经比较过时,但对于初学者来说,它仍然是一个理解GWT基本概念和工作原理的好...
标题 "gwt-dev-2.6.0-rc4.zip" 暗示这是一个与Google Web Toolkit (GWT)相关的开发工具包,具体版本为2.6.0的Release Candidate 4。GWT是一个开放源代码的Java框架,用于构建和优化富互联网应用程序(RIA)。这个...
2. **gwt-dev-plugin-x86.msi**:这是一个Windows安装程序包,用于32位系统。MSI是Microsoft Installer的文件格式,用户可以通过双击此文件进行标准的Windows安装过程。 3. **gwt-dev-plugin.xpi**:这是Firefox...