1
4 import org.aswing.Component;
5 import org.aswing.Container;
6 import org.aswing.UIManager;
7 import org.aswing.geom.Dimension;
8 import org.aswing.geom.Point;
9 import org.aswing.geom.Rectangle;
10 import org.aswing.LayoutManager;
11 import org.aswing.Viewportable;
12 import org.aswing.ViewportLayout;
13 import org.aswing.plaf.ViewportUI;
14
15
16
20 class org.aswing.JViewport extends Container implements Viewportable {
21
22
28 public static var ON_STATE_CHANGED:String = "onStateChanged";
29
30 private var verticalUnitIncrement:Number;
31 private var verticalBlockIncrement:Number;
32 private var horizontalUnitIncrement:Number;
33 private var horizontalBlockIncrement:Number;
34
35 private var view:Component;
36
37
42 public function JViewport(view:Component){
43 super();
44 setName("JViewport");
45 if(view != undefined) setView(view);
46 setLayout(new ViewportLayout());
47 updateUI();
48 }
49
50
51
52 public function updateUI():Void{
53 setUI(ViewportUI(UIManager.getUI(this)));
54 }
55
56
57 public function setUI(newUI:ViewportUI):Void{
58 super.setUI(newUI);
59 }
60
61
62
63
64 public function getUIClassID():String{
65 return "ViewportUI";
66 }
67
68
71 public function setLayout(layout:LayoutManager):Void{
72 if(layout instanceof ViewportLayout){
73 super.setLayout(layout);
74 }else{
75 trace(this + " Only on set ViewportLayout to JViewport");
76 throw new Error(this + " Only on set ViewportLayout to JViewport");
77 }
78 }
79
80
81
94 public function setView(view:Component):Void{
95 if(this.view != view){
96 this.view = view;
97 removeAll();
98
99 if(view != null){
100 super.insert(-1, view);
101 }
102 fireStateChanged();
103 }
104 }
105
106 public function getView():Component{
107 return view;
108 }
109
110
113 public function setVerticalUnitIncrement(increment:Number):Void{
114 if(verticalUnitIncrement != increment){
115 verticalUnitIncrement = increment;
116 fireStateChanged();
117 }
118 }
119
120
123 public function setVerticalBlockIncrement(increment:Number):Void{
124 if(verticalBlockIncrement != increment){
125 verticalBlockIncrement = increment;
126 fireStateChanged();
127 }
128 }
129
130
133 public function setHorizontalUnitIncrement(increment:Number):Void{
134 if(horizontalUnitIncrement != increment){
135 horizontalUnitIncrement = increment;
136 fireStateChanged();
137 }
138 }
139
140
143 public function setHorizontalBlockIncrement(increment:Number):Void{
144 if(horizontalBlockIncrement != increment){
145 horizontalBlockIncrement = increment;
146 fireStateChanged();
147 }
148 }
149
150
151
155 public function append(com:Component, constraints:Object):Void{
156 setView(com);
157 }
158
159
163 public function insert(i:Number, com:Component, constraints:Object):Void{
164 setView(com);
165 }
166
167
168
169
172 public function getVerticalUnitIncrement():Number{
173 if(verticalUnitIncrement != undefined){
174 return verticalUnitIncrement;
175 }else{
176 return 1;
177 }
178 }
179
180
183 public function getVerticalBlockIncrement():Number{
184 if(verticalBlockIncrement != undefined){
185 return verticalBlockIncrement;
186 }else{
187 return getExtentSize().height-1;
188 }
189 }
190
191
194 public function getHorizontalUnitIncrement():Number{
195 if(horizontalUnitIncrement != undefined){
196 return horizontalUnitIncrement;
197 }else{
198 return 1;
199 }
200 }
201
202
205 public function getHorizontalBlockIncrement():Number{
206 if(horizontalBlockIncrement != undefined){
207 return horizontalBlockIncrement;
208 }else{
209 return getExtentSize().width - 1;
210 }
211 }
212
213 public function setViewportTestSize(s:Dimension):Void{
214 setSize(s);
215 }
216
217 public function getExtentSize() : Dimension {
218 return getInsets().inroundsSize(getSize());
219 }
220
221
225 public function getViewSize() : Dimension {
226 if(view == null){
227 return new Dimension();
228 }else{
229 return view.getPreferredSize();
230 }
231 }
232
233
237 public function getViewPosition() : Point {
238 if(view != null){
239 var p:Point = view.getLocation();
240 var ir:Rectangle = getInsets().getInsideBounds(getSize().getBounds());
241 p.x = ir.x - p.x;
242 p.y = ir.y - p.y;
243 return p;
244 }else{
245 return null;
246 }
247 }
248
249 public function setViewPosition(p : Point) : Void {
250 restrictionViewPos(p);
251 if(!p.equals(getViewPosition())){
252 var ir:Rectangle = getInsets().getInsideBounds(getSize().getBounds());
253 view.setLocation(ir.x-p.x, ir.y-p.y);
254 view.revalidate();
255 fireStateChanged();
256 }
257 }
258
259 public function scrollRectToVisible(contentRect : Rectangle) : Void {
260 setViewPosition(new Point(contentRect.x, contentRect.y));
261 }
262
263
268 public function scrollToBottomLeft():Void{
269 setViewPosition(new Point(0, Number.MAX_VALUE));
270 }
271
276 public function scrollToBottomRight():Void{
277 setViewPosition(new Point(Number.MAX_VALUE, Number.MAX_VALUE));
278 }
279
284 public function scrollToTopLeft():Void{
285 setViewPosition(new Point(0, 0));
286 }
287
292 public function scrollToTopRight():Void{
293 setViewPosition(new Point(Number.MAX_VALUE, 0));
294 }
295
296 private function restrictionViewPos(p:Point):Point{
297 var maxPos:Point = getViewMaxPos();
298 p.x = Math.max(0, Math.min(maxPos.x, p.x));
299 p.y = Math.max(0, Math.min(maxPos.y, p.y));
300 return p;
301 }
302
303 private function getViewMaxPos():Point{
304 var showSize:Dimension = getExtentSize();
305 var viewSize:Dimension = getViewSize();
306 var p:Point = new Point(viewSize.width-showSize.width, viewSize.height-showSize.height);
307 if(p.x < 0) p.x = 0;
308 if(p.y < 0) p.y = 0;
309 return p;
310 }
311
312
319 public function toViewCoordinatesSize(size : Dimension) : Dimension {
320 return new Dimension(size.width, size.height);
321 }
322
323
330 public function toViewCoordinatesLocation(p : Point) : Point {
331 return new Point(p.x, p.y);
332 }
333
334
341 public function toScreenCoordinatesSize(size:Dimension):Dimension{
342 return new Dimension(size.width, size.height);
343 }
344
345
352 public function toScreenCoordinatesLocation(p:Point):Point{
353 return new Point(p.x, p.y);
354 }
355
356 public function addChangeListener(func:Function, obj:Object):Object{
357 return addEventListener(Component.ON_STATE_CHANGED, func, obj);
358 }
359
360 public function getViewportPane() : Component {
361 return this;
362 }
363
364 }
365