Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    #16
    I'm getting closer, I think. Using SmartGWT Power 2.5 release and I've followed this example in the Showcase. The complete source for my new CanvasItem subclass is below.

    I've copied the technique as faithfully as I can from the example. The only difference is that I'm using setEditorType(new IpClassPicker()) on the data source field instead of creating an IpClassPicker and using that in form.setFields().

    The form displays nothing next to the field what has this editor type. I can see from the dev console that it is "showing" a CanvasItem, but it appears that there is no canvas.

    I'm also showing that same field in a ListGrid with a FilterEditor. When the grid displays (and when the form displays) I see this in the dev console.

    Code:
    19:10:21.533 [ERROR] [ipgui] 19:10:21.532:MUP0:WARN:CanvasItem:isc_CanvasItem_0:CanvasItem: itemClass defined with no canvas property - creating a default canvas for this item.
    com.smartgwt.client.core.JsObject$SGWT_WARN: 19:10:21.532:MUP0:WARN:CanvasItem:isc_CanvasItem_0:CanvasItem: itemClass defined with no canvas property - creating a default canvas for this item.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
        at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
        at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
        at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
        at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
        at com.smartgwt.client.widgets.form.DynamicForm.create(DynamicForm.java)
        at com.smartgwt.client.widgets.BaseWidget.getOrCreateJsObj(BaseWidget.java:361)
        at sun.reflect.GeneratedMethodAccessor90.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.MethodDispatch.invoke(MethodDispatch.java:71)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
        at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
        at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289)
        at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
        at com.smartgwt.client.widgets.form.ValuesManager.addMember(ValuesManager.java)
        at com.islandpacific.gui.client.IpTabbedForm.createForms(IpTabbedForm.java:220)
        at com.islandpacific.gui.client.IpTabbedForm.<init>(IpTabbedForm.java:162)
        at com.islandpacific.gui.client.IpTabbedForm.<init>(IpTabbedForm.java:128)
        at com.islandpacific.gui.client.IpGenericEditor.<init>(IpGenericEditor.java:78)
        at com.islandpacific.gui.client.ConstantsDefinition.getEditTab(ConstantsDefinition.java:168)
        at com.islandpacific.gui.client.ConstantsDefinition.handleMenuSelection(ConstantsDefinition.java:116)
        at com.islandpacific.gui.client.ConstantsDefinition$2.onClick(ConstantsDefinition.java:108)
        at com.smartgwt.client.widgets.menu.events.MenuItemClickEvent.dispatch(MenuItemClickEvent.java:98)
        at com.smartgwt.client.widgets.menu.events.MenuItemClickEvent.dispatch(MenuItemClickEvent.java:1)
        at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
        at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193)
        at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
        at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:127)
        at com.smartgwt.client.core.DataClass.fireEvent(DataClass.java:248)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        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.MethodDispatch.invoke(MethodDispatch.java:71)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
        at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
        at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
        at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
        at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
        at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:214)
        at sun.reflect.GeneratedMethodAccessor98.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.MethodDispatch.invoke(MethodDispatch.java:71)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:281)
        at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:531)
        at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
        at java.lang.Thread.run(Thread.java:680)
    Code:
    package com.islandpacific.gui.client;
    
    import com.google.gwt.core.client.JavaScriptObject;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.fields.CanvasItem;
    import com.smartgwt.client.widgets.form.fields.FormItem;
    import com.smartgwt.client.widgets.form.fields.IPickTreeItem;
    import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
    import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
    import com.smartgwt.client.widgets.form.fields.events.FormItemInitHandler;
    import com.smartgwt.client.widgets.form.fields.events.ShowValueEvent;
    import com.smartgwt.client.widgets.form.fields.events.ShowValueHandler;
    
    public class IpClassPicker extends CanvasItem{
    
    	private static final int CLASS_TO_NODE_MULTIPLIER = 100000;
    
    	public IpClassPicker() {
    		super();
    		init();
    	}
    
    	public IpClassPicker(JavaScriptObject jso) {
    		super(jso);
    		init();
    	}
    
    	public IpClassPicker(String name) {
    		super(name);
    		init();
    	}
    
    	public IpClassPicker(String name, String title) {
    		super(name, title);
    		init();
    	}
    
    	/**
    	 * Initialize the data source and field name to use for the picker
    	 */
    	private void init() {
    
    		setShouldSaveValue(true); 
    		setInitHandler(new FormItemInitHandler() {
    
    			@Override
    			public void onInit(FormItem item) {
    				
    				// Create a pick tree for selecting a class from the merchandise hierarchy.
    				DataSource merchHierarchyDS = DataSource.get(IslandPacificDSConstants.DATASOURCE_IPMerchHierarchyDS);
    				IPickTreeItem pickTree = new IPickTreeItem(item.getName(), item.getTitle());
    				pickTree.setDataSource(merchHierarchyDS);
    				pickTree.setValueField("ID");
    				pickTree.setDisplayField("Name");
    				pickTree.setLoadDataOnDemand(false);
    				pickTree.setCanSelectParentItems(false);
    				pickTree.addChangedHandler(new ChangedHandler() {					
    					@Override
    					public void onChanged(ChangedEvent event) {
    						storeValue((Integer)event.getValue() - CLASS_TO_NODE_MULTIPLIER);
    					}
    				});
    				
    				// Create a form with the pickTree in it and set that as our canvas.
    				DynamicForm form = new DynamicForm();
    				form.setFields(pickTree);
    				setCanvas(form);
    
    				// Add a handler to show the value when the form this item is in gets populated with values.
    				// This will translate the class number into the pick tree node ID.
    				addShowValueHandler(new ShowValueHandler() {
    					@Override
    					public void onShowValue(ShowValueEvent event) {
    						DynamicForm form = (DynamicForm)((CanvasItem)event.getSource()).getCanvas();
    						IPickTreeItem pickTree = (IPickTreeItem) form.getItem(getName());  
    						pickTree.setValue((Integer)event.getDataValue() + CLASS_TO_NODE_MULTIPLIER);
    					}
    				});				
    			}
    		});  
    		
    	}
    
    	@Override
    	public void focusInItem() {
    		DynamicForm form = (DynamicForm)this.getCanvas();
    		IPickTreeItem pickTree = (IPickTreeItem) form.getItem(getName());  
    		pickTree.focusInItem();
    	};
    }

    Comment


      #17
      Note the special instructions for a class used via setEditorType() - use strictly handlers, no method overrides, etc.

      Comment


        #18
        As far as I can tell I'm following those rules. The constructor sets an initHandler and adds a ShowValueHandler. I was overriding the focusInItem() method but I've removed that and it made no difference.

        One thing that may be a clue is that in the onInit method in the initHandler I'm setting the Canvas for my CanvasItem by calling setCanvas(form). That's how the example shows it being done.

        But I noticed in another example that the FormItem passed in to onInit() is cast to a CanvasItem and setCanvas is called on that, as in ((CanvasItem)item).setCanvas(form). But when I try that I get a cast exception saying that a FormItem cannot be cast to a CanvasItem.

        It must be something simple as there is just not much code involved. Can you see what I've got wrong in the code I posted earlier?

        Comment


          #19
          I found it. It was that last rule about converting the event's FormItem.

          CanvasItem item = new CanvasItem(itemIn.getJsObj());

          Comment

          Working...
          X