KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > Toolbar


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.EventObject JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.TooManyListenersException JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import javax.swing.*;
38 import javax.swing.border.*;
39 import javax.swing.event.*;
40 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
41 import javax.swing.plaf.synth.Region JavaDoc;
42 import javax.swing.plaf.synth.SynthConstants JavaDoc;
43 import javax.swing.plaf.synth.SynthContext JavaDoc;
44 import javax.swing.plaf.synth.SynthLookAndFeel JavaDoc;
45 import javax.swing.plaf.synth.SynthStyle JavaDoc;
46 import javax.swing.plaf.synth.SynthStyleFactory JavaDoc;
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 /**
55  * Toolbar provides a component which is useful for displaying commonly used
56  * actions. It can be dragged inside its <code>ToolbarPanel</code> to
57  * customize its location.
58  *
59  * @author David Peroutka, Libor Kramolis
60  */

61 public class Toolbar extends JToolBar /*implemented by patchsuperclass MouseInputListener*/ {
62     /** Basic toolbar height.
63      @deprecated Use getBasicHeight instead. */

64     @Deprecated JavaDoc
65     public static final int BASIC_HEIGHT = 34;
66     
67     /** 5 pixels is tolerance of toolbar height so toolbar can be high (BASIC_HEIGHT + HEIGHT_TOLERANCE)
68         but it will be set to BASIC_HEIGHT high. */

69     static int HEIGHT_TOLERANCE = 5;
70     /** TOP of toolbar empty border. */
71     static int TOP = 2;
72     /** LEFT of toolbar empty border. */
73     static int LEFT = 3;
74     /** BOTTOM of toolbar empty border. */
75     static int BOTTOM = 2;
76     /** RIGHT of toolbar empty border. */
77     static int RIGHT = 3;
78     /** Residual size of the toolbar when dragged far right */
79     static int RESIDUAL_WIDTH = 16;
80    
81
82     /** is toolbar floatable */
83     private boolean floatable;
84     /** Toolbar DnDListener */
85     private DnDListener listener;
86     /** Toolbar mouse listener */
87     private ToolbarMouseListener mouseListener;
88     /** display name of the toolbar */
89     private String JavaDoc displayName;
90     
91     /** Used for lazy creation of Folder and DisplayName */
92     private DataFolder backingFolder;
93     /* FolderInstance that will track all the changes in backingFolder */
94     private Folder processor;
95
96     //needed to turn off the painting of toolbar button borders on ocean/jdk1.5
97
private static final boolean isMetalLaF =
98             MetalLookAndFeel JavaDoc.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 JavaDoc 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 JavaDoc synthIconClass = null;
125         
126     private static boolean testExecuted = false;
127     
128     /** Create a new Toolbar with empty name. */
129     public Toolbar () {
130         this (""); // NOI18N
131
}
132
133     /** Create a new not floatable Toolbar with programmatic name.
134      * Display name is set to be the same as name */

135     public Toolbar (String JavaDoc name) {
136         this (name, name, false);
137     }
138
139     /** Create a new not floatable Toolbar with specified programmatic name
140      * and display name */

141     public Toolbar (String JavaDoc name, String JavaDoc displayName) {
142         this (name, displayName, false);
143     }
144     
145     /** Create a new <code>Toolbar</code>.
146      * @param name a <code>String</code> containing the associated name
147      * @param f specified if Toolbar is floatable
148      * Display name of the toolbar is set equal to the name.
149      */

150     public Toolbar (String JavaDoc 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     /**
162      * Test if SynthIcon is available and can be used for painting native Toolbar
163      * D&D handle. If not use our own handle. Reflection is used here as it is Sun
164      * proprietary API.
165      */

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 JavaDoc 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             //dragging over toolbar's grip
200
resetDropGesture();
201         } else {
202             //find out whether we want to drop before or after this component
203
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     /**
248      * Remove a toolbar button represented by the given Transferable.
249      */

250     private void removeButton( Transferable t ) {
251         try {
252             Object JavaDoc o = null;
253             if( t.isDataFlavorSupported( buttonDataFlavor ) ) {
254                 o = t.getTransferData( buttonDataFlavor ); //XXX
255
}
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 JavaDoc() {
261
262                                                                public void run() {
263                                                                    try {
264                                                                        backingFolder.delete();
265                                                                    }
266                                                                    catch (java.io.IOException JavaDoc 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 JavaDoc ioe) {
278             Exceptions.printStackTrace(ioe);
279         }
280     }
281     
282     /**
283      * Perform the drop operation.
284      *
285      * @return True if the drop has been successful.
286      */

287     private boolean handleDrop( Transferable t ) {
288         try {
289             Object JavaDoc 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 JavaDoc ioe) {
305             Exceptions.printStackTrace(ioe);
306         }
307         return false;
308     }
309
310     /**
311      * Component index of the button under the drag cursor, or -1 when the cursor
312      * is above the toolbar drag handle
313      */

314     int dropTargetButtonIndex = -1;
315     /**
316      * Component index of the button being dragged, only used when dragging a button
317      * within the same toolbar.
318      */

319     int dragSourceButtonIndex = -1;
320     /**
321      * True if the button being dragged should be dropped BEFORE the button
322      * under the drag cursor.
323      */

324     boolean insertBefore = true;
325     /**
326      * True indicates the toolbar instance whose button is being dragged.
327      */

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 JavaDoc e) {
352                     //do nothing
353
}
354             }
355         }
356         DragGestureRecognizer dgr = null;
357
358         public void dragEnter(DragSourceDragEvent e) {
359             //handled in dragMouseMoved
360
}
361
362         public void dragOver(DragSourceDragEvent e) {
363             //handled in dragMouseMoved
364
}
365         
366         public void dragExit(DragSourceEvent e) {
367             //handled in dragMouseMoved
368
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                  //do not allow to drag toolbar separators
390
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 JavaDoc 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             //ignore
421
}
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             //ignore
436
}
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     /**
482      * Add a new toolbar button represented by the given DataObject.
483      */

484     private boolean addButton( DataObject dobj, int dropIndex, boolean dropBefore ) throws IOException JavaDoc {
485         if( null == dobj )
486             return false;
487         //check if the dropped button (action) already exists in this toolbar
488
String JavaDoc objName = dobj.getName();
489         DataObject[] children = backingFolder.getChildren();
490         for( int i=0; i<children.length; i++ ) {
491             //TODO is comparing DataObject names ok?
492
if( objName.equals( children[i].getName() ) ) {
493                 //user dropped to toolbat a new button that already exists in this toolbar
494
//just move the existing button to a new position
495
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         //find the added object
505
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 ); //put the button to its proper position
516

517         return true;
518     }
519     
520     /**
521      * Move toolbar button to a new position.
522      */

523     private boolean moveButton( DataObject ob, int dropIndex, boolean dropBefore ) throws IOException JavaDoc {
524         //find out which button is currently under the drag cursor
525
DataObject objUnderCursor = getDataObjectUnderDropCursor( dropIndex, dropBefore );
526
527         if( !isDragSourceToolbar ) {
528             //move button to the new toolbar
529
ob.move(backingFolder);
530         }
531
532         reorderButtons( ob, objUnderCursor );
533         //else we're dragging a button to an empty toolbar
534
return true;
535     }
536     
537     private void reorderButtons( DataObject objToMove, DataObject objUnderCursor ) throws IOException JavaDoc {
538         java.util.List JavaDoc<DataObject> children =
539                 new ArrayList JavaDoc<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                //the drag cursor cannot be positioned above toolbar's drag handle
571
return dropTargetButtonIndex >= 0
572                //when toolbar has buttons '1 2 3 4 5' and we're dragging button 3,
573
//do not allow drop between buttons 2 and 3 and also between buttons 3 and 3
574
&& !(isDragSourceToolbar && (dragSourceButtonIndex == dropTargetButtonIndex //drop index 3
575
|| (dropTargetButtonIndex == dragSourceButtonIndex-1 && !insertBefore) //drop index 2
576
|| (dropTargetButtonIndex == dragSourceButtonIndex+1 && insertBefore))) //drop index 4
577
//dragging a button to an empty toolbar
578
|| (dropTargetButtonIndex < 0 && getComponentCount() == 1);
579     }
580
581     /** Start tracking content of the underlaying folder if not doing so yet */
582     final Folder waitFinished() {
583         // check for too early call (from constructor and UI.setUp...)
584
if (backingFolder == null) return null;
585         
586         if(processor == null && isVisible()) {
587             processor = new Folder(); // It will start tracking immediatelly
588
}
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     /** Overridden to set focusable to false for any AbstractButton
609      * subclasses which are added */

610     protected void addImpl(Component c, Object JavaDoc constraints, int idx) {
611         //issue 39896, after opening dialog from toolbar button, focus
612
//remains on toolbar button. Does not create an accessibility issue -
613
//all standard toolbar buttons are also available via the keyboard
614
if (c instanceof AbstractButton) {
615             c.setFocusable(false);
616             ((JComponent) c).setOpaque(false);
617             if( isMetalLaF && (isJdk15 || isJdk16)) {
618                 //JDK 1.5 metal/ocean resets borders, so fix it this way
619
((AbstractButton) c).setBorderPainted(false);
620                 ((AbstractButton) c).setOpaque(false);
621             }
622             //This is active for GTK L&F. It should be fixed in JDK
623
//but it is not fixed in JDK 6.0.
624
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     /**
643      * Create a new <code>Toolbar</code>.
644      * @param name a <code>String</code> containing the associated name
645      * @param f specified if Toolbar is floatable
646      */

647     public Toolbar (String JavaDoc name, String JavaDoc displayName, boolean f) {
648         super();
649         setDisplayName (displayName);
650         initAll(name, f);
651     }
652     
653     /** Returns basic toolbar height according to preferred icons size. Used by
654      * toolbar layout manager.
655      * @return basic toolbar height
656      * @since 4.15
657      */

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 JavaDoc name, boolean f) {
667         floatable = f;
668         mouseListener = null;
669
670         setName (name);
671         
672         setFloatable (false);
673         String JavaDoc lAndF = UIManager.getLookAndFeel().getName();
674         
675         if (lAndF.equals("Windows")) {
676             //Get rid of extra height, also allow for minimalist main
677
//window
678
setBorder(Boolean.getBoolean("netbeans.small.main.window") ?
679                 BorderFactory.createEmptyBorder(1,1,1,1) :
680                 BorderFactory.createEmptyBorder()); //NOI18N
681
} else if (!"Aqua".equals(UIManager.getLookAndFeel().getID()) && !"GTK".equals(UIManager.getLookAndFeel().getID())){
682             Border b = UIManager.getBorder ("ToolBar.border"); //NOI18N
683

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); // NOI18N
695
}
696         addGrip();
697
698         getAccessibleContext().setAccessibleName(displayName == null ? getName() : displayName);
699         getAccessibleContext().setAccessibleDescription(getName());
700     }
701
702     public String JavaDoc getUIClassID() {
703         if (UIManager.get("Nb.Toolbar.ui") != null) { //NOI18N
704
return "Nb.Toolbar.ui"; //NOI18N
705
} else {
706             return super.getUIClassID();
707         }
708     }
709     
710     public Dimension getPreferredSize() {
711         String JavaDoc 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     /** Removes all ACTION components. */
745     public void removeAll () {
746         super.removeAll();
747         addGrip();
748     }
749
750     /**
751      * When Toolbar is floatable, ToolbarBump is added as Grip as first toolbar component
752      * modified by Michael Wever, to use l&f's grip/bump. */

753     void addGrip () {
754         if (floatable) {
755             /** Uses L&F's grip **/
756             String JavaDoc lAndF = UIManager.getLookAndFeel().getName();
757             //XXX should use getID() note getName() - Tim
758
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                 //setFloatable(true);
770
} else {
771                 //Default for Metal and uknown L&F
772
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     /** Compute with HEIGHT_TOLERANCE number of rows for specific toolbar height.
789      * @param height of some toolbar
790      * @return number of rows
791      */

792     static public int rowCount (int height) {
793         return 1 + height / (getBasicHeight() + HEIGHT_TOLERANCE+customFontHeightCorrection);
794     }
795
796     /** Set DnDListener to Toolbar.
797      * @param l DndListener for toolbar
798      */

799     public void setDnDListener (DnDListener l) {
800         listener = l;
801     }
802     
803     /** @return Display name of this toolbar. Display name is localizable,
804      * on the contrary to the programmatic name */

805     public String JavaDoc getDisplayName () {
806         if (displayName == null) {
807             if (!backingFolder.isValid()) {
808                 // #17020
809
return backingFolder.getName();
810             }
811             return backingFolder.getNodeDelegate ().getDisplayName ();
812         }
813         return displayName;
814     }
815     
816     /** Sets new display name of this toolbar. Display name is localizable,
817      * on the contrary to the programmatic name */

818     public void setDisplayName (String JavaDoc displayName) {
819         this.displayName = displayName;
820     }
821
822     /** Fire drag of Toolbar
823      * @param dx distance of horizontal dragging
824      * @param dy distance of vertical dragging
825      * @param type type of toolbar dragging
826      */

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     /** Fire drop of Toolbar
833      * @param dx distance of horizontal dropping
834      * @param dy distance of vertical dropping
835      * @param type type of toolbar dropping
836      */

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     /** Toolbar mouse listener. */
848     class ToolbarMouseListener extends MouseInputAdapter {
849         /** Is toolbar dragging now. */
850         private boolean dragging = false;
851         /** Start point of dragging. */
852         private Point startPoint = null;
853
854         /** Invoked when a mouse button has been pressed on a component. */
855         public void mousePressed (MouseEvent e) {
856             startPoint = e.getPoint();
857         }
858
859         /** Invoked when a mouse button has been released on a component. */
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         /** Invoked when a mouse button is pressed on a component and then dragged. */
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     } // end of inner class ToolbarMouseListener
907

908     /**
909      * This class can be used to produce a <code>Toolbar</code> instance from
910      * the given <code>DataFolder</code>.
911      */

912     final class Folder extends FolderInstance {
913
914         /**
915          * Creates a new folder on the specified <code>DataFolder</code>.
916          *
917          */

918         public Folder () {
919             super (backingFolder);
920             recreate ();
921         }
922
923         /**
924          * Full name of the data folder's primary file separated by dots.
925          * @return the name
926          */

927         public String JavaDoc instanceName () {
928             return Toolbar.this.getClass().getName();
929         }
930
931         /**
932          * Returns the root class of all objects.
933          * @return Object.class
934          */

935         public Class JavaDoc instanceClass ()
936         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
937             return Toolbar.this.getClass();
938         }
939
940         /** If no instance cookie, tries to create execution action on the
941          * data object.
942          */

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 JavaDoc<Object JavaDoc, Object JavaDoc> cookiesToObjects = new HashMap JavaDoc<Object JavaDoc, Object JavaDoc>();
957     
958     protected Object JavaDoc instanceForCookie (DataObject obj, InstanceCookie cookie)
959     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
960         Object JavaDoc result = super.instanceForCookie(obj, cookie);
961         cookiesToObjects.put (result, obj);
962         return result;
963     }
964         
965
966         /**
967          * Accepts only cookies that can provide <code>Toolbar</code>.
968          * @param cookie an <code>InstanceCookie</code> to test
969          * @return true if the cookie can provide accepted instances
970          */

971         protected InstanceCookie acceptCookie (InstanceCookie cookie)
972         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
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 JavaDoc 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         /**
990          * Returns a <code>Toolbar.Folder</code> cookie for the specified
991          * <code>DataFolder</code>.
992          * @param df a <code>DataFolder</code> to create the cookie for
993          * @return a <code>Toolbar.Folder</code> for the specified folder
994          */

995         protected InstanceCookie acceptFolder(DataFolder df) {
996             return null; // PENDING new Toolbar.Folder(df);
997
}
998
999         /**
1000         * Updates the <code>Toolbar</code> represented by this folder.
1001         *
1002         * @param cookies array of instance cookies for the folder
1003         * @return the updated <code>ToolbarPool</code> representee
1004         */

1005        protected Object JavaDoc createInstance(final InstanceCookie[] cookies)
1006        throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
1007            // refresh the toolbar's content
1008
Toolbar.this.removeAll();
1009            for (int i = 0; i < cookies.length; i++) {
1010                try {
1011                    java.lang.Object JavaDoc obj = cookies[i].instanceCreate();
1012                    java.lang.Object JavaDoc 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 JavaDoc) {
1018                        // remove border and grip if requested. "Fixed" toolbar
1019
// item has to live alone in toolbar now
1020
if ((obj instanceof javax.swing.JComponent JavaDoc) &&
1021                            "Fixed".equals(((javax.swing.JComponent JavaDoc) obj).getClientProperty("Toolbar"))) {
1022                            floatable = false;
1023                            org.openide.awt.Toolbar.this.removeAll();
1024                            setBorder(null);
1025                        }
1026                        if (obj instanceof javax.swing.JComponent JavaDoc) {
1027                            if (org.openide.awt.ToolbarPool.getDefault().getPreferredIconSize() ==
1028                                24) {
1029                                ((javax.swing.JComponent JavaDoc) obj).putClientProperty("PreferredIconSize",
1030                                                                                 new java.lang.Integer JavaDoc(24));
1031                            }
1032                            ((javax.swing.JComponent JavaDoc) obj).putClientProperty("file",
1033                                                                             file);
1034                        }
1035                        org.openide.awt.Toolbar.this.add((java.awt.Component JavaDoc) obj);
1036                        continue;
1037                    }
1038                    if (obj instanceof javax.swing.Action JavaDoc) {
1039                        javax.swing.Action JavaDoc a = (javax.swing.Action JavaDoc) obj;
1040                        javax.swing.JButton JavaDoc 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 JavaDoc(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 JavaDoc ex) {
1061                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, ex);
1062                }
1063                catch (java.lang.ClassNotFoundException JavaDoc ex) {
1064                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, ex);
1065                }
1066                finally {
1067                    cookiesToObjects.clear();
1068                }
1069            }
1070
1071            // invalidate the toolbar, trigger proper relayout
1072
Toolbar.this.invalidate ();
1073            return Toolbar.this;
1074        }
1075
1076        /** Recreate the instance in AWT thread.
1077        */

1078        protected Task postCreationTask (Runnable JavaDoc run) {
1079            return new AWTTask (run);
1080        }
1081
1082    } // end of inner class Folder
1083

1084    /** Bumps for floatable toolbar */
1085    private final class ToolbarBump extends JPanel {
1086        /** Top gap. */
1087        static final int TOPGAP = 2;
1088        /** Bottom gap. */
1089        static final int BOTGAP = 2;
1090        /** Width of bump element. */
1091        static final int WIDTH = 6;
1092        
1093        /** Minimum size. */
1094        Dimension dim;
1095        /** Maximum size. */
1096        Dimension max;
1097
1098        static final long serialVersionUID =-8819972936203315277L;
1099
1100        /** Create new ToolbarBump. */
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        /** Paint bumps to specific Graphics. */
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        /** @return minimum size */
1132        public Dimension getMinimumSize () {
1133            return dim;
1134        }
1135
1136        /** @return preferred size */
1137        public Dimension getPreferredSize () {
1138            return this.getMinimumSize ();
1139        }
1140
1141        public Dimension getMaximumSize () {
1142            return max;
1143        }
1144    } // end of inner class ToolbarBump
1145

1146    /** Bumps for floatable toolbar GTK L&F */
1147    private final class ToolbarGtk extends JPanel {
1148        /** Top gap. */
1149        static final int TOPGAP = 2;
1150        /** Bottom gap. */
1151        static final int BOTGAP = 2;
1152        /** Width of bump element. */
1153        static final int WIDTH = 6;
1154        
1155        /** Minimum size. */
1156        Dimension dim;
1157        /** Maximum size. */
1158        Dimension max;
1159
1160        static final long serialVersionUID = -8819972936203315277L;
1161        
1162        /** Create new ToolbarBump. */
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        /** Paint bumps to specific Graphics. */
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 JavaDoc region = Region.TOOL_BAR;
1177                SynthLookAndFeel JavaDoc laf = (SynthLookAndFeel JavaDoc)UIManager.getLookAndFeel();
1178                SynthStyleFactory JavaDoc sf = laf.getStyleFactory();
1179                SynthStyle JavaDoc style = sf.getStyle(Toolbar.this, region);
1180                SynthContext JavaDoc context = new SynthContext JavaDoc(Toolbar.this, region, style, SynthConstants.DEFAULT);
1181
1182                // for vertical toolbar, you'll need to ask for getIconHeight() instead
1183
Method JavaDoc m = null;
1184                try {
1185                    m = synthIconClass.getMethod("getIconWidth",Icon.class, SynthContext JavaDoc.class);
1186                } catch (NoSuchMethodException JavaDoc exc) {
1187                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc);
1188                }
1189                int width = 0;
1190                //width = SynthIcon.getIconWidth(icon, context);
1191
try {
1192                    width = (Integer JavaDoc) m.invoke(null, new Object JavaDoc [] {icon, context});
1193                } catch (IllegalAccessException JavaDoc exc) {
1194                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc);
1195                } catch (InvocationTargetException JavaDoc exc) {
1196                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc);
1197                }
1198                try {
1199                    m = synthIconClass.getMethod("paintIcon",Icon.class,SynthContext JavaDoc.class,
1200                    Graphics.class,Integer.TYPE,Integer.TYPE,Integer.TYPE,Integer.TYPE);
1201                } catch (NoSuchMethodException JavaDoc exc) {
1202                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc);
1203                }
1204                //SynthIcon.paintIcon(icon, context, g, 0, 0, width, height);
1205
try {
1206                    m.invoke(null, new Object JavaDoc [] {icon,context,g,new Integer JavaDoc(0),new Integer JavaDoc(0),
1207                    new Integer JavaDoc(width),new Integer JavaDoc(height)});
1208                } catch (IllegalAccessException JavaDoc exc) {
1209                    Logger.getLogger(Toolbar.class.getName()).log(Level.WARNING, null, exc);
1210                } catch (InvocationTargetException JavaDoc 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        /** @return minimum size */
1236        public Dimension getMinimumSize () {
1237            return dim;
1238        }
1239
1240        /** @return preferred size */
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    } // end of inner class ToolbarGtk
1249

