`
flashcloud
  • 浏览: 188776 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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简介.docx

    5. **异步通信(Ajax)**:GWT内置了异步通信机制,通过GWT的RequestBuilder或GWT-RPC(Remote Procedure Call)实现与服务器的无缝交互,实现页面的无刷新更新。 6. **本地化支持**:GWT支持多语言环境,开发者...

    gwt-maven-plugin:开始使用Maven构建GWT项目

    gwt-maven-plugin 该插件旨在通过提供两种特定的打包方式: gwt-lib和gwt-app ,使使用Maven构建GWT项目更加容易。 基本用法 将插件添加到您的POM并启用扩展: &lt; groupId&gt;net.ltgt.gwt.maven&lt;/ groupId&gt; ...

    gwt-widgets-server1.1.jar

    GWT整合Spring时需要这个包,在官网上没有最新的jar包,这是自己用jar命令生成的,并测试可以使用。

    gxt-2.1.1-gwt2 最新的

    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

    gtw框架火狐浏览器安装插件 Google :gwt-dev-plugin

    火狐浏览器插件,gwt-dev-plugin 最新下载使用。本人已经测试可用。gwt框架

    gwt-maven-plugin:旧版GWT Maven插件

    现在,该插件被认为是legacy GWT maven plugin (又名mojo GWT maven插件),而新插件被认为是new generation GWT maven plugin (又名tbroyer GWT maven插件)。 仍然支持旧版maven插件,但强烈建议将新插件用于新...

    gwt-dev-plugin-1.26-rc1.xpi for firefox插件

    原地址如下 http://google-web-toolkit.googlecode.com/files/gwt-dev-plugin-1.26-rc1.xpi

    gwt-ext-tree

    GWT-Ext-Tree 是一个基于 Google Web Toolkit (GWT) 的组件库,它扩展了 GWT 的功能,提供了一套强大的、可定制的树形控件。GWT 是一个用于构建富互联网应用程序(RIA)的 Java 开发框架,允许开发者使用 Java 语言...

    gwt-test-utils:gwt-test-utils是一个Java框架,允许以高效,简便的方式测试GWT客户端代码

    怀念Gaël gwt-test-utils是用于应用程序的Java测试框架。 它提供了一种简单的方法来为GWT客户端代码编写快速的Java测试,而无需GWTTestCase或任何servlet容器实例! 这意味着您可以不受限制地使用任何Java工具:...

    Gwt-Ext基础-中级-进阶

    这个压缩包包含的资源是关于Gwt-Ext的基础、中级和进阶学习资料,适合想要深入理解和应用Gwt-Ext的开发者。 在"基础篇"中,你将学习到以下知识点: 1. **GWT概述**:Google Web Toolkit是一个用于构建高性能、跨...

    gwt-dev-plugin-x86-对ie浏览器使用

    标题中的"gwt-dev-plugin-x86-对ie浏览器使用"指的是一个特定的GWT开发插件,适用于x86架构的机器,并且是专为Internet Explorer(IE)浏览器设计的。在GWT的早期版本中,为了实现Java到JavaScript的编译和在浏览器...

    GWT进阶教程

    2. **GWT MVP模式**:Model-View-Presenter模式是一种常见的GWT应用架构,有助于分离视图、模型和逻辑,提升代码可维护性。 3. **GWT RAPID-UI**:GWT的Rapid-UI技术,如Cell Widgets,使开发者能快速构建动态表格...

    Gwt安装环境和精彩实例讲解

    2. **cypal.studio.for.gwt插件安装**:将`cypal.studio.for.gwt-1.0.zip`文件解压至Eclipse的`plugins`目录中。 3. **GWT路径设置**:在Eclipse中设置GWT插件路径。具体步骤如下: - 打开Eclipse,进入相应的设置...

    log4j-gwt:log4j-gwt使GWT客户端可以使用log4j-开源

    "Log4j-GWT" 是一个专为Google Web Toolkit (GWT) 设计的日志框架,它让GWT应用能够利用Apache Log4j的强大功能。Log4j是一个广泛使用的Java日志库,以其灵活性和可配置性而闻名。通过Log4j-GWT,开发人员可以在GWT...

    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-ex t学习必备资料gwt ext gwt-ext gwt-...

    gwt-servlet-2.3.0.jar

    GWT-Servlet是GWT框架的一部分,主要负责处理服务器端的交互。`gwt-servlet-2.3.0.jar`是GWT 2.3.0版本的Servlet库,这个库包含了运行GWT应用所需的服务器端组件。 在GWT的应用程序中,客户端部分通常由JavaScript...

    gwt-rpc-serialization:重用 gwt-storage 和 gwt-rpc 序列化技术在客户端序列化对象的概念证明

    GWT-RPC使用高效的二进制编码,这使得数据传输更快速且节省带宽。在GWT-RPC中,开发者定义服务接口和服务实现,GWT编译器会自动生成对应的JavaScript代码,处理这些远程调用的细节。 **GWT-Storage序列化** GWT-...

    gwt实例,基于gwt-windows-1.5.2

    这个实例“gwt-windows-1.5.2”是GWT的一个早期版本,它提供了一个在Windows环境下开发GWT应用的示例。GWT 1.5.2发布于2008年,虽然现在已经比较过时,但对于初学者来说,它仍然是一个理解GWT基本概念和工作原理的好...

    gwt-dev-2.6.0-rc4.zip

    标题 "gwt-dev-2.6.0-rc4.zip" 暗示这是一个与Google Web Toolkit (GWT)相关的开发工具包,具体版本为2.6.0的Release Candidate 4。GWT是一个开放源代码的Java框架,用于构建和优化富互联网应用程序(RIA)。这个...

    gwt-dev-plugin for IE、FireFox、Chrome

    2. **gwt-dev-plugin-x86.msi**:这是一个Windows安装程序包,用于32位系统。MSI是Microsoft Installer的文件格式,用户可以通过双击此文件进行标准的Windows安装过程。 3. **gwt-dev-plugin.xpi**:这是Firefox...

Global site tag (gtag.js) - Google Analytics