1 32 33 package it.businesslogic.ireport.gui.docking; 34 35 import it.businesslogic.ireport.gui.event.TabPaneChangedEvent; 36 import it.businesslogic.ireport.gui.event.TabPaneChangedListener; 37 import java.awt.Component ; 38 import java.awt.Dimension ; 39 import java.awt.datatransfer.DataFlavor ; 40 import java.awt.datatransfer.Transferable ; 41 import java.awt.dnd.DnDConstants ; 42 import java.awt.dnd.DropTarget ; 43 import java.awt.dnd.DropTargetContext ; 44 import java.awt.dnd.DropTargetDragEvent ; 45 import java.awt.dnd.DropTargetDropEvent ; 46 import java.awt.dnd.DropTargetEvent ; 47 import java.awt.dnd.DropTargetListener ; 48 import java.awt.event.ActionEvent ; 49 import java.awt.event.ActionListener ; 50 import java.beans.PropertyChangeListener ; 51 import java.util.HashMap ; 52 import java.util.Vector ; 53 import javax.swing.Icon ; 54 import javax.swing.JButton ; 55 import javax.swing.JPanel ; 56 import javax.swing.JSplitPane ; 57 import it.businesslogic.ireport.util.I18n; 58 59 63 public class DockingContainer extends javax.swing.JPanel implements TabPaneChangedListener, ActionListener { 64 65 public static final int INSERT_MODE_SHAREDPOSTION = 0; 66 public static final int INSERT_MODE_NEWPOSITION = 1; 67 68 private int oldWidth = 0; 69 private boolean compressed = false; 70 71 private Vector panelViews = new Vector (); 72 private Vector tabPanes = new Vector (); 73 private int positions = 0; 74 75 private DropTarget dropTarget; 76 private DropTargetListener dtListener; 77 78 public static final int POSITION_RIGHT = 0; 79 public static final int POSITION_LEFT = 1; 80 81 private int position = POSITION_LEFT; 82 83 private int preferredDividerLocation = -1; 84 private int realCurrentSize = 0; 85 86 87 public DockingContainer() { 88 this(POSITION_LEFT); 89 } 90 91 92 public DockingContainer(int position) { 93 initComponents(); 94 95 this.setSize(new Dimension (200,100)); 96 this.setPreferredSize(new Dimension (200,100)); 97 98 99 this.dtListener = new DTListener(); 100 102 this.setDropTarget( new DropTarget (this, 103 DnDConstants.ACTION_MOVE, 104 new DTListener())); 105 this.setBorder(null); 106 applyI18n(); 107 108 } 109 110 111 private JButton createButton(PanelView pv) 112 { 113 114 JButton jButton = new JButton (); 115 116 Icon icon = new VTextIcon(jButton1,pv.getName(), (getPosition() == POSITION_LEFT) ? VTextIcon.ROTATE_LEFT : VTextIcon.ROTATE_RIGHT); 117 jButton.setIcon( icon ); 118 jButton.setPreferredSize( new Dimension (20, icon.getIconHeight()+ 28)); 119 jButton.setMinimumSize( new Dimension (20, icon.getIconHeight()+ 28)); 120 121 jButton.setActionCommand( pv.getId()+"" ); 122 jButton.addActionListener( this ); 123 124 return jButton; 125 } 126 127 public void actionPerformed(ActionEvent e) { 128 129 String s= e.getActionCommand(); 131 try { 132 PanelView pv = getPanelViewById( Integer.parseInt( s )); 133 134 pv.setMinimized(false); 135 pv.setPosition( positions); 136 137 recreateAll(); 138 } catch (Exception ex) { 139 140 ex.printStackTrace(); 141 } 142 143 } 144 145 149 public void addPanel(String name, Component component, boolean closable) 150 { 151 insertPanel(positions, name, component, closable); 152 153 } 154 178 179 public boolean contains(Component c) 180 { 181 for (int i=0; i<getPanelViews().size(); ++i) 182 { 183 PanelView pv = (PanelView)getPanelViews().elementAt(i); 184 if (pv.getComponent() == c) 185 { 186 return true; 187 } 188 } 189 190 return false; 191 } 192 193 198 public PanelView getPanelView(Component c) 199 { 200 for (int i=0; i<getPanelViews().size(); ++i) 201 { 202 PanelView pv = (PanelView)getPanelViews().elementAt(i); 203 if (pv.getComponent() == c) 204 { 205 return pv; 206 } 207 } 208 209 return null; 210 } 211 212 217 public PanelView getPanelViewById(int id) 218 { 219 for (int i=0; i<getPanelViews().size(); ++i) 220 { 221 PanelView pv = (PanelView)getPanelViews().elementAt(i); 222 if (pv.getId() == id) 223 { 224 return pv; 225 } 226 } 227 228 return null; 229 } 230 231 public void insertPanel(int position, String name, Component component, int insertMode, boolean closable) 232 { 233 if (component == null) return; 234 if (name == null) name = "Panel"; 235 236 for (int i=0; i<getPanelViews().size(); ++i) 238 { 239 PanelView pv = (PanelView)getPanelViews().elementAt(i); 240 if (pv.getComponent() == component) 241 { 242 return; 243 } 244 } 245 246 248 253 if (position >= positions) 255 { 256 position = positions; 257 positions++; 258 } 259 else if (insertMode == INSERT_MODE_NEWPOSITION) 260 { 261 for (int i=0; i<getPanelViews().size(); ++i) 262 { 263 PanelView pv = (PanelView)getPanelViews().elementAt(i); 264 265 if (pv.getPosition() >= position) 266 { 267 pv.setPosition( pv.getPosition()+1 ); 268 } 269 } 270 positions++; 271 } 272 273 panelViews.add( new PanelView(name, component, position, closable) ); 274 recreateAll(); 275 276 } 277 281 public void insertPanel(int position, String name, Component component, boolean closable) 282 { 283 insertPanel(position, name, component, INSERT_MODE_NEWPOSITION, closable); 284 } 285 286 290 public void removePanel(Component component) 291 { 292 293 int position = -1; 294 for (int i=0; i<getPanelViews().size(); ++i) 295 { 296 PanelView pv = (PanelView)getPanelViews().elementAt(i); 297 298 if (pv.getComponent() == component) 299 { 300 position = pv.getPosition(); 301 getPanelViews().remove(pv); 303 break; 304 } 305 } 306 307 308 309 if (position >= 0) 311 { 312 boolean found = false; 313 for (int i=0; i<getPanelViews().size(); ++i) 314 { 315 PanelView pv = (PanelView)getPanelViews().elementAt(i); 316 if (pv.isMinimized()) continue; 317 if (pv.getPosition() == position) 318 { 319 found = true; 320 break; 321 } 322 } 323 324 if (!found) 325 { 326 for (int i=0; i<getPanelViews().size(); ++i) 327 { 328 PanelView pv = (PanelView)getPanelViews().elementAt(i); 329 if (pv.isMinimized()) continue; 330 if (pv.getPosition() > position) 331 { 332 pv.setPosition( pv.getPosition()-1); 333 } 334 } 335 positions--; 336 } 337 recreateAll(); 338 } 339 this.updateUI(); 340 } 341 342 343 private void normalizePositions() 344 { 345 HashMap map = new HashMap (); 347 int differentPositions = 0; 348 349 for (int i=0; i<getPanelViews().size(); ++i) 350 { 351 PanelView pv = (PanelView)getPanelViews().elementAt(i); 352 if (pv.isMinimized()) continue; 353 map.put(new Integer (pv.getPosition()),""); 354 } 355 356 differentPositions = map.size(); 357 358 for (int k=0; k<differentPositions; ++k) 359 { 360 361 while (getPanelCount(k) == 0) 362 { 363 downPosition(k); 365 } 366 } 367 368 this.positions = differentPositions; 369 } 370 371 374 private void downPosition(int minimumPosition) 375 { 376 for (int i=0; i<getPanelViews().size(); ++i) 377 { 378 PanelView pv = (PanelView)getPanelViews().elementAt(i); 379 if (pv.isMinimized()) continue; 380 if (pv.getPosition() > minimumPosition) 381 { 382 pv.setPosition( pv.getPosition()-1); 383 } 384 } 385 } 386 387 388 393 public void moveComponent(PanelView panelView, int newPosition) 394 { 395 moveComponent(panelView, newPosition, INSERT_MODE_NEWPOSITION); 396 } 397 398 403 public void moveComponent(PanelView panelView, int newPosition, int mode) 404 { 405 removePanel(panelView.getComponent()); 406 insertPanel(newPosition,panelView.getName(), panelView.getComponent(), mode, panelView.isClosable()); 407 } 408 409 414 public void mergePosition(int newPosition) 415 { 416 if (positions == 1) return; 417 if (newPosition >= positions) return; 418 if ( newPosition == 0 && positions>1) newPosition=1; 419 for (int i=0; i<getPanelViews().size(); ++i) 420 { 421 PanelView pv = (PanelView)getPanelViews().elementAt(i); 422 423 if (pv.isMinimized()) continue; 424 425 if (pv.getPosition() >= newPosition) 426 { 427 pv.setPosition( pv.getPosition()-1); 428 } 429 430 } 431 432 positions--; 433 recreateAll(); 434 } 435 436 437 438 public void recreateAll() 439 { 440 this.removeAll(); 441 442 443 jPanel1.removeAll(); 444 445 int numPositions = positions; 446 447 normalizePositions(); 448 449 if (positions > 0) 450 { 451 if (compressed) 452 { 453 compressed = false; 454 this.setSize(getOldWidth(), 1); 455 } 456 457 458 tabPanes.removeAllElements(); 459 Vector tabbedPanes = new Vector (); 460 if (positions == 0) return; 461 462 add( addComponents(0),java.awt.BorderLayout.CENTER); 463 this.updateUI(); 464 } 465 else 466 { 467 compressed = true; 468 setOldWidth( getSize().width); 469 this.setSize(0, 1); 470 } 471 472 int minimizedButton = 0; 473 for (int i=0; i<getPanelViews().size(); ++i) 474 { 475 PanelView pv = (PanelView)getPanelViews().elementAt(i); 476 if (pv.isMinimized()) 477 { 478 JButton b = createButton(pv); 479 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 480 gridBagConstraints.gridx = 0; 481 gridBagConstraints.weightx = 1.0; 482 gridBagConstraints.weighty = 0; 483 jPanel1.add(b,gridBagConstraints); 484 minimizedButton++; 485 } 486 } 487 488 if (minimizedButton > 0) 489 { 490 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 491 gridBagConstraints.gridx = 0; 492 gridBagConstraints.weightx = 1.0; 493 gridBagConstraints.weighty = 1.0; 494 jPanel1.add(new JPanel (), gridBagConstraints); 495 add( jPanel1, (getPosition() == POSITION_LEFT) ? java.awt.BorderLayout.WEST : java.awt.BorderLayout.EAST); 496 } 497 498 if (this.getParent() != null && 499 this.getParent() instanceof JSplitPane ) 500 { 501 JSplitPane sp = (JSplitPane )this.getParent(); 502 int currentWidth = realCurrentSize; 503 if (positions == 0) 504 { 505 506 508 if (getPosition() == POSITION_LEFT) 509 { 510 sp.setDividerLocation( jPanel1.getWidth() ); 511 } 512 else 513 { 514 515 this.setSize(new Dimension (24,0)); 517 sp.setDividerLocation( (int)(sp.getBounds().getWidth())-sp.getDividerSize()- 24 ); 518 } 519 } 520 else 521 { 522 int width = currentWidth; 523 if (width <= 150 + 24) 524 { 525 width = 150+ 24; 526 } 527 if (getPosition() == POSITION_LEFT) 531 { 532 sp.setDividerLocation( width ); 533 } 534 else 535 { 536 sp.setDividerLocation( getPreferredDividerLocation() - ( width - currentWidth)); 537 538 539 } 542 } 543 544 sp.updateUI(); 545 546 } 547 this.updateUI(); 548 } 549 550 public void setSelectedComponent(Component component) 551 { 552 for (int i=0; i<tabPanes.size(); ++i) 553 { 554 JDraggableTabbedPane tab = (JDraggableTabbedPane)tabPanes.get(i); 555 556 try { 557 tab.setSelectedComponent( component ); 558 break; 559 } catch (Exception ex) {} 560 } 561 } 562 563 public Component addComponents(int pos) 564 { 565 566 JDraggableTabbedPane jTabbedPane = new JDraggableTabbedPane(); 567 jTabbedPane.setPosition(pos); 568 jTabbedPane.setOrientation( getPosition()); 569 jTabbedPane.setDockingContainer(this); 570 jTabbedPane.addTabPaneChangedListener( this ); 571 572 tabPanes.add(jTabbedPane); 573 574 575 for (int i=0; i<getPanelViews().size(); ++i) 576 { 577 PanelView pv = (PanelView)getPanelViews().elementAt(i); 578 if (pv.isMinimized()) continue; 579 if (pv.getPosition() == pos) 580 { 581 jTabbedPane.addTab(pv.getName(), pv.getComponent(), pv.isClosable()); 582 } 583 584 } 586 587 if (pos < positions-1) 588 { 589 javax.swing.JSplitPane jSplitPane = new javax.swing.JSplitPane (); 590 jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); 591 jSplitPane.setBorder(null); 592 594 jSplitPane.setResizeWeight( 1.0/(positions-pos)); 595 jSplitPane.setLeftComponent(jTabbedPane); 596 jSplitPane.setRightComponent( addComponents(pos+1) ); 597 jSplitPane.setDividerSize(5); 598 return jSplitPane; 599 } 600 else 601 { 602 return jTabbedPane; 603 } 604 } 605 606 607 public Vector getPanelViews() { 608 return panelViews; 609 } 610 611 public int getPanelCount(int position) 612 { 613 int p_number = 0; 614 for (int i=0; i<getPanelViews().size(); ++i) 615 { 616 PanelView pv = (PanelView)getPanelViews().elementAt(i); 617 if (pv.isMinimized()) continue; 618 if (pv.getPosition() == position) 619 { 620 p_number++; 621 } 622 } 623 624 return p_number; 625 } 626 627 public void setPanelViews(Vector panelViews) { 628 this.panelViews = panelViews; 629 } 630 631 public int getOldWidth() { 632 return oldWidth; 633 } 634 635 public void setOldWidth(int oldWidth) { 636 this.oldWidth = oldWidth; 637 } 638 639 public boolean isCompressed() { 640 return compressed; 641 } 642 643 public void setCompressed(boolean compressed) { 644 this.compressed = compressed; 645 } 646 647 652 private void initComponents() { 654 java.awt.GridBagConstraints gridBagConstraints; 655 656 jPanel1 = new javax.swing.JPanel (); 657 jButton1 = new javax.swing.JButton (); 658 659 setLayout(new java.awt.BorderLayout ()); 660 661 addComponentListener(new java.awt.event.ComponentAdapter () { 662 public void componentResized(java.awt.event.ComponentEvent evt) { 663 formComponentResized(evt); 664 } 665 }); 666 667 jPanel1.setLayout(new java.awt.GridBagLayout ()); 668 669 jPanel1.setPreferredSize(new java.awt.Dimension (24, 10)); 670 jButton1.setBorder(null); 671 jButton1.setBorderPainted(false); 672 jButton1.setFocusPainted(false); 673 jButton1.setFocusable(false); 674 jButton1.setMaximumSize(new java.awt.Dimension (33, 500)); 675 jButton1.setMinimumSize(new java.awt.Dimension (33, 100)); 676 jButton1.setPreferredSize(new java.awt.Dimension (33, 100)); 677 jButton1.setRequestFocusEnabled(false); 678 gridBagConstraints = new java.awt.GridBagConstraints (); 679 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 680 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 681 gridBagConstraints.weightx = 1.0; 682 gridBagConstraints.weighty = 1.0; 683 jPanel1.add(jButton1, gridBagConstraints); 684 685 add(jPanel1, java.awt.BorderLayout.WEST); 686 687 } 689 private void formComponentResized(java.awt.event.ComponentEvent evt) { 691 realCurrentSize = (int)evt.getComponent().getSize().getWidth(); 692 } 695 public void tabPaneChanged(TabPaneChangedEvent evt) { 696 697 if (evt.getOperation() == evt.CLOSED) 698 { 699 this.removePanel( evt.getTabComponent() ); 700 701 } 702 else if (evt.getOperation() == evt.MINIMIZED) 703 { 704 PanelView pv = getPanelView(evt.getTabComponent()); 706 pv.setMinimized(true); 707 recreateAll(); 708 } 709 710 } 711 712 713 private javax.swing.JButton jButton1; 715 private javax.swing.JPanel jPanel1; 716 718 class DTListener implements DropTargetListener { 719 720 721 722 726 public void dragEnter(DropTargetDragEvent e) { 727 e.acceptDrag(e.getDropAction()); 728 } 729 730 734 public void dragOver(DropTargetDragEvent e) { 735 e.acceptDrag(e.getDropAction()); 736 } 737 738 public void dropActionChanged(DropTargetDragEvent e) { 739 } 740 741 public void dragExit(DropTargetEvent e) { 742 } 743 744 755 public void drop(DropTargetDropEvent dtde) { 756 try { 757 758 DropTargetContext context = dtde.getDropTargetContext(); 759 760 Transferable tr = dtde.getTransferable(); 761 762 DataFlavor [] df = tr.getTransferDataFlavors(); 763 764 if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.gui.docking.PanelView")) 765 { 766 java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor (it.businesslogic.ireport.gui.docking.PanelView.class, it.businesslogic.ireport.gui.docking.PanelView.class.getName()); 767 it.businesslogic.ireport.gui.docking.PanelView panelView = (it.businesslogic.ireport.gui.docking.PanelView)tr.getTransferData( myFlavor ); 768 769 if (DockingContainer.this != null) 770 { 771 if (panelView.getDockingContainer() != DockingContainer.this) 772 { 773 panelView.getDockingContainer().removePanel(panelView.getComponent()); 774 DockingContainer.this.insertPanel(0, panelView.getName(), panelView.getComponent(), panelView.isClosable()); 775 } 776 } 777 } 778 779 context.dropComplete(true); 780 } catch (Exception ex) 781 { 782 ex.printStackTrace(); 783 } 784 } 785 786 787 } 788 789 public int getPosition() { 790 return position; 791 } 792 793 public void setPosition(int position) { 794 this.position = position; 795 } 796 797 public int getPreferredDividerLocation() { 798 return preferredDividerLocation; 799 } 800 801 public void setPreferredDividerLocation(int preferredDividerLocation) { 802 this.preferredDividerLocation = preferredDividerLocation; 803 } 804 805 public void applyI18n(){ 806 809 } 810 } 811 | Popular Tags |