KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > Bullet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.swt.custom;
12
13 import org.eclipse.swt.*;
14
15 /**
16  * Instances of this class represent bullets in the <code>StyledText</code>.
17  * <p>
18  * The hashCode() method in this class uses the values of the public
19  * fields to compute the hash value. When storing instances of the
20  * class in hashed collections, do not modify these fields after the
21  * object has been inserted.
22  * </p>
23  * <p>
24  * Application code does <em>not</em> need to explicitly release the
25  * resources managed by each instance when those instances are no longer
26  * required, and thus no <code>dispose()</code> method is provided.
27  * </p>
28  *
29  * @see StyledText#setLineBullet(int, int, Bullet)
30  *
31  * @since 3.2
32  */

33 public class Bullet {
34     public int type;
35     public StyleRange style;
36     public String JavaDoc text;
37     int[] linesIndices;
38     int count;
39
40 /**
41  * Create a new bullet the specified style, the type is set to ST.BULLET_DOT.
42  * The style must have a glyph metrics set.
43  *
44  * @param style the style
45  *
46  * @exception IllegalArgumentException <ul>
47  * <li>ERROR_NULL_ARGUMENT when the style or the glyph metrics are null</li>
48  * </ul>
49  */

50 public Bullet(StyleRange style) {
51     this(ST.BULLET_DOT, style);
52 }
53 /**
54  * Create a new bullet the specified style and type.
55  * The style must have a glyph metrics set.
56  *
57  * @param style the style
58  *
59  * @exception IllegalArgumentException <ul>
60  * <li>ERROR_NULL_ARGUMENT when the style or the glyph metrics are null</li>
61  * </ul>
62  */

63 public Bullet(int type, StyleRange style) {
64     if (style == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
65     if (style.metrics == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
66     this.type = type;
67     this.style = style;
68 }
69 void addIndices (int startLine, int lineCount) {
70     if (linesIndices == null) {
71         linesIndices = new int[lineCount];
72         count = lineCount;
73         for (int i = 0; i < lineCount; i++) linesIndices[i] = startLine + i;
74     } else {
75         int modifyStart = 0;
76         while (modifyStart < count) {
77             if (startLine <= linesIndices[modifyStart]) break;
78             modifyStart++;
79         }
80         int modifyEnd = modifyStart;
81         while (modifyEnd < count) {
82             if (startLine + lineCount <= linesIndices[modifyEnd]) break;
83             modifyEnd++;
84         }
85         int newSize = modifyStart + lineCount + count - modifyEnd;
86         if (newSize > linesIndices.length) {
87             int[] newLinesIndices = new int[newSize];
88             System.arraycopy(linesIndices, 0, newLinesIndices, 0, count);
89             linesIndices = newLinesIndices;
90         }
91         System.arraycopy(linesIndices, modifyEnd, linesIndices, modifyStart + lineCount, count - modifyEnd);
92         for (int i = 0; i < lineCount; i++) linesIndices[modifyStart + i] = startLine + i;
93         count = newSize;
94     }
95 }
96 int indexOf (int lineIndex) {
97     for (int i = 0; i < count; i++) {
98         if (linesIndices[i] == lineIndex) return i;
99     }
100     return -1;
101 }
102 public int hashCode() {
103     return style.hashCode() ^ type;
104 }
105 int[] removeIndices (int startLine, int replaceLineCount, int newLineCount, boolean update) {
106     if (count == 0) return null;
107     if (startLine > linesIndices[count - 1]) return null;
108     int endLine = startLine + replaceLineCount;
109     int delta = newLineCount - replaceLineCount;
110     for (int i = 0; i < count; i++) {
111         int index = linesIndices[i];
112         if (startLine <= index) {
113             int j = i;
114             while (j < count) {
115                 if (linesIndices[j] >= endLine) break;
116                 j++;
117             }
118             if (update) {
119                 for (int k = j; k < count; k++) linesIndices[k] += delta;
120             }
121             int[] redrawLines = new int[count - j];
122             System.arraycopy(linesIndices, j, redrawLines, 0, count - j);
123             System.arraycopy(linesIndices, j, linesIndices, i, count - j);
124             count -= (j - i);
125             return redrawLines;
126         }
127     }
128     for (int i = 0; i < count; i++) linesIndices[i] += delta;
129     return null;
130 }
131 int size() {
132     return count;
133 }
134 }
135
Popular Tags