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  // UTGBMedaka Project
18  //
19  // ViewLoaderTrack.java
20  // Since: Aug 13, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import org.utgenome.gwt.utgb.client.UTGBClientException;
28  import org.utgenome.gwt.utgb.client.UTGBEntryPointBase;
29  import org.utgenome.gwt.utgb.client.track.Track;
30  import org.utgenome.gwt.utgb.client.track.TrackBase;
31  import org.utgenome.gwt.utgb.client.track.TrackGroup;
32  import org.utgenome.gwt.utgb.client.ui.FormLabel;
33  import org.utgenome.gwt.utgb.client.view.TrackView;
34  
35  import com.google.gwt.core.client.GWT;
36  import com.google.gwt.event.dom.client.ClickEvent;
37  import com.google.gwt.event.dom.client.ClickHandler;
38  import com.google.gwt.event.dom.client.KeyCodes;
39  import com.google.gwt.event.dom.client.KeyPressEvent;
40  import com.google.gwt.event.dom.client.KeyPressHandler;
41  import com.google.gwt.user.client.DOM;
42  import com.google.gwt.user.client.rpc.AsyncCallback;
43  import com.google.gwt.user.client.ui.Button;
44  import com.google.gwt.user.client.ui.DialogBox;
45  import com.google.gwt.user.client.ui.FileUpload;
46  import com.google.gwt.user.client.ui.FormPanel;
47  import com.google.gwt.user.client.ui.HorizontalPanel;
48  import com.google.gwt.user.client.ui.TextBox;
49  import com.google.gwt.user.client.ui.VerticalPanel;
50  import com.google.gwt.user.client.ui.Widget;
51  import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
52  import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
53  
54  /**
55   * Track for loading view files in Silk format.
56   * 
57   * @author leo
58   * 
59   */
60  public class ViewLoaderTrack extends TrackBase {
61  	public static TrackFactory factory() {
62  		return new TrackFactory() {
63  			public Track newInstance() {
64  				return new ViewLoaderTrack();
65  			}
66  		};
67  	}
68  
69  	VerticalPanel panel = new VerticalPanel();
70  	TextBox urlBox = new TextBox();
71  
72  	public ViewLoaderTrack() {
73  		super("View Loader");
74  		// load view via HTTP
75  		HorizontalPanel hp = new HorizontalPanel();
76  		hp.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
77  		hp.add(new FormLabel("View Silk URL: "));
78  		urlBox.setWidth("400px");
79  		urlBox.addKeyPressHandler(new KeyPressHandler() {
80  			public void onKeyPress(KeyPressEvent e) {
81  				if (e.getCharCode() == KeyCodes.KEY_ENTER) {
82  					downloadView(urlBox.getText());
83  				}
84  			}
85  		});
86  		Button loadButton = new Button("load");
87  		loadButton.addClickHandler(new ClickHandler() {
88  			public void onClick(ClickEvent e) {
89  				downloadView(urlBox.getText());
90  			}
91  		});
92  		hp.add(urlBox);
93  		hp.add(loadButton);
94  		// load view from a file
95  		final FormPanel fileUploadForm = new FormPanel();
96  		fileUploadForm.setAction(GWT.getModuleBaseURL() + "utgb-core/loadview");
97  		fileUploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
98  		fileUploadForm.setMethod(FormPanel.METHOD_POST);
99  		HorizontalPanel formButtonPanel = new HorizontalPanel();
100 		FileUpload fileBox = new FileUpload();
101 		fileBox.setName("file");
102 		fileBox.setWidth("300px");
103 		Button uploadButton = new Button("submit");
104 		uploadButton.addClickHandler(new ClickHandler() {
105 
106 			public void onClick(ClickEvent e) {
107 				fileUploadForm.submit();
108 			}
109 		});
110 		formButtonPanel.add(new FormLabel("View Silk File:"));
111 		formButtonPanel.add(fileBox);
112 		formButtonPanel.add(uploadButton);
113 		fileUploadForm.add(formButtonPanel);
114 		DOM.setStyleAttribute(fileUploadForm.getElement(), "margin", "0");
115 		fileUploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
116 			public void onSubmitComplete(SubmitCompleteEvent e) {
117 
118 				getFrame().setNowLoading();
119 				String viewXML = extractEmbeddedSilkInComment(e.getResults());
120 				setViewSilk(viewXML);
121 			}
122 		});
123 		// set panes
124 		panel.setStyleName("toolbox");
125 		panel.add(hp);
126 		panel.add(fileUploadForm);
127 	}
128 
129 	private static String extractEmbeddedSilkInComment(String html) {
130 		html = html.replaceFirst("<!--", "");
131 		html = html.replaceFirst("-->", "");
132 		return html;
133 	}
134 
135 	private void downloadView(final String url) {
136 		getFrame().setNowLoading();
137 		getBrowserService().getHTTPContent(url, new AsyncCallback<String>() {
138 			public void onFailure(Throwable caught) {
139 				GWT.log("failed to load " + url, caught);
140 				getFrame().loadingDone();
141 			}
142 
143 			public void onSuccess(String viewXML) {
144 
145 				setViewSilk(viewXML);
146 				getFrame().loadingDone();
147 			}
148 		});
149 	}
150 
151 	private void setViewSilk(String viewSilk) {
152 		if (viewSilk == null)
153 			return;
154 
155 		UTGBEntryPointBase.showLoadingMessage();
156 		getBrowserService().createTrackView(viewSilk, new AsyncCallback<TrackView>() {
157 			public void onFailure(Throwable e) {
158 				GWT.log(e.getMessage(), e);
159 				DialogBox dialog = new DialogBox();
160 				dialog.setText(e.getMessage());
161 				dialog.show();
162 				UTGBEntryPointBase.hideLoadingMessage();
163 			}
164 
165 			public void onSuccess(TrackView v) {
166 				TrackGroup newGroup;
167 				try {
168 					newGroup = TrackGroup.createTrackGroup(v);
169 					TrackGroup rootTrackGroup = getTrackGroup().getRootTrackGroup();
170 					rootTrackGroup.clear();
171 					rootTrackGroup.addTrackGroup(newGroup);
172 				}
173 				catch (UTGBClientException e) {
174 					GWT.log(e.getMessage(), e);
175 					DialogBox dialog = new DialogBox();
176 					dialog.setText(e.getMessage());
177 					dialog.show();
178 				}
179 
180 				UTGBEntryPointBase.hideLoadingMessage();
181 			}
182 		});
183 
184 	}
185 
186 	public Widget getWidget() {
187 		return panel;
188 	}
189 }