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  // utgb-core Project
18  //
19  // WindowFrame.java
20  // Since: 2007/11/27
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.ui;
26  
27  import org.utgenome.gwt.widget.client.Style;
28  
29  import com.google.gwt.user.client.ui.Composite;
30  import com.google.gwt.user.client.ui.FlexTable;
31  import com.google.gwt.user.client.ui.Label;
32  import com.google.gwt.user.client.ui.SimplePanel;
33  import com.google.gwt.user.client.ui.Widget;
34  import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
35  
36  /**
37   * A frame with round corners. You can set arbitrary widgets to this frame by using {@link #setWidget(Widget)}.
38   * 
39   * @author leo
40   * 
41   */
42  public class RoundCornerFrame extends Composite {
43  	private final FlexTable layoutFrame = new FlexTable();
44  	private Border upperFrame;
45  	private VerticalBar westBar;
46  	private final FlexTable contentFrame = new FlexTable();
47  	private SimplePanel panel = new SimplePanel();
48  	private VerticalBar eastBar;
49  	private Border lowerFrame;
50  
51  	public static final int NORTH = 1;
52  	public static final int EAST = 1 << 1;
53  	public static final int WEST = 1 << 2;
54  	public static final int SOUTH = 1 << 3;
55  	public static final int BORDER_FULL = NORTH | EAST | WEST | SOUTH;
56  
57  	private float alpha = 1f;
58  	private int borderFlag;
59  	private int cornerRadius;
60  	private String borderColor;
61  
62  	public RoundCornerFrame(String color) {
63  		this(color, 0.9f, 4);
64  	}
65  
66  	public RoundCornerFrame(String color, float alpha, int borderWidth) {
67  		this(color, alpha, borderWidth, BORDER_FULL);
68  	}
69  
70  	public RoundCornerFrame(String color, float alpha, int borderWidth, int borderFlag) {
71  		assert (borderFlag <= BORDER_FULL);
72  
73  		this.borderFlag = borderFlag;
74  		this.cornerRadius = borderWidth;
75  		this.borderColor = color;
76  		this.alpha = alpha;
77  
78  		drawFrame();
79  
80  		initWidget(layoutFrame);
81  	}
82  
83  	@Override
84  	public void setWidget(Widget w) {
85  		panel.setWidget(w);
86  	}
87  
88  	public void setBorderWidth(int borderWidth) {
89  		this.cornerRadius = borderWidth;
90  		drawFrame();
91  	}
92  
93  	private void drawFrame() {
94  		layoutFrame.clear();
95  		contentFrame.clear();
96  
97  		layoutFrame.setPixelSize(1, 1);
98  
99  		upperFrame = new Border(cornerRadius, Border.UPPER, borderFlag, borderColor, alpha);
100 		lowerFrame = new Border(cornerRadius, Border.LOWER, borderFlag, borderColor, alpha);
101 		westBar = new VerticalBar(cornerRadius, borderColor, alpha);
102 		eastBar = new VerticalBar(cornerRadius, borderColor, alpha);
103 
104 		layoutFrame.setCellPadding(0);
105 		layoutFrame.setCellSpacing(0);
106 
107 		contentFrame.setCellPadding(0);
108 		contentFrame.setCellSpacing(0);
109 		contentFrame.setSize("100%", "100%");
110 
111 		if ((borderFlag & NORTH) != 0) {
112 			layoutFrame.setWidget(0, 0, upperFrame);
113 		}
114 
115 		layoutFrame.setWidget(1, 0, contentFrame);
116 		if ((borderFlag & WEST) != 0) {
117 			contentFrame.setWidget(0, 0, westBar);
118 		}
119 
120 		contentFrame.setWidget(0, 1, panel);
121 		Style.semiTransparentBackground(panel, borderColor, alpha);
122 
123 		contentFrame.getCellFormatter().setWidth(0, 1, "100%");
124 		if ((borderFlag & EAST) != 0) {
125 			contentFrame.setWidget(0, 2, eastBar);
126 		}
127 
128 		if ((borderFlag & SOUTH) != 0) {
129 			layoutFrame.setWidget(2, 0, lowerFrame);
130 		}
131 
132 		CellFormatter cf = layoutFrame.getCellFormatter();
133 		cf.setHeight(0, 0, cornerRadius + "px");
134 		cf.setHeight(1, 0, "100%");
135 		cf.setWidth(1, 0, "100%");
136 		cf.setHeight(2, 0, cornerRadius + "px");
137 
138 	}
139 
140 	public void setFrameColor(String color, float alpha) {
141 		this.borderColor = color;
142 		this.alpha = alpha;
143 		drawFrame();
144 	}
145 
146 	static class VerticalBar extends Label {
147 		public VerticalBar(int radius, String color, float alpha) {
148 			this.setSize(radius + "px", "100%");
149 			Style.fontSize(this, 0);
150 			Style.semiTransparentBackground(this, color, alpha);
151 		}
152 	}
153 
154 }