All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metawidget.gwt.client.widgetbuilder.GwtWidgetBuilder Maven / Gradle / Ivy

There is a newer version: 4.2
Show newest version
// Metawidget
//
// This file is dual licensed under both the LGPL
// (http://www.gnu.org/licenses/lgpl-2.1.html) and the EPL
// (http://www.eclipse.org/org/documents/epl-v10.php). As a
// recipient of Metawidget, you may choose to receive it under either
// the LGPL or the EPL.
//
// Commercial licenses are also available. See http://metawidget.org
// for details.

package org.metawidget.gwt.client.widgetbuilder;

import static org.metawidget.inspector.InspectionResultConstants.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.metawidget.gwt.client.ui.GwtMetawidget;
import org.metawidget.gwt.client.ui.GwtUtils;
import org.metawidget.gwt.client.ui.GwtValueAccessor;
import org.metawidget.gwt.client.ui.Stub;
import org.metawidget.util.simple.StringUtils;
import org.metawidget.widgetbuilder.iface.WidgetBuilder;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * WidgetBuilder for GWT environments.
 * 

* Creates native GWT Widgets, such as TextBox and ListBox, to suit the * inspected fields. * * @author Richard Kennard */ public class GwtWidgetBuilder implements WidgetBuilder, GwtValueAccessor { // // Public methods // public Object getValue( Widget widget ) { // CheckBox (must come before HasText, because CheckBox extends // ButtonBase which implements HasHTML which extends HasText) if ( widget instanceof CheckBox ) { return ( (CheckBox) widget ).getValue(); } // HasText if ( widget instanceof HasText ) { return ( (HasText) widget ).getText(); } // ListBox if ( widget instanceof ListBox ) { ListBox listBox = (ListBox) widget; return listBox.getValue( listBox.getSelectedIndex() ); } return null; } public boolean setValue( Widget widget, Object value ) { // CheckBox (must come before HasText, because CheckBox extends // ButtonBase which implements HasHTML which extends HasText) if ( widget instanceof CheckBox ) { ( (CheckBox) widget ).setValue( (Boolean) value ); return true; } // HasText if ( widget instanceof HasText ) { ( (HasText) widget ).setText( StringUtils.quietValueOf( value ) ); return true; } // ListBox if ( widget instanceof ListBox ) { GwtUtils.setListBoxSelectedItem( (ListBox) widget, StringUtils.quietValueOf( value ) ); return true; } // Not for us return false; } public Widget buildWidget( String elementName, Map attributes, GwtMetawidget metawidget ) { // Hidden if ( TRUE.equals( attributes.get( HIDDEN ) ) ) { return new Stub(); } // Action if ( ACTION.equals( elementName ) ) { return new Button( metawidget.getLabelString( attributes ) ); } String type = GwtUtils.getActualClassOrType( attributes ); // If no type, assume a String if ( type == null ) { type = String.class.getName(); } // Support mandatory Booleans (can be rendered as a checkbox, even though they have a // Lookup) if ( "Boolean".equals( type ) && TRUE.equals( attributes.get( REQUIRED ) ) ) { return new CheckBox(); } // Lookups String lookup = attributes.get( LOOKUP ); if ( lookup != null && !"".equals( lookup ) ) { ListBox listBox = new ListBox(); listBox.setVisibleItemCount( 1 ); addListBoxItems( listBox, GwtUtils.fromString( lookup, StringUtils.SEPARATOR_COMMA_CHAR ), GwtUtils.fromString( attributes.get( LOOKUP_LABELS ), StringUtils.SEPARATOR_COMMA_CHAR ), attributes ); return listBox; } if ( GwtUtils.isPrimitive( type ) ) { // booleans if ( "boolean".equals( type ) ) { return new CheckBox(); } // chars if ( "char".equals( type ) ) { TextBox textbox = new TextBox(); textbox.setMaxLength( 1 ); return textbox; } // Everything else return new TextBox(); } // Strings if ( String.class.getName().equals( type ) ) { if ( TRUE.equals( attributes.get( MASKED ) ) ) { return new PasswordTextBox(); } if ( TRUE.equals( attributes.get( LARGE ) ) ) { return new TextArea(); } TextBox textBox = new TextBox(); String maximumLength = attributes.get( MAXIMUM_LENGTH ); if ( maximumLength != null && !"".equals( maximumLength ) ) { textBox.setMaxLength( Integer.parseInt( maximumLength ) ); } return textBox; } // Dates if ( Date.class.getName().equals( type ) ) { return new TextBox(); } if ( GwtUtils.isPrimitiveWrapper( type ) ) { // Characters if ( Character.class.getName().equals( type ) ) { TextBox textbox = new TextBox(); textbox.setMaxLength( 1 ); return textbox; } // Numbers return new TextBox(); } // Collections if ( GwtUtils.isCollection( type ) ) { return new Stub(); } // Not simple, but don't expand if ( TRUE.equals( attributes.get( DONT_EXPAND ) ) ) { return new TextBox(); } // Nested Metawidget return null; } // // Private methods // private void addListBoxItems( ListBox listBox, List values, List labels, Map attributes ) { if ( values == null ) { return; } // Empty option // // Note: GWT doesn't seem to be able to set null for the // value. It always comes back as String "null" if ( GwtUtils.needsEmptyLookupItem( attributes ) ) { addListBoxItem( listBox, "", null ); } // See if we're using labels if ( labels != null && !labels.isEmpty() && labels.size() != values.size() ) { throw new RuntimeException( "Labels list must be same size as values list" ); } // Add the select items for ( int loop = 0, length = values.size(); loop < length; loop++ ) { String value = values.get( loop ); String label = null; if ( labels != null && !labels.isEmpty() ) { label = labels.get( loop ); } addListBoxItem( listBox, value, label ); } } private void addListBoxItem( ListBox listBox, String value, String label ) { if ( label != null ) { listBox.addItem( label, value ); return; } listBox.addItem( value ); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy