KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > wrapped > WrappedDisplayComponent


1 package text.wrapped;
2
3 import text.*;
4
5 import java.awt.*;
6 import java.awt.image.*;
7 import java.awt.event.*;
8 import java.util.*;
9 import javax.swing.*;
10 import javax.swing.event.*;
11 import java.awt.font.*;
12
13 public class WrappedDisplayComponent extends JComponent implements ChangeListener
14 {
15    protected WrappedData data;
16
17    public WrappedDisplayComponent()
18    {
19       setOpaque(false);
20       setDoubleBuffered(false);
21    }
22
23    public void setWrappedData(WrappedData _data)
24    {
25       data = _data;
26    }
27
28    public void stateChanged(ChangeEvent e)
29    {
30       repaint();
31    }
32
33    public void paint (Graphics g)
34    {
35       TextSource.initGraphics(g);
36
37       WrappedContainer text = data.getCurrentText();
38
39       data.setExtent((getHeight() - 5) / (TextSource.fontMetrics.getHeight() * 3));
40
41       if (text == null)
42       {
43          return;
44       }
45
46       int checkY = (g.getClipBounds()).y - 10; // reverse these if
47
int checkH = checkY+(g.getClipBounds()).height + 20; // painting fucks up
48

49       int width = super.getWidth();
50       int height = super.getHeight();
51
52       g.setFont(TextSource.clientFont);
53
54       int baseline = height - 5; // gives us a 5 pixel buffer
55
// between the textbox and the textarea
56

57       WrappedContainer head = text;
58       AttributedText[] strings;
59
60       if (data.getSelection() != null)
61       {
62          data.getSelection().clear();
63       }
64
65       while (head != null && baseline > 0)
66       {
67          head.touch(width);
68          
69          strings = head.getWrappedText();
70
71          for (int x = 0; x < strings.length && baseline > 0; x++)
72          {
73             if (baseline <= checkH && baseline >= checkY)
74             {
75                TextSource.drawBackground(g, strings[x], 0, baseline);
76                drawSelection(g, strings[x], baseline, x == 0);
77                TextSource.drawForeground(g, strings[x], 0, baseline);
78             }
79
80             baseline -= (TextSource.fontMetrics.getHeight() + 2);
81          }
82
83          head = head.next();
84       }
85    }
86
87    /** Draws the selected text and interacts with the SelectionSpace object calculating the actual text we're going to put
88        into the clipboard. This is some fugly code. As far as I can tell it works though. */

89    public void drawSelection(Graphics g, AttributedText text, int baseline, boolean beginning)
90    {
91       if (data.getSelection() == null)
92       {
93          return;
94       }
95
96       SelectionSpace select = data.getSelection();
97
98       g.setColor(UIManager.getColor("TextField.selectionBackground")); // setup the appropriate color...
99

100       String JavaDoc stext;
101
102       if (select.isOnlyLine(baseline))
103       {
104          int indexStart = text.getRange(0, select.getSingleStart()).length();
105          int indexStop = text.getRange(0, select.getSingleEnd()).length();
106
107          stext = text.getText();
108
109          int height = TextSource.fontMetrics.getHeight();
110          int start = TextSource.fontMetrics.stringWidth(stext.substring(0, indexStart));
111          int stop = TextSource.fontMetrics.stringWidth(stext.substring(0, indexStop));
112
113          select.append(stext.substring(indexStart, indexStop));
114
115          g.fillRect(start, baseline - height + 2, stop - start, height + 2); // the +2 may become a +4
116

117 // g.setColor(Color.black);
118
// g.drawString(stext.substring(indexStart, indexStop), start, baseline);
119
}
120       else if (select.isStartLine(baseline))
121       {
122          if (beginning)
123          {
124             select.touch();
125          }
126
127          stext = text.getRange(0, select.getSelectionStart());
128
129          int height = TextSource.fontMetrics.getHeight();
130          int width = TextSource.fontMetrics.stringWidth(stext);
131
132          String JavaDoc temps = text.getText();
133
134          stext = temps.substring(stext.length(), temps.length());
135          select.append(stext);
136
137          g.fillRect(width, baseline - height + 2, text.getWidth() - width, height + 2); // the +2 may become a +4
138

139  // g.setColor(Color.black);
140
// g.drawString(stext, width, baseline);
141
}
142       else if (select.isEndLine(baseline))
143       {
144          if (beginning)
145          {
146             select.touch();
147          }
148
149          int height = TextSource.fontMetrics.getHeight();
150          int width = TextSource.fontMetrics.stringWidth(text.getRange(0, select.getSelectionEnd()));
151
152          if (text.isIndent())
153          {
154             stext = text.next.getRange(0, select.getSelectionEnd());
155             select.append(stext);
156             select.append(" ");
157          }
158          else
159          {
160             stext = text.getRange(0, select.getSelectionEnd());
161             select.append(stext);
162          }
163
164          g.fillRect(0, baseline - height + 2, width, height + 2); // the +2 may become a +4
165

166  // g.setColor(Color.black);
167
// g.drawString(text.getRange(0, select.getSelectionEnd()), 0, baseline);
168
}
169       else if (select.isSelectedLine(baseline))
170       {
171          if (beginning)
172          {
173             select.touch();
174          }
175
176          int height = TextSource.fontMetrics.getHeight();
177          g.fillRect(0, baseline - height + 2, text.getWidth(), height + 2); // the +2 may become a +4
178

179    // g.setColor(Color.black);
180
// g.drawString(text.getText(), 0, baseline);
181

182          if (text.isIndent())
183          {
184             text = text.next;
185             select.append(text.getText());
186             select.append(" ");
187          }
188          else
189          {
190             select.append(text.getText());
191          }
192
193       }
194 // g.setColor(Color.red); // just some debugging to see the clipping for the text area..
195
// ((Graphics2D)g).draw(select.getChangedArea());
196
}
197 }
198
Popular Tags