View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 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-widget Project
18  //
19  // TrackPanelBase.java
20  // Since: May 1, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.widget.client.impl;
26  
27  import org.utgenome.gwt.widget.client.TrackButtonListener;
28  import org.utgenome.gwt.widget.client.TrackFrame;
29  import org.utgenome.gwt.widget.client.UTGBDesignFactory;
30  
31  import com.google.gwt.user.client.DOM;
32  import com.google.gwt.user.client.Event;
33  import com.google.gwt.user.client.ui.Composite;
34  import com.google.gwt.user.client.ui.Image;
35  import com.google.gwt.user.client.ui.Widget;
36  
37  /**
38   * base implementation of the track panel
39   * 
40   * @author leo
41   * 
42   */
43  public abstract class TrackPanelBase extends TrackFrame {
44  
45  	protected IconSetPanel iconSet = new IconSetPanel(this);
46  	private TrackResizeButton resizeButton;
47  
48  	private boolean enableResizeX = true;
49  	private boolean enableResizeY = true;
50  
51  	public TrackPanelBase() {
52  		UTGBDesignFactory designFactory = new UTGBDesignFactory();
53  		Image resizeBar = new Image(designFactory.getUTGBImageBundle().windowResizeIcon());
54  		resizeBar.setTitle("drag this to resize");
55  
56  		resizeButton = new TrackResizeButton(this, resizeBar);
57  	}
58  
59  	public void enableResizeWidth(boolean enable) {
60  		this.enableResizeX = enable;
61  	}
62  
63  	public void enableResizeHeight(boolean enable) {
64  		this.enableResizeY = enable;
65  	}
66  
67  	class TrackResizeButton extends Composite {
68  		private final TrackFrame resizeTarget;
69  
70  		private Image resizeBar;
71  
72  		private int initialY_Position = 0;
73  		private int initialX_Position = 0;
74  		private int initial_height = 0;
75  		private int initial_width = 0;
76  
77  		private boolean isFocusing = false;
78  		private boolean isCapturing = false;
79  
80  		public TrackResizeButton(TrackFrame resizeTarget, Image resizeBar) {
81  			this.resizeTarget = resizeTarget;
82  			this.resizeBar = resizeBar;
83  
84  			initWidget(resizeBar);
85  		}
86  
87  		@Override
88  		public void onBrowserEvent(Event event) {
89  
90  			int type = DOM.eventGetType(event);
91  			switch (type) {
92  			case Event.ONMOUSEDOWN:
93  				isFocusing = true;
94  				// onClickStart();
95  				DOM.setCapture(getElement());
96  				isCapturing = true;
97  				// Prevent dragging (on some browsers);
98  				DOM.eventPreventDefault(event);
99  
100 				initialX_Position = event.getClientX();
101 				initialY_Position = event.getClientY();
102 				initial_width = resizeTarget.getOffsetWidth();
103 				initial_height = resizeTarget.getOffsetHeight();
104 				break;
105 			case Event.ONMOUSEUP:
106 				if (isCapturing) {
107 					isCapturing = false;
108 					DOM.releaseCapture(getElement());
109 				}
110 				break;
111 			case Event.ONMOUSEMOVE:
112 				if (isCapturing) {
113 					// Prevent dragging (on other browsers);
114 					DOM.eventPreventDefault(event);
115 
116 					final int diffX = event.getClientX() - initialX_Position;
117 					final int diffY = event.getClientY() - initialY_Position;
118 
119 					final int newWidth = initial_width + diffX;
120 					final int newHeight = initial_height + diffY;
121 					if (enableResizeX && newWidth >= 0)
122 						resizeTarget.setWidth(newWidth);
123 					if (enableResizeY && newHeight >= 0)
124 						resizeTarget.setHeight(newHeight);
125 				}
126 				break;
127 			case Event.ONMOUSEOUT:
128 				break;
129 			case Event.ONMOUSEOVER:
130 				break;
131 			case Event.ONCLICK:
132 				// we handle clicks ourselves
133 				return;
134 			case Event.ONBLUR:
135 				if (isFocusing) {
136 					isFocusing = false;
137 				}
138 				break;
139 			case Event.ONLOSECAPTURE:
140 				if (isCapturing) {
141 					isCapturing = false;
142 				}
143 				break;
144 			}
145 
146 			super.onBrowserEvent(event);
147 
148 		}
149 	}
150 
151 	public IconSetPanel getIconSetPanel() {
152 		return iconSet;
153 	}
154 
155 	public Widget getResizeButton() {
156 		return resizeButton;
157 	}
158 
159 	public void addTrackButtonListener(TrackButtonListener listener) {
160 		iconSet.addTrackButtonListener(listener);
161 	}
162 
163 	public void setLoading(boolean loading) {
164 		iconSet.setLoading(loading);
165 	}
166 
167 	public void setVisible(int buttonSet, boolean visible) {
168 		iconSet.setVisible(buttonSet, visible);
169 	}
170 
171 	public void showAdjustHightButton(boolean show) {
172 		iconSet.setVisible(TrackFrame.BUTTON_ADJUSTHEIGHT, show);
173 	}
174 
175 	public void showCloseButton(boolean show) {
176 		iconSet.setVisible(TrackFrame.BUTTON_CLOSE, show);
177 	}
178 
179 	public void showConfigButton(boolean show) {
180 		iconSet.setVisible(TrackFrame.BUTTON_CONFIG, show);
181 	}
182 
183 	public void showMinimizeButton(boolean show) {
184 		iconSet.setVisible(TrackFrame.BUTTON_MINIMIZE, show);
185 	}
186 
187 	public void showReloadButton(boolean show) {
188 		iconSet.setVisible(TrackFrame.BUTTON_RELOAD, show);
189 	}
190 
191 }