001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.widget.tree;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.mygwt.ui.client.Events;
014: import net.mygwt.ui.client.MyGWT;
015: import net.mygwt.ui.client.event.BaseEvent;
016: import net.mygwt.ui.client.util.Util;
017:
018: import com.google.gwt.user.client.Command;
019: import com.google.gwt.user.client.DeferredCommand;
020:
021: /**
022: * A multi-select tree selection model.
023: *
024: * <dl>
025: * <dt><b>Events:</b></dt>
026: *
027: * <dd><b>SelectionChange</b> : (source this, widget, item, items)<br>
028: * <div>Fires after the selection has changed.</div>
029: * <ul>
030: * <li>source : this</li>
031: * <li>widget : the table</li>
032: * <li>item : single selection or null</li>
033: * <li>items : multi selection or null</li>
034: * </ul>
035: * </dd>
036: * </dl>
037: */
038: public class MultiSelectionModel extends TreeSelectionModel {
039:
040: private List selItems;
041:
042: /**
043: * Creates a new multi select selection model.
044: */
045: public MultiSelectionModel() {
046: selItems = new ArrayList();
047: }
048:
049: public void deselectAll() {
050: if (selItems.size() > 0) {
051: while (selItems.size() > 0) {
052: TreeItem item = (TreeItem) selItems.remove(0);
053: item.getUI().onSelectedChange(false);
054: }
055: BaseEvent be = new BaseEvent();
056: be.widget = tree;
057: be.items = Util.createArray(selItems);
058: fireEvent(Events.SelectionChange, be);
059: tree.fireEvent(Events.SelectionChange, be);
060: }
061: }
062:
063: /**
064: * Returns a list of selected items.
065: *
066: * @return the selected items
067: */
068: public TreeItem[] getSelection() {
069: return (TreeItem[]) selItems.toArray(new TreeItem[0]);
070: }
071:
072: public boolean isSelected(TreeItem item) {
073: return selItems.contains(item);
074: }
075:
076: public void select(TreeItem item) {
077: select(item, false);
078: }
079:
080: /**
081: * Selects a item.
082: *
083: * @param item the item to be selected
084: * @param keepSelected <code>true</code> to preserve selections
085: */
086: public void select(final TreeItem item, boolean keepSelected) {
087: if (!keepSelected) {
088: deselectAll();
089: }
090: if (isSelected(item)) {
091: lastSelItem = item;
092: return;
093: }
094: selItems.add(item);
095: lastSelItem = item;
096: selItem = item;
097: item.getUI().onSelectedChange(true);
098: BaseEvent be = new BaseEvent();
099: be.widget = tree;
100: be.items = Util.createArray(selItems);
101: fireEvent(Events.SelectionChange, be);
102: tree.fireEvent(Events.SelectionChange, be);
103: if (MyGWT.isSafari) {
104: DeferredCommand.addCommand(new Command() {
105: public void execute() {
106: tree.focus();
107: }
108: });
109: }
110: }
111:
112: /**
113: * Selects a list of item's.
114: *
115: * @param items the items to be selected
116: */
117: public void selectItems(List items) {
118: for (int i = 0; i < items.size(); i++) {
119: TreeItem item = (TreeItem) items.get(i);
120: select(item);
121: }
122: }
123:
124: /**
125: * Deslects a list of items.
126: *
127: * @param items the items to be deselected
128: */
129: public void deselect(List items) {
130: for (int i = 0; i < items.size(); i++) {
131: TreeItem item = (TreeItem) items.get(i);
132: deselect(item);
133: }
134: }
135:
136: public void deselect(TreeItem item) {
137: if (selItems.contains(item)) {
138: item.getUI().onSelectedChange(false);
139: selItems.remove(item);
140: BaseEvent be = new BaseEvent();
141: be.widget = tree;
142: be.items = Util.createArray(selItems);
143: fireEvent(Events.SelectionChange, be);
144: tree.fireEvent(Events.SelectionChange, be);
145: }
146: }
147:
148: protected void onItemClick(TreeItem item, BaseEvent be) {
149: if (be.isRightClick()) {
150: return;
151: }
152: if (!be.within(item.getUI().getJointEl())) {
153: if (isSelected(item) && be.isControlKey()) {
154: deselect(item);
155: } else {
156: select(item, be.isControlKey());
157: }
158: }
159: }
160:
161: }
|