001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.forms.widgets;
011:
012: import java.util.Hashtable;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.graphics.GC;
016: import org.eclipse.swt.graphics.Point;
017: import org.eclipse.swt.widgets.Canvas;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020:
021: public class ControlSegment extends ObjectSegment implements
022: IFocusSelectable {
023: private boolean fill;
024: private int width = SWT.DEFAULT;
025: private int height = SWT.DEFAULT;
026:
027: public ControlSegment() {
028: }
029:
030: public void setFill(boolean fill) {
031: this .fill = fill;
032: }
033:
034: public void setWidth(int width) {
035: this .width = width;
036: }
037:
038: public void setHeight(int height) {
039: this .height = height;
040: }
041:
042: public Control getControl(Hashtable resourceTable) {
043: Object obj = resourceTable.get(getObjectId());
044: if (obj instanceof Control) {
045: Control c = (Control) obj;
046: if (!c.isDisposed())
047: return c;
048: }
049: return null;
050: }
051:
052: protected Point getObjectSize(Hashtable resourceTable, int wHint) {
053: Control control = getControl(resourceTable);
054: if (control == null)
055: return new Point(0, 0);
056: int realWhint = FormUtil.getWidthHint(wHint, control);
057: Point size = control.computeSize(realWhint, SWT.DEFAULT);
058: if (realWhint != SWT.DEFAULT && fill)
059: size.x = Math.max(size.x, realWhint);
060: if (width != SWT.DEFAULT)
061: size.x = width;
062: if (height != SWT.DEFAULT)
063: size.y = height;
064: return size;
065: }
066:
067: public void layout(GC gc, int width, Locator loc,
068: Hashtable resourceTable, boolean selected) {
069: super .layout(gc, width, loc, resourceTable, selected);
070: Control control = getControl(resourceTable);
071: if (control != null)
072: control.setBounds(getBounds());
073: }
074:
075: public boolean setFocus(Hashtable resourceTable, boolean next) {
076: Control c = getControl(resourceTable);
077: if (c != null) {
078: return setFocus(c, next);
079: }
080: return false;
081: }
082:
083: private boolean setFocus(Control c, boolean direction) {
084: if (c instanceof Composite) {
085: Composite comp = (Composite) c;
086: Control[] tabList = comp.getTabList();
087: if (direction) {
088: for (int i = 0; i < tabList.length; i++) {
089: if (setFocus(tabList[i], direction))
090: return true;
091: }
092: } else {
093: for (int i = tabList.length - 1; i >= 0; i--) {
094: if (setFocus(tabList[i], direction))
095: return true;
096: }
097: }
098: if (!(c instanceof Canvas))
099: return false;
100: }
101: return c.setFocus();
102: }
103:
104: public boolean isFocusSelectable(Hashtable resourceTable) {
105: Control c = getControl(resourceTable);
106: if (c != null)
107: return true;
108: return false;
109: }
110: }
|