001: /*******************************************************************************
002: * Copyright (c) 2000, 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.*;
016:
017: public abstract class ObjectSegment extends ParagraphSegment {
018: public static final int TOP = 1;
019:
020: public static final int MIDDLE = 2;
021:
022: public static final int BOTTOM = 3;
023:
024: private int alignment = BOTTOM;
025: private boolean nowrap = false;
026: private Rectangle bounds;
027: private String objectId;
028:
029: public int getVerticalAlignment() {
030: return alignment;
031: }
032:
033: void setVerticalAlignment(int alignment) {
034: this .alignment = alignment;
035: }
036:
037: public String getObjectId() {
038: return objectId;
039: }
040:
041: void setObjectId(String objectId) {
042: this .objectId = objectId;
043: }
044:
045: protected abstract Point getObjectSize(Hashtable resourceTable,
046: int wHint);
047:
048: public boolean advanceLocator(GC gc, int wHint, Locator loc,
049: Hashtable objectTable, boolean computeHeightOnly) {
050: Point objectSize = getObjectSize(objectTable, wHint);
051: int iwidth = 0;
052: int iheight = 0;
053: boolean newLine = false;
054:
055: if (objectSize != null) {
056: iwidth = objectSize.x + (isSelectable() ? 2 : 0);
057: iheight = objectSize.y + (isSelectable() ? 2 : 0);
058: }
059: if (wHint != SWT.DEFAULT && !nowrap && loc.x + iwidth > wHint) {
060: // new line
061: if (computeHeightOnly)
062: loc.collectHeights();
063: loc.x = loc.indent;
064: loc.x += iwidth;
065: loc.y += loc.rowHeight;
066: loc.width = loc.indent + iwidth;
067: loc.rowHeight = iheight;
068: loc.leading = 0;
069: newLine = true;
070: } else {
071: loc.x += iwidth;
072: loc.width += iwidth;
073: loc.rowHeight = Math.max(loc.rowHeight, iheight);
074: }
075: return newLine;
076: }
077:
078: public boolean contains(int x, int y) {
079: if (bounds == null)
080: return false;
081: return bounds.contains(x, y);
082: }
083:
084: public boolean intersects(Rectangle rect) {
085: if (bounds == null)
086: return false;
087: return bounds.intersects(rect);
088: }
089:
090: public Rectangle getBounds() {
091: return bounds;
092: }
093:
094: public boolean isSelectable() {
095: return false;
096: }
097:
098: /**
099: * @return Returns the nowrap.
100: */
101: public boolean isNowrap() {
102: return nowrap;
103: }
104:
105: /**
106: * @param nowrap The nowrap to set.
107: */
108: public void setNowrap(boolean nowrap) {
109: this .nowrap = nowrap;
110: }
111:
112: public void paint(GC gc, boolean hover, Hashtable resourceTable,
113: boolean selected, SelectionData selData,
114: Rectangle repaintRegion) {
115: }
116:
117: /* (non-Javadoc)
118: * @see org.eclipse.ui.internal.forms.widgets.ParagraphSegment#layout(org.eclipse.swt.graphics.GC, int, org.eclipse.ui.internal.forms.widgets.Locator, java.util.Hashtable, boolean, org.eclipse.ui.internal.forms.widgets.SelectionData)
119: */
120: public void layout(GC gc, int width, Locator loc,
121: Hashtable resourceTable, boolean selected) {
122: Point size = getObjectSize(resourceTable, width);
123:
124: int objWidth = 0;
125: int objHeight = 0;
126: if (size != null) {
127: objWidth = size.x + (isSelectable() ? 2 : 0);
128: objHeight = size.y + (isSelectable() ? 2 : 0);
129: } else
130: return;
131: loc.width = objWidth;
132:
133: if (!nowrap && loc.x + objWidth > width) {
134: // new row
135: loc.newLine();
136: loc.rowCounter++;
137: }
138: int ix = loc.x;
139: int iy = loc.y;
140:
141: if (alignment == MIDDLE)
142: iy = loc.getMiddle(objHeight, false);
143: else if (alignment == BOTTOM)
144: iy = loc.getBaseline(objHeight, false);
145: loc.x += objWidth;
146: loc.rowHeight = Math.max(loc.rowHeight, objHeight);
147: bounds = new Rectangle(ix, iy, objWidth, objHeight);
148: }
149:
150: /* (non-Javadoc)
151: * @see org.eclipse.ui.internal.forms.widgets.ParagraphSegment#computeSelection(org.eclipse.swt.graphics.GC, java.util.Hashtable, boolean, org.eclipse.ui.internal.forms.widgets.SelectionData)
152: */
153: public void computeSelection(GC gc, Hashtable resourceTable,
154: SelectionData selData) {
155: // TODO we should add this to the selection
156: // if we want to support rich text
157: }
158: }
|