KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalToolBarUI


1 /*
2  * @(#)MetalToolBarUI.java 1.39 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.metal;
9
10 import javax.swing.*;
11 import java.awt.Color JavaDoc;
12 import java.awt.Component JavaDoc;
13 import java.awt.Container JavaDoc;
14 import java.awt.Dimension JavaDoc;
15 import java.awt.Frame JavaDoc;
16 import java.awt.Graphics JavaDoc;
17 import java.awt.GraphicsEnvironment JavaDoc;
18 import java.awt.Insets JavaDoc;
19 import java.awt.Point JavaDoc;
20 import java.awt.Rectangle JavaDoc;
21 import java.awt.event.*;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.*;
24
25 import java.beans.PropertyChangeListener JavaDoc;
26
27 import javax.swing.event.*;
28 import javax.swing.border.*;
29 import javax.swing.plaf.*;
30 import javax.swing.plaf.basic.*;
31
32 /**
33  * A Metal Look and Feel implementation of ToolBarUI. This implementation
34  * is a "combined" view/controller.
35  * <p>
36  *
37  * @version 1.39 12/19/03
38  * @author Jeff Shapiro
39  */

40 public class MetalToolBarUI extends BasicToolBarUI
41 {
42     /**
43      * An array of WeakReferences that point to JComponents. This will contain
44      * instances of JToolBars and JMenuBars and is used to find
45      * JToolBars/JMenuBars that border each other.
46      */

47     private static java.util.List JavaDoc components = new ArrayList();
48
49     /**
50      * This protected field is implemenation specific. Do not access directly
51      * or override. Use the create method instead.
52      *
53      * @see #createContainerListener
54      */

55     protected ContainerListener contListener;
56
57     /**
58      * This protected field is implemenation specific. Do not access directly
59      * or override. Use the create method instead.
60      *
61      * @see #createRolloverListener
62      */

63     protected PropertyChangeListener JavaDoc rolloverListener;
64
65     private static Border nonRolloverBorder;
66
67
68     /**
69      * Registers the specified component.
70      */

71     synchronized static void register(JComponent c) {
72         if (c == null) {
73             // Exception is thrown as convenience for callers that are
74
// typed to throw an NPE.
75
throw new NullPointerException JavaDoc("JComponent must be non-null");
76         }
77         components.add(new WeakReference JavaDoc(c));
78     }
79
80     /**
81      * Unregisters the specified component.
82      */

83     synchronized static void unregister(JComponent c) {
84         for (int counter = components.size() - 1; counter >= 0; counter--) {
85             // Search for the component, removing any flushed references
86
// along the way.
87
WeakReference JavaDoc ref = (WeakReference JavaDoc)components.get(counter);
88             Object JavaDoc target = ((WeakReference JavaDoc)components.get(counter)).get();
89
90             if (target == c || target == null) {
91                 components.remove(counter);
92             }
93         }
94     }
95
96     /**
97      * Finds a previously registered component of class <code>target</code>
98      * that shares the JRootPane ancestor of <code>from</code>.
99      */

100     synchronized static Object JavaDoc findRegisteredComponentOfType(JComponent from,
101                                                              Class JavaDoc target) {
102         JRootPane rp = SwingUtilities.getRootPane(from);
103         if (rp != null) {
104             for (int counter = components.size() - 1; counter >= 0; counter--){
105                 Object JavaDoc component = ((WeakReference JavaDoc)components.get(counter)).
106                                    get();
107
108                 if (component == null) {
109                     // WeakReference has gone away, remove the WeakReference
110
components.remove(counter);
111                 }
112                 else if (target.isInstance(component) && SwingUtilities.
113                          getRootPane((Component JavaDoc)component) == rp) {
114                     return component;
115                 }
116             }
117         }
118         return null;
119     }
120
121     /**
122      * Returns true if the passed in JMenuBar is above a horizontal
123      * JToolBar.
124      */

125     static boolean doesMenuBarBorderToolBar(JMenuBar c) {
126         JToolBar tb = (JToolBar)MetalToolBarUI.
127                     findRegisteredComponentOfType(c, JToolBar.class);
128         if (tb != null && tb.getOrientation() == JToolBar.HORIZONTAL) {
129             JRootPane rp = SwingUtilities.getRootPane(c);
130             Point JavaDoc point = new Point JavaDoc(0, 0);
131             point = SwingUtilities.convertPoint(c, point, rp);
132             int menuX = point.x;
133             int menuY = point.y;
134             point.x = point.y = 0;
135             point = SwingUtilities.convertPoint(tb, point, rp);
136             return (point.x == menuX && menuY + c.getHeight() == point.y &&
137                     c.getWidth() == tb.getWidth());
138         }
139         return false;
140     }
141
142     public static ComponentUI createUI( JComponent c )
143     {
144     return new MetalToolBarUI JavaDoc();
145     }
146
147     public void installUI( JComponent c )
148     {
149         super.installUI( c );
150         register(c);
151     }
152
153     public void uninstallUI( JComponent c )
154     {
155         super.uninstallUI( c );
156     nonRolloverBorder = null;
157         unregister(c);
158     }
159
160     protected void installListeners() {
161         super.installListeners();
162
163     contListener = createContainerListener();
164     if (contListener != null) {
165         toolBar.addContainerListener(contListener);
166     }
167     rolloverListener = createRolloverListener();
168     if (rolloverListener != null) {
169         toolBar.addPropertyChangeListener(rolloverListener);
170     }
171     }
172
173     protected void uninstallListeners() {
174         super.uninstallListeners();
175
176     if (contListener != null) {
177         toolBar.removeContainerListener(contListener);
178     }
179     rolloverListener = createRolloverListener();
180     if (rolloverListener != null) {
181         toolBar.removePropertyChangeListener(rolloverListener);
182     }
183     }
184
185     protected Border createRolloverBorder() {
186     return super.createRolloverBorder();
187     }
188
189     protected Border createNonRolloverBorder() {
190         return super.createNonRolloverBorder();
191     }
192
193
194     /**
195      * Creates a non rollover border for Toggle buttons in the toolbar.
196      */

197     private Border createNonRolloverToggleBorder() {
198     return createNonRolloverBorder();
199     }
200     
201     protected void setBorderToNonRollover(Component JavaDoc c) {
202     if (c instanceof JToggleButton && !(c instanceof JCheckBox)) {
203         // 4735514, 4886944: The method createNonRolloverToggleBorder() is
204
// private in BasicToolBarUI so we can't override it. We still need
205
// to call super from this method so that it can save away the
206
// original border and then we install ours.
207

208         // Before calling super we get a handle to the old border, because
209
// super will install a non-UIResource border that we can't
210
// distinguish from one provided by an application.
211
JToggleButton b = (JToggleButton)c;
212         Border border = b.getBorder();
213         super.setBorderToNonRollover(c);
214         if (border instanceof UIResource) {
215         if (nonRolloverBorder == null) {
216             nonRolloverBorder = createNonRolloverToggleBorder();
217         }
218         b.setBorder(nonRolloverBorder);
219         }
220     } else {
221         super.setBorderToNonRollover(c);
222     }
223     }
224
225
226     /**
227      * Creates a container listener that will be added to the JToolBar.
228      * If this method returns null then it will not be added to the
229      * toolbar.
230      *
231      * @return an instance of a <code>ContainerListener</code> or null
232      */

233     protected ContainerListener createContainerListener() {
234     return null;
235     }
236
237     /**
238      * Creates a property change listener that will be added to the JToolBar.
239      * If this method returns null then it will not be added to the
240      * toolbar.
241      *
242      * @return an instance of a <code>PropertyChangeListener</code> or null
243      */

244     protected PropertyChangeListener JavaDoc createRolloverListener() {
245     return null;
246     }
247
248     protected MouseInputListener createDockingListener( )
249     {
250     return new MetalDockingListener( toolBar );
251     }
252
253     protected void setDragOffset(Point JavaDoc p) {
254     if (!GraphicsEnvironment.isHeadless()) {
255         if (dragWindow == null) {
256         dragWindow = createDragWindow(toolBar);
257         }
258         dragWindow.setOffset(p);
259     }
260     }
261
262     /**
263      * If necessary paints the background of the component, then invokes
264      * <code>paint</code>.
265      *
266      * @param g Graphics to paint to
267      * @param c JComponent painting on
268      * @throws NullPointerException if <code>g</code> or <code>c</code> is
269      * null
270      * @see javax.swing.plaf.ComponentUI#update
271      * @see javax.swing.plaf.ComponentUI#paint
272      * @since 1.5
273      */

274     public void update(Graphics JavaDoc g, JComponent c) {
275         if (c.isOpaque() && (c.getBackground() instanceof UIResource) &&
276                             ((JToolBar)c).getOrientation() ==
277                       JToolBar.HORIZONTAL && UIManager.get(
278                      "MenuBar.gradient") != null) {
279             JRootPane rp = SwingUtilities.getRootPane(c);
280             JMenuBar mb = (JMenuBar)findRegisteredComponentOfType(
281                                     c, JMenuBar.class);
282             if (mb != null && mb.isOpaque() &&
283                               (mb.getBackground() instanceof UIResource)) {
284                 Point JavaDoc point = new Point JavaDoc(0, 0);
285                 point = SwingUtilities.convertPoint(c, point, rp);
286                 int x = point.x;
287                 int y = point.y;
288                 point.x = point.y = 0;
289                 point = SwingUtilities.convertPoint(mb, point, rp);
290                 if (point.x == x && y == point.y + mb.getHeight() &&
291                      mb.getWidth() == c.getWidth() &&
292                      MetalUtils.drawGradient(c, g, "MenuBar.gradient",
293                      0, -mb.getHeight(), c.getWidth(), c.getHeight() +
294                      mb.getHeight(), true)) {
295                     paint(g, c);
296                     return;
297                 }
298             }
299             if (MetalUtils.drawGradient(c, g, "MenuBar.gradient",
300                            0, 0, c.getWidth(), c.getHeight(), true)) {
301                 paint(g, c);
302                 return;
303             }
304         }
305         super.update(g, c);
306     }
307
308     // No longer used. Cannot remove for compatibility reasons
309
protected class MetalContainerListener
310     extends BasicToolBarUI.ToolBarContListener {}
311
312     // No longer used. Cannot remove for compatibility reasons
313
protected class MetalRolloverListener
314     extends BasicToolBarUI.PropertyListener {}
315
316     protected class MetalDockingListener extends DockingListener {
317         private boolean pressedInBumps = false;
318
319     public MetalDockingListener(JToolBar t) {
320         super(t);
321     }
322
323     public void mousePressed(MouseEvent e) {
324         super.mousePressed(e);
325             if (!toolBar.isEnabled()) {
326                 return;
327             }
328         pressedInBumps = false;
329         Rectangle JavaDoc bumpRect = new Rectangle JavaDoc();
330
331         if (toolBar.getOrientation() == JToolBar.HORIZONTAL) {
332         int x = MetalUtils.isLeftToRight(toolBar) ? 0 : toolBar.getSize().width-14;
333         bumpRect.setBounds(x, 0, 14, toolBar.getSize().height);
334         } else { // vertical
335
bumpRect.setBounds(0, 0, toolBar.getSize().width, 14);
336         }
337         if (bumpRect.contains(e.getPoint())) {
338             pressedInBumps = true;
339                 Point JavaDoc dragOffset = e.getPoint();
340                 if (!MetalUtils.isLeftToRight(toolBar)) {
341                     dragOffset.x -= (toolBar.getSize().width
342                      - toolBar.getPreferredSize().width);
343                 }
344                 setDragOffset(dragOffset);
345         }
346     }
347
348     public void mouseDragged(MouseEvent e) {
349         if (pressedInBumps) {
350             super.mouseDragged(e);
351         }
352     }
353     } // end class MetalDockingListener
354
}
355
Popular Tags