1250    /** Recognizes if XP theme is set.
1251     * @return true if XP theme is set, false otherwise
1252     */

1253    private static Boolean JavaDoc isXP = null;
1254    private static boolean isXPTheme () {
1255        if (isXP == null) {
1256            Boolean JavaDoc xp = (Boolean JavaDoc)Toolkit.getDefaultToolkit().
1257            getDesktopProperty("win.xpstyle.themeActive"); //NOI18N
1258
isXP = Boolean.TRUE.equals(xp)? Boolean.TRUE : Boolean.FALSE;
1259        }
1260        return isXP.booleanValue();
1261    }
1262    
1263    private final class ToolbarAqua extends JPanel {
1264        /** Width of grip */
1265        static final int WIDTH = 8;
1266        /** Minimum size. */
1267        Dimension dim;
1268        /** Maximum size. */
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 JavaDoc 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        /** @return minimum size */
1303        public Dimension getMinimumSize () {
1304            return dim;
1305        }
1306        
1307        /** @return preferred size */
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 JavaDoc<RenderingHints.Key, Object JavaDoc> hintsMap = null;
1318    @SuppressWarnings JavaDoc("unchecked")
1319    static final Map JavaDoc getHints() {
1320        //XXX We REALLY need to put this in a graphics utils lib
1321
if (hintsMap == null) {
1322            //Thanks to Phil Race for making this possible
1323
hintsMap = (Map JavaDoc<RenderingHints.Key, Object JavaDoc>)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
1324
if (hintsMap == null) {
1325                hintsMap = new HashMap JavaDoc<RenderingHints.Key, Object JavaDoc>();
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 JavaDoc ui) {
1333        super.setUI(ui);
1334        if( null != backingFolder && null != processor ) {
1335            //recreate the toolbar buttons as their borders need to be reset
1336
processor.recreate();
1337        }
1338    }
1339    
1340    private final class ToolbarXP extends JPanel {
1341        /** Width of grip */
1342        static final int WIDTH = 7;
1343        /** Minimum size. */
1344        Dimension dim;
1345        /** Maximum size. */
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                //first draw the rectangular highlight below each dot
1360
g.setColor(UIManager.getColor("controlLtHighlight")); //NOI18N
1361
g.fillRect(x + 1, i + 1, 2, 2);
1362                //Get the shadow color. We'll paint the darkest dot first,
1363
//and work our way to the lightest
1364
Color col = UIManager.getColor("controlShadow"); //NOI18N
1365
g.setColor(col);
1366                //draw the darkest dot
1367
g.drawLine(x+1, i+1, x+1, i+1);
1368                
1369                //Get the color components and calculate the amount each component
1370
//should increase per dot
1371
int red = col.getRed();
1372                int green = col.getGreen();
1373                int blue = col.getBlue();
1374                
1375                //Get the default component background - we start with the dark
1376
//color, and for each dot, add a percentage of the difference
1377
//between this and the background color
1378
Color back = getBackground();
1379                int rb = back.getRed();
1380                int gb = back.getGreen();
1381                int bb = back.getBlue();
1382                
1383                //Get the amount to increment each component for each dot
1384
int incr = (rb - red) / 5;
1385                int incg = (gb - green) / 5;
1386                int incb = (bb - blue) / 5;
1387                
1388                //Increment the colors
1389
red += incr;
1390                green += incg;
1391                blue += incb;
1392                //Create a slightly lighter color and draw the dot
1393
col = new Color(red, green, blue);
1394                g.setColor(col);
1395                g.drawLine(x+1, i, x+1, i);
1396                
1397                //And do it for the next dot, and so on, for all four dots
1398
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        /** @return minimum size */
1415        public Dimension getMinimumSize () {
1416            return dim;
1417        }
1418        
1419        /** @return preferred size */
1420        public Dimension getPreferredSize () {
1421            return this.getMinimumSize ();
1422        }
1423        
1424        public Dimension getMaximumSize () {
1425            return max;
1426        }
1427    }
1428    
1429  /*
1430    public static void main(String[] args) {
1431        JFrame jf = new JFrame();
1432        jf.getContentPane().add (new ToolbarXP());
1433        jf.setSize(new java.awt.Dimension(200,200));
1434        jf.setLocation(20,20);
1435        jf.show();
1436    }
1437   */

1438
1439    
1440    /** Grip for floatable toolbar, used for Windows Classic L&F */
1441    private final class ToolbarGrip extends JPanel {
1442        /** Horizontal gaps. */
1443        static final int HGAP = 1;
1444        /** Vertical gaps. */
1445        static final int VGAP = 2;
1446        /** Step between two grip elements. */
1447        static final int STEP = 1;
1448        /** Width of grip element. */
1449        static final int WIDTH = 2;
1450
1451        /** Number of grip elements. */
1452        int columns;
1453        /** Minimum size. */
1454        Dimension dim;
1455        /** Maximum size. */
1456        Dimension max;
1457
1458        static final long serialVersionUID =-8819972936203315276L;
1459
1460        /** Create new ToolbarGrip for default number of grip elements. */
1461        public ToolbarGrip () {
1462            this(1);
1463        }
1464
1465        /** Create new ToolbarGrip for specific number of grip elements.
1466         * @param col number of grip elements
1467         */

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        /** Paint grip to specific Graphics. */
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); // grip element is 3D rectangle now
1488
}
1489
1490        }
1491
1492        /** @return minimum size */
1493        public Dimension getMinimumSize () {
1494            return dim;
1495        }
1496
1497        /** @return preferred size */
1498        public Dimension getPreferredSize () {
1499            return this.getMinimumSize();
1500        }
1501        
1502        public Dimension getMaximumSize () {
1503            return max;
1504        }
1505        
1506    } // end of inner class ToolbarGrip
1507

