3 import java.awt.event.*;
5 class TextCanvas
extends Canvas {
13 TextCanvas(TextPanel tp) {
16 addMouseMotionListener(tp);
20 public void setBounds(
int x,
int y,
int width,
int height) {
21 super.setBounds(x, y, width, height);
27 public void update(Graphics g) {
31 public void paint(Graphics g) {
32 if(tp==null || g==null)
return;
33 Dimension d = getSize();
35 int iHeight = d.height;
37 if(iWidth<=0 || iHeight<=0)
return;
38 g.setColor(Color.lightGray);
40 makeImage(iWidth,iHeight);
41 if(tp.iRowHeight==0 || (tp.iColWidth[0]==0&&tp.iRowCount>0)) {
42 tp.iRowHeight=fMetrics.getHeight()+2;
43 for(
int i=0;i<tp.iColCount;i++)
48 gImage.setColor(Color.white);
49 gImage.fillRect(0,0,iWidth,iHeight);
51 drawColumnLabels(iWidth);
52 int y=tp.iRowHeight+1-tp.iY;
54 while(y<tp.iRowHeight+1) {
60 for(;y<iHeight && j<tp.iRowCount;j++,y+=tp.iRowHeight) {
62 for(
int i=0;i<tp.iColCount;i++) {
63 int w=tp.iColWidth[i];
64 Color b=Color.white,t=Color.black;
65 if(j>=tp.selStart && j<=tp.selEnd) {
69 gImage.fillRect(x,y,w-1,tp.iRowHeight);
72 char[] chars = getChars(i,j);
74 gImage.drawChars(chars,0,chars.length,x+2,y+tp.iRowHeight-5);
79 g.drawImage(iImage,0,0,null);
82 void makeImage(
int iWidth,
int iHeight) {
83 iImage=createImage(iWidth,iHeight);
86 gImage=iImage.getGraphics();
88 fFont=
new Font(
"Dialog",Font.PLAIN,12);
89 gImage.setFont(fFont);
91 fMetrics=gImage.getFontMetrics();
94 void drawColumnLabels(
int iWidth) {
95 gImage.setColor(Color.darkGray);
96 gImage.drawLine(0,tp.iRowHeight,iWidth,tp.iRowHeight);
98 for(
int i=0;i<tp.iColCount;i++) {
99 int w=tp.iColWidth[i];
100 gImage.setColor(Color.lightGray);
101 gImage.fillRect(x+1,0,w,tp.iRowHeight);
102 gImage.setColor(Color.black);
103 if (tp.sColHead[i]!=null)
104 gImage.drawString(tp.sColHead[i],x+2,tp.iRowHeight-5);
105 if (tp.iColCount>1) {
106 gImage.setColor(Color.darkGray);
107 gImage.drawLine(x+w-1,0,x+w-1,tp.iRowHeight-1);
108 gImage.setColor(Color.white);
109 gImage.drawLine(x+w,0,x+w,tp.iRowHeight-1);
113 gImage.setColor(Color.lightGray);
114 gImage.fillRect(0,0,1,tp.iRowHeight);
115 gImage.fillRect(x+1,0,iWidth-x,tp.iRowHeight);
117 gImage.setColor(Color.darkGray);
118 gImage.drawLine(0,0,iWidth,0);
121 char[] getChars(
int column,
int row) {
122 if (row>=tp.vData.size())
124 char[] chars = (
char[])(tp.vData.elementAt(row));
128 if (tp.iColCount==1) {
129 for (
int i=0; i<chars.length; i++) {
138 int length = chars.length;
140 while (column>tabs) {
141 if (chars[start]==
'\t')
147 if (start<0 || start>=chars.length) {
148 System.out.println(
"start="+start+
", chars.length="+chars.length);
151 if (chars[start]==
'\t')
155 while (chars[end]!=
'\t' && end<(length-1))
157 if (chars[end]==
'\t')
160 char[] chars2 =
new char[end-start+1];
161 for (
int i=0,j=start; i<chars2.length; i++,j++) {
162 chars2[i] = chars[j];
168 void calcAutoWidth(
int column) {
169 if (tp.sColHead==null || column>=tp.iColWidth.length)
172 fMetrics=gImage.getFontMetrics();
179 if (column==0 && tp.sColHead[0].equals(
" "))
182 char[] chars = tp.sColHead[column].toCharArray();
183 w = Math.max(w,fMetrics.charsWidth(chars,0,chars.length));
186 int rowCount = Math.min(tp.iRowCount, maxRows);
187 for(
int row=0; row<rowCount; row++) {
188 char[] chars = getChars(column,row);
190 w = Math.max(w,fMetrics.charsWidth(chars,0,chars.length));
192 tp.iColWidth[column] = w+15;