GWT: Bounded TextField in GXT

This article provides a solution for creating a TextField with bounded user input in Sencha GXT (aka Ext GWT), for use in GWT applications.

The Problem

Recently, I was looking for a way to limit the amount of text a user could enter on an HTML form within a GXT application – for example, limiting a zip code input field to five characters. I wanted something that functions identically to the maxlength attribute found in HTML <input type=”text”> fields. While GXT does offer a maxlength attribute within a TextField object, it does not function as one might expect. Instead of limiting the number of characters the user could enter, it interacts with the validation process and displays a validation error (such as highlighting the TextField in red) after the fact if a user entered too many characters.

The Solution

I discovered a post on the Sencha support forum that shows how to create a subclass of TextField with the expected functionality, ie, to limit the number of characters the user can type to be the maxlength attribute. Since I believe such a class is desirable throughout my application, I generalized the original anonymous inner class solution to be a regular Java class.

Simply add the following class to your application and replace all your instances of TextField with BoundedTextField and the maxlength attribute will function as a text limiter. I hope Sencha will eventually include support for this form of maxlength functionality within the API. Thanks to Sven at Sencha for providing the original solution.

package net.selikoff.gxt.ui.widget;

import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.Element;

/**
 * Class works like TextField class but Max Length specifies the maximum
 * amount user can type, instead of being used as limit for validation.
 * 
 * @author Scott Selikoff
 */
public class BoundedTextField<D> extends TextField<D> {
	@Override
	public void setMaxLength(int m) {
		super.setMaxLength(m);
		if (rendered) {
			getInputEl().setElementAttribute("maxLength", m);
		}
	}

	@Override
	protected void onRender(Element target, int index) {
		super.onRender(target, index);
		getInputEl().setElementAttribute("maxLength", getMaxLength());
	}
}

Nintendo 4DS Announced

Nintendo 4DS Following the successful launch of the Nintendo 3DS last week, Nintendo fans got a treat when it was revealed this Friday morning that the next version of the hand-held portable would be called the “Nintendo 4DS”, and support what game industry experts call “time-altering software”.

The Infinite Generation Portable

One of the lead developers was quoted in an interview as saying “After pioneering motion-based controllers and releasing the first 3D device that doesn’t require goofy glasses, we figured space-time vortex manipulation was the obvious next step. At Nintendo, we don’t just think of our products as ‘gaming devices,’ but as devices that let you alter the fabric of space and time itself”.

President and CEO of Nintendo of America Reginald “Reggie” Fils-Aime was also quoted as saying “Cha-ching!”.

Time Travel
Nintendo software engineer Hubert Farnsworth said that the device uses patented time-extending and compressing software, based on the same technology that makes standardized tests seem to last forever.

“Once we realized it was possible to warp time through the mere application of algebra and geometry, it was a simple leap to designing a working time-warping chip (TWC),” Farnsworth said. “I can’t imagine any problems with putting this technology in the hands of children.”

Nintendo had no comment on rumors that 11th Century Chinese Emperor Shigeru Miyamoto, whose name is the same as the famed creator of Mario and The Legend of Zelda, was actually a time traveler from the future who used his collection of innovative platformers and game mechanics to conquer the country in less than 2 years.

What do you think? Will Nintendo’s ability to make you your own grandfather pay off or will it be crushed by Sony’s PSP2, which supports a revolutionary (for Sony) touchpad.

Future Release Date

Nintendo also announced they will have the device finished in a couple hundred years. Despite this production delay, Nintendo promises they will then use the completed Nintendo 4DS to travel back in time so that the device will be available by the holidays. Price is expected to be around 50 trillion dollars, and comes with a 2GB memory card, RAR (really-augmented-reality) cards, and a plutonium charging station.

Live Blogging with an iPad

iPad with Keyboard and Dock As regular readers know, Jeanne and I blogged heavily at TheServerSide Java Symposium 2011, writing over 30 articles in a three-day period. What we may not have had time to mention is that we both did so entirely from a pair of Apple iPads. Below is a summary of some of things I learned during my experience.

1. Set up and Blogging Software
For my blogging spree, I used an Apple iPad 2 with an Apple Wireless Keyboard. I started with the WordPress iPad application, but found it too frustrating to use. The local/remote saving of articles was problematic, as Jeanne explains, and the interface felt very last-minute and cheap. I had three options remaining for blogging: the WordPress web interface in Safari, word processing software such as Apple Pages, or a plain old text editor. Since Internet was unstable at the conference, the first solution was out. I also vetoed fancy word processing software, since in the end the articles would be copied as plain text to the web browser for publishing. I settled on a plain text editor and was pleasantly surprised that my document management app, GoodReader, doubled as a text editor.

2. GoodReader as a Live Blogging Software
I liked blogging in GoodReader right away because it presented me with a full-screen text editor and my documents were always saved locally. I wrote up dozens of articles in GoodReader and then pasted each article into the WordPress web browser editor for publishing, which only took a minute. The only downside of using GoodReader was in the creation and naming of new files. By default, new text files are saved as “New File.txt”. In order to change the name of an article, you have to exit out of writing, open the file management interface, select the file, rename it, close the file management interface, and re-open the file. I would have preferred a more direct approach, such as offering me the chance to name the file when I created it. Also, a spell-checker after I had finished typing would have been nice.

3. iPad + Keyboard
Prior to this conference, I had never used my iPad with a keyboard. After writing thousands of words with the wireless keyboard, I can honestly say I would never go back to touch-typing for blogging. With the keyboard, the iPad became much closer to a real laptop — or more accurately a netbook — but was much smaller, lighter, and lasted a lot longer on a single charge than my full-sized MacBook Pro.

Advantages:

  • Easy access to special characters such as @, #, $, often used in developer presentations. Also easy access to HTML brackets for writing HTML tags quickly.
  • Arrow keys for easy navigation, particularly on web-pages with missing scroll bars. Side note: Jeanne discovered you can use two fingers to scroll in HTML windows with missing scroll bars.
  • Command-Up and Command-Down worked as Home/End keys moving the cursor to the top/bottom of the window.
  • Special keys give instant access to control brightness to prevent people from viewing what I write if I wanted to check my mail.
  • I could type faster with fewer mistakes; tactile feedback means I don’t need to look at my hands while typing.
  • Shortcut keys! I never realized how much I relied on Copy/Paste/Cut/Select All while writing until they were taken away!
  • Shortcut to show/hide on-screen keyboard using the eject button.

Limitations/Notes

  • Turn off AutoCorrect. On a keyboard mistakes are far less common and AutoCorrect often makes mistakes with technical words. Rejecting AutoCorrect suggestions interrupted typing and slowed it to a crawl since it required an on-screen click.
  • No Command-F for find, much needed feature.
  • Command-S for saving with the application. Had to touch the screen to save. This is more likely a limitation of the software than iOS, although it’s difficult know for sure.
  • The Apple Wireless Keyboard desperately needs an “Off” switch. I had a lot of issues on the final day with putting the iPad in my bag only to discover later that the battery had been drained by 10% when I wasn’t using it due to the keyboard waking up and activating.

Conclusion
I loved typing with an iPad, so long as a wireless keyboard was available. Contrasting this to JavaOne two years ago, where I had to go from presentation to presentation hunting power outlets because my MacBook could barely go half the day without power. I could honestly see taking it to business meetings and typing for hours on it. While there are still some additions I’d like to see to iOS, such as Command-F for find, the experience was quite enjoyable.