View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 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-core Project
18  //
19  // MiniBrowserTrack.java
20  // Since: May 26, 2009
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import org.utgenome.gwt.utgb.client.db.datatype.StringType;
28  import org.utgenome.gwt.utgb.client.track.Design;
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.TrackConfig;
32  import org.utgenome.gwt.utgb.client.track.TrackConfigChange;
33  import org.utgenome.gwt.utgb.client.track.TrackFrame;
34  import org.utgenome.gwt.utgb.client.track.TrackGroup;
35  import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
36  import org.utgenome.gwt.widget.client.Style;
37  
38  import com.google.gwt.event.dom.client.ErrorEvent;
39  import com.google.gwt.event.dom.client.ErrorHandler;
40  import com.google.gwt.event.dom.client.LoadEvent;
41  import com.google.gwt.event.dom.client.LoadHandler;
42  import com.google.gwt.user.client.DOM;
43  import com.google.gwt.user.client.ui.FlexTable;
44  import com.google.gwt.user.client.ui.Frame;
45  import com.google.gwt.user.client.ui.Image;
46  import com.google.gwt.user.client.ui.Widget;
47  
48  /**
49   * Track for embedding HTML page via iframe tag.
50   * 
51   * Usage:
52   * 
53   * <pre>
54   * -track 
55   *  -name: Browser
56   *  -class: MiniBrowser
57   *  -property
58   *    -URL: http://www.google.com
59   *    -type: {frame or image}
60   * </pre>
61   * 
62   * @author leo
63   * 
64   */
65  public class MiniBrowserTrack extends TrackBase {
66  	private String type = "frame";
67  	private String trackBaseURL;
68  	private FlexTable layoutPanel = new FlexTable();
69  	private Image trackImage = new Image();
70  	private Frame frame = new Frame();
71  	private boolean isWidgetReady = false;
72  
73  	protected TrackConfig config = new TrackConfig(this);
74  
75  	public static TrackFactory factory() {
76  		return new TrackFactory() {
77  			public Track newInstance() {
78  				return new MiniBrowserTrack();
79  			}
80  		};
81  	}
82  
83  	public MiniBrowserTrack() {
84  		super("Mini Browser");
85  
86  		Style.fullWidth(layoutPanel);
87  		layoutPanel.setCellPadding(0);
88  		layoutPanel.setCellSpacing(0);
89  		Style.fontSize(layoutPanel, 0);
90  
91  		Style.fullWidth(frame);
92  		Style.margin(frame, 0);
93  		Style.padding(frame, 0);
94  		DOM.setElementProperty(frame.getElement(), "align", "left");
95  		DOM.setElementPropertyInt(frame.getElement(), "marginHight", 0);
96  		DOM.setElementPropertyInt(frame.getElement(), "marginWidth", 0);
97  		DOM.setElementPropertyInt(frame.getElement(), "frameBorder", 0);
98  
99  		Style.hideHorizontalScrollBar(layoutPanel);
100 		Style.hideHorizontalScrollBar(frame);
101 	}
102 
103 	public Widget getWidget() {
104 		return layoutPanel;
105 	}
106 
107 	@Override
108 	public void draw() {
109 
110 		//layoutPanel.getCellFormatter().setWidth(0, 0, (leftMargin > 0 ? leftMargin + "px" : "1px"));
111 
112 		String trackURL = getTrackURL();
113 		if (type.equals("frame")) {
114 			if (!isWidgetReady) {
115 				layoutPanel.setWidget(0, 0, frame);
116 				isWidgetReady = true;
117 			}
118 			frame.setUrl(trackURL);
119 		}
120 		else if (type.equals("image")) {
121 			// use image 
122 			if (!isWidgetReady) {
123 				layoutPanel.setWidget(0, 0, trackImage);
124 				isWidgetReady = true;
125 			}
126 			trackImage.setUrl(getTrackURL());
127 			getFrame().setNowLoading();
128 		}
129 	}
130 
131 	protected String getTrackURL() {
132 		return trackBaseURL;
133 	}
134 
135 	@Override
136 	public TrackConfig getConfig() {
137 		return config;
138 	}
139 
140 	@Override
141 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
142 
143 		trackImage.addLoadHandler(new LoadHandler() {
144 			public void onLoad(LoadEvent e) {
145 				getFrame().onUpdateTrackWidget();
146 				getFrame().loadingDone();
147 			}
148 		});
149 		trackImage.addErrorHandler(new ErrorHandler() {
150 			public void onError(ErrorEvent e) {
151 				trackImage.setUrl(Design.IMAGE_NOT_AVAILABLE);
152 				getFrame().loadingDone();
153 			}
154 		});
155 
156 		int height = this.getDefaultWindowHeight();
157 		if (height > 0) {
158 			frame.setHeight(height + "px");
159 			layoutPanel.setHeight(height + "px");
160 		}
161 
162 		// set up the configuration panel
163 
164 		config.addConfig("URL", new StringType("URL"), trackBaseURL);
165 	}
166 
167 	@Override
168 	public void onChangeTrackConfig(TrackConfigChange change) {
169 		if (change.contains("URL")) {
170 			trackBaseURL = change.getValue("URL");
171 			draw();
172 		}
173 	}
174 
175 	@Override
176 	public void saveProperties(CanonicalProperties saveData) {
177 		saveData.add("type", type);
178 		saveData.add("URL", trackBaseURL);
179 	}
180 
181 	@Override
182 	public void restoreProperties(CanonicalProperties properties) {
183 		trackBaseURL = properties.get("URL", trackBaseURL);
184 		type = properties.get("type", type);
185 	}
186 }