1 7 package org.ejtools.adwt.service; 8 9 import java.awt.Component ; 10 import java.awt.Dimension ; 11 import java.awt.Insets ; 12 import java.awt.Point ; 13 import java.awt.Rectangle ; 14 import java.beans.PropertyVetoException ; 15 16 import javax.swing.DefaultDesktopManager ; 17 import javax.swing.JComponent ; 18 import javax.swing.JDesktopPane ; 19 import javax.swing.JInternalFrame ; 20 import javax.swing.JScrollPane ; 21 import javax.swing.JViewport ; 22 import javax.swing.border.Border ; 23 24 33 public class MDIDesktopPane extends JDesktopPane 34 { 35 36 protected MDIDesktopManager manager; 37 38 protected static int FRAME_OFFSET = 30; 39 40 41 42 public MDIDesktopPane() 43 { 44 this.manager = new MDIDesktopManager(this); 45 this.setDesktopManager(manager); 46 this.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); 47 } 48 49 50 56 public Component add(JInternalFrame frame) 57 { 58 JInternalFrame [] frames = this.getAllFrames(); 59 Point p; 60 int w; 61 int h; 62 63 Component retval = super.add(frame); 64 this.checkDesktopSize(); 65 66 if (frames.length > 0) 67 { 68 p = frames[0].getLocation(); 69 p.x = p.x + FRAME_OFFSET; 70 p.y = p.y + FRAME_OFFSET; 71 } 72 else 73 { 74 p = new Point (0, 0); 75 } 76 frame.setLocation(p.x, p.y); 77 if (frame.isResizable()) 78 { 79 w = getWidth() - (getWidth() / 3); 80 h = getHeight() - (getHeight() / 3); 81 if (w < frame.getMinimumSize().getWidth()) 82 { 83 w = (int) frame.getMinimumSize().getWidth(); 84 } 85 if (h < frame.getMinimumSize().getHeight()) 86 { 87 h = (int) frame.getMinimumSize().getHeight(); 88 } 89 frame.setSize(w, h); 90 } 91 92 this.moveToFront(frame); 93 frame.setVisible(true); 94 try 95 { 96 frame.setSelected(true); 97 } 98 catch (PropertyVetoException e) 99 { 100 frame.toBack(); 101 } 102 103 return retval; 104 } 105 106 107 108 public void cascadeFrames() 109 { 110 DesktopArranger.setCurrent(DesktopArranger.CASCADE); 111 this.arrangeFrames(); 112 } 113 114 115 120 public void remove(Component c) 121 { 122 super.remove(c); 123 this.checkDesktopSize(); 124 } 126 127 128 133 public void setAllSize(Dimension d) 134 { 135 this.setMinimumSize(d); 136 this.setMaximumSize(d); 137 this.setPreferredSize(d); 138 } 139 140 141 147 public void setAllSize(int width, int height) 148 { 149 this.setAllSize(new Dimension (width, height)); 150 } 151 152 153 161 public void setBounds(int x, int y, int w, int h) 162 { 163 super.setBounds(x, y, w, h); 164 this.checkDesktopSize(); 165 } 166 167 168 169 public void tileFrames() 170 { 171 DesktopArranger.setCurrent(DesktopArranger.TILES); 172 this.arrangeFrames(); 173 } 174 175 176 177 private void arrangeFrames() 178 { 179 JInternalFrame frames[] = getAllFrames(); 180 DesktopArranger.getCurrent().arrange(this.manager, frames, this.getBounds()); 181 } 182 183 184 185 private void checkDesktopSize() 186 { 187 if (this.getParent() != null && this.isVisible()) 188 { 189 this.manager.resizeDesktop(); 190 } 191 } 192 193 194 200 protected class MDIDesktopManager extends DefaultDesktopManager 201 { 202 203 protected MDIDesktopPane desktop; 204 205 206 211 public MDIDesktopManager(MDIDesktopPane desktop) 212 { 213 this.desktop = desktop; 214 } 215 216 217 222 public void endDraggingFrame(JComponent f) 223 { 224 super.endDraggingFrame(f); 225 this.resizeDesktop(); 226 } 227 228 229 234 public void endResizingFrame(JComponent f) 235 { 236 super.endResizingFrame(f); 237 this.resizeDesktop(); 238 } 239 240 241 242 public void setNormalSize() 243 { 244 JScrollPane scrollPane = this.getScrollPane(); 245 int x = 0; 246 int y = 0; 247 Insets scrollInsets = this.getScrollPaneInsets(); 248 249 if (scrollPane != null) 250 { 251 Dimension d = scrollPane.getVisibleRect().getSize(); 252 if (scrollPane.getBorder() != null) 253 { 254 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, 255 d.getHeight() - scrollInsets.top - scrollInsets.bottom); 256 } 257 258 d.setSize(d.getWidth() - 20, d.getHeight() - 20); 259 this.desktop.setAllSize(x, y); 260 scrollPane.invalidate(); 261 scrollPane.validate(); 262 } 263 } 264 265 266 267 protected void resizeDesktop() 268 { 269 int x = 0; 270 int y = 0; 271 JScrollPane scrollPane = this.getScrollPane(); 272 Insets scrollInsets = this.getScrollPaneInsets(); 273 274 if (scrollPane != null) 275 { 276 JInternalFrame frames[] = this.desktop.getAllFrames(); 277 for (int i = 0; i < frames.length; i++) 278 { 279 if (frames[i].getX() + frames[i].getWidth() > x) 280 { 281 x = frames[i].getX() + frames[i].getWidth(); 282 } 283 if (frames[i].getY() + frames[i].getHeight() > y) 284 { 285 y = frames[i].getY() + frames[i].getHeight(); 286 } 287 } 288 Dimension d = scrollPane.getVisibleRect().getSize(); 289 if (scrollPane.getBorder() != null) 290 { 291 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, 292 d.getHeight() - scrollInsets.top - scrollInsets.bottom); 293 } 294 295 if (x <= d.getWidth()) 296 { 297 x = ((int) d.getWidth()) - 20; 298 } 299 if (y <= d.getHeight()) 300 { 301 y = ((int) d.getHeight()) - 20; 302 } 303 this.desktop.setAllSize(x, y); 304 scrollPane.invalidate(); 305 scrollPane.validate(); 306 } 307 } 308 309 310 315 private JScrollPane getScrollPane() 316 { 317 if (this.desktop.getParent() instanceof JViewport ) 318 { 319 JViewport viewPort = (JViewport ) this.desktop.getParent(); 320 if (viewPort.getParent() instanceof JScrollPane ) 321 { 322 return (JScrollPane ) viewPort.getParent(); 323 } 324 } 325 return null; 326 } 327 328 329 334 private Insets getScrollPaneInsets() 335 { 336 JScrollPane scrollPane = this.getScrollPane(); 337 if (scrollPane != null) 338 { 339 Border border = this.getScrollPane().getBorder(); 340 if (border != null) 341 { 342 return border.getBorderInsets(scrollPane); 343 } 344 } 345 return new Insets (0, 0, 0, 0); 346 } 347 } 348 349 350 356 protected static class CascadeArranger extends DesktopArranger 357 { 358 359 private static int FRAME_OFFSET = 30; 360 361 362 363 protected CascadeArranger() { } 364 365 366 371 public void arrange(MDIDesktopManager manager, JInternalFrame [] frames, Rectangle bounds) 372 { 373 manager.setNormalSize(); 374 375 int x = 0; 376 int y = 0; 377 378 int frameWidth = bounds.width - frames.length * FRAME_OFFSET; 379 if (frameWidth < (bounds.width / 2)) 380 { 381 frameWidth = bounds.width / 2; 382 } 383 384 int frameHeight = bounds.height - frames.length * FRAME_OFFSET; 385 if (frameHeight < (bounds.height / 2)) 386 { 387 frameHeight = bounds.height / 2; 388 } 389 390 for (int i = frames.length - 1; i >= 0; i--) 391 { 392 if (!frames[i].isIcon()) 393 { 394 frames[i].setSize(frameWidth, frameHeight); 395 frames[i].setLocation(x, y); 396 x = x + FRAME_OFFSET; 397 y = y + FRAME_OFFSET; 398 if ((x > (bounds.width - frameWidth)) || (y > (bounds.height - frameHeight))) 399 { 400 x = 0; 401 y = 0; 402 } 403 } 404 } 405 } 406 } 407 408 409 415 protected abstract static class DesktopArranger 416 { 417 418 private static DesktopArranger current; 419 420 public final static DesktopArranger CASCADE = new CascadeArranger(); 421 422 public final static DesktopArranger MAXIMIZE = new MaximizeArranger(); 423 424 public final static DesktopArranger TILES = new TileArranger(); 425 426 427 428 protected DesktopArranger() { } 429 430 431 438 public abstract void arrange(MDIDesktopManager manager, JInternalFrame [] frames, Rectangle bounds); 439 440 441 446 public static DesktopArranger getCurrent() 447 { 448 if (DesktopArranger.current == null) 449 { 450 return DesktopArranger.CASCADE; 451 } 452 return current; 453 } 454 455 456 461 public static void setCurrent(DesktopArranger arranger) 462 { 463 DesktopArranger.current = arranger; 464 } 465 } 466 467 468 474 protected static class MaximizeArranger extends DesktopArranger 475 { 476 477 protected MaximizeArranger() { } 478 479 480 485 public void arrange(MDIDesktopManager manager, JInternalFrame [] frames, Rectangle bounds) 486 { 487 manager.setNormalSize(); 488 try 489 { 490 for (int i = 0; i < frames.length; i++) 491 { 492 frames[i].setMaximum(true); 493 } 494 } 495 catch (PropertyVetoException pve) 496 { 497 } 499 } 500 } 501 502 503 509 protected static class TileArranger extends DesktopArranger 510 { 511 512 protected TileArranger() { } 513 514 515 520 public void arrange(MDIDesktopManager manager, JInternalFrame [] frames, Rectangle bounds) 521 { 522 int totalNonIconFrames = 0; 523 524 for (int i = 0; i < frames.length; i++) 525 { 526 if (!frames[i].isIcon()) 528 { 529 totalNonIconFrames++; 530 } 531 } 532 533 int curCol = 0; 534 int curRow = 0; 535 int i = 0; 536 537 if (totalNonIconFrames > 0) 538 { 539 int numCols = (int) Math.sqrt(totalNonIconFrames); 541 542 int frameWidth = bounds.width / numCols; 543 for (curCol = 0; curCol < numCols; curCol++) 544 { 545 int numRows = totalNonIconFrames / numCols; 546 int remainder = totalNonIconFrames % numCols; 547 548 if ((numCols - curCol) <= remainder) 549 { 550 numRows++; 551 } 553 554 int frameHeight = bounds.height / numRows; 555 for (curRow = 0; curRow < numRows; curRow++) 556 { 557 while (frames[i].isIcon()) 558 { 559 i++; 561 } 562 563 frames[i].setBounds(curCol * frameWidth, curRow * frameHeight, frameWidth, frameHeight); 564 i++; 565 } 566 } 567 } 568 } 569 } 570 571 } 572 573 | Popular Tags |