KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > Grid


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb.util;
68
69 import java.util.Vector JavaDoc;
70 import java.awt.Color JavaDoc;
71 import java.awt.Cursor JavaDoc;
72 import java.awt.Dimension JavaDoc;
73 import java.awt.Event JavaDoc;
74 import java.awt.Font JavaDoc;
75 import java.awt.FontMetrics JavaDoc;
76 import java.awt.Graphics JavaDoc;
77 import java.awt.Image JavaDoc;
78 import java.awt.Panel JavaDoc;
79 import java.awt.Scrollbar JavaDoc;
80 import java.awt.SystemColor JavaDoc;
81
82 // sqlbob@users 20020401 - patch 1.7.0 by sqlbob (RMP) - enhancements
83

84 /**
85  *
86  * @author Thomas Mueller (Hypersonic SQL Group)
87  * @version 1.8.0
88  * @since Hypersonic SQL
89  */

90 class Grid extends Panel JavaDoc {
91
92     // drawing
93
private Dimension JavaDoc dMinimum;
94
95 // boucherb@users changed access for databasemanager2
96
protected Font JavaDoc fFont;
97
98 // --------------------------------------------------
99
private FontMetrics JavaDoc fMetrics;
100     private Graphics JavaDoc gImage;
101     private Image JavaDoc iImage;
102
103     // height / width
104
private int iWidth, iHeight;
105     private int iRowHeight, iFirstRow;
106     private int iGridWidth, iGridHeight;
107     private int iX, iY;
108
109     // data
110
// boucherb@users changed access for databasemanager2
111
protected String JavaDoc[] sColHead = new String JavaDoc[0];
112     protected Vector JavaDoc vData = new Vector JavaDoc();
113
114 // --------------------------------------------------
115
private int[] iColWidth;
116     private int iColCount;
117
118 // boucherb@users changed access for databasemanager2
119
protected int iRowCount;
120
121 // --------------------------------------------------
122
// scrolling
123
private Scrollbar JavaDoc sbHoriz, sbVert;
124     private int iSbWidth, iSbHeight;
125     private boolean bDrag;
126     private int iXDrag, iColDrag;
127
128     /**
129      * Constructor declaration
130      *
131      */

132     public Grid() {
133
134         super();
135
136         fFont = new Font JavaDoc("Dialog", Font.PLAIN, 12);
137
138         setLayout(null);
139
140         sbHoriz = new Scrollbar JavaDoc(Scrollbar.HORIZONTAL);
141
142         add(sbHoriz);
143
144         sbVert = new Scrollbar JavaDoc(Scrollbar.VERTICAL);
145
146         add(sbVert);
147     }
148
149     /**
150      * Method declaration
151      *
152      *
153      * @return
154      */

155     String JavaDoc[] getHead() {
156         return sColHead;
157     }
158
159     /**
160      * Method declaration
161      *
162      *
163      * @return
164      */

165     Vector JavaDoc getData() {
166         return vData;
167     }
168
169     /**
170      * Method declaration
171      *
172      *
173      * @param d
174      */

175     public void setMinimumSize(Dimension JavaDoc d) {
176         dMinimum = d;
177     }
178
179     /**
180      * Method declaration
181      *
182      *
183      * @param x
184      * @param y
185      * @param w
186      * @param h
187      */

188     public void setBounds(int x, int y, int w, int h) {
189
190         // fredt@users 20011210 - patch 450412 by elise@users
191
super.setBounds(x, y, w, h);
192
193         iSbHeight = sbHoriz.getPreferredSize().height;
194         iSbWidth = sbVert.getPreferredSize().width;
195         iHeight = h - iSbHeight;
196         iWidth = w - iSbWidth;
197
198         sbHoriz.setBounds(0, iHeight, iWidth, iSbHeight);
199         sbVert.setBounds(iWidth, 0, iSbWidth, iHeight);
200         adjustScroll();
201
202         iImage = null;
203
204         repaint();
205     }
206
207     /**
208      * Method declaration
209      *
210      *
211      * @param head
212      */

213     public void setHead(String JavaDoc[] head) {
214
215         iColCount = head.length;
216         sColHead = new String JavaDoc[iColCount];
217         iColWidth = new int[iColCount];
218
219         for (int i = 0; i < iColCount; i++) {
220             sColHead[i] = head[i];
221             iColWidth[i] = 100;
222         }
223
224         iRowCount = 0;
225         iRowHeight = 0;
226         vData = new Vector JavaDoc();
227     }
228
229     /**
230      * Method declaration
231      *
232      *
233      * @param data
234      */

235     public void addRow(String JavaDoc[] data) {
236
237         if (data.length != iColCount) {
238             return;
239         }
240
241         String JavaDoc[] row = new String JavaDoc[iColCount];
242
243         for (int i = 0; i < iColCount; i++) {
244             row[i] = data[i];
245
246             if (row[i] == null) {
247                 row[i] = "(null)";
248             }
249         }
250
251         vData.addElement(row);
252
253         iRowCount++;
254     }
255
256     /**
257      * Method declaration
258      *
259      */

260     public void update() {
261         adjustScroll();
262         repaint();
263     }
264
265     /**
266      * Method declaration
267      *
268      */

269     void adjustScroll() {
270
271         if (iRowHeight == 0) {
272             return;
273         }
274
275         int w = 0;
276
277         for (int i = 0; i < iColCount; i++) {
278             w += iColWidth[i];
279         }
280
281         iGridWidth = w;
282         iGridHeight = iRowHeight * (iRowCount + 1);
283
284         sbHoriz.setValues(iX, iWidth, 0, iGridWidth);
285
286         int v = iY / iRowHeight,
287             h = iHeight / iRowHeight;
288
289         sbVert.setValues(v, h, 0, iRowCount + 1);
290
291         iX = sbHoriz.getValue();
292         iY = iRowHeight * sbVert.getValue();
293     }
294
295     /**
296      * Method declaration
297      *
298      *
299      * @param e
300      *
301      * @return
302      */

303
304     // fredt@users 20020130 - comment by fredt
305
// to remove this deprecated method we need to rewrite the Grid class as a
306
// ScrollPane component
307
// sqlbob: I believe that changing to the JDK1.1 event handler
308
// would require browsers to use the Java plugin.
309
public boolean handleEvent(Event JavaDoc e) {
310
311         switch (e.id) {
312
313             case Event.SCROLL_LINE_UP :
314             case Event.SCROLL_LINE_DOWN :
315             case Event.SCROLL_PAGE_UP :
316             case Event.SCROLL_PAGE_DOWN :
317             case Event.SCROLL_ABSOLUTE :
318                 iX = sbHoriz.getValue();
319                 iY = iRowHeight * sbVert.getValue();
320
321                 repaint();
322
323                 return true;
324         }
325
326         return super.handleEvent(e);
327     }
328
329     /**
330      * Method declaration
331      *
332      *
333      * @param g
334      */

335     public void paint(Graphics JavaDoc g) {
336
337         if (g == null) {
338             return;
339         }
340
341         if (sColHead.length == 0) {
342             super.paint(g);
343
344             return;
345         }
346
347         if (iWidth <= 0 || iHeight <= 0) {
348             return;
349         }
350
351         g.setColor(SystemColor.control);
352         g.fillRect(iWidth, iHeight, iSbWidth, iSbHeight);
353
354         if (iImage == null) {
355             iImage = createImage(iWidth, iHeight);
356             gImage = iImage.getGraphics();
357
358             gImage.setFont(fFont);
359
360             if (fMetrics == null) {
361                 fMetrics = gImage.getFontMetrics();
362             }
363         }
364
365         if (iRowHeight == 0) {
366             iRowHeight = getMaxHeight(fMetrics);
367
368             for (int i = 0; i < iColCount; i++) {
369                 calcAutoWidth(i);
370             }
371
372             adjustScroll();
373         }
374
375         gImage.setColor(Color.white);
376         gImage.fillRect(0, 0, iWidth, iHeight);
377         gImage.setColor(Color.darkGray);
378         gImage.drawLine(0, iRowHeight, iWidth, iRowHeight);
379
380         int x = -iX;
381
382         for (int i = 0; i < iColCount; i++) {
383             int w = iColWidth[i];
384
385             gImage.setColor(SystemColor.control);
386             gImage.fillRect(x + 1, 0, w - 2, iRowHeight);
387             gImage.setColor(Color.black);
388             gImage.drawString(sColHead[i], x + 2, iRowHeight - 5);
389             gImage.setColor(Color.darkGray);
390             gImage.drawLine(x + w - 1, 0, x + w - 1, iRowHeight - 1);
391             gImage.setColor(Color.white);
392             gImage.drawLine(x + w, 0, x + w, iRowHeight - 1);
393
394             x += w;
395         }
396
397         gImage.setColor(SystemColor.control);
398         gImage.fillRect(0, 0, 1, iRowHeight);
399         gImage.fillRect(x + 1, 0, iWidth - x, iRowHeight);
400         gImage.drawLine(0, 0, 0, iRowHeight - 1);
401
402         int y = iRowHeight + 1 - iY;
403         int j = 0;
404
405         while (y < iRowHeight + 1) {
406             j++;
407
408             y += iRowHeight;
409         }
410
411         iFirstRow = j;
412         y = iRowHeight + 1;
413
414         for (; y < iHeight && j < iRowCount; j++, y += iRowHeight) {
415             x = -iX;
416
417             for (int i = 0; i < iColCount; i++) {
418                 int w = iColWidth[i];
419                 Color JavaDoc b = Color.white,
420                       t = Color.black;
421
422                 gImage.setColor(b);
423                 gImage.fillRect(x, y, w - 1, iRowHeight - 1);
424                 gImage.setColor(t);
425                 gImage.drawString(getDisplay(i, j), x + 2,
426                                   y + iRowHeight - 5);
427                 gImage.setColor(Color.lightGray);
428                 gImage.drawLine(x + w - 1, y, x + w - 1, y + iRowHeight - 1);
429                 gImage.drawLine(x, y + iRowHeight - 1, x + w - 1,
430                                 y + iRowHeight - 1);
431
432                 x += w;
433             }
434
435             gImage.setColor(Color.white);
436             gImage.fillRect(x, y, iWidth - x, iRowHeight - 1);
437         }
438
439         g.drawImage(iImage, 0, 0, this);
440     }
441
442     /**
443      * Method declaration
444      *
445      *
446      * @param g
447      */

448     public void update(Graphics JavaDoc g) {
449         paint(g);
450     }
451
452     /**
453      * Method declaration
454      *
455      *
456      * @param e
457      * @param x
458      * @param y
459      *
460      * @return
461      */

462     public boolean mouseMove(Event JavaDoc e, int x, int y) {
463
464         if (y <= iRowHeight) {
465             int xb = x;
466
467             x += iX - iGridWidth;
468
469             int i = iColCount - 1;
470
471             for (; i >= 0; i--) {
472                 if (x > -7 && x < 7) {
473                     break;
474                 }
475
476                 x += iColWidth[i];
477             }
478
479             if (i >= 0) {
480                 if (!bDrag) {
481                     setCursor(new Cursor JavaDoc(Cursor.E_RESIZE_CURSOR));
482
483                     bDrag = true;
484                     iXDrag = xb - iColWidth[i];
485                     iColDrag = i;
486                 }
487
488                 return true;
489             }
490         }
491
492         return mouseExit(e, x, y);
493     }
494
495     /**
496      * Method declaration
497      *
498      *
499      * @param e
500      * @param x
501      * @param y
502      *
503      * @return
504      */

505     public boolean mouseDrag(Event JavaDoc e, int x, int y) {
506
507         if (bDrag && x < iWidth) {
508             int w = x - iXDrag;
509
510             if (w < 0) {
511                 w = 0;
512             }
513
514             iColWidth[iColDrag] = w;
515
516             adjustScroll();
517             repaint();
518         }
519
520         return true;
521     }
522
523     /**
524      * Method declaration
525      *
526      *
527      * @param e
528      * @param x
529      * @param y
530      *
531      * @return
532      */

533     public boolean mouseExit(Event JavaDoc e, int x, int y) {
534
535         if (bDrag) {
536             setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
537
538             bDrag = false;
539         }
540
541         return true;
542     }
543
544     /**
545      * Method declaration
546      *
547      *
548      * @return
549      */

550     public Dimension JavaDoc preferredSize() {
551         return dMinimum;
552     }
553
554     /**
555      * Method declaration
556      *
557      *
558      * @return
559      */

560     public Dimension JavaDoc getPreferredSize() {
561         return dMinimum;
562     }
563
564     /**
565      * Method declaration
566      *
567      *
568      * @return
569      */

570     public Dimension JavaDoc getMinimumSize() {
571         return dMinimum;
572     }
573
574     /**
575      * Method declaration
576      *
577      *
578      * @return
579      */

580     public Dimension JavaDoc minimumSize() {
581         return dMinimum;
582     }
583
584     /**
585      * Method declaration
586      *
587      *
588      * @param i
589      */

590     private void calcAutoWidth(int i) {
591
592         int w = 10;
593
594         w = Math.max(w, fMetrics.stringWidth(sColHead[i]));
595
596         for (int j = 0; j < iRowCount; j++) {
597             String JavaDoc[] s = (String JavaDoc[]) (vData.elementAt(j));
598
599             w = Math.max(w, fMetrics.stringWidth(s[i]));
600         }
601
602         iColWidth[i] = w + 6;
603     }
604
605     /**
606      * Method declaration
607      *
608      *
609      * @param x
610      * @param y
611      *
612      * @return
613      */

614     private String JavaDoc getDisplay(int x, int y) {
615         return (((String JavaDoc[]) (vData.elementAt(y)))[x]);
616     }
617
618     /**
619      * Method declaration
620      *
621      *
622      * @param x
623      * @param y
624      *
625      * @return
626      */

627     private String JavaDoc get(int x, int y) {
628         return (((String JavaDoc[]) (vData.elementAt(y)))[x]);
629     }
630
631     /**
632      * Method declaration
633      *
634      *
635      * @param f
636      *
637      * @return
638      */

639     private static int getMaxHeight(FontMetrics JavaDoc f) {
640         return f.getHeight() + 4;
641     }
642 }
643
Popular Tags