001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Darrell Meyer <darrell@mygwt.net> - derived implementation
011: *******************************************************************************/package net.mygwt.ui.client.widget;
012:
013: import net.mygwt.ui.client.Events;
014:
015: import com.google.gwt.user.client.DOM;
016: import com.google.gwt.user.client.ui.HorizontalPanel;
017: import com.google.gwt.user.client.ui.WidgetHelper;
018:
019: /**
020: * A standard tool bar.
021: *
022: * <dt><b>Events:</b></dt>
023: *
024: * <dd><b>BeforeAdd</b> : (widget, item, index)<br>
025: * <div>Fires before a item is added or inserted. Listeners can set the
026: * <code>doit</code> field to <code>false</code> to cancel the action.</div>
027: * <ul>
028: * <li>widget : this</li>
029: * <li>item : the item being added</li>
030: * <li>index : the index at which the item will be added</li>
031: * </ul>
032: * </dd>
033: *
034: * <dd><b>BeforeRemove</b> : (widget, item)<br>
035: * <div>Fires before a item is removed. Listeners can set the <code>doit</code>
036: * field to <code>false</code> to cancel the action.</div>
037: * <ul>
038: * <li>widget : this</li>
039: * <li>item : the item being removed</li>
040: * </ul>
041: * </dd>
042: *
043: * <dd><b>Add</b> : (widget, item, index)<br>
044: * <div>Fires after a item has been added or inserted.</div>
045: * <ul>
046: * <li>widget : this</li>
047: * <li>item : the item that was added</li>
048: * <li>index : the index at which the item will be added</li>
049: * </ul>
050: * </dd>
051: *
052: * <dd><b>Remove</b> : (widget, item)<br>
053: * <div>Fires after a item has been removed.</div>
054: * <ul>
055: * <li>widget : this</li>
056: * <li>item : the item being removed</li>
057: * </ul>
058: * </dd>
059: *
060: * <dt><b>CSS:</b></dt>
061: * <dd>my-toolbar (the tool bar)</dd>
062: * </dl>
063: *
064: * @see ToolItem
065: */
066: public class ToolBar extends Container {
067:
068: private HorizontalPanel panel;
069:
070: /**
071: * Creates a new tool bar.
072: */
073: public ToolBar() {
074: attachChildren = false;
075: baseStyle = "my-toolbar";
076: }
077:
078: /**
079: * Adds a item to the tool bar.
080: *
081: * @param item the item to add
082: */
083: public void add(ToolItem item) {
084: insert(item, getItemCount());
085: }
086:
087: /**
088: * Returns the item at the specified index.
089: *
090: * @param index the item index
091: * @return the item at the index
092: */
093: public ToolItem getItem(int index) {
094: if ((index < 0) || (index >= items.size()))
095: return null;
096: return (ToolItem) items.get(index);
097: }
098:
099: /**
100: * Returns the number of tool item's.
101: *
102: * @return the item count
103: */
104: public int getItemCount() {
105: return items.size();
106: }
107:
108: /**
109: * Returns the index of the item.
110: *
111: * @param item the item
112: * @return the index
113: */
114: public int indexOf(ToolItem item) {
115: return items.indexOf(item);
116: }
117:
118: /**
119: * Inserts a item into the tool bar.
120: *
121: * @param item the item to add
122: * @param index the insert location
123: */
124: public void insert(ToolItem item, int index) {
125: if (fireEvent(Events.BeforeAdd, this , item, index)) {
126: items.add(index, item);
127: if (rendered) {
128: renderItem(item, index);
129: }
130: fireEvent(Events.Add, this , item, index);
131: }
132: }
133:
134: /**
135: * Removes a tool item.
136: *
137: * @param item the item to be removed
138: */
139: public void remove(ToolItem item) {
140: if (fireEvent(Events.BeforeRemove, this , item)) {
141: items.remove(item);
142: if (rendered) {
143: panel.remove(item);
144: }
145: fireEvent(Events.Remove, this , item);
146: }
147: }
148:
149: /**
150: * Removes all tool items.
151: */
152: public void removeAll() {
153: int size = getItemCount();
154: for (int i = 0; i < size; i++) {
155: ToolItem item = getItem(0);
156: remove(item);
157: }
158: }
159:
160: protected void doAttachChildren() {
161: WidgetHelper.doAttach(panel);
162: }
163:
164: protected void doDetachChildren() {
165: WidgetHelper.doDetach(panel);
166: }
167:
168: protected void onRender() {
169: setElement(DOM.createDiv());
170: setStyleName(baseStyle);
171:
172: panel = new HorizontalPanel();
173: panel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
174: panel.setSpacing(2);
175:
176: DOM.appendChild(getElement(), panel.getElement());
177:
178: renderAll();
179: }
180:
181: private void renderAll() {
182: int count = getItemCount();
183: for (int i = 0; i < count; i++) {
184: ToolItem item = getItem(i);
185: renderItem(item, i);
186: }
187: }
188:
189: private void renderItem(ToolItem item, int index) {
190: panel.insert(item, index);
191: }
192:
193: }
|