1508    /** DnDListener is Drag and Drop listener for Toolbar motion events. */
1509    public interface DnDListener extends java.util.EventListener JavaDoc {
1510        /** Invoced when toolbar is dragged. */
1511        public void dragToolbar (DnDEvent e);
1512
1513        /** Invoced when toolbar is dropped. */
1514        public void dropToolbar (DnDEvent e);
1515    } // end of interface DnDListener
1516

1517
1518    /** DnDEvent is Toolbar's drag and drop event. */
1519    public static class DnDEvent extends EventObject JavaDoc {
1520        /** Type of DnDEvent. Dragging with only one Toolbar. */
1521        public static final int DND_ONE = 1;
1522        /** Type of DnDEvent. Only horizontal dragging with Toolbar and it's followers. */
1523        public static final int DND_END = 2;
1524        /** Type of DnDEvent. Only vertical dragging with whole lines. */
1525        public static final int DND_LINE = 3;
1526
1527        /** Name of toolbar where event occured. */
1528        private String JavaDoc name;
1529        /** distance of horizontal dragging */
1530        private int dx;
1531        /** distance of vertical dragging */
1532        private int dy;
1533        /** Type of event. */
1534        private int type;
1535
1536        static final long serialVersionUID =4389530973297716699L;
1537        public DnDEvent (Toolbar toolbar, String JavaDoc 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        /** @return name of toolbar where event occured. */
1547        public String JavaDoc getName () {
1548            return name;
1549        }
1550
1551        /** @return distance of horizontal dragging */
1552        public int getDX () {
1553            return dx;
1554        }
1555
1556        /** @return distance of vertical dragging */
1557        public int getDY () {
1558            return dy;
1559        }
1560
1561        /** @return type of event. */
1562        public int getType () {
1563            return type;
1564        }
1565    } // end of class DnDEvent
1566

1567    /**
1568     * A button that provides a default icon when no text and no custom icon have been set.
1569     */

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} // end of class Toolbar
1592
Popular Tags