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  // GenomeBrowser Project
18  //
19  // TrackQueue.java
20  // Since: Jun 6, 2007
21  //
22  // $URL$ 
23  // $Author$s
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track;
26  
27  import java.util.Iterator;
28  
29  import org.utgenome.gwt.utgb.client.UTGBEntryPointBase;
30  import org.utgenome.gwt.utgb.client.ui.DraggableTable;
31  import org.utgenome.gwt.utgb.client.ui.WindowBox;
32  import org.utgenome.gwt.utgb.client.ui.WindowListener;
33  
34  import com.google.gwt.user.client.ui.Composite;
35  
36  /**
37   * {@link TrackQueue} is a widget that draws tracks
38   * 
39   * @author leo
40   * 
41   */
42  public class TrackQueue extends Composite implements TrackUpdateListener, TrackLayoutManager, WindowListener {
43  	private TrackGroup _trackGroup;
44  	private final DraggableTable _trackQueue = new DraggableTable();
45  
46  	public TrackQueue(TrackGroup trackGroup) {
47  		this._trackGroup = trackGroup;
48  		init();
49  		initWidget(_trackQueue);
50  	}
51  
52  	private void init() {
53  		_trackQueue.setStyleName("track-queue");
54  		initTrackGroup(_trackGroup);
55  
56  		//		Window.addResizeHandler(new ResizeHandler() {
57  		//
58  		//			public void onResize(ResizeEvent e) {
59  		//				int width = e.getWidth();
60  		//				int panelWidth = width - (TrackFrame.INFOPANEL_WIDTH + TrackFrame.SCROLLBAR_WIDTH);
61  		//				if (panelWidth > 0) {
62  		//					_trackGroup.setTrackWindowWidth(panelWidth);
63  		//				}
64  		//
65  		//			}
66  		//		});
67  
68  	}
69  
70  	private void initTrackGroup(TrackGroup trackGroup) {
71  		trackGroup.setTrackLayoutManager(this);
72  		trackGroup.addTrackUpdateListener(this);
73  	}
74  
75  	private void sync(TrackGroup group) {
76  		// resync TrackQueue and TrackGroup
77  		for (Iterator<Track> it = group.getTrackList().iterator(); it.hasNext();) {
78  			Track track = it.next();
79  			onInsertTrack(track);
80  		}
81  		for (Iterator<TrackGroup> it = group.getTrackGroupList().iterator(); it.hasNext();) {
82  			TrackGroup trackGroup = it.next();
83  			sync(trackGroup);
84  		}
85  	}
86  
87  	public WindowBox createWindow() {
88  		WindowBox win = new WindowBox(this);
89  		win.addWindowListener(this);
90  		return win;
91  	}
92  
93  	public void adjustHeight() {
94  		int height = _trackGroup.getHeight();
95  		_trackQueue.setHeight(height + "px");
96  	}
97  
98  	public int getTrackIndex(Track track) {
99  		return _trackQueue.getIndex(track.getFrame());
100 	}
101 
102 	public void onRemoveTrack(Track track) {
103 		_trackQueue.remove(track.getFrame());
104 		/*
105 		 * // when track queue is empty if(_trackQueue.empty()) { _trackGroup.addTrack(new ToolBoxTrack()); }
106 		 */
107 	}
108 
109 	public void onInsertTrack(Track track) {
110 		onInsertTrack(track, -1);
111 	}
112 
113 	public void onInsertTrack(Track track, int beforeIndex) {
114 		TrackFrame frame = new TrackFrame(track, _trackGroup.getTrackWindow().getPixelWidth(), track.getDefaultWindowHeight());
115 		if (beforeIndex < 0) {
116 			// insert to the tail of the queue
117 			_trackQueue.add(frame, frame.getFrameBar());
118 		}
119 		else
120 			_trackQueue.insert(frame, frame.getFrameBar(), beforeIndex);
121 		// draw the track
122 		track.refresh();
123 	}
124 
125 	public void onAddTrackGroup(TrackGroup trackGroup) {
126 		initTrackGroup(trackGroup);
127 		for (Track track : trackGroup.getTrackList()) {
128 			onInsertTrack(track);
129 		}
130 		for (TrackGroup group : trackGroup.getTrackGroupList()) {
131 			onAddTrackGroup(group);
132 		}
133 	}
134 
135 	public void onRemoveTrackGroup(TrackGroup trackGroup) {
136 		for (Track track : trackGroup.getTrackList()) {
137 			onRemoveTrack(track);
138 		}
139 		for (TrackGroup group : trackGroup.getTrackGroupList()) {
140 			onRemoveTrackGroup(group);
141 		}
142 	}
143 
144 	public void onResizeTrack() {
145 		adjustHeight();
146 	}
147 
148 	protected void onLoad() {
149 		for (Track track : _trackGroup.getAllTrackList()) {
150 			track.getFrame().adjustFrameHeight();
151 		}
152 		adjustHeight();
153 		// remove loading message
154 		UTGBEntryPointBase.hideLoadingMessage();
155 	}
156 
157 	public void onButtonClick(WindowBox window, int buttonType) {
158 		// do nothing
159 	}
160 
161 	public void onResizeWindow(WindowBox window, int newWindowSize) {
162 		int trackWindowWidth = newWindowSize - TrackFrame.INFOPANEL_WIDTH;
163 		if (trackWindowWidth <= 0)
164 			trackWindowWidth = 1;
165 		TrackWindow trackWindow = _trackGroup.getTrackWindow();
166 		TrackGroupPropertyWriter writer = _trackGroup.getPropertyWriter();
167 		writer.setTrackWindowSize(trackWindowWidth);
168 	}
169 
170 	public void onResizeTrackWindow(int newWindowSize) {
171 		_trackQueue.setWidth((TrackFrame.INFOPANEL_WIDTH + newWindowSize + TrackFrame.SCROLLBAR_WIDTH) + "px");
172 	}
173 }