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:
009: package com.gwtext.client.widgets;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.google.gwt.user.client.Element;
013: import com.gwtext.client.widgets.form.Field;
014:
015: /**
016: * Basic Toolbar class.
017: */
018: public class Toolbar extends BoxComponent {
019:
020: private static JavaScriptObject configPrototype;
021:
022: static {
023: init();
024: }
025:
026: private static native void init()/*-{
027: var c = new $wnd.Ext.Toolbar();
028: @com.gwtext.client.widgets.Toolbar::configPrototype = c.initialConfig;
029: }-*/;
030:
031: protected JavaScriptObject getConfigPrototype() {
032: return configPrototype;
033: }
034:
035: /**
036: * Create a new Toolbar.
037: */
038: public Toolbar() {
039: }
040:
041: public Toolbar(JavaScriptObject jsObj) {
042: super (jsObj);
043: }
044:
045: public String getXType() {
046: return "toolbar";
047: }
048:
049: protected native JavaScriptObject create(JavaScriptObject config) /*-{
050: if(!config.items) config.items = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
051: return new $wnd.Ext.Toolbar(config);
052: }-*/;
053:
054: /**
055: * Adds a button to the toolbar.
056: *
057: * @param button the button
058: */
059: public void addButton(ToolbarButton button) {
060: if (isRendered()) {
061: JavaScriptObject componentJS = button.isCreated() ? button
062: .getOrCreateJsObj() : button.getConfig();
063: addButtonPostRender(componentJS);
064:
065: } else {
066: JavaScriptObject componentJS = button.isCreated() ? button
067: .getOrCreateJsObj() : button.getConfig();
068: addButtonPreRender(componentJS);
069: }
070: }
071:
072: private native void addButtonPreRender(JavaScriptObject componentJS) /*-{
073: var config = this.@com.gwtext.client.widgets.Component::config;
074:
075: if(!config.items) {
076: config.items = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
077: }
078: config.items.push(componentJS);
079: }-*/;
080:
081: private native void addButtonPostRender(JavaScriptObject componentJS) /*-{
082: var panel = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
083: panel.addButton(componentJS);
084: }-*/;
085:
086: /**
087: * Adds a toolbar menu button.
088: *
089: * @param button the button to add
090: */
091: public void addButton(ToolbarMenuButton button) {
092: if (isRendered()) {
093: JavaScriptObject componentJS = button.isCreated() ? button
094: .getOrCreateJsObj() : button.getConfig();
095: addButtonPostRender(componentJS);
096:
097: } else {
098: JavaScriptObject componentJS = button.isCreated() ? button
099: .getOrCreateJsObj() : button.getConfig();
100: addButtonPreRender(componentJS);
101: }
102: }
103:
104: /**
105: * Adds any standard element to the toolbar.
106: *
107: * @param element the element to add
108: */
109: public void addElement(Element element) {
110: addItem(new ToolbarItem(element));
111: }
112:
113: /**
114: * Adds a dynamically rendered {@link Field} (TextField, ComboBox, etc). Note: the field should not have been rendered yet.
115: *
116: * @param field the field to add
117: */
118: public void addField(Field field) {
119: if (isRendered()) {
120: JavaScriptObject componentJS = field.isCreated() ? field
121: .getOrCreateJsObj() : field.getConfig();
122: addFieldPostRender(componentJS);
123:
124: } else {
125: JavaScriptObject componentJS = field.isCreated() ? field
126: .getOrCreateJsObj() : field.getConfig();
127: addFieldPreRender(componentJS);
128: }
129: }
130:
131: private static native void addFieldPostRender(JavaScriptObject field)/*-{
132: var toolbar = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
133: toolbar.addField(field);
134: }-*/;
135:
136: private native void addFieldPreRender(JavaScriptObject componentJS) /*-{
137: var config = this.@com.gwtext.client.widgets.Component::config;
138:
139: if(!config.items) {
140: config.items = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
141: }
142: config.items.push(componentJS);
143: }-*/;
144:
145: //http://extjs.com/forum/showthread.php?t=2470
146: /**
147: * Adds a fill element that forces subsequent additions to the right side of the toolbar.
148: */
149: public void addFill() {
150: if (isRendered()) {
151: addFillRendered();
152: } else {
153: addItem(new ToolbarFill());
154: }
155: }
156:
157: private native void addFillRendered() /*-{
158: var toolbar = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
159: toolbar.addFill();
160: }-*/;
161:
162: /**
163: * Adds any {@link ToolbarItem} or subclass.
164: *
165: * @param item the item to add
166: */
167: public void addItem(ToolbarItem item) {
168: if (isRendered()) {
169: JavaScriptObject componentJS = item.getJsObj();
170: addItemPostRender(componentJS);
171:
172: } else {
173: JavaScriptObject componentJS = item.getJsObj();
174: addItemPreRender(componentJS);
175: }
176: }
177:
178: private native void addItemPreRender(JavaScriptObject componentJS) /*-{
179: var config = this.@com.gwtext.client.widgets.Component::config;
180:
181: if(!config.items) {
182: config.items = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
183: }
184: config.items.push(componentJS);
185: }-*/;
186:
187: private native void addItemPostRender(JavaScriptObject componentJS) /*-{
188: var panel = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
189: panel.addItem(componentJS);
190: }-*/;
191:
192: /**
193: * Adds a separator.
194: */
195: public void addSeparator() {
196: if (isRendered()) {
197: addSeparatorRendered();
198: } else {
199: addItem(new ToolbarSeparator());
200: }
201: }
202:
203: private native void addSeparatorRendered() /*-{
204: var toolbar = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
205: var separator = toolbar.addSeparator();
206: }-*/;
207:
208: /**
209: * Adds a spacer element.
210: *
211: */
212: public void addSpacer() {
213: if (isRendered()) {
214: addSpacerRendered();
215: } else {
216: addItem(new ToolbarSpacer());
217: }
218: }
219:
220: private native void addSpacerRendered() /*-{
221: var toolbar = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
222: var spacer = toolbar.addSpacer();
223: }-*/;
224:
225: /**
226: * Adds text to the toolbar.
227: *
228: * @param text the text
229: */
230: public void addText(String text) {
231: if (isRendered()) {
232: addTextRendered(text);
233: } else {
234: addItem(new ToolbarTextItem(text));
235: }
236: }
237:
238: private native void addTextRendered(String text) /*-{
239: var toolbar = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
240: toolbar.addText(text);
241: }-*/;
242:
243: //todo2
244: /**
245: * Inserts a ToolbarItem at the specified index.
246: *
247: * @param index the index to insert at
248: * @param item the idnem to inser
249: */
250: /*public void insertItem(int index, ToolbarItem item) {
251: insertButton(getJsObj(), index, item.getJsObj());
252: }
253:
254: private native void insertButton(JavaScriptObject toolbar, int index, JavaScriptObject item) *//*-{
255: toolbar.insertButton(index, item);
256: }-*//*;
257: */
258: /**
259: * Inserts a ToolbarButton at the specified index.
260: *
261: * @param index the index to insert at
262: * @param button the button to isnert
263: */
264: /* public void insertButton(int index, ToolbarButton button) {
265: insertButton(getJsObj(), index, button.getJsObj());
266: }*/
267: }
|