View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // UTGB Common Project
18  //
19  // TreePanel.java
20  // Since: 2007/05/28
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.ui.treeview;
26  
27  import com.google.gwt.event.dom.client.ClickEvent;
28  import com.google.gwt.event.dom.client.ClickHandler;
29  import com.google.gwt.user.client.ui.Composite;
30  import com.google.gwt.user.client.ui.HorizontalPanel;
31  import com.google.gwt.user.client.ui.Image;
32  import com.google.gwt.user.client.ui.Panel;
33  import com.google.gwt.user.client.ui.VerticalPanel;
34  import com.google.gwt.user.client.ui.Widget;
35  
36  /**
37   * TreePanel
38   * 
39   * @author leo
40   * 
41   */
42  public class TreePanel extends Composite {
43  	private VerticalPanel _basePanel = new VerticalPanel();
44  	private Widget _content = null;
45  	private VerticalPanel _childPanel = new VerticalPanel();
46  
47  	private TreePanel _nextSibling = null;
48  	private TreePanel _firstChild = null;
49  
50  	public TreePanel() {
51  		init();
52  	}
53  
54  	public TreePanel(Widget content) {
55  		this._content = content;
56  		init();
57  	}
58  
59  	private void init() {
60  		_basePanel.setStyleName("tree-panel");
61  		initWidget(_basePanel);
62  	}
63  
64  	public TreePanel getNextSibling() {
65  		return _nextSibling;
66  	}
67  
68  	public TreePanel getFirstChild() {
69  		return _firstChild;
70  	}
71  
72  	public TreePanel addChild(Widget w) {
73  		return addChild(new TreePanel(w));
74  	}
75  
76  	public TreePanel addSiblign(Widget w) {
77  		return addSibling(new TreePanel(w));
78  	}
79  
80  	public TreePanel addChild(TreePanel child) {
81  		if (_firstChild == null)
82  			return _firstChild = child;
83  		else
84  			return _firstChild.addSibling(child);
85  	}
86  
87  	public TreePanel addSibling(TreePanel sibling) {
88  		if (_nextSibling == null)
89  			return _nextSibling = sibling;
90  		else
91  			return _nextSibling.addSibling(sibling);
92  	}
93  
94  	public int numChildren() {
95  		if (_firstChild == null)
96  			return 0;
97  		else
98  			return _firstChild.numSiblings();
99  	}
100 
101 	public int numSiblings() {
102 		TreePanel cursor = _nextSibling;
103 		int count = 0;
104 		while (cursor != null) {
105 			count++;
106 			cursor = cursor._nextSibling;
107 		}
108 		return count;
109 	}
110 
111 	public void layout(Panel panel) {
112 
113 		// setup the base panel
114 		_basePanel.clear();
115 		_basePanel.add(new TreePanelSwitch());
116 		if (_content != null)
117 			_basePanel.add(_content);
118 		_basePanel.add(_childPanel);
119 
120 		panel.add(this);
121 		if (_nextSibling != null) {
122 			HorizontalPanel siblingPanel = new HorizontalPanel();
123 			panel.add(siblingPanel);
124 			_nextSibling.layout(siblingPanel);
125 		}
126 
127 		if (_firstChild != null) {
128 			_firstChild.layout(_childPanel);
129 		}
130 	}
131 
132 }
133 
134 class TreePanelSwitch extends Image implements ClickHandler {
135 	private boolean _isOpen = true;
136 
137 	private static String OPEN_IMAGE = "image/tree_open.gif";
138 	private static String CLOSED_IMAGE = "image/tree_closed.gif";
139 	private static String WHITE_IMAGE = "image/tree_white.gif";
140 
141 	public TreePanelSwitch() {
142 		super(OPEN_IMAGE);
143 	}
144 
145 	public void onClick(ClickEvent e) {
146 		_isOpen = !_isOpen;
147 		this.setUrl(CLOSED_IMAGE);
148 	}
149 
150 }