KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > ControlSegment


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.forms.widgets;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.GC;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.widgets.Canvas;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21
22 public class ControlSegment extends ObjectSegment implements IFocusSelectable {
23     private boolean fill;
24     private int width = SWT.DEFAULT;
25     private int height = SWT.DEFAULT;
26     
27     public ControlSegment() {
28     }
29     
30     public void setFill(boolean fill) {
31         this.fill = fill;
32     }
33     
34     public void setWidth(int width) {
35         this.width = width;
36     }
37     
38     public void setHeight(int height) {
39         this.height = height;
40     }
41     
42     public Control getControl(Hashtable JavaDoc resourceTable) {
43         Object JavaDoc obj = resourceTable.get(getObjectId());
44         if (obj instanceof Control) {
45             Control c = (Control)obj;
46             if (!c.isDisposed())
47                 return c;
48         }
49         return null;
50     }
51
52     protected Point getObjectSize(Hashtable JavaDoc resourceTable, int wHint) {
53         Control control = getControl(resourceTable);
54         if (control==null)
55             return new Point(0,0);
56         int realWhint = FormUtil.getWidthHint(wHint, control);
57         Point size = control.computeSize(realWhint, SWT.DEFAULT);
58         if (realWhint!=SWT.DEFAULT && fill)
59             size.x = Math.max(size.x, realWhint);
60         if (width !=SWT.DEFAULT)
61             size.x = width;
62         if (height != SWT.DEFAULT)
63             size.y = height;
64         return size;
65     }
66     
67     public void layout(GC gc, int width, Locator loc, Hashtable JavaDoc resourceTable,
68             boolean selected) {
69         super.layout(gc, width, loc, resourceTable, selected);
70         Control control = getControl(resourceTable);
71         if (control!=null)
72             control.setBounds(getBounds());
73     }
74
75     public boolean setFocus(Hashtable JavaDoc resourceTable, boolean next) {
76         Control c = getControl(resourceTable);
77         if (c!=null) {
78             return setFocus(c, next);
79         }
80         return false;
81     }
82     
83     private boolean setFocus(Control c, boolean direction) {
84         if (c instanceof Composite) {
85             Composite comp = (Composite)c;
86             Control [] tabList = comp.getTabList();
87             if (direction) {
88                 for (int i=0; i<tabList.length; i++) {
89                     if (setFocus(tabList[i], direction))
90                         return true;
91                 }
92             }
93             else {
94                 for (int i=tabList.length-1; i>=0; i--) {
95                     if (setFocus(tabList[i], direction))
96                         return true;
97                 }
98             }
99             if (!(c instanceof Canvas))
100                 return false;
101         }
102         return c.setFocus();
103     }
104
105     public boolean isFocusSelectable(Hashtable JavaDoc resourceTable) {
106         Control c = getControl(resourceTable);
107         if (c!=null)
108             return true;
109         return false;
110     }
111 }
112
Popular Tags