View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.widgets.tabs.impl;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.student.common.ui.client.mvc.Callback;
24  import org.kuali.student.common.ui.client.widgets.ClickablePanel;
25  import org.kuali.student.common.ui.client.widgets.KSLabel;
26  import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
27  import org.kuali.student.common.ui.client.widgets.menus.KSListPanel;
28  import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanelAbstract;
29  import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanel.TabPanelStyle;
30  import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanel.TabPosition;
31  
32  import com.google.gwt.event.dom.client.ClickEvent;
33  import com.google.gwt.event.dom.client.ClickHandler;
34  import com.google.gwt.event.dom.client.MouseOutEvent;
35  import com.google.gwt.event.dom.client.MouseOutHandler;
36  import com.google.gwt.event.dom.client.MouseOverEvent;
37  import com.google.gwt.event.dom.client.MouseOverHandler;
38  import com.google.gwt.user.client.ui.ComplexPanel;
39  import com.google.gwt.user.client.ui.Composite;
40  import com.google.gwt.user.client.ui.FlowPanel;
41  import com.google.gwt.user.client.ui.HorizontalPanel;
42  import com.google.gwt.user.client.ui.Image;
43  import com.google.gwt.user.client.ui.Label;
44  import com.google.gwt.user.client.ui.SimplePanel;
45  import com.google.gwt.user.client.ui.Widget;
46  
47  public class KSTabPanelImpl extends KSTabPanelAbstract{
48  	
49  	private VerticalFlowPanel container = new VerticalFlowPanel();
50  	private FlowPanel tabRow = new FlowPanel();
51  	//private HorizontalPanel left = new HorizontalPanel();
52  	//private HorizontalPanel right = new HorizontalPanel();
53  	private KSListPanel left = new KSListPanel();
54  	private KSListPanel right = new KSListPanel();
55  	private SimplePanel content = new SimplePanel();
56  	private Tab selectedTab;
57  	private Map<String, Tab> tabMap = new HashMap<String, Tab>();
58  	
59  
60  	
61  /*	protected class TabPanel extends SimplePanel implements HasAllMouseHandlers{
62  
63  		@Override
64  		public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
65  			// TODO Auto-generated method stub
66  			return null;
67  		}
68  
69  		@Override
70  		public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
71  			// TODO Auto-generated method stub
72  			return null;
73  		}
74  
75  		@Override
76  		public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
77  			// TODO Auto-generated method stub
78  			return null;
79  		}
80  
81  		@Override
82  		public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
83  			// TODO Auto-generated method stub
84  			return null;
85  		}
86  
87  		@Override
88  		public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
89  			// TODO Auto-generated method stub
90  			return null;
91  		}
92  
93  		@Override
94  		public HandlerRegistration addMouseWheelHandler(
95  				MouseWheelHandler handler) {
96  			// TODO Auto-generated method stub
97  			return null;
98  		}
99  
100 		
101 	}*/
102 	
103 	protected class Tab extends Composite{
104 		private boolean selected = false;
105 		private Widget tab;
106 		private ClickablePanel tabPanel = new ClickablePanel();
107 		private Widget displayContent;
108 		private int labelIndex = -1;
109 		private String tabKey = "";
110 		private List<Callback<String>> callbacks = new ArrayList<Callback<String>>();
111 		
112 		public Tab(String key, String label, Image image, Widget displayContent){
113 			tabKey = key;
114 			HorizontalPanel tabContent = new HorizontalPanel();
115 			tabContent.add(image);
116 			KSLabel labelWidget = new KSLabel(label);
117 			tabContent.add(labelWidget);
118 			labelIndex = tabContent.getWidgetIndex(labelWidget);
119 			init(tabContent, displayContent);
120 			
121 		}
122 		
123 		public Tab(String key, String label, Widget displayContent){
124 			tabKey = key;
125 			KSLabel labelWidget = new KSLabel(label);
126 			init(labelWidget, displayContent);
127 		}
128 		
129 		public Tab(String key, Widget tab, Widget displayContent){
130 			tabKey = key;
131 			init(tab, displayContent);
132 		}
133 		
134 		public void addCallback(Callback<String> callback) {
135 			callbacks.add(callback);
136 		}
137 		
138 		public void emptyCallbacks(){
139 			callbacks = new ArrayList<Callback<String>>();
140 		}
141 
142 		public String getTabKey() {
143 			return tabKey;
144 		}
145 
146 		public void init(Widget tab, Widget displayContent){
147 			this.tab = tab;
148 			this.tabPanel.setWidget(tab);
149 			tabPanel.addMouseOverHandler(new MouseOverHandler(){
150 
151 				@Override
152 				public void onMouseOver(MouseOverEvent event) {
153 					Tab.this.onMouseOver();
154 					
155 				}
156 			});
157 			tabPanel.addClickHandler(new ClickHandler(){
158 
159 				@Override
160 				public void onClick(ClickEvent event) {
161 					Tab.this.onSelect();
162 					if(!callbacks.isEmpty()){
163 						for(Callback<String> callback: callbacks){
164 							callback.exec(tabKey);
165 						}
166 					}
167 				}
168 			});
169 			tabPanel.addMouseOutHandler(new MouseOutHandler(){
170 
171 				@Override
172 				public void onMouseOut(MouseOutEvent event) {
173 					Tab.this.onMouseOut();
174 					
175 				}
176 			});
177 			tabPanel.addStyleName("KS-TabPanel-Tab");
178 			this.initWidget(tabPanel);
179 			this.displayContent = displayContent;
180 		}
181 		
182 		public void addTabStyleName(String style){
183 			tabPanel.addStyleName(style);
184 		}
185 		
186 		public void onSelect(){
187 			KSTabPanelImpl.this.deselectCurrentTab();
188 			KSTabPanelImpl.this.selectedTab = Tab.this;
189 			selected = true;
190 			this.onMouseOut();
191 			tabPanel.addStyleName("KS-TabPanel-Tab-Selected");
192 			if(tab instanceof Label || tab instanceof KSLabel){
193 				tab.addStyleName("KS-TabPanel-Tab-Label-Selected");
194 			}
195 			else if(tab instanceof ComplexPanel){
196 				if(labelIndex != -1){
197 					Widget w = ((ComplexPanel) tab).getWidget(labelIndex);
198 					w.addStyleName("KS-TabPanel-Tab-Label-Selected");
199 				}
200 			}
201 			KSTabPanelImpl.this.content.clear();
202 			KSTabPanelImpl.this.content.setWidget(displayContent);
203 		}
204 		
205 		public void onDeselect(){
206 			selected = false;
207 			tabPanel.removeStyleName("KS-TabPanel-Tab-Selected");
208 			if(tab instanceof Label || tab instanceof KSLabel){
209 				tab.removeStyleName("KS-TabPanel-Tab-Label-Selected");
210 			}
211 			else if(tab instanceof ComplexPanel){
212 				if(labelIndex != -1){
213 					Widget w = ((ComplexPanel) tab).getWidget(labelIndex);
214 					w.removeStyleName("KS-TabPanel-Tab-Label-Selected");
215 				}
216 			}
217 		}
218 		
219 		public void onMouseOver(){
220 			if(!selected){
221 				tabPanel.addStyleName("KS-TabPanel-Tab-Hover");
222 				if(tab instanceof Label || tab instanceof KSLabel){
223 					tab.addStyleName("KS-TabPanel-Tab-Label-Hover");
224 				}
225 				else if(tab instanceof ComplexPanel){
226 					if(labelIndex != -1){
227 						Widget w = ((ComplexPanel) tab).getWidget(labelIndex);
228 						w.addStyleName("KS-TabPanel-Tab-Label-Hover");
229 					}
230 				}
231 			}
232 		}
233 		
234 		public void onMouseOut(){
235 			tabPanel.removeStyleName("KS-TabPanel-Tab-Hover");
236 			if(tab instanceof Label || tab instanceof KSLabel){
237 				tab.removeStyleName("KS-TabPanel-Tab-Label-Hover");
238 			}
239 			else if(tab instanceof ComplexPanel){
240 				if(labelIndex != -1){
241 					Widget w = ((ComplexPanel) tab).getWidget(labelIndex);
242 					w.removeStyleName("KS-TabPanel-Tab-Label-Hover");
243 				}
244 			}
245 		}
246 	}
247 	
248 	public KSTabPanelImpl(){
249 		//left.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
250 		//right.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
251 		//tabRow.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
252 		tabRow.add(left);
253 		tabRow.add(right);
254 		tabRow.addStyleName("KS-TabPanel-TabRow");
255 		left.addStyleName("KS-TabPanel-Left-Panel");
256 		right.addStyleName("KS-TabPanel-Right-Panel");
257 		container.addStyleName("KS-TabPanel-Full");
258 		content.addStyleName("KS-TabPanel-Content");
259 		container.add(tabRow);
260 		container.add(content);
261 		this.initWidget(container);
262 	}
263 	
264 	public KSTabPanelImpl(TabPanelStyle style){
265 		//left.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
266 		//right.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
267 		//tabRow.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
268 		tabRow.add(left);
269 		tabRow.add(right);
270 		tabRow.addStyleName("KS-TabPanel-TabRow");
271 		left.addStyleName("KS-TabPanel-Left-Panel");
272 		right.addStyleName("KS-TabPanel-Right-Panel");
273 
274 		container.addStyleName("KS-TabPanel");
275 		content.addStyleName("KS-TabPanel-Content");
276 		container.add(tabRow);
277 		container.add(content);
278 		this.initWidget(container);
279 	}
280 	
281 	public void setTabPanelStyle(TabPanelStyle style){
282 		if(style == TabPanelStyle.FULL_PAGE){
283 			container.setStyleName("KS-TabPanel-Full");
284 		}
285 		else if(style == TabPanelStyle.SMALL){
286 			container.setStyleName("KS-TabPanel-Small");
287 		}
288 	}
289 
290 	private void deselectCurrentTab() {
291 		if(selectedTab != null){
292 			selectedTab.onDeselect();
293 		}
294 	}
295 
296 	@Override
297 	public void selectTab(String key){
298 		Tab tab = tabMap.get(key);
299 		tab.onSelect();
300 	}
301 
302 	@Override
303 	public void removeTab(String key){
304 		Tab tab = tabMap.get(key);
305 		tab.removeFromParent();
306 	}
307 	
308 	private void positionTab(Tab tab, TabPosition position){
309 		if(position == TabPosition.LEFT){
310 			tab.addStyleName("KS-TabPanel-Tab-Left");
311 			left.add(tab);
312 		}
313 		else{
314 			tab.addStyleName("KS-TabPanel-Tab-Right");
315 			right.add(tab);
316 		}
317 	}
318 
319 	@Override
320 	public void addTab(String key, Widget tabWidget, Widget content, TabPosition position){
321 		Tab tab = new Tab(key, tabWidget, content);
322 		tabMap.put(key, tab);
323 		positionTab(tab, position);
324 	}
325 
326 	@Override
327 	public void addTab(String key, String label, Widget content, TabPosition position){
328 		Tab tab = new Tab(key, label, content);
329 		tabMap.put(key, tab);
330 		positionTab(tab, position);
331 	}
332 
333 	@Override
334 	public void addTab(String key, String label, Image image, Widget content, TabPosition position){
335 		Tab tab = new Tab(key, label, image, content);
336 		tabMap.put(key, tab);
337 		positionTab(tab, position);
338 	}
339 
340 	@Override
341 	public void addTab(String key, String label, Image image, Widget content){
342 		Tab tab = new Tab(key, label, image, content);
343 		tabMap.put(key, tab);
344 		positionTab(tab, TabPosition.LEFT);
345 	}
346 
347 	@Override
348 	public void addTab(String key, String label, Widget content){
349 		Tab tab = new Tab(key, label, content);
350 		tabMap.put(key, tab);
351 		positionTab(tab, TabPosition.LEFT);
352 	}
353 	
354 	@Override
355 	public void addTab(String key, Widget tabWidget, Widget content){
356 		Tab tab = new Tab(key, tabWidget, content);
357 		tabMap.put(key, tab);
358 		positionTab(tab, TabPosition.LEFT);
359 	}
360 
361 	@Override
362 	public void addStyleName(String style) {
363 		container.addStyleName(style);
364 	}
365 
366 	@Override
367 	public void addTabCustomCallback(final String key, final Callback<String> callback) {
368 		Tab tab = tabMap.get(key);
369 		if(tab != null){
370 			tab.addCallback(callback);
371 		}
372 		
373 	}
374 
375 	@Override
376 	public String getSelectedTabKey() {
377 		if(selectedTab != null){
378 			return selectedTab.getTabKey();
379 		}
380 		else{
381 			return "";
382 		}
383 		
384 	}
385 
386 	@Override
387 	public int getTabCount() {
388 		return tabMap.size();
389 	}
390 
391 	@Override
392 	public void removeTabCustomCallbacks(String key) {
393 		Tab tab = tabMap.get(key);
394 		if(tab != null){
395 			tab.emptyCallbacks();
396 		}
397 		
398 	}
399 
400 	@Override
401 	public boolean hasTabKey(String key) {
402 		return tabMap.containsKey(key);
403 	}
404 
405 	
406 }