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  // DraggableTable.java
20  // Since: May 31, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.ui;
26  
27  import java.util.Iterator;
28  
29  import com.allen_sauer.gwt.dnd.client.PickupDragController;
30  import com.allen_sauer.gwt.dnd.client.drop.IndexedDropController;
31  import com.google.gwt.user.client.ui.AbsolutePanel;
32  import com.google.gwt.user.client.ui.Composite;
33  import com.google.gwt.user.client.ui.VerticalPanel;
34  import com.google.gwt.user.client.ui.Widget;
35  
36  /**
37   * DraggabeleTable supports drag & drop replacement of internal widgets.
38   * 
39   * Note: it is better to set the width of the internal widgets to prevent them from expanding to the entire window.
40   * 
41   * @author leo
42   * 
43   */
44  public class DraggableTable extends Composite {
45  	public static final String TABLE_STYLE = "dg-table";
46  
47  	private AbsolutePanel _boundaryPanel = new AbsolutePanel();
48  	private VerticalPanel _verticalPanel = new VerticalPanel();
49  
50  	private PickupDragController widgetDragController;
51  	private IndexedDropController widgetDropController;
52  
53  	public DraggableTable() {
54  		_boundaryPanel.setSize("100%", "100%");
55  		_boundaryPanel.add(_verticalPanel);
56  		_verticalPanel.setStyleName(TABLE_STYLE);
57  
58  		// do not allow drop for _boundaryPanel
59  		widgetDragController = new PickupDragController(_boundaryPanel, false);
60  		widgetDropController = new IndexedDropController(_verticalPanel);
61  
62  		widgetDragController.setBehaviorConstrainedToBoundaryPanel(false);
63  		widgetDragController.registerDropController(widgetDropController);
64  
65  		initWidget(_boundaryPanel);
66  	}
67  
68  	/**
69  	 * Widget must implements SourcesMouseEvents
70  	 * 
71  	 * @param w
72  	 *            widget
73  	 */
74  	public void add(Widget w) {
75  		widgetDragController.makeDraggable(w);
76  		_verticalPanel.add(w);
77  	}
78  
79  	/**
80  	 * Get the index of the specified child widget position
81  	 * 
82  	 * @param w
83  	 * @return
84  	 */
85  	public int getIndex(Widget w) {
86  		return _verticalPanel.getWidgetIndex(w);
87  	}
88  
89  	/**
90  	 * Add the widge with the specified drag handle before the location beforeIndex
91  	 * 
92  	 * @param w
93  	 * @param dragHandle
94  	 * @param beforeIndex
95  	 */
96  	public void insert(Widget w, Widget dragHandle, int beforeIndex) {
97  		widgetDragController.makeDraggable(w, dragHandle);
98  		_verticalPanel.insert(w, beforeIndex);
99  	}
100 
101 	/**
102 	 * Add the widget with the specified drag hanel
103 	 * 
104 	 * @param w
105 	 * @param dragHandle
106 	 *            draggable part in the Widget w
107 	 */
108 	public void add(Widget w, Widget dragHandle) {
109 		widgetDragController.makeDraggable(w, dragHandle);
110 		_verticalPanel.add(w);
111 	}
112 
113 	public Iterator<Widget> iterator() {
114 		return _verticalPanel.iterator();
115 	}
116 
117 	public void remove(Widget w) {
118 		_verticalPanel.remove(w);
119 	}
120 
121 	public boolean empty() {
122 		return _verticalPanel.getWidgetCount() == 0;
123 	}
124 
125 	public void clear() {
126 		_verticalPanel.clear();
127 	}
128 
129 }