View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 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-widget Project
18  //
19  // Tab.java
20  // Since: May 2, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.widget.client;
26  
27  import com.google.gwt.event.dom.client.ClickEvent;
28  import com.google.gwt.event.dom.client.ClickHandler;
29  import com.google.gwt.event.dom.client.MouseDownHandler;
30  import com.google.gwt.event.dom.client.MouseOutEvent;
31  import com.google.gwt.event.dom.client.MouseOutHandler;
32  import com.google.gwt.event.dom.client.MouseOverEvent;
33  import com.google.gwt.event.dom.client.MouseOverHandler;
34  import com.google.gwt.user.client.ui.Composite;
35  import com.google.gwt.user.client.ui.FlexTable;
36  import com.google.gwt.user.client.ui.FocusPanel;
37  import com.google.gwt.user.client.ui.HorizontalPanel;
38  import com.google.gwt.user.client.ui.Label;
39  import com.google.gwt.user.client.ui.TabPanel;
40  import com.google.gwt.user.client.ui.VerticalPanel;
41  import com.google.gwt.user.client.ui.Widget;
42  
43  /**
44   * Tab widget for the tab panel
45   * 
46   * @author leo
47   * 
48   */
49  public class Tab extends Composite implements MouseOutHandler, MouseOverHandler {
50  
51  	private FocusPanel focusPanel = new FocusPanel();
52  	private FlexTable layoutFrame = new FlexTable();
53  	private Label left = new Label();
54  	private HorizontalPanel center = new HorizontalPanel();
55  	private Label right = new Label();
56  	private Label centerLabel = new Label();
57  
58  	UTGBDesignFactory designFactory = new UTGBDesignFactory();
59  	private boolean enableClose = true;
60  	private Icon closeButton;
61  
62  	private TabPanel parentTabPanel = null;
63  	private Widget tabWidget = null;
64  
65  	public Tab(String label) {
66  		drawWidget(label);
67  
68  		centerLabel.addMouseOutHandler(this);
69  		centerLabel.addMouseOverHandler(this);
70  
71  		initWidget(focusPanel);
72  	}
73  
74  	public void addMouseDownHandler(MouseDownHandler handler) {
75  		centerLabel.addMouseDownHandler(handler);
76  	}
77  
78  	private void drawWidget(String label) {
79  
80  		layoutFrame.setCellPadding(0);
81  		layoutFrame.setCellSpacing(0);
82  
83  		Style.backgroundNoRepeat(left);
84  		Style.fontSize(left, 0);
85  
86  		left.setPixelSize(9, 21);
87  
88  		centerLabel.setText(label);
89  		center.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
90  		center.add(centerLabel);
91  
92  		Style.fullWidth(center);
93  		Style.textAlign(center, "center");
94  
95  		Style.backgroundRepeatX(center);
96  		Style.fontSize(centerLabel, 13);
97  		Style.fontFamily(centerLabel, "Arial, Helvetica, Tahoma");
98  		Style.bold(centerLabel);
99  		Style.fontColor(centerLabel, "#666666");
100 		Style.nowrap(centerLabel);
101 		Style.fullWidth(centerLabel);
102 		center.setHeight("21px");
103 
104 		Style.backgroundNoRepeat(right);
105 		Style.backgroundPosition(right, "top right");
106 		Style.fontSize(right, 0);
107 
108 		center.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
109 		closeButton = designFactory.getTabCloseButton();
110 		Style.padding(closeButton, Style.LEFT, 3);
111 		Style.padding(closeButton, Style.TOP, 2);
112 		center.add(closeButton);
113 
114 		closeButton.addClickHandler(new ClickHandler() {
115 
116 			public void onClick(ClickEvent arg0) {
117 				if (!enableClose)
118 					return;
119 				if (parentTabPanel != null) {
120 					if (tabWidget != null)
121 						parentTabPanel.remove(tabWidget);
122 				}
123 			}
124 
125 		});
126 
127 		right.setPixelSize(9, 21);
128 
129 		layoutFrame.setHeight("21px");
130 		layoutFrame.setWidth("100px");
131 		layoutFrame.setWidget(0, 0, left);
132 		layoutFrame.setWidget(0, 1, center);
133 		layoutFrame.setWidget(0, 2, right);
134 		layoutFrame.getRowFormatter().setVerticalAlign(0, VerticalPanel.ALIGN_MIDDLE);
135 		layoutFrame.getCellFormatter().setWidth(0, 1, "100%");
136 
137 		setBackground(false);
138 
139 		focusPanel.setWidget(layoutFrame);
140 	}
141 
142 	public void enableCloseButton(boolean enableClose) {
143 		this.enableClose = enableClose;
144 		//		
145 		// if (enableClose)
146 		// closeButton.setIcon(designFactory.getTabCloseButton());
147 		// else
148 		// closeButton.setIconImage(designFactory.getUTGBImageBundle().disabledIcon().createImage());
149 
150 		closeButton.setVisible(enableClose);
151 
152 	}
153 
154 	public void setParenTabPanel(TabPanel tabPanel, Widget tabWidet) {
155 		this.parentTabPanel = tabPanel;
156 		this.tabWidget = tabWidet;
157 	}
158 
159 	private void setBackground(boolean onMouse) {
160 		if (onMouse) {
161 			Style.backgroundImage(left, "theme/default/wframe_lw.gif");
162 			Style.backgroundImage(center, "theme/default/wframe_cw.gif");
163 			Style.backgroundImage(right, "theme/default/wframe_rw.gif");
164 		}
165 		else {
166 			Style.backgroundImage(left, "theme/default/wframe_l.gif");
167 			Style.backgroundImage(center, "theme/default/wframe_c.gif");
168 			Style.backgroundImage(right, "theme/default/wframe_r.gif");
169 		}
170 	}
171 
172 	private boolean isSelected = false;
173 
174 	public void setSelect(boolean selected) {
175 		isSelected = selected;
176 		setBackground(selected);
177 	}
178 
179 	public void onMouseOver(MouseOverEvent arg0) {
180 		setBackground(true);
181 	}
182 
183 	public void onMouseOut(MouseOutEvent arg0) {
184 		setBackground(isSelected | false);
185 	}
186 
187 }