001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.client.widgets;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011: import com.gwtext.client.util.JavaScriptObjectHelper;
012: import com.gwtext.client.widgets.form.*;
013: import com.gwtext.client.widgets.grid.EditorGridPanel;
014: import com.gwtext.client.widgets.grid.GridPanel;
015: import com.gwtext.client.widgets.grid.PropertyGridPanel;
016: import com.gwtext.client.widgets.menu.Item;
017:
018: /**
019: * Component factory class.
020: */
021: public class ComponentFactory {
022:
023: /**
024: * Return a Component from the passed native component object.
025: *
026: * @param jsObj native object
027: * @return the corresponding Component.
028: * @see com.gwtext.client.widgets.Component
029: */
030: public static Component getComponent(JavaScriptObject jsObj) {
031: Object componentJ = JavaScriptObjectHelper
032: .getAttributeAsObject(jsObj, "__compJ");
033: if (componentJ != null) {
034: return (Component) componentJ;
035: }
036: String xtype = getXType(jsObj);
037:
038: if (xtype == null) {
039: //commenting out now because only issue with treenode passed as Component as noted below, and its not a serious issue
040: //uncomment once ext fixes.
041: //http://extjs.com/forum/showthread.php?t=2324
042: //String message = "Illegal Component without xtype " + JavaScriptObjectHelper.getPropertiesAsString(jsObj);
043: //GWT.log(message, new IllegalArgumentException(message));
044: return null;
045: }
046: if (xtype.equalsIgnoreCase("box")) {
047: return new BoxComponent(jsObj);
048: } else if (xtype.equalsIgnoreCase("button")) {
049: return new Button(jsObj);
050: } else if (xtype.equalsIgnoreCase("colorpalette")) {
051: return new ColorPalette(jsObj);
052: } else if (xtype.equalsIgnoreCase("cycle")) {
053: return new CycleButton(jsObj);
054: } else if (xtype.equalsIgnoreCase("dataview")) {
055: return new DataView(jsObj);
056: } else if (xtype.equalsIgnoreCase("datepicker")) {
057: return new DatePicker(jsObj);
058: } else if (xtype.equalsIgnoreCase("editor")) {
059: return new Editor(jsObj);
060: } else if (xtype.equalsIgnoreCase("editorgrid")) {
061: return new EditorGridPanel(jsObj);
062: } else if (xtype.equalsIgnoreCase("propertygrid")) {
063: return new PropertyGridPanel(jsObj);
064: } else if (xtype.equalsIgnoreCase("grid")) {
065: return new GridPanel(jsObj);
066: } else if (xtype.equalsIgnoreCase("paging")) {
067: return new PagingToolbar(jsObj);
068: } else if (xtype.equalsIgnoreCase("button")) {
069: return new Button(jsObj);
070: } else if (xtype.equalsIgnoreCase("panel")) {
071: return new Panel(jsObj);
072: } else if (xtype.equalsIgnoreCase("progress")) {
073: return new ProgressBar(jsObj);
074: } else if (xtype.equalsIgnoreCase("splitbutton")) {
075: return new SplitButton(jsObj);
076: } else if (xtype.equalsIgnoreCase("tabpanel")) {
077: return new TabPanel(jsObj);
078: } else if (xtype.equalsIgnoreCase("window")) {
079: return new Window(jsObj);
080: } else if (xtype.equalsIgnoreCase("gwtwidget")) {
081: return new WidgetComponent(jsObj);
082: }
083: //toolbar components - tbitem, tbseparator, tbspacer, tbfill, tbtext, tbbutton, tbsplit?
084: else if (xtype.equalsIgnoreCase("toolbar")) {
085: return new Toolbar(jsObj);
086: }
087: //menu items
088: else if (xtype.equalsIgnoreCase("menu-item")) {
089: return new Item(jsObj);
090: }
091: //form components
092: else if (xtype.equalsIgnoreCase("checkbox")) {
093: return new Checkbox(jsObj);
094: } else if (xtype.equalsIgnoreCase("combo")) {
095: return new ComboBox(jsObj);
096: } else if (xtype.equalsIgnoreCase("datefield")) {
097: return new DateField(jsObj);
098: } else if (xtype.equalsIgnoreCase("fieldset")) {
099: return new FieldSet(jsObj);
100: } else if (xtype.equalsIgnoreCase("form")) {
101: return new FormPanel(jsObj);
102: } else if (xtype.equalsIgnoreCase("hidden")) {
103: return new Hidden(jsObj);
104: } else if (xtype.equalsIgnoreCase("htmleditor")) {
105: return new HtmlEditor(jsObj);
106: } else if (xtype.equalsIgnoreCase("numberfield")) {
107: return new NumberField(jsObj);
108: } else if (xtype.equalsIgnoreCase("radio")) {
109: return new Radio(jsObj);
110: } else if (xtype.equalsIgnoreCase("textarea")) {
111: return new TextArea(jsObj);
112: } else if (xtype.equalsIgnoreCase("textfield")) {
113: return new TextField(jsObj);
114: } else if (xtype.equalsIgnoreCase("timefield")) {
115: return new TimeField(jsObj);
116: } else {
117: throw new IllegalArgumentException("Unrecognized xtype "
118: + xtype);
119: }
120: }
121:
122: private static native String getXType(JavaScriptObject jsObj) /*-{
123: //when a TreeNode is dragged out of a TreePanel, the Container 'remove' event is called with the TreeNode passed as the Component
124: //but a TreeNode is not a Component. temp fix until this is resolved.
125: //http://extjs.com/forum/showthread.php?t=2324
126: var xtype = jsObj.getXType ? jsObj.getXType() : null;
127: return xtype === undefined ? null : xtype;
128: }-*/;
129: }
|