1 65 66 67 package org.hsqldb.util; 68 69 import java.util.Vector ; 70 71 import java.awt.Color ; 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 import java.awt.Toolkit ; 82 83 89 class Tree extends Panel { 90 91 private static Font fFont; 93 private static FontMetrics fMetrics; 94 private static int iRowHeight; 95 private static int iIndentWidth; 96 private int iMaxTextLength; 97 98 private Dimension dMinimum; 100 private Graphics gImage; 101 private Image iImage; 102 103 private int iWidth, iHeight; 105 private int iFirstRow; 106 private int iTreeWidth, iTreeHeight; 107 private int iX, iY; 108 109 private Vector vData; 111 private int iRowCount; 112 113 private Scrollbar sbHoriz, sbVert; 115 private int iSbWidth, iSbHeight; 116 117 static { 118 fFont = new Font ("Dialog", Font.PLAIN, 12); 119 fMetrics = Toolkit.getDefaultToolkit().getFontMetrics(fFont); 120 iRowHeight = getMaxHeight(fMetrics); 121 iIndentWidth = 12; 122 } 123 124 128 Tree() { 129 130 super(); 131 132 vData = new Vector (); 133 134 setLayout(null); 135 136 sbHoriz = new Scrollbar (Scrollbar.HORIZONTAL); 137 138 add(sbHoriz); 139 140 sbVert = new Scrollbar (Scrollbar.VERTICAL); 141 142 add(sbVert); 143 } 144 145 151 public void setMinimumSize(Dimension d) { 152 dMinimum = d; 153 } 154 155 164 165 public void setBounds(int x, int y, int w, int h) { 168 169 super.setBounds(x, y, w, h); 170 171 iSbHeight = sbHoriz.getPreferredSize().height; 172 iSbWidth = sbVert.getPreferredSize().width; 173 iHeight = h - iSbHeight; 174 iWidth = w - iSbWidth; 175 176 sbHoriz.setBounds(0, iHeight, iWidth, iSbHeight); 177 sbVert.setBounds(iWidth, 0, iSbWidth, iHeight); 178 adjustScroll(); 179 180 iImage = null; 181 182 repaint(); 183 } 184 185 189 public void removeAll() { 190 191 vData = new Vector (); 192 iRowCount = 0; 193 194 adjustScroll(); 195 196 iMaxTextLength = 10; 197 198 repaint(); 199 } 200 201 210 public void addRow(String key, String value, String state, int color) { 211 212 String [] row = new String [4]; 213 214 if (value == null) { 215 value = ""; 216 } 217 218 row[0] = key; 219 row[1] = value; 220 row[2] = state; row[3] = String.valueOf(color); 222 223 vData.addElement(row); 224 225 int len = fMetrics.stringWidth(value); 226 227 if (len > iMaxTextLength) { 228 iMaxTextLength = len; 229 } 230 231 iRowCount++; 232 } 233 234 241 public void addRow(String key, String value) { 242 addRow(key, value, null, 0); 243 } 244 245 249 public void update() { 250 adjustScroll(); 251 repaint(); 252 } 253 254 258 void adjustScroll() { 259 260 iTreeHeight = iRowHeight * (iRowCount + 1); 261 262 iTreeWidth = iMaxTextLength * 2; 264 265 sbHoriz.setValues(iX, iWidth, 0, iTreeWidth); 266 267 int v = iY / iRowHeight, 268 h = iHeight / iRowHeight; 269 270 sbVert.setValues(v, h, 0, iRowCount + 1); 271 272 iX = sbHoriz.getValue(); 273 iY = iRowHeight * sbVert.getValue(); 274 } 275 276 284 285 public boolean handleEvent(Event e) { 289 290 switch (e.id) { 291 292 case Event.SCROLL_LINE_UP : 293 case Event.SCROLL_LINE_DOWN : 294 case Event.SCROLL_PAGE_UP : 295 case Event.SCROLL_PAGE_DOWN : 296 case Event.SCROLL_ABSOLUTE : 297 iX = sbHoriz.getValue(); 298 iY = iRowHeight * sbVert.getValue(); 299 300 repaint(); 301 302 return true; 303 } 304 305 return super.handleEvent(e); 306 } 307 308 314 public void paint(Graphics g) { 315 316 if (g == null || iWidth <= 0 || iHeight <= 0) { 317 return; 318 } 319 320 g.setColor(SystemColor.control); 321 g.fillRect(iWidth, iHeight, iSbWidth, iSbHeight); 322 323 if (iImage == null) { 324 iImage = createImage(iWidth, iHeight); 325 gImage = iImage.getGraphics(); 326 327 gImage.setFont(fFont); 328 } 329 330 gImage.setColor(Color.white); 331 gImage.fillRect(0, 0, iWidth, iHeight); 332 333 int[] lasty = new int[100]; 334 String [] root = new String [100]; 335 336 root[0] = ""; 337 338 int currentindent = 0; 339 int y = iRowHeight; 340 341 y -= iY; 342 343 boolean closed = false; 344 345 for (int i = 0; i < iRowCount; i++) { 346 String [] s = (String []) vData.elementAt(i); 347 String key = s[0]; 348 String data = s[1]; 349 String folder = s[2]; 350 int ci = currentindent; 351 352 for (; ci > 0; ci--) { 353 if (key.startsWith(root[ci])) { 354 break; 355 } 356 } 357 358 if (root[ci].length() < key.length()) { 359 ci++; 360 } 361 362 if (closed && ci > currentindent) { 363 continue; 364 } 365 366 closed = folder != null && folder.equals("+"); 367 root[ci] = key; 368 369 int x = iIndentWidth * ci - iX; 370 371 gImage.setColor(Color.lightGray); 372 gImage.drawLine(x, y, x + iIndentWidth, y); 373 gImage.drawLine(x, y, x, lasty[ci]); 374 375 lasty[ci + 1] = y; 376 377 int py = y + iRowHeight / 3; 378 int px = x + iIndentWidth * 2; 379 380 if (folder != null) { 381 lasty[ci + 1] += 4; 382 383 int rgb = Integer.parseInt(s[3]); 384 385 gImage.setColor(rgb == 0 ? Color.white 386 : new Color (rgb)); 387 gImage.fillRect(x + iIndentWidth - 3, y - 3, 7, 7); 388 gImage.setColor(Color.black); 389 gImage.drawRect(x + iIndentWidth - 4, y - 4, 8, 8); 390 gImage.drawLine(x + iIndentWidth - 2, y, 391 x + iIndentWidth + 2, y); 392 393 if (folder.equals("+")) { 394 gImage.drawLine(x + iIndentWidth, y - 2, 395 x + iIndentWidth, y + 2); 396 } 397 } else { 398 px -= iIndentWidth; 399 } 400 401 gImage.setColor(Color.black); 402 gImage.drawString(data, px, py); 403 404 currentindent = ci; 405 y += iRowHeight; 406 } 407 408 g.drawImage(iImage, 0, 0, this); 409 } 410 411 417 public void update(Graphics g) { 418 paint(g); 419 } 420 421 427 public Dimension preferredSize() { 428 return dMinimum; 429 } 430 431 437 public Dimension getPreferredSize() { 438 return dMinimum; 439 } 440 441 447 public Dimension getMinimumSize() { 448 return dMinimum; 449 } 450 451 457 public Dimension minimumSize() { 458 return dMinimum; 459 } 460 461 471 public boolean mouseDown(Event e, int x, int y) { 472 473 if (iRowHeight == 0 || x > iWidth || y > iHeight) { 474 return true; 475 } 476 477 y += iRowHeight / 2; 478 479 String [] root = new String [100]; 480 481 root[0] = ""; 482 483 int currentindent = 0; 484 int cy = iRowHeight; 485 boolean closed = false; 486 int i = 0; 487 488 y += iY; 489 490 for (; i < iRowCount; i++) { 491 String [] s = (String []) vData.elementAt(i); 492 String key = s[0]; 493 String folder = s[2]; 494 int ci = currentindent; 495 496 for (; ci > 0; ci--) { 497 if (key.startsWith(root[ci])) { 498 break; 499 } 500 } 501 502 if (root[ci].length() < key.length()) { 503 ci++; 504 } 505 506 if (closed && ci > currentindent) { 507 continue; 508 } 509 510 if (cy <= y && cy + iRowHeight > y) { 511 break; 512 } 513 514 root[ci] = key; 515 closed = folder != null && folder.equals("+"); 516 currentindent = ci; 517 cy += iRowHeight; 518 } 519 520 if (i >= 0 && i < iRowCount) { 521 String [] s = (String []) vData.elementAt(i); 522 String folder = s[2]; 523 524 if (folder != null && folder.equals("+")) { 525 folder = "-"; 526 } else if (folder != null && folder.equals("-")) { 527 folder = "+"; 528 } 529 530 s[2] = folder; 531 532 vData.setElementAt(s, i); 533 repaint(); 534 } 535 536 return true; 537 } 538 539 547 private static int getMaxHeight(FontMetrics f) { 548 return f.getHeight() + 2; 549 } 550 } 551 | Popular Tags |