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  // WindowControlTrack.java
20  // Since: Jun 19, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  
30  import org.utgenome.gwt.utgb.client.track.Track;
31  import org.utgenome.gwt.utgb.client.track.TrackBase;
32  import org.utgenome.gwt.utgb.client.track.TrackFrame;
33  import org.utgenome.gwt.utgb.client.track.TrackGroup;
34  import org.utgenome.gwt.utgb.client.ui.FormLabel;
35  
36  import com.google.gwt.core.client.GWT;
37  import com.google.gwt.event.dom.client.ClickEvent;
38  import com.google.gwt.event.dom.client.ClickHandler;
39  import com.google.gwt.event.dom.client.KeyCodes;
40  import com.google.gwt.event.dom.client.KeyPressEvent;
41  import com.google.gwt.event.dom.client.KeyPressHandler;
42  import com.google.gwt.user.client.ui.Button;
43  import com.google.gwt.user.client.ui.HorizontalPanel;
44  import com.google.gwt.user.client.ui.TextBox;
45  import com.google.gwt.user.client.ui.Widget;
46  
47  /**
48   * Track window control buttons
49   * 
50   * @author leo
51   * 
52   */
53  public class WindowControlTrack extends TrackBase {
54  
55  	public static TrackFactory factory() {
56  		return new TrackFactory() {
57  			public Track newInstance() {
58  				return new WindowControlTrack();
59  			}
60  		};
61  	}
62  
63  	private HorizontalPanel _panel = new HorizontalPanel();
64  	private Button _hideAndShowButton = new Button("minimize tracks");
65  	private Button _packAndUnPackButton = new Button("pack tracks");
66  	private Button _closeAllButton = new Button("close tracks");
67  	private TextBox _windowSizeInput = new TextBox();
68  	private TrackGroup _rootTrackGroup;
69  	private final Track _self = this;
70  
71  	public WindowControlTrack() {
72  		super("Window Controller");
73  
74  		_hideAndShowButton.addClickHandler(new ClickHandler() {
75  			private boolean _minimized = false;
76  
77  			public void onClick(ClickEvent e) {
78  				_rootTrackGroup.setResizeNotification(false);
79  				if (!_minimized) {
80  					for (Track track : getTrackGroup().getAllTrackList()) {
81  						if (track.equals(_self))
82  							continue;
83  						track.getFrame().minimize();
84  					}
85  					_hideAndShowButton.setText("open tracks");
86  				}
87  				else {
88  					for (Track track : getTrackGroup().getAllTrackList()) {
89  						if (track.equals(_self))
90  							continue;
91  						track.getFrame().open();
92  					}
93  					_hideAndShowButton.setText("minimize tracks");
94  				}
95  				_rootTrackGroup.setResizeNotification(true);
96  				_rootTrackGroup.notifyResize();
97  
98  				_minimized = !_minimized;
99  			}
100 		});
101 
102 		_packAndUnPackButton.addClickHandler(new ClickHandler() {
103 			private boolean _packed = false;
104 
105 			public void onClick(ClickEvent e) {
106 				_rootTrackGroup.setResizeNotification(false);
107 				if (!_packed) {
108 					for (Track track : getTrackGroup().getAllTrackList()) {
109 						if (track.equals(_self))
110 							continue;
111 						track.getFrame().pack();
112 					}
113 					_packAndUnPackButton.setText("unpack tracks");
114 				}
115 				else {
116 					for (Track track : getTrackGroup().getAllTrackList()) {
117 						if (track.equals(_self))
118 							continue;
119 						track.getFrame().unpack();
120 					}
121 					_packAndUnPackButton.setText("pack tracks");
122 				}
123 				_rootTrackGroup.setResizeNotification(true);
124 				_rootTrackGroup.notifyResize();
125 				_packed = !_packed;
126 			}
127 		});
128 
129 		_closeAllButton.addClickHandler(new ClickHandler() {
130 			public void onClick(ClickEvent e) {
131 				_rootTrackGroup.setResizeNotification(false);
132 
133 				// create a list of tracks to be closed
134 				ArrayList<Track> closeTarget = new ArrayList<Track>();
135 				for (Track track : getTrackGroup().getAllTrackList()) {
136 					closeTarget.add(track);
137 				}
138 
139 				// close tracks
140 				TrackGroup trackGroup = getTrackGroup();
141 				for (Iterator<Track> it = closeTarget.iterator(); it.hasNext();) {
142 					Track track = it.next();
143 					trackGroup.removeTrack(track);
144 				}
145 				_rootTrackGroup.setResizeNotification(true);
146 				_rootTrackGroup.notifyResize();
147 			}
148 		});
149 
150 		_windowSizeInput.setMaxLength(4);
151 		_windowSizeInput.addKeyPressHandler(new KeyPressHandler() {
152 			public void onKeyPress(KeyPressEvent e) {
153 				if (e.getCharCode() == KeyCodes.KEY_ENTER) {
154 					try {
155 						int windowSize = Integer.parseInt(_windowSizeInput.getText());
156 						if (windowSize >= 500) {
157 							getTrackGroup().setTrackWindowWidth(windowSize);
158 						}
159 					}
160 					catch (NumberFormatException ex) {
161 						GWT.log(_windowSizeInput.getText() + " is not a integer", ex);
162 					}
163 				}
164 			}
165 
166 		});
167 
168 		_panel.add(_hideAndShowButton);
169 		_panel.add(_packAndUnPackButton);
170 		_panel.add(_closeAllButton);
171 		_panel.add(new FormLabel("window size (>= 500): "));
172 		_panel.add(_windowSizeInput);
173 	}
174 
175 	public Widget getWidget() {
176 		return _panel;
177 	}
178 
179 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
180 		trackFrame.pack();
181 		trackFrame.disablePack();
182 
183 		_rootTrackGroup = group.getRootTrackGroup();
184 	}
185 
186 }