1 65 66 67 package org.hsqldb.util; 68 69 import java.util.Vector ; 70 import java.awt.Color ; 71 import java.awt.Cursor ; 72 import java.awt.Dimension ; 73 import java.awt.Event ; 74 import java.awt.Font ; 75 import java.awt.FontMetrics ; 76 import java.awt.Graphics ; 77 import java.awt.Image ; 78 import java.awt.Panel ; 79 import java.awt.Scrollbar ; 80 import java.awt.SystemColor ; 81 82 84 90 class Grid extends Panel { 91 92 private Dimension dMinimum; 94 95 protected Font fFont; 97 98 private FontMetrics fMetrics; 100 private Graphics gImage; 101 private Image iImage; 102 103 private int iWidth, iHeight; 105 private int iRowHeight, iFirstRow; 106 private int iGridWidth, iGridHeight; 107 private int iX, iY; 108 109 protected String [] sColHead = new String [0]; 112 protected Vector vData = new Vector (); 113 114 private int[] iColWidth; 116 private int iColCount; 117 118 protected int iRowCount; 120 121 private Scrollbar sbHoriz, sbVert; 124 private int iSbWidth, iSbHeight; 125 private boolean bDrag; 126 private int iXDrag, iColDrag; 127 128 132 public Grid() { 133 134 super(); 135 136 fFont = new Font ("Dialog", Font.PLAIN, 12); 137 138 setLayout(null); 139 140 sbHoriz = new Scrollbar (Scrollbar.HORIZONTAL); 141 142 add(sbHoriz); 143 144 sbVert = new Scrollbar (Scrollbar.VERTICAL); 145 146 add(sbVert); 147 } 148 149 155 String [] getHead() { 156 return sColHead; 157 } 158 159 165 Vector getData() { 166 return vData; 167 } 168 169 175 public void setMinimumSize(Dimension d) { 176 dMinimum = d; 177 } 178 179 188 public void setBounds(int x, int y, int w, int h) { 189 190 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 213 public void setHead(String [] head) { 214 215 iColCount = head.length; 216 sColHead = new String [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 (); 227 } 228 229 235 public void addRow(String [] data) { 236 237 if (data.length != iColCount) { 238 return; 239 } 240 241 String [] row = new String [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 260 public void update() { 261 adjustScroll(); 262 repaint(); 263 } 264 265 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 303 304 public boolean handleEvent(Event 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 335 public void paint(Graphics 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 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 448 public void update(Graphics g) { 449 paint(g); 450 } 451 452 462 public boolean mouseMove(Event 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 (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 505 public boolean mouseDrag(Event 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 533 public boolean mouseExit(Event e, int x, int y) { 534 535 if (bDrag) { 536 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 537 538 bDrag = false; 539 } 540 541 return true; 542 } 543 544 550 public Dimension preferredSize() { 551 return dMinimum; 552 } 553 554 560 public Dimension getPreferredSize() { 561 return dMinimum; 562 } 563 564 570 public Dimension getMinimumSize() { 571 return dMinimum; 572 } 573 574 580 public Dimension minimumSize() { 581 return dMinimum; 582 } 583 584 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 [] s = (String []) (vData.elementAt(j)); 598 599 w = Math.max(w, fMetrics.stringWidth(s[i])); 600 } 601 602 iColWidth[i] = w + 6; 603 } 604 605 614 private String getDisplay(int x, int y) { 615 return (((String []) (vData.elementAt(y)))[x]); 616 } 617 618 627 private String get(int x, int y) { 628 return (((String []) (vData.elementAt(y)))[x]); 629 } 630 631 639 private static int getMaxHeight(FontMetrics f) { 640 return f.getHeight() + 4; 641 } 642 } 643 | Popular Tags |