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  // CSS.java
20  // Since: Jun 26, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.widget.client;
26  
27  import org.utgenome.gwt.utgb.client.util.BrowserInfo;
28  
29  import com.google.gwt.user.client.DOM;
30  import com.google.gwt.user.client.ui.Widget;
31  
32  /**
33   * {@link Style} is a helper class to edit CSS (Cascading Style Sheet) design of widgets by setting their DOM
34   * properties.
35   * 
36   * @author leo
37   * 
38   */
39  public class Style {
40  	public static final String CSS_BORDER = "border";
41  	public static final String CSS_PADDING = "padding";
42  	public static final String CSS_MARGIN = "margin";
43  	public static final String CSS_FONT_COLOR = "color";
44  	public static final String CSS_FONT_WEIGHT = "fontWeight";
45  	public static final String CSS_FONT_SIZE = "fontSize";
46  	public static final String CSS_FONT_FAMILY = "fontFamily";
47  	public static final String CSS_WHITESPACE = "whiteSpace";
48  	public static final String CSS_NOWRAP = "nowrap";
49  	public static final String CSS_BACKGROUND_IMAGE = "backgroundImage";
50  	public static final String CSS_BACKGROUND_REPEAT = "backgroundRepeat";
51  	public static final String CSS_BACKGROUND_POSITION = "backgroundPosition";
52  	public static final String CSS_BACKGROUND_COLOR = "backgroundColor";
53  	public static final String CSS_OVERFLOW_X = "overflowX";
54  	public static final String CSS_OVERFLOW_Y = "overflowY";
55  	public static final String OVERFLOW_AUTO = "auto";
56  	public static final String OVERFLOW_HIDDEN = "hidden";
57  	public static final String CSS_Z_INDEX = "zIndex";
58  
59  	public static void visibilityHidden(Widget w) {
60  		set(w, "visibility", "hidden");
61  	}
62  
63  	/**
64  	 * set the overlap position of the widget. Widgets with the higher z-index will be front on the display
65  	 * 
66  	 * @param w
67  	 *            the widget
68  	 * @param zIndex
69  	 */
70  	public static void zIndex(Widget w, int zIndex) {
71  		set(w, CSS_Z_INDEX, Integer.toString(zIndex));
72  	}
73  
74  	public static void bold(Widget w) {
75  		set(w, CSS_FONT_WEIGHT, "bold");
76  	}
77  
78  	public static void normal(Widget w) {
79  		set(w, CSS_FONT_WEIGHT, "normal");
80  	}
81  
82  	public static void italic(Widget w) {
83  		set(w, CSS_FONT_WEIGHT, "italic");
84  	}
85  
86  	public static void fontSize(Widget w, int pixelFontSize) {
87  		set(w, CSS_FONT_SIZE, pixelFontSize + "px");
88  	}
89  
90  	public static void fontFamily(Widget w, String[] fontFamily) {
91  		set(w, CSS_FONT_FAMILY, StringUtil.join(fontFamily, ", "));
92  	}
93  
94  	public static void fontFamily(Widget w, String fontFamily) {
95  		set(w, CSS_FONT_FAMILY, fontFamily);
96  	}
97  
98  	public static void nowrap(Widget w) {
99  		set(w, CSS_WHITESPACE, "nowrap");
100 	}
101 
102 	public static void preserveWhiteSpace(Widget w) {
103 		set(w, CSS_WHITESPACE, "pre");
104 	}
105 
106 	public static void overflowHidden(Widget w) {
107 		set(w, "overflow", OVERFLOW_HIDDEN);
108 	}
109 
110 	public static void disableScroll(Widget w) {
111 		hideHorizontalScrollBar(w);
112 		hideVerticalScrollBar(w);
113 	}
114 
115 	public static void hideHorizontalScrollBar(Widget w) {
116 		set(w, CSS_OVERFLOW_X, OVERFLOW_HIDDEN);
117 	}
118 
119 	public static void hideVerticalScrollBar(Widget w) {
120 		set(w, CSS_OVERFLOW_Y, OVERFLOW_HIDDEN);
121 	}
122 
123 	/**
124 	 * @param w
125 	 * @param imageURL
126 	 *            background image url relative to the GWT module base URL
127 	 */
128 	public static void backgroundImage(Widget w, String imageURL) {
129 		// String url = GWT.getModuleBaseURL() + imageURL;
130 		set(w, CSS_BACKGROUND_IMAGE, "url(" + imageURL + ")");
131 	}
132 
133 	public static void backgroundNoRepeat(Widget w) {
134 		set(w, CSS_BACKGROUND_REPEAT, "no-repeat");
135 	}
136 
137 	public static void backgroundPosition(Widget w, String value) {
138 		set(w, CSS_BACKGROUND_POSITION, value);
139 	}
140 
141 	public static void backgroundRepeat(Widget w) {
142 		set(w, CSS_BACKGROUND_REPEAT, "repeat");
143 	}
144 
145 	public static void backgroundRepeatY(Widget w) {
146 		set(w, CSS_BACKGROUND_REPEAT, "repeat-y");
147 	}
148 
149 	public static void backgroundRepeatX(Widget w) {
150 		set(w, CSS_BACKGROUND_REPEAT, "repeat-x");
151 	}
152 
153 	public static void backgroundColor(Widget w, String color) {
154 		set(w, CSS_BACKGROUND_COLOR, color);
155 	}
156 
157 	public static void fullWidth(Widget w) {
158 		w.setWidth("100%");
159 	}
160 
161 	public static void fullHeight(Widget w) {
162 		w.setHeight("100%");
163 	}
164 
165 	public static void fullSize(Widget w) {
166 		w.setSize("100%", "100%");
167 	}
168 
169 	public static final String BORDER_SOLID = "solid";
170 	public static final String BORDER_DASHED = "dashed";
171 	public static final String BORDER_INSET = "inset";
172 	public static final String BORDER_OUTSET = "outset";
173 	public static final String COLOR_WHITE = "white";
174 	public static final String COLOR_RED = "red";
175 	public static final String COLOR_BLACK = "black";
176 	public static final String COLOR_GRAY = "gray";
177 	public static final String COLOR_SKYBLUE = "#99CCFF";
178 
179 	/**
180 	 * @param w
181 	 * @param borderWidth
182 	 *            pixel width
183 	 * @param borderType
184 	 * @param color
185 	 */
186 	public static void border(Widget w, int borderWidth, String borderType, String color) {
187 		String borderStyle = StringUtil.joinWithWS(new String[] { borderWidth + "px", borderType, color });
188 		set(w, CSS_BORDER, borderStyle);
189 	}
190 
191 	public static int TOP = 1;
192 	public static int BOTTOM = 1 << 1;
193 	public static int LEFT = 1 << 2;
194 	public static int RIGHT = 1 << 3;
195 	private static int[] _direction = { TOP, BOTTOM, LEFT, RIGHT };
196 	private static String[] _directionStr = { "Top", "Bottom", "Left", "Right" };
197 
198 	/**
199 	 * @param w
200 	 * @param NEWSflag
201 	 * @param borderWidth
202 	 *            pixel width
203 	 * @param borderType
204 	 * @param color
205 	 */
206 	public static void border(Widget w, int NEWSflag, int borderWidth, String borderType, String color) {
207 		String borderStyle = StringUtil.joinWithWS(new String[] { borderWidth + "px", borderType, color });
208 		for (int i = 0; i < _direction.length; i++) {
209 			if ((NEWSflag & _direction[i]) != 0)
210 				set(w, CSS_BORDER + _directionStr[i], borderStyle);
211 		}
212 	}
213 
214 	public static final String CSS_BORDER_COLLAPSE = "borderCollapse";
215 
216 	public static void borderCollapse(Widget w) {
217 		set(w, CSS_BORDER_COLLAPSE, "collapse");
218 	}
219 
220 	/**
221 	 * @param w
222 	 * @param paddingSize
223 	 */
224 	public static void padding(Widget w, int paddingSize) {
225 		set(w, CSS_PADDING, paddingSize + "px");
226 	}
227 
228 	public static void padding(Widget w, int NEWSflag, int paddingSize) {
229 		for (int i = 0; i < _direction.length; i++) {
230 			if ((NEWSflag & _direction[i]) != 0)
231 				set(w, CSS_PADDING + _directionStr[i], paddingSize + "px");
232 		}
233 	}
234 
235 	public static void margin(Widget w, int marginSize) {
236 		set(w, CSS_MARGIN, marginSize + "px");
237 	}
238 
239 	public static void margin(Widget w, int NEWSflag, int marginSize) {
240 		for (int i = 0; i < _direction.length; i++) {
241 			if ((NEWSflag & _direction[i]) != 0)
242 				set(w, CSS_MARGIN + _directionStr[i], marginSize + "px");
243 		}
244 	}
245 
246 	public static final String CSS_CURSOR = "cursor";
247 	public static final String CURSOR_MOVE = "move";
248 	public static final String CURSOR_AUTO = "auto";
249 	public static final String CURSOR_POINTER = "pointer";
250 	public static final String CURSOR_CROSSHAIR = "crosshair";
251 	public static final String CURSOR_RESIZE_E = "e-resize";
252 	public static final String CURSOR_RESIZE_N = "n-resize";
253 	public static final String CURSOR_RESIZE_SE = "se-resize";
254 	public static final String CURSOR_TEXT = "text";
255 	public static final String CURSOR_HELP = "help";
256 	public static final String CSS_DISPLAY = "display";
257 	public static final String DISPLAY_BLOCK = "block";
258 
259 	public static void cursor(Widget w, String cursorType) {
260 		set(w, CSS_CURSOR, cursorType);
261 	}
262 
263 	public static void fullBlock(Widget w) {
264 		set(w, CSS_DISPLAY, DISPLAY_BLOCK);
265 		fullWidth(w);
266 		fullHeight(w);
267 	}
268 
269 	public static final String CSS_VERTICAL_ALIGN = "verticalAlign";
270 
271 	public static void verticalAlign(Widget w, String value) {
272 		set(w, CSS_VERTICAL_ALIGN, value);
273 	}
274 
275 	/**
276 	 * Set the position of the widget from the right border
277 	 * 
278 	 * @param w
279 	 * @param value
280 	 */
281 	public static void right(Widget w, String value) {
282 		set(w, "right", value);
283 	}
284 
285 	/**
286 	 * Set the text alignment of the widget
287 	 * 
288 	 * @param w
289 	 * @param value
290 	 */
291 	public static void textAlign(Widget w, String value) {
292 		set(w, "textAlign", value);
293 	}
294 
295 	public static void trimOverflowedText(Widget w) {
296 		set(w, "textOverflow", "ellipsis");
297 		set(w, "overflow", "hidden");
298 		set(w, "whiteSpace", "nowrap");
299 	}
300 
301 	/**
302 	 * Make the background of the widget semi-transparent color
303 	 * 
304 	 * @param colorCode
305 	 *            Hex RGB color code
306 	 */
307 	public static void semiTransparentBackground(Widget w, String colorCode, float alpha) {
308 		backgroundImage(w, "utgb-core/transparent?color=" + colorCode + "&opacity=" + alpha);
309 	}
310 
311 	/**
312 	 * Set CSS style
313 	 * 
314 	 * @param w
315 	 * @param cssPropertyName
316 	 * @param value
317 	 */
318 	public static void set(Widget w, String cssPropertyName, String value) {
319 		DOM.setStyleAttribute(w.getElement(), cssPropertyName, value);
320 	}
321 
322 	/**
323 	 * Non-constractable
324 	 */
325 	private Style() {
326 	}
327 
328 	public static void position(Widget w, String value) {
329 		set(w, "position", value);
330 	}
331 
332 	public static void overflowAuto(Widget w) {
333 		set(w, CSS_OVERFLOW_X, "auto");
334 		set(w, CSS_OVERFLOW_Y, "auto");
335 	}
336 
337 	public static void fontColor(Widget w, String color) {
338 		set(w, CSS_FONT_COLOR, color);
339 	}
340 
341 	public static void transform(Widget w, String transformCommand) {
342 		setExt(w, "Transform", transformCommand);
343 	}
344 
345 	public static void scale(Widget w, double scale) {
346 		transform(w, "scale(" + scale + ")");
347 	}
348 
349 	public static void scale(Widget w, double scaleX, double scaleY) {
350 		transform(w, "scale(" + scaleX + "," + scaleY + ")");
351 	}
352 
353 	public static void scaleX(Widget w, double scaleX) {
354 		transform(w, "scaleX(" + scaleX + ")");
355 	}
356 
357 	public static void scaleXwithAnimation(Widget w, double scaleX, double durationInSec) {
358 		// animation
359 		setExt(w, "TransitionProperty", getCSS3prefix() + "transform");
360 		setExt(w, "TransitionDuration", durationInSec + "s");
361 		setExt(w, "TransitionTimingFunction", "ease-out");
362 		transform(w, "scaleX(" + scaleX + ")");
363 	}
364 
365 	public static void scaleXwithAnimation(Widget w, double scaleX, int newPixelX, double durationInSec) {
366 		// animation
367 		setExt(w, "TransitionProperty", getCSS3prefix() + "transform, left");
368 		setExt(w, "TransitionDuration", durationInSec + "s");
369 		setExt(w, "TransitionTimingFunction", "ease-out");
370 		set(w, "left", newPixelX + "px");
371 		transform(w, "scaleX(" + scaleX + ")");
372 		setExt(w, "TransformOrigin", "0");
373 	}
374 
375 	public static void scaleY(Widget w, double scaleY) {
376 		transform(w, "scaleY(" + scaleY + ")");
377 	}
378 
379 	public static void translate(Widget w, String translateCommand) {
380 		setExt(w, "Translate", translateCommand);
381 	}
382 
383 	public static void translateX(Widget w, int x) {
384 		translate(w, "translateX(" + x + ")");
385 	}
386 
387 	public static void translateY(Widget w, int y) {
388 		translate(w, "translateY(" + y + ")");
389 	}
390 
391 	private static String css3_prefix;
392 	private static String css3_dom_prefix;
393 
394 	public static String getCSS3prefix() {
395 
396 		if (css3_prefix != null)
397 			return css3_prefix;
398 
399 		switch (BrowserInfo.getBrowserType()) {
400 		case Chrome:
401 		case Safari:
402 		case MobileSafari:
403 			css3_prefix = "-webkit-";
404 			break;
405 		case Firefox:
406 			css3_prefix = "-moz-";
407 			break;
408 		case Opera:
409 			css3_prefix = "-o-";
410 			break;
411 		default:
412 			css3_prefix = "";
413 			break;
414 		}
415 
416 		return css3_prefix;
417 	}
418 
419 	public static String getCSS3DOMprefix() {
420 
421 		if (css3_dom_prefix != null)
422 			return css3_dom_prefix;
423 
424 		switch (BrowserInfo.getBrowserType()) {
425 		case Chrome:
426 		case Safari:
427 		case MobileSafari:
428 			css3_dom_prefix = "Webkit";
429 			break;
430 		case Firefox:
431 			css3_dom_prefix = "Moz";
432 			break;
433 		case Opera:
434 			css3_dom_prefix = "O";
435 			break;
436 		default:
437 			css3_dom_prefix = "";
438 			break;
439 		}
440 
441 		return css3_dom_prefix;
442 	}
443 
444 	/**
445 	 * Set a CSS3 property with a browser-specific prefix.
446 	 * 
447 	 * @param w
448 	 * @param property
449 	 * @param value
450 	 */
451 	public static void setExt(Widget w, String property, String value) {
452 		set(w, getCSS3DOMprefix() + property, value);
453 	}
454 
455 	public static void scrollX(Widget w, int destX, double sec) {
456 		// animation
457 		setExt(w, "TransitionProperty", "left");
458 		setExt(w, "TransitionDuration", sec + "s");
459 		setExt(w, "TransitionTimingFunction", "ease-out");
460 		set(w, "left", destX + "px");
461 	}
462 
463 	public static void scrollY(Widget w, int destY, double sec) {
464 		// animation
465 		setExt(w, "TransitionProperty", "top");
466 		setExt(w, "TransitionDuration", sec + "s");
467 		setExt(w, "TransitionTimingFunction", "ease-out");
468 		set(w, "top", destY + "px");
469 	}
470 
471 	/**
472 	 * Set the opacity of the Widget
473 	 * 
474 	 * @param w
475 	 * @param alpha
476 	 */
477 	public static void opacity(Widget w, double alpha) {
478 		switch (BrowserInfo.getBrowserType()) {
479 		case IE:
480 			set(w, "filter", "alpha(opacity=" + alpha + ")");
481 			break;
482 		default:
483 			set(w, "opacity", Double.toString(alpha));
484 			break;
485 		}
486 	}
487 }