001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.graphics.Color;
015: import org.eclipse.swt.graphics.GC;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.graphics.Rectangle;
019:
020: public class BulletParagraph extends Paragraph {
021: public static final int CIRCLE = 1;
022:
023: public static final int TEXT = 2;
024:
025: public static final int IMAGE = 3;
026:
027: private int style = CIRCLE;
028:
029: private String text;
030:
031: private int CIRCLE_DIAM = 5;
032:
033: private int SPACING = 10;
034:
035: private int indent = -1;
036:
037: private int bindent = -1;
038:
039: private Rectangle bbounds;
040:
041: /**
042: * Constructor for BulletParagraph.
043: *
044: * @param addVerticalSpace
045: */
046: public BulletParagraph(boolean addVerticalSpace) {
047: super (addVerticalSpace);
048: }
049:
050: public int getIndent() {
051: int ivalue = indent;
052: if (ivalue != -1)
053: return ivalue;
054: switch (style) {
055: case CIRCLE:
056: ivalue = CIRCLE_DIAM + SPACING;
057: break;
058: default:
059: ivalue = 20;
060: break;
061: }
062: return getBulletIndent() + ivalue;
063: }
064:
065: public int getBulletIndent() {
066: if (bindent != -1)
067: return bindent;
068: return 0;
069: }
070:
071: /*
072: * @see IBulletParagraph#getBulletStyle()
073: */
074: public int getBulletStyle() {
075: return style;
076: }
077:
078: public void setBulletStyle(int style) {
079: this .style = style;
080: }
081:
082: public void setBulletText(String text) {
083: this .text = text;
084: }
085:
086: public void setIndent(int indent) {
087: this .indent = indent;
088: }
089:
090: public void setBulletIndent(int bindent) {
091: this .bindent = bindent;
092: }
093:
094: /*
095: * @see IBulletParagraph#getBulletText()
096: */
097: public String getBulletText() {
098: return text;
099: }
100:
101: public void layout(GC gc, int width, Locator loc, int lineHeight,
102: Hashtable resourceTable, IHyperlinkSegment selectedLink) {
103: computeRowHeights(gc, width, loc, lineHeight, resourceTable);
104: layoutBullet(gc, loc, lineHeight, resourceTable);
105: super .layout(gc, width, loc, lineHeight, resourceTable,
106: selectedLink);
107: }
108:
109: public void paint(GC gc, Rectangle repaintRegion,
110: Hashtable resourceTable, IHyperlinkSegment selectedLink,
111: SelectionData selData) {
112: paintBullet(gc, repaintRegion, resourceTable);
113: super .paint(gc, repaintRegion, resourceTable, selectedLink,
114: selData);
115: }
116:
117: private void layoutBullet(GC gc, Locator loc, int lineHeight,
118: Hashtable resourceTable) {
119: int x = loc.x - getIndent() + getBulletIndent();
120: int rowHeight = ((int[]) loc.heights.get(0))[0];
121: if (style == CIRCLE) {
122: int y = loc.y + rowHeight / 2 - CIRCLE_DIAM / 2;
123: bbounds = new Rectangle(x, y, CIRCLE_DIAM, CIRCLE_DIAM);
124: } else if (style == TEXT && text != null) {
125: //int height = gc.getFontMetrics().getHeight();
126: Point textSize = gc.textExtent(text);
127: bbounds = new Rectangle(x, loc.y, textSize.x, textSize.y);
128: } else if (style == IMAGE && text != null) {
129: Image image = (Image) resourceTable.get(text);
130: if (image != null) {
131: Rectangle ibounds = image.getBounds();
132: int y = loc.y + rowHeight / 2 - ibounds.height / 2;
133: bbounds = new Rectangle(x, y, ibounds.width,
134: ibounds.height);
135: }
136: }
137: }
138:
139: public void paintBullet(GC gc, Rectangle repaintRegion,
140: Hashtable resourceTable) {
141: if (bbounds == null)
142: return;
143: int x = bbounds.x;
144: int y = bbounds.y;
145: if (repaintRegion != null) {
146: x -= repaintRegion.x;
147: y -= repaintRegion.y;
148: }
149: if (style == CIRCLE) {
150: Color bg = gc.getBackground();
151: Color fg = gc.getForeground();
152: gc.setBackground(fg);
153: gc.fillRectangle(x, y + 1, 5, 3);
154: gc.fillRectangle(x + 1, y, 3, 5);
155: gc.setBackground(bg);
156: } else if (style == TEXT && text != null) {
157: gc.drawText(text, x, y);
158: } else if (style == IMAGE && text != null) {
159: Image image = (Image) resourceTable.get(text);
160: if (image != null)
161: gc.drawImage(image, x, y);
162: }
163: }
164: }
|