1 19 20 package org.openide.awt; 21 22 import java.awt.*; 23 import java.awt.datatransfer.*; 24 import java.awt.dnd.*; 25 import java.awt.event.*; 26 import java.io.IOException ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.EventObject ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import java.util.TooManyListenersException ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 import javax.swing.*; 38 import javax.swing.border.*; 39 import javax.swing.event.*; 40 import javax.swing.plaf.metal.MetalLookAndFeel ; 41 import javax.swing.plaf.synth.Region ; 42 import javax.swing.plaf.synth.SynthConstants ; 43 import javax.swing.plaf.synth.SynthContext ; 44 import javax.swing.plaf.synth.SynthLookAndFeel ; 45 import javax.swing.plaf.synth.SynthStyle ; 46 import javax.swing.plaf.synth.SynthStyleFactory ; 47 import org.openide.cookies.InstanceCookie; 48 import org.openide.loaders.*; 49 import org.openide.nodes.Node; 50 import org.openide.util.*; 51 import org.openide.util.actions.Presenter; 52 import org.openide.util.datatransfer.ExTransferable; 53 54 61 public class Toolbar extends JToolBar { 62 64 @Deprecated 65 public static final int BASIC_HEIGHT = 34; 66 67 69 static int HEIGHT_TOLERANCE = 5; 70 71 static int TOP = 2; 72 73 static int LEFT = 3; 74 75 static int BOTTOM = 2; 76 77 static int RIGHT = 3; 78 79 static int RESIDUAL_WIDTH = 16; 80 81 82 83 private boolean floatable; 84 85 private DnDListener listener; 86 87 private ToolbarMouseListener mouseListener; 88 89 private String displayName; 90 91 92 private DataFolder backingFolder; 93 94 private Folder processor; 95 96 private static final boolean isMetalLaF = 98 MetalLookAndFeel .class.isAssignableFrom(UIManager.getLookAndFeel().getClass()); 99 private static final boolean isJdk15; 100 private static final boolean isJdk16; 101 102 static final long serialVersionUID = 5011742660516204764L; 103 104 static { 105 String javaVersion = System.getProperty( "java.version" ); 106 isJdk15 = javaVersion.startsWith( "1.5" ); 107 isJdk16 = javaVersion.startsWith( "1.6" ); 108 } 109 110 private static final int customFontHeightCorrection; 111 112 static { 113 int customFontSize = UIManager.getInt( "customFontSize" ); 114 if( customFontSize < 1 ) 115 customFontSize = 1; 116 117 int defaultFontSize = UIManager.getInt( "nbDefaultFontSize" ); 118 if( defaultFontSize <= 0 ) 119 defaultFontSize = 11; 120 121 customFontHeightCorrection = Math.max( customFontSize - defaultFontSize, 0 ); 122 } 123 124 private static Class synthIconClass = null; 125 126 private static boolean testExecuted = false; 127 128 129 public Toolbar () { 130 this (""); } 132 133 135 public Toolbar (String name) { 136 this (name, name, false); 137 } 138 139 141 public Toolbar (String name, String displayName) { 142 this (name, displayName, false); 143 } 144 145 150 public Toolbar (String name, boolean f) { 151 this (name, name, f); 152 } 153 154 Toolbar(DataFolder folder, boolean f) { 155 super(); 156 backingFolder = folder; 157 initAll(folder.getName(), f); 158 initDnD(); 159 } 160 161 166 private static boolean useSynthIcon () { 167 if (!testExecuted) { 168 testExecuted = true; 169 try { 170 synthIconClass = Class.forName("sun.swing.plaf.synth.SynthIcon"); 171 } catch (ClassNotFoundException exc) { 172 Logger.getLogger(Toolbar.class.getName()).log(Level.INFO, null, exc); 173 } 174 } 175 return (synthIconClass != null); 176 } 177 178 private void initDnD() { 179 DropTarget dt = new DropTarget(this, getDnd()); 180 } 181 182 DataFolder getFolder() { 183 return backingFolder; 184 } 185 186 187 public void paint( Graphics g ) { 188 super.paint( g ); 189 if( -1 != dropTargetButtonIndex ) { 190 paintDropGesture( g ); 191 } 192 } 193 194 private void updateDropGesture( DropTargetDragEvent e ) { 195 Point p = e.getLocation(); 196 Component c = getComponentAt(p); 197 int index = Toolbar.this.getComponentIndex(c); 198 if( index == 0 ) { 199 resetDropGesture(); 201 } else { 202 boolean b = p.x <= c.getLocation().x + c.getWidth() / 2; 204 if( index != dropTargetButtonIndex || b != insertBefore ) { 205 dropTargetButtonIndex = index; 206 insertBefore = b; 207 repaint(); 208 } 209 } 210 } 211 212 private void resetDropGesture() { 213 dropTargetButtonIndex = -1; 214 repaint(); 215 } 216 217 private void paintDropGesture( Graphics g ) { 218 Component c = getComponentAtIndex( dropTargetButtonIndex ); 219 if( null == c ) 220 return; 221 222 Point location = c.getLocation(); 223 int cursorLocation = location.x; 224 if( !insertBefore ) { 225 cursorLocation += c.getWidth(); 226 if( dropTargetButtonIndex == getComponentCount()-1 ) 227 cursorLocation -= 3; 228 } 229 drawDropLine( g, cursorLocation ); 230 } 231 232 private void drawDropLine( Graphics g, int x ) { 233 Color oldColor = g.getColor(); 234 g.setColor( Color.black ); 235 int height = getHeight(); 236 g.drawLine( x, 3, x, height-4 ); 237 g.drawLine( x-1, 3, x-1, height-4 ); 238 239 g.drawLine( x+1, 2, x+1+2, 2 ); 240 g.drawLine( x+1, height-3, x+1+2, height-3 ); 241 242 g.drawLine( x-2, 2, x-2-2, 2 ); 243 g.drawLine( x-2, height-3, x-2-2, height-3 ); 244 g.setColor( oldColor ); 245 } 246 247 250 private void removeButton( Transferable t ) { 251 try { 252 Object o = null; 253 if( t.isDataFlavorSupported( buttonDataFlavor ) ) { 254 o = t.getTransferData( buttonDataFlavor ); } 256 if( null != o && o instanceof DataObject ) { 257 ((DataObject) o).delete(); 258 repaint(); 259 if( backingFolder.getChildren().length == 0 ) { 260 javax.swing.SwingUtilities.invokeLater(new java.lang.Runnable () { 261 262 public void run() { 263 try { 264 backingFolder.delete(); 265 } 266 catch (java.io.IOException e) { 267 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, 268 null, 269 e); 270 } 271 } 272 }); 273 } 274 } 275 } catch( UnsupportedFlavorException ex ) { 276 Exceptions.printStackTrace(ex); 277 } catch (IOException ioe) { 278 Exceptions.printStackTrace(ioe); 279 } 280 } 281 282 287 private boolean handleDrop( Transferable t ) { 288 try { 289 Object o; 290 if( t.isDataFlavorSupported( actionDataFlavor ) ) { 291 o = t.getTransferData( actionDataFlavor ); 292 if( o instanceof Node ) { 293 DataObject dobj = (DataObject)((Node)o).getLookup().lookup( DataObject.class ); 294 return addButton( dobj, dropTargetButtonIndex-1, insertBefore ); 295 } 296 } else { 297 o = t.getTransferData( buttonDataFlavor ); 298 if( o instanceof DataObject ) { 299 return moveButton( (DataObject)o, dropTargetButtonIndex-1, insertBefore ); 300 } 301 } 302 } catch (UnsupportedFlavorException e) { 303 Exceptions.printStackTrace(e); 304 } catch (IOException ioe) { 305 Exceptions.printStackTrace(ioe); 306 } 307 return false; 308 } 309 310 314 int dropTargetButtonIndex = -1; 315 319 int dragSourceButtonIndex = -1; 320 324 boolean insertBefore = true; 325 328 boolean isDragSourceToolbar = false; 329 330 private static DataFlavor buttonDataFlavor = new DataFlavor( DataObject.class, "Toolbar Item" ); 331 private static DataFlavor actionDataFlavor = new DataFlavor( Node.class, "Action Node" ); 332 333 private DnDSupport dnd; 334 private class DnDSupport implements DragSourceListener, DragGestureListener, DropTargetListener, DragSourceMotionListener { 335 private DragSource dragSource = new DragSource(); 336 337 private Cursor dragMoveCursor = DragSource.DefaultMoveDrop; 338 private Cursor dragNoDropCursor = DragSource.DefaultMoveNoDrop; 339 private Cursor dragRemoveCursor = Utilities.createCustomCursor( Toolbar.this, Utilities.loadImage( "org/openide/loaders/delete.gif"), "NO_ACTION_MOVE" ); 340 341 public DnDSupport() { 342 dragSource.addDragSourceMotionListener(this); 343 } 344 345 public void register(Component c) { 346 dgr = dragSource.createDefaultDragGestureRecognizer(c, DnDConstants.ACTION_MOVE, this); 347 if (dgr != this.dgr) { 348 this.dgr = dgr; 349 try { 350 dgr.addDragGestureListener(this); 351 } catch (TooManyListenersException e) { 352 } 354 } 355 } 356 DragGestureRecognizer dgr = null; 357 358 public void dragEnter(DragSourceDragEvent e) { 359 } 361 362 public void dragOver(DragSourceDragEvent e) { 363 } 365 366 public void dragExit(DragSourceEvent e) { 367 resetDropGesture(); 369 } 370 371 public void dragDropEnd(DragSourceDropEvent e) { 372 isDragSourceToolbar = false; 373 Component sourceComponent = e.getDragSourceContext().getComponent(); 374 if( sourceComponent instanceof JButton ) { 375 ((JButton)sourceComponent).getModel().setRollover( false ); 376 } 377 sourceComponent.repaint(); 378 resetDropGesture(); 379 if ( e.getDropSuccess() == false && !isInToolbarPanel( e.getLocation() ) ) { 380 removeButton( e.getDragSourceContext().getTransferable() ); 381 } 382 } 383 384 public void dragGestureRecognized(DragGestureEvent e) { 385 if( !ToolbarPool.getDefault().isInEditMode() ) 386 return; 387 try { 388 Component c = e.getComponent(); 389 if( c instanceof JToolBar.Separator || "grip".equals( c.getName() ) ) 391 return; 392 Transferable t = null; 393 if (c instanceof JComponent) { 394 final DataObject dob = (DataObject) ((JComponent) c).getClientProperty("file"); 395 if (dob != null) { 396 t = new ExTransferable.Single( buttonDataFlavor ) { 397 public Object getData() { 398 return dob; 399 } 400 }; 401 } 402 } 403 if( c instanceof JButton ) { 404 ((JButton)c).getModel().setArmed( false ); 405 ((JButton)c).getModel().setPressed( false ); 406 ((JButton)c).getModel().setRollover( true ); 407 } 408 if (t != null) { 409 dragSourceButtonIndex = Toolbar.this.getComponentIndex( c ); 410 isDragSourceToolbar = true; 411 dragSource.startDrag(e, dragMoveCursor, t, this); 412 } 413 414 } catch ( InvalidDnDOperationException idoe ) { 415 Exceptions.printStackTrace(idoe); 416 } 417 } 418 419 public void dropActionChanged (DragSourceDragEvent e) { 420 } 422 423 public void drop(DropTargetDropEvent dtde) { 424 if( validateDropPosition() ) { 425 dtde.dropComplete( handleDrop( dtde.getTransferable() ) ); 426 } 427 resetDropGesture(); 428 } 429 430 public void dragExit(DropTargetEvent dte) { 431 resetDropGesture(); 432 } 433 434 public void dropActionChanged(DropTargetDragEvent dtde) { 435 } 437 438 public void dragEnter(DropTargetDragEvent e) { 439 if( e.isDataFlavorSupported( buttonDataFlavor ) 440 || e.isDataFlavorSupported( actionDataFlavor ) ) { 441 e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); 442 } else { 443 e.rejectDrag(); 444 } 445 } 446 447 public void dragOver(DropTargetDragEvent e) { 448 if( e.isDataFlavorSupported( buttonDataFlavor ) 449 || e.isDataFlavorSupported( actionDataFlavor ) ) { 450 updateDropGesture( e ); 451 if( !validateDropPosition() ) { 452 e.rejectDrag(); 453 } else { 454 e.acceptDrag( DnDConstants.ACTION_COPY_OR_MOVE ); 455 } 456 } else { 457 e.rejectDrag(); 458 } 459 } 460 461 public void dragMouseMoved(DragSourceDragEvent e) { 462 DragSourceContext context = e.getDragSourceContext(); 463 int action = e.getDropAction(); 464 if ((action & DnDConstants.ACTION_MOVE) != 0) { 465 context.setCursor( dragMoveCursor ); 466 } else { 467 if( isInToolbarPanel( e.getLocation() ) ) 468 context.setCursor( dragNoDropCursor ); 469 else 470 context.setCursor( dragRemoveCursor ); 471 } 472 } 473 } 474 475 private boolean isInToolbarPanel( Point p ) { 476 Component c = ToolbarPool.getDefault(); 477 SwingUtilities.convertPointFromScreen( p, c ); 478 return c.contains( p ); 479 } 480 481 484 private boolean addButton( DataObject dobj, int dropIndex, boolean dropBefore ) throws IOException { 485 if( null == dobj ) 486 return false; 487 String objName = dobj.getName(); 489 DataObject[] children = backingFolder.getChildren(); 490 for( int i=0; i<children.length; i++ ) { 491 if( objName.equals( children[i].getName() ) ) { 493 isDragSourceToolbar = true; 496 return moveButton( children[i], dropIndex, dropBefore ); 497 } 498 } 499 500 DataObject objUnderCursor = getDataObjectUnderDropCursor( dropIndex, dropBefore ); 501 502 DataShadow shadow = DataShadow.create( backingFolder, dobj ); 503 504 DataObject newObj = null; 506 children = backingFolder.getChildren(); 507 for( int i=0; i<children.length; i++ ) { 508 if( objName.equals( children[i].getName() ) ) { 509 newObj = children[i]; 510 break; 511 } 512 } 513 514 if( null != newObj ) 515 reorderButtons( newObj, objUnderCursor ); 517 return true; 518 } 519 520 523 private boolean moveButton( DataObject ob, int dropIndex, boolean dropBefore ) throws IOException { 524 DataObject objUnderCursor = getDataObjectUnderDropCursor( dropIndex, dropBefore ); 526 527 if( !isDragSourceToolbar ) { 528 ob.move(backingFolder); 530 } 531 532 reorderButtons( ob, objUnderCursor ); 533 return true; 535 } 536 537 private void reorderButtons( DataObject objToMove, DataObject objUnderCursor ) throws IOException { 538 java.util.List <DataObject> children = 539 new ArrayList <DataObject>( Arrays.asList( backingFolder.getChildren() ) ); 540 if( null == objUnderCursor ) { 541 children.remove( objToMove ); 542 children.add( objToMove ); 543 } else { 544 int targetIndex = children.indexOf( objUnderCursor ); 545 int currentIndex = children.indexOf( objToMove ); 546 if( currentIndex < targetIndex ) 547 targetIndex--; 548 children.remove( objToMove ); 549 children.add( targetIndex, objToMove ); 550 } 551 552 backingFolder.setOrder( children.toArray( new DataObject[children.size()]) ); 553 } 554 555 private DataObject getDataObjectUnderDropCursor( int dropIndex, boolean dropBefore ) { 556 DataObject[] buttons = backingFolder.getChildren(); 557 DataObject objUnderCursor = null; 558 boolean appendToEnd = false; 559 if( buttons.length > 0 ) { 560 if( !dropBefore ) 561 dropIndex++; 562 if( dropIndex < buttons.length ) { 563 objUnderCursor = buttons[dropIndex]; 564 } 565 } 566 return objUnderCursor; 567 } 568 569 private boolean validateDropPosition() { 570 return dropTargetButtonIndex >= 0 572 && !(isDragSourceToolbar && (dragSourceButtonIndex == dropTargetButtonIndex || (dropTargetButtonIndex == dragSourceButtonIndex-1 && !insertBefore) || (dropTargetButtonIndex == dragSourceButtonIndex+1 && insertBefore))) || (dropTargetButtonIndex < 0 && getComponentCount() == 1); 579 } 580 581 582 final Folder waitFinished() { 583 if (backingFolder == null) return null; 585 586 if(processor == null && isVisible()) { 587 processor = new Folder(); } 589 return processor; 590 } 591 592 public void addNotify() { 593 super.addNotify(); 594 waitFinished(); 595 } 596 597 public Component[] getComponents () { 598 waitFinished (); 599 return super.getComponents (); 600 } 601 602 public void setVisible(boolean b) { 603 super.setVisible(b); 604 waitFinished(); 605 } 606 607 private static final Insets emptyInsets = new Insets(1,1,1,1); 608 610 protected void addImpl(Component c, Object constraints, int idx) { 611 if (c instanceof AbstractButton) { 615 c.setFocusable(false); 616 ((JComponent) c).setOpaque(false); 617 if( isMetalLaF && (isJdk15 || isJdk16)) { 618 ((AbstractButton) c).setBorderPainted(false); 620 ((AbstractButton) c).setOpaque(false); 621 } 622 if( isJdk16 && !isMetalLaF ) { 625 ((AbstractButton) c).setMargin( emptyInsets ); 626 } 627 } else if( c instanceof JToolBar.Separator ) { 628 JToolBar.Separator separator = (JToolBar.Separator)c; 629 if (getOrientation() == VERTICAL) { 630 separator.setOrientation(JSeparator.HORIZONTAL); 631 } else { 632 separator.setOrientation(JSeparator.VERTICAL); 633 } 634 } 635 636 super.addImpl (c, constraints, idx); 637 if( !("grip".equals(c.getName()) || (c instanceof JToolBar.Separator)) ) { 638 getDnd().register(c); 639 } 640 } 641 642 647 public Toolbar (String name, String displayName, boolean f) { 648 super(); 649 setDisplayName (displayName); 650 initAll(name, f); 651 } 652 653 658 public static int getBasicHeight () { 659 if (ToolbarPool.getDefault().getPreferredIconSize() == 24) { 660 return 44; 661 } else { 662 return 34; 663 } 664 } 665 666 private void initAll(String name, boolean f) { 667 floatable = f; 668 mouseListener = null; 669 670 setName (name); 671 672 setFloatable (false); 673 String lAndF = UIManager.getLookAndFeel().getName(); 674 675 if (lAndF.equals("Windows")) { 676 setBorder(Boolean.getBoolean("netbeans.small.main.window") ? 679 BorderFactory.createEmptyBorder(1,1,1,1) : 680 BorderFactory.createEmptyBorder()); } else if (!"Aqua".equals(UIManager.getLookAndFeel().getID()) && !"GTK".equals(UIManager.getLookAndFeel().getID())){ 682 Border b = UIManager.getBorder ("ToolBar.border"); 684 if ((b==null) || (b instanceof javax.swing.plaf.metal.MetalBorders.ToolBarBorder)) 685 b=BorderFactory.createEtchedBorder (EtchedBorder.LOWERED); 686 setBorder (new CompoundBorder ( 687 b, 688 new EmptyBorder (TOP, LEFT, BOTTOM, RIGHT)) 689 ); 690 691 } 692 693 if (!"Aqua".equals(UIManager.getLookAndFeel().getID())) { 694 putClientProperty("JToolBar.isRollover", Boolean.TRUE); } 696 addGrip(); 697 698 getAccessibleContext().setAccessibleName(displayName == null ? getName() : displayName); 699 getAccessibleContext().setAccessibleDescription(getName()); 700 } 701 702 public String getUIClassID() { 703 if (UIManager.get("Nb.Toolbar.ui") != null) { return "Nb.Toolbar.ui"; } else { 706 return super.getUIClassID(); 707 } 708 } 709 710 public Dimension getPreferredSize() { 711 String lfid = UIManager.getLookAndFeel().getID(); 712 int minheight; 713 714 if (ToolbarPool.getDefault().getPreferredIconSize() == 24) { 715 if ("Aqua".equals(lfid)) { 716 minheight = 29 + 8; 717 } else if ("Metal".equals(lfid)) { 718 minheight = 36 + 8; 719 } else if ("Windows".equals(lfid)) { 720 minheight = isXPTheme() ? (23 + 8) : (27 + 8); 721 } else if ("GTK".equals(lfid)) { 722 minheight = 30 + 8; 723 } else { 724 minheight = 28 + 8; 725 } 726 } else { 727 if ("Aqua".equals(lfid)) { 728 minheight = 29; 729 } else if ("Metal".equals(lfid)) { 730 minheight = 36; 731 } else if ("Windows".equals(lfid)) { 732 minheight = isXPTheme() ? 23 : 27; 733 } else if ("GTK".equals(lfid)) { 734 minheight = 30; 735 } else { 736 minheight = 28; 737 } 738 } 739 Dimension result = super.getPreferredSize(); 740 result.height = Math.max (result.height, minheight); 741 return result; 742 } 743 744 745 public void removeAll () { 746 super.removeAll(); 747 addGrip(); 748 } 749 750 753 void addGrip () { 754 if (floatable) { 755 756 String lAndF = UIManager.getLookAndFeel().getName(); 757 JPanel dragarea = null; 759 if (lAndF.equals("Windows")) { 760 if (isXPTheme()) { 761 dragarea = (JPanel) new ToolbarXP(); 762 } else { 763 dragarea = (JPanel) new ToolbarGrip(); 764 } 765 } else if (UIManager.getLookAndFeel().getID().equals("Aqua")) { 766 dragarea = (JPanel) new ToolbarAqua(); 767 } else if (UIManager.getLookAndFeel().getID().equals("GTK")) { 768 dragarea = (JPanel) new ToolbarGtk(); 769 } else { 771 dragarea = (JPanel)new ToolbarBump(); 773 } 774 if (mouseListener == null) { 775 mouseListener = new ToolbarMouseListener (); 776 } 777 778 if (dragarea != null) { 779 dragarea.addMouseListener (mouseListener); 780 dragarea.addMouseMotionListener (mouseListener); 781 782 dragarea.setName ("grip"); 783 add (dragarea); 784 } 785 } 786 } 787 788 792 static public int rowCount (int height) { 793 return 1 + height / (getBasicHeight() + HEIGHT_TOLERANCE+customFontHeightCorrection); 794 } 795 796 799 public void setDnDListener (DnDListener l) { 800 listener = l; 801 } 802 803 805 public String getDisplayName () { 806 if (displayName == null) { 807 if (!backingFolder.isValid()) { 808 return backingFolder.getName(); 810 } 811 return backingFolder.getNodeDelegate ().getDisplayName (); 812 } 813 return displayName; 814 } 815 816 818 public void setDisplayName (String displayName) { 819 this.displayName = displayName; 820 } 821 822 827 protected void fireDragToolbar (int dx, int dy, int type) { 828 if (listener != null) 829 listener.dragToolbar (new DnDEvent (this, getName(), dx, dy, type)); 830 } 831 832 837 protected void fireDropToolbar (int dx, int dy, int type) { 838 if (listener != null) 839 listener.dropToolbar (new DnDEvent (this, getName(), dx, dy, type)); 840 } 841 842 synchronized final MouseInputListener mouseDelegate () { 843 if (mouseListener == null) mouseListener = new ToolbarMouseListener (); 844 return mouseListener; 845 } 846 847 848 class ToolbarMouseListener extends MouseInputAdapter { 849 850 private boolean dragging = false; 851 852 private Point startPoint = null; 853 854 855 public void mousePressed (MouseEvent e) { 856 startPoint = e.getPoint(); 857 } 858 859 860 public void mouseReleased (MouseEvent e) { 861 if (dragging) { 862 863 int dx = getX() + e.getX() - startPoint.x > getParent().getWidth() - RESIDUAL_WIDTH ? 864 0 : e.getX() - startPoint.x; 865 866 fireDropToolbar (dx, 867 e.getY() - startPoint.y, 868 DnDEvent.DND_ONE); 869 dragging = false; 870 } 871 } 872 873 874 public void mouseDragged (MouseEvent e) { 875 int m = e.getModifiers(); 876 int type = DnDEvent.DND_ONE; 877 int dx; 878 879 if (e.isControlDown()) 880 type = DnDEvent.DND_LINE; 881 else if (((m & InputEvent.BUTTON2_MASK) != 0) || 882 ((m & InputEvent.BUTTON3_MASK) != 0)) 883 type = DnDEvent.DND_END; 884 if (startPoint == null) { 885 startPoint = new Point (e.getX(), e.getY()); 886 } 887 888 if ( getX() + e.getX() + startPoint.x > getParent().getWidth() - RESIDUAL_WIDTH ) { 889 if ( getX() >= getParent().getWidth() - RESIDUAL_WIDTH ) { 890 dx = 0; 891 } 892 else { 893 dx = getParent().getWidth() - RESIDUAL_WIDTH - getX(); 894 } 895 } 896 else { 897 dx = e.getX() - startPoint.x; 898 } 899 900 fireDragToolbar ( dx, 901 e.getY() - startPoint.y, 902 type); 903 dragging = true; 904 } 905 906 } 908 912 final class Folder extends FolderInstance { 913 914 918 public Folder () { 919 super (backingFolder); 920 recreate (); 921 } 922 923 927 public String instanceName () { 928 return Toolbar.this.getClass().getName(); 929 } 930 931 935 public Class instanceClass () 936 throws java.io.IOException , ClassNotFoundException { 937 return Toolbar.this.getClass(); 938 } 939 940 943 protected InstanceCookie acceptDataObject (DataObject dob) { 944 InstanceCookie ic = super.acceptDataObject (dob); 945 if (ic == null) { 946 JButton button = ExecBridge.createButton (dob); 947 if (button != null) { 948 button.putClientProperty ("file", dob); 949 } 950 return button != null ? new InstanceSupport.Instance (button) : null; 951 } else { 952 return ic; 953 } 954 } 955 956 private Map <Object , Object > cookiesToObjects = new HashMap <Object , Object >(); 957 958 protected Object instanceForCookie (DataObject obj, InstanceCookie cookie) 959 throws IOException , ClassNotFoundException { 960 Object result = super.instanceForCookie(obj, cookie); 961 cookiesToObjects.put (result, obj); 962 return result; 963 } 964 965 966 971 protected InstanceCookie acceptCookie (InstanceCookie cookie) 972 throws java.io.IOException , ClassNotFoundException { 973 boolean is; 974 975 if (cookie instanceof InstanceCookie.Of) { 976 InstanceCookie.Of of = (InstanceCookie.Of)cookie; 977 is = of.instanceOf (Component.class) || 978 of.instanceOf (Presenter.Toolbar.class) || 979 of.instanceOf (Action.class); 980 } else { 981 Class c = cookie.instanceClass(); 982 is = Component.class.isAssignableFrom(c) || 983 Presenter.Toolbar.class.isAssignableFrom(c) || 984 Action.class.isAssignableFrom (c); 985 } 986 return is ? cookie : null; 987 } 988 989 995 protected InstanceCookie acceptFolder(DataFolder df) { 996 return null; } 998 999 1005 protected Object createInstance(final InstanceCookie[] cookies) 1006 throws java.io.IOException , ClassNotFoundException { 1007 Toolbar.this.removeAll(); 1009 for (int i = 0; i < cookies.length; i++) { 1010 try { 1011 java.lang.Object obj = cookies[i].instanceCreate(); 1012 java.lang.Object file = cookiesToObjects.get(obj); 1013 1014 if (obj instanceof org.openide.util.actions.Presenter.Toolbar) { 1015 obj = ((org.openide.util.actions.Presenter.Toolbar) obj).getToolbarPresenter(); 1016 } 1017 if (obj instanceof java.awt.Component ) { 1018 if ((obj instanceof javax.swing.JComponent ) && 1021 "Fixed".equals(((javax.swing.JComponent ) obj).getClientProperty("Toolbar"))) { 1022 floatable = false; 1023 org.openide.awt.Toolbar.this.removeAll(); 1024 setBorder(null); 1025 } 1026 if (obj instanceof javax.swing.JComponent ) { 1027 if (org.openide.awt.ToolbarPool.getDefault().getPreferredIconSize() == 1028 24) { 1029 ((javax.swing.JComponent ) obj).putClientProperty("PreferredIconSize", 1030 new java.lang.Integer (24)); 1031 } 1032 ((javax.swing.JComponent ) obj).putClientProperty("file", 1033 file); 1034 } 1035 org.openide.awt.Toolbar.this.add((java.awt.Component ) obj); 1036 continue; 1037 } 1038 if (obj instanceof javax.swing.Action ) { 1039 javax.swing.Action a = (javax.swing.Action ) obj; 1040 javax.swing.JButton b = new org.openide.awt.Toolbar.DefaultIconButton(); 1041 1042 if (org.openide.awt.ToolbarPool.getDefault().getPreferredIconSize() == 1043 24) { 1044 b.putClientProperty("PreferredIconSize", 1045 new java.lang.Integer (24)); 1046 } 1047 if (null == a.getValue(javax.swing.Action.SMALL_ICON) && 1048 (null == a.getValue(javax.swing.Action.NAME) || 1049 a.getValue(javax.swing.Action.NAME).toString().length() == 1050 0)) { 1051 a.putValue(javax.swing.Action.SMALL_ICON, 1052 new ImageIcon( Utilities.loadImage( "org/openide/loaders/unknown.gif") )); 1053 } 1054 org.openide.awt.Actions.connect(b, a); 1055 b.putClientProperty("file", file); 1056 org.openide.awt.Toolbar.this.add(b); 1057 continue; 1058 } 1059 } 1060 catch (java.io.IOException ex) { 1061 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, ex); 1062 } 1063 catch (java.lang.ClassNotFoundException ex) { 1064 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, ex); 1065 } 1066 finally { 1067 cookiesToObjects.clear(); 1068 } 1069 } 1070 1071 Toolbar.this.invalidate (); 1073 return Toolbar.this; 1074 } 1075 1076 1078 protected Task postCreationTask (Runnable run) { 1079 return new AWTTask (run); 1080 } 1081 1082 } 1084 1085 private final class ToolbarBump extends JPanel { 1086 1087 static final int TOPGAP = 2; 1088 1089 static final int BOTGAP = 2; 1090 1091 static final int WIDTH = 6; 1092 1093 1094 Dimension dim; 1095 1096 Dimension max; 1097 1098 static final long serialVersionUID =-8819972936203315277L; 1099 1100 1101 public ToolbarBump () { 1102 super(); 1103 int width = WIDTH; 1104 dim = new Dimension (width, width); 1105 max = new Dimension (width, Integer.MAX_VALUE); 1106 this.setToolTipText (Toolbar.this.getDisplayName()); 1107 } 1108 1109 1110 public void paint (Graphics g) { 1111 Dimension size = this.getSize (); 1112 int height = size.height - BOTGAP; 1113 g.setColor (this.getBackground ()); 1114 1115 for (int x = 0; x+1 < size.width; x+=4) { 1116 for (int y = TOPGAP; y+1 < height; y+=4) { 1117 g.setColor (this.getBackground ().brighter ()); 1118 g.drawLine (x, y, x, y); 1119 if (x+5 < size.width && y+5 < height) { 1120 g.drawLine (x+2, y+2, x+2, y+2); 1121 } 1122 g.setColor (this.getBackground ().darker ().darker ()); 1123 g.drawLine (x+1, y+1, x+1, y+1); 1124 if (x+5 < size.width && y+5 < height) { 1125 g.drawLine (x+3, y+3, x+3, y+3); 1126 } 1127 } 1128 } 1129 } 1130 1131 1132 public Dimension getMinimumSize () { 1133 return dim; 1134 } 1135 1136 1137 public Dimension getPreferredSize () { 1138 return this.getMinimumSize (); 1139 } 1140 1141 public Dimension getMaximumSize () { 1142 return max; 1143 } 1144 } 1146 1147 private final class ToolbarGtk extends JPanel { 1148 1149 static final int TOPGAP = 2; 1150 1151 static final int BOTGAP = 2; 1152 1153 static final int WIDTH = 6; 1154 1155 1156 Dimension dim; 1157 1158 Dimension max; 1159 1160 static final long serialVersionUID = -8819972936203315277L; 1161 1162 1163 public ToolbarGtk () { 1164 super(); 1165 int width = WIDTH; 1166 dim = new Dimension (width, width); 1167 max = new Dimension (width, Integer.MAX_VALUE); 1168 this.setToolTipText (Toolbar.this.getDisplayName()); 1169 } 1170 1171 1172 public void paint (Graphics g) { 1173 if (useSynthIcon()) { 1174 int height = Toolbar.this.getHeight() - BOTGAP; 1175 Icon icon = UIManager.getIcon("ToolBar.handleIcon"); 1176 Region region = Region.TOOL_BAR; 1177 SynthLookAndFeel laf = (SynthLookAndFeel )UIManager.getLookAndFeel(); 1178 SynthStyleFactory sf = laf.getStyleFactory(); 1179 SynthStyle style = sf.getStyle(Toolbar.this, region); 1180 SynthContext context = new SynthContext (Toolbar.this, region, style, SynthConstants.DEFAULT); 1181 1182 Method m = null; 1184 try { 1185 m = synthIconClass.getMethod("getIconWidth",Icon.class, SynthContext .class); 1186 } catch (NoSuchMethodException exc) { 1187 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc); 1188 } 1189 int width = 0; 1190 try { 1192 width = (Integer ) m.invoke(null, new Object [] {icon, context}); 1193 } catch (IllegalAccessException exc) { 1194 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc); 1195 } catch (InvocationTargetException exc) { 1196 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc); 1197 } 1198 try { 1199 m = synthIconClass.getMethod("paintIcon",Icon.class,SynthContext .class, 1200 Graphics.class,Integer.TYPE,Integer.TYPE,Integer.TYPE,Integer.TYPE); 1201 } catch (NoSuchMethodException exc) { 1202 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc); 1203 } 1204 try { 1206 m.invoke(null, new Object [] {icon,context,g,new Integer (0),new Integer (0), 1207 new Integer (width),new Integer (height)}); 1208 } catch (IllegalAccessException exc) { 1209 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc); 1210 } catch (InvocationTargetException exc) { 1211 Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc); 1212 } 1213 } else { 1214 Dimension size = this.getSize(); 1215 int height = size.height - BOTGAP; 1216 g.setColor (this.getBackground ()); 1217 1218 for (int x = 0; x+1 < size.width; x+=4) { 1219 for (int y = TOPGAP; y+1 < height; y+=4) { 1220 g.setColor (this.getBackground ().brighter ()); 1221 g.drawLine (x, y, x, y); 1222 if (x+5 < size.width && y+5 < height) { 1223 g.drawLine (x+2, y+2, x+2, y+2); 1224 } 1225 g.setColor (this.getBackground ().darker ().darker ()); 1226 g.drawLine (x+1, y+1, x+1, y+1); 1227 if (x+5 < size.width && y+5 < height) { 1228 g.drawLine (x+3, y+3, x+3, y+3); 1229 } 1230 } 1231 } 1232 } 1233 } 1234 1235 1236 public Dimension getMinimumSize () { 1237 return dim; 1238 } 1239 1240 1241 public Dimension getPreferredSize () { 1242 return new Dimension(WIDTH,Toolbar.this.getHeight() - BOTGAP - TOPGAP); 1243 } 1244 1245 public Dimension getMaximumSize () { 1246 return max; 1247 } 1248 } 1250 1253 private static Boolean isXP = null; 1254 private static boolean isXPTheme () { 1255 if (isXP == null) { 1256 Boolean xp = (Boolean )Toolkit.getDefaultToolkit(). 1257 getDesktopProperty("win.xpstyle.themeActive"); isXP = Boolean.TRUE.equals(xp)? Boolean.TRUE : Boolean.FALSE; 1259 } 1260 return isXP.booleanValue(); 1261 } 1262 1263 private final class ToolbarAqua extends JPanel { 1264 1265 static final int WIDTH = 8; 1266 1267 Dimension dim; 1268 1269 Dimension max; 1270 static final long serialVersionUID =-8819972972003315277L; 1271 1272 public ToolbarAqua() { 1273 dim = new Dimension (WIDTH, WIDTH); 1274 max = new Dimension (WIDTH, Integer.MAX_VALUE); 1275 this.setToolTipText (Toolbar.this.getDisplayName()); 1276 } 1277 1278 public void paintComponent (Graphics g) { 1279 super.paintComponent(g); 1280 java.awt.Graphics2D g2d = (Graphics2D) g; 1281 g2d.addRenderingHints(getHints()); 1282 1283 int sz = 5; 1284 1285 int y = ((getHeight() / 2) - (sz / 2)) - 2; 1286 int x = ((getWidth() / 2) - (sz / 2)) - 2; 1287 1288 GradientPaint gradient = new GradientPaint(x+1, y+1, Color.BLACK, 1289 x+sz-1, y+sz-1, Color.WHITE); 1290 1291 Paint paint = g2d.getPaint(); 1292 1293 g2d.setPaint(gradient); 1294 g2d.drawArc(x,y,sz,sz,0,359); 1295 1296 g.setColor(new Color(240,240,240)); 1297 g.drawLine(x+(sz/2), y + (sz/2),x+(sz/2), y + (sz/2)); 1298 1299 g2d.setPaint(paint); 1300 } 1301 1302 1303 public Dimension getMinimumSize () { 1304 return dim; 1305 } 1306 1307 1308 public Dimension getPreferredSize () { 1309 return this.getMinimumSize (); 1310 } 1311 1312 public Dimension getMaximumSize () { 1313 return max; 1314 } 1315 } 1316 1317 private static java.util.Map <RenderingHints.Key, Object > hintsMap = null; 1318 @SuppressWarnings ("unchecked") 1319 static final Map getHints() { 1320 if (hintsMap == null) { 1322 hintsMap = (Map <RenderingHints.Key, Object >)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); if (hintsMap == null) { 1325 hintsMap = new HashMap <RenderingHints.Key, Object >(); 1326 hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 1327 } 1328 } 1329 return hintsMap; 1330 } 1331 1332 public void setUI(javax.swing.plaf.ToolBarUI ui) { 1333 super.setUI(ui); 1334 if( null != backingFolder && null != processor ) { 1335 processor.recreate(); 1337 } 1338 } 1339 1340 private final class ToolbarXP extends JPanel { 1341 1342 static final int WIDTH = 7; 1343 1344 Dimension dim; 1345 1346 Dimension max; 1347 1348 static final long serialVersionUID =-8819972936203315277L; 1349 public ToolbarXP() { 1350 dim = new Dimension (WIDTH, WIDTH); 1351 max = new Dimension (WIDTH, Integer.MAX_VALUE); 1352 this.setToolTipText (Toolbar.this.getDisplayName()); 1353 } 1354 1355 public void paintComponent (Graphics g) { 1356 super.paintComponent(g); 1357 int x = 3; 1358 for (int i=4; i < getHeight()-4; i+=4) { 1359 g.setColor(UIManager.getColor("controlLtHighlight")); g.fillRect(x + 1, i + 1, 2, 2); 1362 Color col = UIManager.getColor("controlShadow"); g.setColor(col); 1366 g.drawLine(x+1, i+1, x+1, i+1); 1368 1369 int red = col.getRed(); 1372 int green = col.getGreen(); 1373 int blue = col.getBlue(); 1374 1375 Color back = getBackground(); 1379 int rb = back.getRed(); 1380 int gb = back.getGreen(); 1381 int bb = back.getBlue(); 1382 1383 int incr = (rb - red) / 5; 1385 int incg = (gb - green) / 5; 1386 int incb = (bb - blue) / 5; 1387 1388 red += incr; 1390 green += incg; 1391 blue += incb; 1392 col = new Color(red, green, blue); 1394 g.setColor(col); 1395 g.drawLine(x+1, i, x+1, i); 1396 1397 red += incr; 1399 green += incg; 1400 blue += incb; 1401 col = new Color(red, green, blue); 1402 g.setColor(col); 1403 g.drawLine(x, i+1, x, i+1); 1404 1405 red += incr; 1406 green += incg; 1407 blue += incb; 1408 col = new Color(red, green, blue); 1409 g.setColor(col); 1410 g.drawLine(x, i, x, i); 1411 } 1412 } 1413 1414 1415 public Dimension getMinimumSize () { 1416 return dim; 1417 } 1418 1419 1420 public Dimension getPreferredSize () { 1421 return this.getMinimumSize (); 1422 } 1423 1424 public Dimension getMaximumSize () { 1425 return max; 1426 } 1427 } 1428 1429 1438 1439 1440 1441 private final class ToolbarGrip extends JPanel { 1442 1443 static final int HGAP = 1; 1444 1445 static final int VGAP = 2; 1446 1447 static final int STEP = 1; 1448 1449 static final int WIDTH = 2; 1450 1451 1452 int columns; 1453 1454 Dimension dim; 1455 1456 Dimension max; 1457 1458 static final long serialVersionUID =-8819972936203315276L; 1459 1460 1461 public ToolbarGrip () { 1462 this(1); 1463 } 1464 1465 1468 public ToolbarGrip (int col) { 1469 super (); 1470 columns = col; 1471 int width = (col - 1) * STEP + col * WIDTH + 2 * HGAP; 1472 dim = new Dimension (width, width); 1473 max = new Dimension (width, Integer.MAX_VALUE); 1474 this.setBorder (new EmptyBorder (VGAP, HGAP, VGAP, HGAP)); 1475 this.setToolTipText (Toolbar.this.getDisplayName()); 1476 } 1477 1478 1479 public void paint (Graphics g) { 1480 Dimension size = this.getSize(); 1481 int top = VGAP; 1482 int bottom = size.height - 1 - VGAP; 1483 int height = bottom - top; 1484 g.setColor ( this.getBackground() ); 1485 1486 for (int i = 0, x = HGAP; i < columns; i++, x += WIDTH + STEP) { 1487 g.draw3DRect (x, top, WIDTH, height, true); } 1489 1490 } 1491 1492 1493 public Dimension getMinimumSize () { 1494 return dim; 1495 } 1496 1497 1498 public Dimension getPreferredSize () { 1499 return this.getMinimumSize(); 1500 } 1501 1502 public Dimension getMaximumSize () { 1503 return max; 1504 } 1505 1506 } 1508 1509 public interface DnDListener extends java.util.EventListener { 1510 1511 public void dragToolbar (DnDEvent e); 1512 1513 1514 public void dropToolbar (DnDEvent e); 1515 } 1517 1518 1519 public static class DnDEvent extends EventObject { 1520 1521 public static final int DND_ONE = 1; 1522 1523 public static final int DND_END = 2; 1524 1525 public static final int DND_LINE = 3; 1526 1527 1528 private String name; 1529 1530 private int dx; 1531 1532 private int dy; 1533 1534 private int type; 1535 1536 static final long serialVersionUID =4389530973297716699L; 1537 public DnDEvent (Toolbar toolbar, String name, int dx, int dy, int type) { 1538 super (toolbar); 1539 1540 this.name = name; 1541 this.dx = dx; 1542 this.dy = dy; 1543 this.type = type; 1544 } 1545 1546 1547 public String getName () { 1548 return name; 1549 } 1550 1551 1552 public int getDX () { 1553 return dx; 1554 } 1555 1556 1557 public int getDY () { 1558 return dy; 1559 } 1560 1561 1562 public int getType () { 1563 return type; 1564 } 1565 } 1567 1570 private static class DefaultIconButton extends JButton { 1571 private Icon unknownIcon; 1572 1573 public Icon getIcon() { 1574 Icon retValue = super.getIcon(); 1575 if( null == retValue && (null == getText() || getText().length() == 0 ) ) { 1576 if (unknownIcon == null) { 1577 unknownIcon = new ImageIcon( Utilities.loadImage( "org/openide/loaders/unknown.gif") ); 1578 } 1579 retValue = unknownIcon; 1580 } 1581 return retValue; 1582 } 1583 } 1584 1585 private DnDSupport getDnd() { 1586 if (dnd == null) { 1587 dnd = new DnDSupport(); 1588 } 1589 return dnd; 1590 } 1591} | Popular Tags |