KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > wrapped > SelectionSpace


1 package text.wrapped;
2
3 import java.awt.*;
4 import text.*;
5
6 public class SelectionSpace
7 {
8     protected boolean ready = false;
9     protected int origin_x;
10     protected int origin_y;
11
12     protected int start_sel, end_sel;
13
14     protected int x, y;
15
16     boolean flipped;
17
18     protected int lineStart = 0;
19     protected int lineEnd = 0;
20
21     protected StringBuffer JavaDoc selection;
22
23     protected Rectangle changed;
24
25     public SelectionSpace(Point p)
26     {
27        origin_x = (int)p.getX();
28        origin_y = (int)p.getY();
29
30        changed = new Rectangle(0, origin_y - ((TextSource.fontMetrics.getHeight() + 2) * 1), 2048, (TextSource.fontMetrics.getHeight() + 2) * 2);
31
32        ready = false;
33     }
34
35     public void clear()
36     {
37        selection = new StringBuffer JavaDoc();
38     }
39
40     public void append(String JavaDoc text)
41     {
42        if (text.length() > 0)
43        {
44           selection.insert(0, text);
45        }
46     }
47
48     public void touch()
49     {
50        if (selection.length() > 0)
51        {
52            append("\n");
53        }
54     }
55
56     public String JavaDoc getSelectedText()
57     {
58        if (selection != null)
59        {
60           return selection.toString();
61        }
62
63        return null;
64     }
65
66     protected int translateToLineNumber(int pixely)
67     {
68        pixely -= 5;
69        int height = TextSource.fontMetrics.getHeight() + 2;
70        return (pixely - (pixely % height)) / height;
71     }
72
73     protected int translateToPixel(int lineno)
74     {
75        return (lineno * (TextSource.fontMetrics.getHeight() + 2)) + 5;
76     }
77
78     public boolean isSelectedLine(int y)
79     {
80        int temp = translateToLineNumber(y);
81        return (ready && temp >= lineStart && temp <= lineEnd);
82     }
83
84     public boolean isOnlyLine(int y)
85     {
86        return isStartLine(y) && isEndLine(y);
87     }
88     
89     public boolean isEndLine(int y)
90     {
91        int temp = translateToLineNumber(y);
92        return (ready && temp == lineEnd);
93     }
94
95     public boolean isStartLine(int y)
96     {
97        int temp = translateToLineNumber(y);
98        return (ready && temp == lineStart);
99     }
100
101     public int getSingleStart()
102     {
103        if (start_sel < end_sel)
104        {
105            return start_sel;
106        }
107        return end_sel;
108     }
109
110     public int getSingleEnd()
111     {
112        if (start_sel < end_sel)
113        {
114            return end_sel;
115        }
116        return start_sel;
117     }
118
119     public int getSelectionStart()
120     {
121        return start_sel;
122     }
123  
124     public int getSelectionEnd()
125     {
126        return end_sel;
127     }
128
129     public Rectangle getChangedArea()
130     {
131        return changed;
132     }
133
134     public void growSelection(Point p)
135     {
136        ready = true;
137
138        x = (int)p.getX();
139        y = (int)p.getY();
140
141        // handle normal selection calculations
142
if (y < origin_y)
143        {
144           flipped = true;
145        }
146        else
147        {
148           flipped = false;
149        }
150  
151        if (flipped)
152        {
153           lineStart = translateToLineNumber(y);
154           lineEnd = translateToLineNumber(origin_y);
155        }
156        else
157        {
158           lineStart = translateToLineNumber(origin_y);
159           lineEnd = translateToLineNumber(y);
160        }
161
162        if (flipped)
163        {
164           start_sel = x;
165           end_sel = origin_x;
166        }
167        else
168        {
169           start_sel = origin_x;
170           end_sel = x;
171        }
172
173
174        // calculate area that we are going to repaint in (kind of an optimization for selection *uNF*
175
// if (!changed.contains(1024, y))
176
// {
177
int start = translateToPixel(lineStart - 1);
178
179           if (start > changed.getY())
180           {
181              start = (int)changed.getY();
182           }
183
184           int end = translateToPixel(lineEnd + 2) - start;
185
186           if (end < changed.getHeight())
187           {
188              end = (int)changed.getHeight();
189           }
190
191           changed.setBounds(0, start, 2048, end);
192     // }
193
}
194 }
195
Popular Tags