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.MyDOM;
015: import net.mygwt.ui.client.MyGWT;
016: import net.mygwt.ui.client.event.BaseEvent;
017: import net.mygwt.ui.client.event.SelectionListener;
018: import net.mygwt.ui.client.event.TypedListener;
019:
020: import com.google.gwt.user.client.DOM;
021: import com.google.gwt.user.client.Element;
022:
023: /**
024: * A item contained in a <code>List</code>.
025: *
026: * <dl>
027: * <dt><b>Events:</b></dt>
028: *
029: * <dd><b>Select</b> : (widget this)<br>
030: * <div>Fired when an item is selected.</div>
031: * <ul>
032: * <li>widget : this</li>
033: * </ul>
034: * </dd>
035: * </dl>
036: *
037: * <dt><b>CSS:</b></dt>
038: * <dd>.my-listitem (list item)</dd>
039: * <dd>.my-listitem .my-listitem-text (list item text)</dd>
040: * </dl>
041: *
042: * @see List
043: */
044: public class ListItem extends Item {
045:
046: List list;
047: IconButton checkBtn;
048: private boolean checked;
049:
050: /**
051: * Creates a new list item.
052: */
053: public ListItem() {
054: baseStyle = "my-listitem";
055: sinkEvents = false;
056: }
057:
058: /**
059: * Creates a new list item.
060: *
061: * @param text the text
062: */
063: public ListItem(String text) {
064: this ();
065: setText(text);
066: }
067:
068: protected void onMouseOver(BaseEvent be) {
069: super .onMouseOver(be);
070: list.hoverItem = this ;
071: }
072:
073: /**
074: * Adds a listener interface to receive selection events.
075: *
076: * @param listener the listener to add
077: */
078: public void addSelectionListener(SelectionListener listener) {
079: TypedListener tl = new TypedListener(listener);
080: addListener(Events.Select, tl);
081: }
082:
083: /**
084: * Returns the item's parent list.
085: *
086: * @return the list
087: */
088: public List getList() {
089: return list;
090: }
091:
092: /**
093: * Returns <code>true</code> if the item is checked.
094: *
095: * @return the checked state
096: */
097: public boolean isChecked() {
098: return checked;
099: }
100:
101: /**
102: * Removes a previously added listener.
103: *
104: * @param listener the listener to be removed
105: */
106: public void removeSelectionListener(SelectionListener listener) {
107: unhook(Events.EffectStart, listener);
108: }
109:
110: /**
111: * Sets the item's checked state.
112: *
113: * @param checked the check state
114: */
115: public void setChecked(boolean checked) {
116: this .checked = checked;
117: if (rendered) {
118: String s = checked ? "icon-checked" : "icon-notchecked";
119: checkBtn.changeStyle(s);
120: }
121: }
122:
123: protected void onClick(BaseEvent be) {
124: fireEvent(Events.Select);
125: }
126:
127: protected void onRender() {
128: super .onRender();
129:
130: if (!MyGWT.isIE) {
131: DOM.setElementPropertyInt(getElement(), "tabIndex", 0);
132: }
133:
134: if (list.check) {
135: checkBtn = new IconButton("icon-notchecked");
136: checkBtn.setStyleAttribute("marginRight", "4px");
137: Element elem = MyDOM.findChild("my-listitem-check",
138: getElement());
139: DOM.appendChild(elem, checkBtn.getElement());
140:
141: if (checked) {
142: setChecked(checked);
143: }
144: }
145:
146: }
147:
148: }
|