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.Style;
015: import net.mygwt.ui.client.MyDOM;
016: import net.mygwt.ui.client.event.BaseEvent;
017: import net.mygwt.ui.client.event.Listener;
018: import net.mygwt.ui.client.event.SelectionListener;
019: import net.mygwt.ui.client.event.TypedListener;
020: import net.mygwt.ui.client.widget.menu.Menu;
021:
022: import com.google.gwt.user.client.Command;
023: import com.google.gwt.user.client.DOM;
024: import com.google.gwt.user.client.DeferredCommand;
025: import com.google.gwt.user.client.Element;
026: import com.google.gwt.user.client.Event;
027:
028: /**
029: * A selectable item in a <code>ToolBar</code>.
030: *
031: * <dl>
032: * <dt><b>Styles:</b></dt>
033: * <dd>PUSH, TOGGLE, MENU, SPLIT_MENU, SEPERATOR</dd>
034: *
035: * <dt><b>Events:</b></dt>
036: *
037: * <dd><b>Select</b> : (widget)<br>
038: * <div>Fires after the item is selected.</div>
039: * <ul>
040: * <li>widget : this</li>
041: * </ul>
042: * </dd>
043: *
044: * <dt><b>CSS:</b></dt>
045: * <dd>my-toolitem (the tool item)</dd>
046: * <dd>my-toolitem-text (the tool item text)</dd>
047: * </dl>
048: *
049: * <p>
050: * Note: Only one of the styles PUSH, TOGGLE, MENU and SPLIT_MENU may be
051: * specified.
052: * </p>
053: */
054: public class ToolItem extends Item {
055:
056: ToolBar toolBar;
057: Menu menu;
058: private Listener menuListener;
059: private int style;
060:
061: /**
062: * Creates a new PUSH toolitem.
063: */
064: public ToolItem() {
065: this (Style.PUSH);
066: }
067:
068: /**
069: * Creates a new tool item.
070: *
071: * @param style the item's style information
072: */
073: public ToolItem(int style) {
074: super ("my-toolitem");
075: this .style = style;
076: setDisabledStyle("my-toolitem-disabled");
077: }
078:
079: /**
080: * Adds a listener interface to receive selection events.
081: *
082: * @param listener the listener to add
083: */
084: public void addSelectionListener(SelectionListener listener) {
085: TypedListener tl = new TypedListener(listener);
086: addListener(Events.Select, tl);
087: }
088:
089: /**
090: * Returns the item's menu.
091: *
092: * @return the menu
093: */
094: public Menu getMenu() {
095: return menu;
096: }
097:
098: /**
099: * Returns the item's parent tool bar.
100: *
101: * @return the toolbar
102: */
103: public ToolBar getToolBar() {
104: return toolBar;
105: }
106:
107: /**
108: * Returns <code>true</code> if the item is selected.
109: *
110: * @return the selected state
111: */
112: public boolean isSelected() {
113: return super .isSelected();
114: }
115:
116: /**
117: * Removes a previously added listener.
118: *
119: * @param listener the listener to be removed
120: */
121: public void removeSelectionListener(SelectionListener listener) {
122: unhook(Events.EffectStart, listener);
123: }
124:
125: public void setIconStyle(String style) {
126: super .setIconStyle(style);
127: if (rendered) {
128: MyDOM.setVisible(midleftElem, true);
129: }
130: }
131:
132: /**
133: * Sets the item's menu.
134: *
135: * <p>
136: * Only applicable when item is of type <code>MENU</code> or
137: * <code>SPLIT_MENU</code>.
138: * </p>
139: *
140: * @param menu the menu for the drop down
141: */
142: public void setMenu(Menu menu) {
143: this .menu = menu;
144: }
145:
146: /**
147: * Sets the item's selected state.
148: *
149: * @param selected the select state
150: */
151: public void setSelected(boolean selected) {
152: super .setSelected(selected);
153: }
154:
155: /**
156: * Sets the item's style. Has no effect if called after the item has been rendered.
157: * @param style the style
158: */
159: public void setStyle(int style) {
160: if (!isRendered()) {
161: this .style = style;
162: }
163: }
164:
165: /**
166: * Sets the item's text.
167: *
168: * @param text the text
169: */
170: public void setText(String text) {
171: super .setText(text);
172: if (rendered) {
173: MyDOM.setVisible(centerElem, true);
174: }
175: }
176:
177: protected void onClick(BaseEvent be) {
178: super .onClick(be);
179: be.stopEvent();
180: switch (style) {
181: case Style.TOGGLE:
182: setSelected(!isSelected(), false);
183: break;
184: case Style.MENU:
185: onMenuClick(be.event);
186: break;
187: case Style.SPLIT_MENU:
188: onSplitMenuClick(be.event);
189: break;
190: default:
191: fireEvent(Events.Select);
192: break;
193: }
194: }
195:
196: protected void onHideMenu() {
197: setSelected(false);
198: menu.removeListener(Events.Close, menuListener);
199: menu.hide();
200: }
201:
202: protected void onMenuClick(Event event) {
203: if (menu == null) {
204: return;
205: }
206: if (isSelected()) {
207: setSelected(false);
208: onHideMenu();
209: } else {
210: setSelected(true);
211: onShowMenu();
212: }
213: }
214:
215: protected void onMouseOut(BaseEvent be) {
216: super .onMouseOut(be);
217: if (style == Style.SPLIT_MENU) {
218: MyDOM
219: .setStyleName(midRightElem, "my-toolitem-split",
220: false);
221: }
222: }
223:
224: protected void onMouseOver(BaseEvent be) {
225: super .onMouseOver(be);
226: if (style == Style.SPLIT_MENU) {
227: MyDOM.setStyleName(midRightElem, "my-toolitem-split", true);
228: }
229: }
230:
231: protected void onRender() {
232: super .onRender();
233:
234: MyDOM.setVisible(centerElem, false);
235: MyDOM.setVisible(midleftElem, false);
236: MyDOM.setVisible(midRightElem, false);
237:
238: if (getText() != null) {
239: MyDOM.setVisible(centerElem, true);
240: }
241:
242: if (iconStyle != null) {
243: MyDOM.setVisible(midleftElem, true);
244: }
245:
246: switch (style) {
247: case Style.SEPARATOR:
248: Element div = DOM.createDiv();
249: MyDOM.setStyleName(div, "my-toolitem-seperator");
250: setElement(div);
251: break;
252: case Style.MENU:
253: case Style.SPLIT_MENU:
254: MyDOM.setVisible(midRightElem, true);
255: Element d = DOM.createDiv();
256: MyDOM.setStyleName(d, "my-toolitem-split");
257: DOM.appendChild(midRightElem, d);
258: break;
259: }
260:
261: menuListener = new Listener() {
262: public void handleEvent(BaseEvent be) {
263: onHideMenu();
264: }
265: };
266: }
267:
268: protected void onShowMenu() {
269: addStyleName(baseStyle + "-sel");
270: final ToolItem fItem = this ;
271: DeferredCommand.addCommand(new Command() {
272: public void execute() {
273: menu.show(fItem);
274: menu.addListener(Events.Close, menuListener);
275: }
276: });
277:
278: }
279:
280: protected void onSplitMenuClick(Event event) {
281: Element target = DOM.eventGetTarget(event);
282: Element right = DOM.getNextSibling(midRightElem);
283: if (DOM.isOrHasChild(midRightElem, target)
284: || DOM.isOrHasChild(right, target)) {
285: onMenuClick(event);
286: } else {
287: fireEvent(Events.Select);
288: }
289: }
290:
291: private void setSelected(boolean selected, boolean supressEvents) {
292: super.setSelected(selected);
293: if (!supressEvents) {
294: fireEvent(Events.Select);
295: }
296: }
297:
298: }
|