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  // Aqua Project
18  //
19  // TabViewer.java
20  // Since: 2007/03/24
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.ui.tab;
26  
27  import java.util.ArrayList;
28  
29  import org.utgenome.gwt.utgb.client.ui.tab.TabEntry.TabEntryFactory;
30  
31  import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
32  import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
33  import com.google.gwt.event.logical.shared.SelectionEvent;
34  import com.google.gwt.event.logical.shared.SelectionHandler;
35  import com.google.gwt.user.client.ui.Composite;
36  import com.google.gwt.user.client.ui.DockPanel;
37  import com.google.gwt.user.client.ui.TabBar;
38  
39  /**
40   * A TabViewer can have several TabEntry classes.
41   * 
42   * <code>
43   * TabViewer v = new TabViewer();
44   * v.addTabEntry(MyTab.factory());
45   * v.addTabEntry(MyTab2.facory());
46   * </code>
47   * 
48   * @author leo
49   * 
50   */
51  public class TabViewer extends Composite implements BeforeSelectionHandler<Integer>, SelectionHandler<Integer> {
52  
53  	private ArrayList<TabEntryFactory> _tabEntryList = new ArrayList<TabEntryFactory>();
54  	private DockPanel _panel = new DockPanel();
55  	private TabBar _tabBar = new TabBar();
56  	private DockPanel _entryPanel = new DockPanel();
57  
58  	public TabViewer() {
59  		_panel.add(_tabBar, DockPanel.NORTH);
60  
61  		_tabBar.addSelectionHandler(this);
62  		_tabBar.addBeforeSelectionHandler(this);
63  
64  		_panel.add(_entryPanel, DockPanel.CENTER);
65  
66  		_panel.setWidth("100%");
67  		_entryPanel.setWidth("100%");
68  
69  		initWidget(_panel);
70  	}
71  
72  	public void selectTab(int index) {
73  		_tabBar.selectTab(index);
74  	}
75  
76  	public void addTabEntry(final TabEntryFactory factory) {
77  		String name = factory.getTabName();
78  		_tabEntryList.add(factory);
79  		_tabBar.addTab(name);
80  	}
81  
82  	public void onBeforeSelection(BeforeSelectionEvent<Integer> e) {
83  
84  		TabEntryFactory factory = _tabEntryList.get(e.getItem());
85  
86  		// calling focus events
87  		for (TabEntryFactory f : _tabEntryList) {
88  			if (f.equals(factory))
89  				continue; // skip the focused tab entry
90  			if (f.isInstanciated()) {
91  				f.getInstance().onLostFocus();
92  			}
93  		}
94  	}
95  
96  	public void onSelection(SelectionEvent<Integer> e) {
97  
98  		_entryPanel.clear();
99  		TabEntryFactory factory = _tabEntryList.get(e.getSelectedItem());
100 		TabEntry entry = factory.getInstance();
101 		_entryPanel.add(entry, DockPanel.CENTER);
102 		entry.onFocus();
103 
104 	}
105 }