1. #1
    Ext User pedrosousa's Avatar
    Join Date
    Oct 2008
    Location
    Portugal [Aveiro]
    Posts
    17
    Vote Rating
    0
    pedrosousa is on a distinguished road

      0  

    Default [FIXED] Binding in TimeField

    [FIXED] Binding in TimeField


    Hi,

    I was trying to use Binding class, and cannot bind with a TimeField. Is this a limitationm or i'm doing something wrong?

    Error:
    Code:
    [ERROR] Uncaught exception escaped
    java.lang.ClassCastException: java.lang.String cannot be cast to com.extjs.gxt.ui.client.data.ModelData
        at com.extjs.gxt.ui.client.widget.form.ComboBox.setValue(ComboBox.java:1)
        at com.extjs.gxt.ui.client.binding.FieldBinding.updateField(FieldBinding.java:166)
        at com.extjs.gxt.ui.client.binding.FieldBinding.bind(FieldBinding.java:77)
        at com.extjs.gxt.ui.client.binding.Bindings.bind(Bindings.java:64)
        at com.extjs.gxt.ui.client.binding.FormBinding.bind(FormBinding.java:64)
        at demo.layout.client.MeoForm.bind(MeoForm.java:43)
        at demo.layout.client.Layout$1.componentSelected(Layout.java:84)
        at com.extjs.gxt.ui.client.event.SelectionListener.handleEvent(SelectionListener.java:20)
        at com.extjs.gxt.ui.client.event.SelectionListener.handleEvent(SelectionListener.java:1)
        at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:74)
        at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:424)
        at com.extjs.gxt.ui.client.widget.button.Button.onClick(Button.java:469)
        at com.extjs.gxt.ui.client.widget.button.Button.onComponentEvent(Button.java:234)
        at com.extjs.gxt.ui.client.widget.Component.onBrowserEvent(Component.java:663)
        at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1308)
        at com.google.gwt.user.client.DOM.dispatchEventAndCatch(DOM.java:1287)
        at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1255)
        at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
        at com.google.gwt.dev.shell.ie.IDispatchImpl.callMethod(IDispatchImpl.java:126)
        at com.google.gwt.dev.shell.ie.IDispatchProxy.invoke(IDispatchProxy.java:155)
        at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke(IDispatchImpl.java:294)
        at com.google.gwt.dev.shell.ie.IDispatchImpl.method6(IDispatchImpl.java:194)
        at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:117)
        at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
        at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
        at com.google.gwt.dev.GWTShell.pumpEventLoop(GWTShell.java:720)
        at com.google.gwt.dev.GWTShell.run(GWTShell.java:593)
        at com.google.gwt.dev.GWTShell.main(GWTShell.java:357)
    I'm using:

    Ext GWT 1.2
    GWT 1.5.3
    JDK 6

    Layout.java
    Code:
    package demo.layout.client;
    
    import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
    import com.extjs.gxt.ui.client.Style.LayoutRegion;
    import com.extjs.gxt.ui.client.Style.Scroll;
    import com.extjs.gxt.ui.client.Style.VerticalAlignment;
    import com.extjs.gxt.ui.client.event.ComponentEvent;
    import com.extjs.gxt.ui.client.event.SelectionListener;
    import com.extjs.gxt.ui.client.widget.ContentPanel;
    import com.extjs.gxt.ui.client.widget.TabItem;
    import com.extjs.gxt.ui.client.widget.TabPanel;
    import com.extjs.gxt.ui.client.widget.Viewport;
    import com.extjs.gxt.ui.client.widget.button.Button;
    import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
    import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
    import com.extjs.gxt.ui.client.widget.layout.TableData;
    import com.extjs.gxt.ui.client.widget.layout.TableLayout;
    import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
    import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.Element;
    import com.google.gwt.user.client.ui.RootPanel;
    
    public class Layout extends Viewport implements EntryPoint {
        
        private ContentPanel center;
        private ContentPanel south;
        private TextToolItem collapse;
        private BorderLayoutData centerData;
        private BorderLayoutData southData;
        private TabPanel tabPanel;
        private ToolBar toolBar;
        
        public Layout() {
            setLayout(new BorderLayout());
        }
    
        public void onModuleLoad() {
            RootPanel.get().add(this);
        }
    
        @Override
        protected void onRender(Element parent, int pos) {
            super.onRender(parent, pos);
            
            // center panel
            center = new ContentPanel();
            center.setHeading("center");
            
            TableLayout tableLayout = new TableLayout(1);
            tableLayout.setWidth("100%");
            
            center.setLayout(tableLayout);
            
            TableData tableData = new TableData(HorizontalAlignment.LEFT, VerticalAlignment.TOP);
            tableData.setWidth(100/1 + "%");
            
            center.setScrollMode(Scroll.AUTO);
            
            ContentPanel p = new ContentPanel();
            p.setHeaderVisible(false);
            p.setBodyBorder(false);
            p.setBorders(false);
            
            final Objecto obj = new Objecto();
            obj.setNameField("coiso");
            obj.setBeginTime("11:11");
            
            final MeoForm form = new MeoForm();
            //form.bind(obj);
            
            Button button = new Button("Bind");
            button.addSelectionListener(new SelectionListener<ComponentEvent>() {
                public void componentSelected(ComponentEvent ce) {
                    form.bind(obj);
                }
            });
            
            center.add(form, tableData);
            center.add(button, tableData);
            
            //tool bar
            toolBar = new ToolBar();
            
            // collapsible tool
            collapse = new TextToolItem("Colapse");
            collapse.addSelectionListener(new SelectionListener<ComponentEvent>() {
                public void componentSelected(ComponentEvent ce) {
                    if(south.isExpanded()) {
                        south.collapse();
                        collapse.setText("Expand");
                    } else {
                        south.expand();
                        collapse.setText("Collapse");
                    }
                }
            });
            
            // tool bar
            toolBar.add(collapse);
            center.setBottomComponent(toolBar);
            
            // south panel
            south = new ContentPanel();
            south.setHeading("south");
            south.setCollapsible(true);
            south.setHideCollapseTool(true);
            south.setHeaderVisible(false);
            
            // Tab panel
            tabPanel = new TabPanel();
            tabPanel.add(new TabItem("Tab1"));
            tabPanel.add(new TabItem("Tab2"));
            tabPanel.add(new TabItem("Tab3"));
            south.add(tabPanel);
            
            // center data
            centerData = new BorderLayoutData(LayoutRegion.CENTER);
            
            // south data
            southData = new BorderLayoutData(LayoutRegion.SOUTH);
            southData.setCollapsible(true);
            southData.setFloatable(false);
            
            // add panels
            add(center, centerData);
            add(south, southData);
        }
    }
    MeoForm.java
    Code:
    package demo.layout.client;
    
    import com.extjs.gxt.ui.client.binding.FormBinding;
    import com.extjs.gxt.ui.client.widget.form.FormPanel;
    import com.extjs.gxt.ui.client.widget.form.TextField;
    import com.extjs.gxt.ui.client.widget.form.TimeField;
    import com.google.gwt.i18n.client.DateTimeFormat;
    
    public class MeoForm extends FormPanel {
    
        private TextField<String> name;
        private TimeField beginTime;
        
        private Objecto obj;
        
        private FormBinding binding;
        
        public MeoForm() {
            super();
            
            initFields();
        }
    
        private void initFields() {
            name = new TextField<String>();  
            name.setName("nameField");  
            name.setFieldLabel("Name");  
            this.add(name);
            
            
            beginTime = new TimeField();
            beginTime.setFieldLabel("beginTime");
            beginTime.setFormat(DateTimeFormat.getFormat("HH:mm"));
            beginTime.setAllowBlank(false);
            beginTime.setName("beginTime");
            this.add(beginTime);
        }
        
        public void bind(Objecto objecto) {
            this.obj = objecto;
            binding = new FormBinding(this);
            binding.autoBind();
            binding.bind(obj);
        }
    }
    Objecto.java
    Code:
    package demo.layout.client;
    
    import com.extjs.gxt.ui.client.data.BaseModelData;
    import com.google.gwt.user.client.rpc.IsSerializable;
    
    public class Objecto extends BaseModelData implements IsSerializable {
    
        private static final long serialVersionUID = 15436382183835233L;
    
        public Objecto() {
            super();
            set("nameField", null);
            set("beginTime", null);
        }
    
        public String getNameField() {
            return get("nameField");
        }
        
        public void setNameField(String nameField) {
            set("nameField", nameField);
        }
    
        public String getBeginTime() {
            return get("beginTime");
        }
    
        public void setBeginTime(String beginTime) {
            set("beginTime", beginTime);
        }
        
        
    }
    Thanks,
    --
    Pedro Sousa

  2. #2
    Sencha - GXT Dev Team darrellmeyer's Avatar
    Join Date
    May 2007
    Location
    Washington, DC
    Posts
    2,242
    Vote Rating
    2
    darrellmeyer is on a distinguished road

      0  

    Default


    I was trying to use Binding class, and cannot bind with a TimeField. Is this a limitationm or i'm doing something wrong?
    This is not a limitation. However, you would normall use a Converter to translate data types form your model to the field. I have just made some changes to TimeField to make things easier.

    With the new code TimeField will use a new Time model type for the drop down. So when setting a value, which is what the binder does, you must set a Time instance. You model will will most likely be using a Date. So in the case, you can add a Convertor to the FieldBinder, and find the matching Time instance from the Date. There are new methods to make this easy.

    Even easier, if you model property is a Date you can use the new TimeFieldBinder:

    Code:
      public void onModuleLoad() {
        final TimeField field = new TimeField();
    
        TimeFieldBinding binding = new TimeFieldBinding(field, "date");
    
        Stock stock = TestData.getStocks().get(0);
        binding.bind(stock);
    
        RootPanel.get().add(field);
      }
    Here is the code for TimeFieldBinder that gives you and idea how the data is converted:

    Code:
    public class TimeFieldBinding extends FieldBinding {
    
      protected TimeField timeField;
    
      /**
       * Creates a new time field binding instance.
       * 
       * @param field the time field
       * @param property the property name
       */
      public TimeFieldBinding(TimeField field, String property) {
        super(field, property);
        this.timeField = field;
      }
    
      @Override
      protected Object onConvertFieldValue(Object value) {
        if (value == null) {
          return null;
        }
        Time time = (Time) value;
        return time.getDate();
      }
    
      @Override
      protected Object onConvertModelValue(Object value) {
        if (value == null) {
          return null;
        }
        Date date = (Date) value;
        return timeField.findModel(date);
      }
    
    }

  3. #3
    Sencha User
    Join Date
    Oct 2009
    Posts
    44
    Vote Rating
    0
    cutout33 is on a distinguished road

      0  

    Question I have the same exception with SimpleComboBox!

    I have the same exception with SimpleComboBox!


    Hello,

    I am having the exact same exception with SimpleComboBox!
    am using the current stable community release!

    Code:
    ava.lang.ClassCastException: java.lang.String
        at com.extjs.gxt.ui.client.widget.form.ComboBox.setValue(ComboBox.java:1)
        at com.extjs.gxt.ui.client.binding.FieldBinding.updateField(FieldBinding.java:166)
        at com.extjs.gxt.ui.client.binding.FieldBinding.bind(FieldBinding.java:80)
        at com.extjs.gxt.ui.client.binding.Bindings$1.execute(Bindings.java:91)
        at com.google.gwt.user.client.CommandExecutor.doExecuteCommands(CommandExecutor.java:310)
        at com.google.gwt.user.client.CommandExecutor$2.run(CommandExecutor.java:205)
        at com.google.gwt.user.client.Timer.fireImpl(Timer.java:160)
        at com.google.gwt.user.client.Timer.fireAndCatch(Timer.java:146)
        at com.google.gwt.user.client.Timer.fire(Timer.java:138)
        at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:592)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
        at com.google.gwt.dev.shell.moz.MethodDispatch.invoke(MethodDispatch.java:80)
        at org.eclipse.swt.internal.gtk.OS._g_main_context_iteration(Native Method)
        at org.eclipse.swt.internal.gtk.OS.g_main_context_iteration(OS.java:1428)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2840)
        at com.google.gwt.dev.SwtHostedModeBase.processEvents(SwtHostedModeBase.java:264)
        at com.google.gwt.dev.HostedModeBase.pumpEventLoop(HostedModeBase.java:557)
        at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:405)
        at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)
    Code:
    SimpleComboBox<String> comboBox = new SimpleComboBox<String>();
            FieldBinding binding = new SimpleComboBoxFieldBinding(einvoiceFlag, "combo");
            binding.bind(object);
            comboBox.setEmptyText("null text");
            comboBox.setFieldLabel(translations.comboBox());
            comboBox.setEditable(false);
            comboBox.add("Y");
            comboBox.add("N");
            comboBox.setName("combo");
            if (object.getFlag() == null || !object.getFlag().equalsIgnoreCase("y")) {
                comboBox.setValue(comboBox.getStore().getAt(1));
            } else {
                comboBox.setValue(comboBox.getStore().getAt(0));
            }
            right.add(comboBox, formData);
    Any ideas?

    Thanks in advance!

  4. #4
    Sencha User
    Join Date
    Oct 2009
    Posts
    44
    Vote Rating
    0
    cutout33 is on a distinguished road

      0  

    Default I gigured it out!

    I gigured it out!


    I should not use a binding field and setName together!
    I removed the setName and it worked just fine!

    sorry for the in convenience but am a newbe

Thread Participants: 2