001: package com.bostechcorp.cbesb.console.client;
002:
003: import com.bostechcorp.cbesb.console.i18n.ConsoleMessages;
004: import com.google.gwt.core.client.GWT;
005: import com.google.gwt.user.client.ui.AbstractImagePrototype;
006: import com.google.gwt.user.client.ui.Composite;
007: import com.google.gwt.user.client.ui.HorizontalPanel;
008: import com.google.gwt.user.client.ui.Image;
009: import com.google.gwt.user.client.ui.Label;
010: import com.google.gwt.user.client.ui.Tree;
011: import com.google.gwt.user.client.ui.TreeItem;
012: import com.google.gwt.user.client.ui.TreeListener;
013: import com.google.gwt.user.client.ui.VerticalPanel;
014:
015: public class AdminTree extends Composite {
016:
017: private Tree tree = new Tree((ConsoleTreeImages) GWT
018: .create(ConsoleTreeImages.class));
019:
020: private Label caption = new Label();
021:
022: public ConsoleImageBundle images;
023:
024: // private List admins = new ArrayList();
025: // private TreeItem selectedAdmin =null;
026: private VerticalPanel panel;
027:
028: /**
029: * @wbp.parser.constructor
030: * @param listener
031: */
032: public AdminTree(TreeListener listener) {
033: tree.addTreeListener(listener);
034: tree.addTreeListener(new AutoExpandListener());
035: panel = new VerticalPanel();
036: initWidget(panel);
037: panel.add(caption);
038: panel.add(tree);
039: setStyleName("adm-List");
040: caption.setStyleName("adm-Caption");
041: caption.setText(((ConsoleMessages) GWT
042: .create(ConsoleMessages.class)).Menu());
043: tree.setWidth("100%");
044: panel.setWidth("100%");
045: caption.setWidth("100%");
046: //
047: images = (ConsoleImageBundle) GWT
048: .create(ConsoleImageBundle.class);
049: }
050:
051: public void addAdmin(final AdminInfo info) {
052: addAdmin(info, null, null);
053: }
054:
055: public void addAdmin(final AdminInfo info,
056: AbstractImagePrototype prototype) {
057: addAdmin(info, null, prototype);
058: }
059:
060: public void addAdmin(final AdminInfo info, String parentTitle) {
061: addAdmin(info, parentTitle, null);
062: }
063:
064: public void addAdmin(final AdminInfo info, String parentTitle,
065: AbstractImagePrototype prototype) {
066: String name = info.getName();
067: TreeItem item = new TreeItem(newImageItem(prototype, " "
068: + name));
069: item.setStyleName("adm-AdminItem");
070: item.setWidth("100%");
071: item.setTitle(name);
072: item.setUserObject(info);
073: //
074: if (parentTitle != null) {
075: for (int i = 0; i < tree.getItemCount(); i++) {
076: if (parentTitle.equals(tree.getItem(i).getTitle())) {
077: tree.getItem(i).addItem(item);
078: break;
079: } else {
080: TreeItem foundItem = findTitleRecursive(
081: parentTitle, tree.getItem(i));
082: if (foundItem != null) {
083: foundItem.addItem(item);
084: break;
085: }
086: }
087: }
088: } else {
089: tree.addItem(item);
090: }
091: }
092:
093: private TreeItem findTitleRecursive(String title, TreeItem root) {
094: TreeItem result = null;
095: for (int i = 0; i < root.getChildCount(); i++) {
096: if (title.equals(root.getChild(i).getTitle())) {
097: result = root.getChild(i);
098: break;
099: } else {
100: findTitleRecursive(title, root.getChild(i));
101: }
102: }
103: return result;
104: }
105:
106: public TreeItem findTitle(String title) {
107: for (int i = 0; i < tree.getItemCount(); i++) {
108: if (title.equals(tree.getItem(i).getTitle())) {
109: return tree.getItem(i);
110: } else {
111: TreeItem foundItem = findTitleRecursive(title, tree
112: .getItem(i));
113: if (foundItem != null) {
114: return foundItem;
115: }
116: }
117: }
118: return null;
119: }
120:
121: private HorizontalPanel newImageItem(AbstractImagePrototype image,
122: String text) {
123: return newImageItem(image, text, "");
124: }
125:
126: private HorizontalPanel newImageItem(AbstractImagePrototype image,
127: String text, String description) {
128: HorizontalPanel panel = new HorizontalPanel();
129:
130: panel.setWidth("100%");
131: if (image != null) {
132: Image img = image.createImage();
133: //img.setStyleName("adm-AdminItem");
134: if (img != null) {
135: img.setTitle(description);
136: panel.add(img);
137: }
138: }
139: Label label = new Label(text);
140: label.setTitle(description);
141: //label.setStyleName("adm-AdminItem");
142: label.setWidth("100%");
143: panel.add(label);
144: return panel;
145: }
146:
147: class AutoExpandListener implements TreeListener {
148: public void onTreeItemSelected(TreeItem item) {
149: item.setState(true);
150: }
151:
152: public void onTreeItemStateChanged(TreeItem item) {
153: }
154:
155: }
156:
157: }
|