KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.*;
17
18
19 public abstract class ObjectSegment extends ParagraphSegment {
20     public static final int TOP = 1;
21
22     public static final int MIDDLE = 2;
23
24     public static final int BOTTOM = 3;
25
26     private int alignment = BOTTOM;
27     private boolean nowrap=false;
28     private Rectangle bounds;
29     private String JavaDoc objectId;
30
31     public int getVerticalAlignment() {
32         return alignment;
33     }
34
35     void setVerticalAlignment(int alignment) {
36         this.alignment = alignment;
37     }
38
39     public String JavaDoc getObjectId() {
40         return objectId;
41     }
42
43     void setObjectId(String JavaDoc objectId) {
44         this.objectId = objectId;
45     }
46     
47     protected abstract Point getObjectSize(Hashtable JavaDoc resourceTable, int wHint);
48
49     public boolean advanceLocator(GC gc, int wHint, Locator loc,
50             Hashtable JavaDoc objectTable, boolean computeHeightOnly) {
51         Point objectSize = getObjectSize(objectTable, wHint);
52         int iwidth = 0;
53         int iheight = 0;
54         boolean newLine = false;
55
56         if (objectSize != null) {
57             iwidth = objectSize.x + (isSelectable()?2:0);
58             iheight = objectSize.y + (isSelectable()?2:0);
59         }
60         if (wHint != SWT.DEFAULT && !nowrap && loc.x + iwidth > wHint) {
61             // new line
62
if (computeHeightOnly)
63                 loc.collectHeights();
64             loc.x = loc.indent;
65             loc.x += iwidth;
66             loc.y += loc.rowHeight;
67             loc.width = loc.indent + iwidth;
68             loc.rowHeight = iheight;
69             loc.leading = 0;
70             newLine = true;
71         } else {
72             loc.x += iwidth;
73             loc.width += iwidth;
74             loc.rowHeight = Math.max(loc.rowHeight, iheight);
75         }
76         return newLine;
77     }
78
79     public boolean contains(int x, int y) {
80         if (bounds==null)
81             return false;
82         return bounds.contains(x, y);
83     }
84     public boolean intersects(Rectangle rect) {
85         if (bounds==null)
86             return false;
87         return bounds.intersects(rect);
88     }
89
90     public Rectangle getBounds() {
91         return bounds;
92     }
93
94     public boolean isSelectable() {
95         return false;
96     }
97     /**
98      * @return Returns the nowrap.
99      */

100     public boolean isNowrap() {
101         return nowrap;
102     }
103     /**
104      * @param nowrap The nowrap to set.
105      */

106     public void setNowrap(boolean nowrap) {
107         this.nowrap = nowrap;
108     }
109     public void paint(GC gc, boolean hover, Hashtable JavaDoc resourceTable, boolean selected, SelectionData selData, Rectangle repaintRegion) {
110     }
111
112     /* (non-Javadoc)
113      * @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)
114      */

115     public void layout(GC gc, int width, Locator loc, Hashtable JavaDoc resourceTable,
116             boolean selected) {
117         Point size = getObjectSize(resourceTable, width);
118
119         int objWidth = 0;
120         int objHeight = 0;
121         if (size != null) {
122             objWidth = size.x + (isSelectable()?2:0);
123             objHeight = size.y + (isSelectable()?2:0);
124         } else
125             return;
126         loc.width = objWidth;
127
128         if (!nowrap && loc.x + objWidth > width) {
129             // new row
130
loc.newLine();
131             loc.rowCounter++;
132         }
133         int ix = loc.x;
134         int iy = loc.y;
135         
136         if (alignment==MIDDLE)
137             iy = loc.getMiddle(objHeight, false);
138         else if (alignment==BOTTOM)
139             iy = loc.getBaseline(objHeight, false);
140         loc.x += objWidth;
141         loc.rowHeight = Math.max(loc.rowHeight, objHeight);
142         bounds = new Rectangle(ix, iy, objWidth, objHeight);
143     }
144     /* (non-Javadoc)
145      * @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)
146      */

147     public void computeSelection(GC gc, Hashtable JavaDoc resourceTable, SelectionData selData) {
148         // TODO we should add this to the selection
149
// if we want to support rich text
150
}
151 }
152
Popular Tags