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: import net.mygwt.ui.client.event.BaseEvent;
015: import net.mygwt.ui.client.event.Listener;
016:
017: import com.google.gwt.user.client.DOM;
018: import com.google.gwt.user.client.Timer;
019: import com.google.gwt.user.client.ui.WidgetHelper;
020:
021: /**
022: * A item in a <code>ExpandBar</code>.
023: *
024: * <dl>
025: * <dt><b>Events:</b></dt>
026: *
027: * <dd><b>BeforeExpand</b> : (widget)<br>
028: * <div>Fires before an item is expanded. Listeners can set the
029: * <code>doit</code> field to <code>false</code> to cancel the expand.</div>
030: * <ul>
031: * <li>widget : this</li>
032: * </ul>
033: * </dd>
034: *
035: * <dd><b>BeforeCollapse</b> : (widget)<br>
036: * <div>Fires before an item is collapsed. Listeners can set the
037: * <code>doit</code> field to <code>false</code> to cancel the collapse.</div>
038: * <ul>
039: * <li>widget : this</li>
040: * </ul>
041: * </dd>
042: *
043: * <dd><b>Expand</b> : (widget)<br>
044: * <div>Fires after an item has been expanded.</div>
045: * <ul>
046: * <li>widget : this</li>
047: * </ul>
048: * </dd>
049: *
050: * <dd><b>Collapse</b> : (widget)<br>
051: * <div>Fires ater a item is collapsed.</div>
052: * <ul>
053: * <li>widget : this</li>
054: * </ul>
055: * </dd>
056: *
057: * <dt><b>CSS:</b></dt>
058: * <dd>.my-expand-item (the item itself)</dd>
059: * <dd>.my-expand-item-hdr (the item header)</dd>
060: * <dd>.my-expand-item-hdr-text (the header text)</dd>
061: * </dl>
062: */
063: public class ExpandItem extends Component {
064:
065: protected WidgetContainer content;
066: protected IconButton collapseBtn;
067:
068: boolean expanded;
069: ExpandBar parent;
070: Item header;
071: private boolean expandOnAttach;
072:
073: /**
074: * Creates a new expand item.
075: */
076: public ExpandItem() {
077: baseStyle = "my-expand-item";
078: header = new Item() {
079: protected void onClick(BaseEvent be) {
080: super .onClick(be);
081: if (parent.headerCollapse) {
082: setExpanded(!isExpanded());
083: }
084: }
085: };
086: content = new WidgetContainer();
087: content.setStyleAttribute("position", "relative");
088: }
089:
090: /**
091: * Returns the item's content container.
092: *
093: * @return the content container
094: */
095: public WidgetContainer getContainer() {
096: return content;
097: }
098:
099: /**
100: * Returns the item's header.
101: *
102: * @return the header
103: */
104: public Item getHeader() {
105: return header;
106: }
107:
108: /**
109: * Returns the item's text.
110: *
111: * @return the text
112: */
113: public String getText() {
114: return header.getText();
115: }
116:
117: /**
118: * Returns <code>true</code> if the item is expanded, and <code>false</code>
119: * otherwise.
120: *
121: * @return the expanded state
122: */
123: public boolean isExpanded() {
124: return expanded;
125: }
126:
127: /**
128: * Sets the expanded state of the item.
129: *
130: * @param expanded the new expanded state
131: */
132: public void setExpanded(boolean expanded) {
133: if (!isAttached()) {
134: if (expanded) {
135: expandOnAttach = true;
136: }
137: return;
138: }
139: if (expanded) {
140: if (parent.fireEvent(Events.BeforeExpand, parent, this )
141: && fireEvent(Events.BeforeExpand)) {
142: this .expanded = expanded;
143: parent.expand(this );
144: }
145: } else {
146: if (parent.fireEvent(Events.BeforeCollapse, parent, this )
147: && fireEvent(Events.BeforeCollapse)) {
148: this .expanded = expanded;
149: parent.collapse(this );
150: }
151: }
152:
153: }
154:
155: public void setHeight(int height) {
156: header.setHeight(height);
157: }
158:
159: /**
160: * Sets the item's icon style.
161: *
162: * @param iconStyle the icon style
163: */
164: public void setIconStyle(String iconStyle) {
165: header.setIconStyle(iconStyle);
166: }
167:
168: /**
169: * Sets the item's text.
170: *
171: * @param text the text
172: */
173: public void setText(String text) {
174: header.setText(text);
175: }
176:
177: protected void doAttachChildren() {
178: WidgetHelper.doAttach(header);
179: WidgetHelper.doAttach(content);
180:
181: content.layout();
182: }
183:
184: protected void doDetachChildren() {
185: WidgetHelper.doDetach(header);
186: WidgetHelper.doDetach(content);
187: }
188:
189: protected void onLoad() {
190: if (expandOnAttach) {
191: expandOnAttach = false;
192: Timer t = new Timer() {
193: public void run() {
194: setExpanded(true);
195: }
196: };
197: t.schedule(200);
198: }
199: }
200:
201: protected void onRender() {
202: setElement(DOM.createDiv());
203: setStyleName(baseStyle);
204:
205: collapseBtn = new ToolButton("my-tool-plus");
206: collapseBtn.addListener(Events.Click, new Listener() {
207: public void handleEvent(BaseEvent be) {
208: setExpanded(!isExpanded());
209: be.stopEvent();
210: }
211: });
212:
213: header.baseStyle = baseStyle + "-hdr";
214: header.addWidget(collapseBtn);
215:
216: DOM.appendChild(getElement(), header.getElement());
217: DOM.appendChild(getElement(), content.getElement());
218:
219: content.setStyleName(baseStyle + "-body");
220: content.setVisible(false);
221:
222: header.setWidth("100%");
223:
224: }
225:
226: }
|