Source Code Cross Referenced for BasicToolBarUI.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » plaf » basic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing.plaf.basic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Vadim L. Bogdanov
019:         * @version $Revision$
020:         */package javax.swing.plaf.basic;
021:
022:        import java.awt.BorderLayout;
023:        import java.awt.Color;
024:        import java.awt.Component;
025:        import java.awt.Container;
026:        import java.awt.Dialog;
027:        import java.awt.Dimension;
028:        import java.awt.Frame;
029:        import java.awt.Graphics;
030:        import java.awt.Insets;
031:        import java.awt.Point;
032:        import java.awt.Rectangle;
033:        import java.awt.Window;
034:        import java.awt.event.ActionEvent;
035:        import java.awt.event.ContainerEvent;
036:        import java.awt.event.ContainerListener;
037:        import java.awt.event.FocusEvent;
038:        import java.awt.event.FocusListener;
039:        import java.awt.event.MouseEvent;
040:        import java.awt.event.WindowAdapter;
041:        import java.awt.event.WindowEvent;
042:        import java.awt.event.WindowListener;
043:        import java.beans.PropertyChangeEvent;
044:        import java.beans.PropertyChangeListener;
045:
046:        import javax.swing.AbstractAction;
047:        import javax.swing.AbstractButton;
048:        import javax.swing.ActionMap;
049:        import javax.swing.InputMap;
050:        import javax.swing.JComponent;
051:        import javax.swing.JDialog;
052:        import javax.swing.JFrame;
053:        import javax.swing.JToolBar;
054:        import javax.swing.KeyStroke;
055:        import javax.swing.LookAndFeel;
056:        import javax.swing.RootPaneContainer;
057:        import javax.swing.SwingConstants;
058:        import javax.swing.SwingUtilities;
059:        import javax.swing.UIManager;
060:        import javax.swing.border.Border;
061:        import javax.swing.event.MouseInputListener;
062:        import javax.swing.plaf.ActionMapUIResource;
063:        import javax.swing.plaf.BorderUIResource;
064:        import javax.swing.plaf.ComponentUI;
065:        import javax.swing.plaf.ToolBarUI;
066:
067:        import org.apache.harmony.x.swing.StringConstants;
068:        import org.apache.harmony.x.swing.Utilities;
069:
070:        public class BasicToolBarUI extends ToolBarUI implements  SwingConstants {
071:            private static final String SAVED_BORDER_PROPERTY = "JToolBar.savedBorder";
072:            private static final String SAVED_ROLLOVER_ENABLED_PROPERTY = "JToolBar.rolloverEnabled";
073:
074:            public class DockingListener implements  MouseInputListener {
075:                protected boolean isDragging;
076:                protected Point origin = new Point();
077:                protected JToolBar toolBar;
078:
079:                public DockingListener(final JToolBar t) {
080:                    toolBar = t;
081:                }
082:
083:                public void mouseDragged(final MouseEvent e) {
084:                    if (!isDragging) {
085:                        isDragging = true;
086:                        origin.setLocation(0, 0);
087:                        SwingUtilities.convertPointToScreen(origin, toolBar);
088:                    }
089:                    dragTo(e.getPoint(), origin);
090:                }
091:
092:                public void mouseReleased(final MouseEvent e) {
093:                    if (isDragging) {
094:                        floatAt(e.getPoint(), origin);
095:                        isDragging = false;
096:                        if (dragWindow != null) {
097:                            dragWindow.setOffset(null);
098:                        }
099:                    }
100:                }
101:
102:                public void mouseClicked(final MouseEvent e) {
103:                }
104:
105:                public void mouseEntered(final MouseEvent e) {
106:                }
107:
108:                public void mouseExited(final MouseEvent e) {
109:                }
110:
111:                public void mouseMoved(final MouseEvent e) {
112:                }
113:
114:                public void mousePressed(final MouseEvent e) {
115:                }
116:            }
117:
118:            protected class DragWindow extends Window {
119:                private Point offset;
120:                private int orientation;
121:                private Color borderColor;
122:
123:                private DragWindow(final Window owner) {
124:                    super (owner);
125:                    setFocusableWindowState(false);
126:                }
127:
128:                public Insets getInsets() {
129:                    return new Insets(1, 1, 1, 1);
130:                }
131:
132:                public void paint(final Graphics g) {
133:                    paintDragWindow(g);
134:                }
135:
136:                public void setBorderColor(final Color c) {
137:                    if (borderColor != c) {
138:                        borderColor = c;
139:                        repaint();
140:                    }
141:                }
142:
143:                public Color getBorderColor() {
144:                    return borderColor;
145:                }
146:
147:                public void setOffset(final Point p) {
148:                    offset = p;
149:                }
150:
151:                public Point getOffset() {
152:                    return offset;
153:                }
154:
155:                public void setOrientation(final int o) {
156:                    orientation = o;
157:                    repaint();
158:                }
159:
160:                private void updateBounds(final int o, final Point newLocation) {
161:                    if (orientation != o) {
162:                        Rectangle bounds = SwingUtilities.getLocalBounds(this );
163:                        setOffset(new Point(offset.y, offset.x));
164:                        bounds.setLocation((int) bounds.getCenterX()
165:                                - getOffset().x, (int) bounds.getCenterY()
166:                                - getOffset().y);
167:                        bounds.setSize(getHeight(), getWidth());
168:                        bounds.setLocation(newLocation);
169:                        setBounds(bounds);
170:                    } else {
171:                        setLocation(newLocation);
172:                    }
173:                    setOrientation(o);
174:                }
175:            }
176:
177:            protected class FrameListener extends WindowAdapter {
178:                public void windowClosing(final WindowEvent w) {
179:                    setFloating(false, null);
180:                }
181:            }
182:
183:            protected class PropertyListener implements  PropertyChangeListener {
184:                public void propertyChange(final PropertyChangeEvent e) {
185:                    String propertyName = e.getPropertyName();
186:                    if ("JToolBar.isRollover".equals(propertyName)) {
187:                        setRolloverBorders(((Boolean) e.getNewValue())
188:                                .booleanValue());
189:                        toolBarSizeChange();
190:                    } else if ("orientation".equals(propertyName)
191:                            || "floatable".equals(propertyName)
192:                            || "borderPainted".equals(propertyName)
193:                            || "margin".equals(propertyName)) {
194:                        toolBarSizeChange();
195:                    }
196:                }
197:
198:                private void toolBarSizeChange() {
199:                    if (isFloating()) {
200:                        toolBar.invalidate();
201:                        ((Container) floatingWindow)
202:                                .setSize(((Container) floatingWindow)
203:                                        .getPreferredSize());
204:                        ((Container) floatingWindow).validate();
205:                    } else {
206:                        toolBar.revalidate();
207:                    }
208:                    toolBar.repaint();
209:                }
210:            }
211:
212:            protected class ToolBarContListener implements  ContainerListener {
213:                public void componentAdded(final ContainerEvent e) {
214:                    if (isRolloverBorders()) {
215:                        setBorderToRollover(e.getChild());
216:                    } else {
217:                        setBorderToNonRollover(e.getChild());
218:                    }
219:                    e.getChild().addFocusListener(toolBarFocusListener);
220:                }
221:
222:                public void componentRemoved(final ContainerEvent e) {
223:                    setBorderToNormal(e.getChild());
224:                    e.getChild().removeFocusListener(toolBarFocusListener);
225:                }
226:            }
227:
228:            protected class ToolBarFocusListener implements  FocusListener {
229:                public void focusGained(final FocusEvent e) {
230:                    focusedCompIndex = toolBar.getComponentIndex(e
231:                            .getComponent());
232:                }
233:
234:                public void focusLost(final FocusEvent e) {
235:                    // does nothing
236:                }
237:            }
238:
239:            private class NavigateAction extends AbstractAction {
240:                private int direction;
241:
242:                public NavigateAction(final int direction) {
243:                    this .direction = direction;
244:                }
245:
246:                public void actionPerformed(final ActionEvent e) {
247:                    navigateFocusedComp(direction);
248:                }
249:            }
250:
251:            private class FloatingWindow extends JDialog {
252:                public FloatingWindow(final Frame owner, final String title) {
253:                    super (owner, title, false);
254:                }
255:
256:                public FloatingWindow(final Dialog owner, final String title) {
257:                    super (owner, title, false);
258:                }
259:            }
260:
261:            public static ComponentUI createUI(final JComponent c) {
262:                return new BasicToolBarUI();
263:            }
264:
265:            protected DragWindow dragWindow;
266:            protected JToolBar toolBar;
267:            protected String constraintBeforeFloating = BorderLayout.NORTH;
268:            protected int focusedCompIndex = -1;
269:
270:            protected Color dockingBorderColor;
271:            protected Color dockingColor;
272:            protected Color floatingBorderColor;
273:            protected Color floatingColor;
274:
275:            protected MouseInputListener dockingListener;
276:            protected PropertyChangeListener propertyListener;
277:            protected ContainerListener toolBarContListener;
278:            protected FocusListener toolBarFocusListener;
279:
280:            /**
281:             * @deprecated
282:             */
283:            protected KeyStroke downKey;
284:            /**
285:             * @deprecated
286:             */
287:            protected KeyStroke leftKey;
288:            /**
289:             * @deprecated
290:             */
291:            protected KeyStroke rightKey;
292:            /**
293:             * @deprecated
294:             */
295:            protected KeyStroke upKey;
296:
297:            private boolean isRolloverBorders;
298:            private Border rolloverBorder;
299:            private Border nonRolloverBorder;
300:            private ActionMap actionMap;
301:            private RootPaneContainer floatingWindow;
302:            private boolean floating;
303:            private Container parentBeforeFloating;
304:            private Window defaultWindowOwner;
305:            private Point floatingLocation = new Point();
306:
307:            private Color darkShadow;
308:            private Color highlight;
309:            private Color light;
310:            private Color shadow;
311:
312:            private Color buttonDarkShadow;
313:            private Color buttonHighlight;
314:            private Color buttonLight;
315:            private Color buttonShadow;
316:
317:            public void installUI(final JComponent c) {
318:                toolBar = (JToolBar) c;
319:
320:                installDefaults();
321:                installComponents();
322:                installListeners();
323:                installKeyboardActions();
324:            }
325:
326:            public void uninstallUI(final JComponent c) {
327:                if (isFloating()) {
328:                    setFloating(false, null);
329:                    if (dragWindow != null) {
330:                        dragWindow.dispose();
331:                        dragWindow = null;
332:                    }
333:                    if (floatingWindow != null) {
334:                        if (floatingWindow instanceof  Window) {
335:                            ((Window) floatingWindow).dispose();
336:                        }
337:                        floatingWindow = null;
338:                    }
339:                }
340:
341:                uninstallDefaults();
342:                uninstallComponents();
343:                uninstallListeners();
344:                uninstallKeyboardActions();
345:            }
346:
347:            public boolean canDock(final Component c, final Point p) {
348:                String constraints = getDockingConstraints(c, p);
349:                if (constraints == BorderLayout.CENTER
350:                        || !(c instanceof  Container)) {
351:                    return false;
352:                }
353:
354:                if (((Container) c).getLayout() instanceof  BorderLayout) {
355:                    BorderLayout borderLayout = (BorderLayout) ((Container) c)
356:                            .getLayout();
357:                    if (borderLayout.getLayoutComponent(constraints) != null) {
358:                        return false;
359:                    }
360:                }
361:                return true;
362:            }
363:
364:            public void setDockingColor(final Color c) {
365:                dockingColor = c;
366:            }
367:
368:            public Color getDockingColor() {
369:                return dockingColor;
370:            }
371:
372:            public void setFloatingColor(final Color c) {
373:                floatingColor = c;
374:            }
375:
376:            public Color getFloatingColor() {
377:                return floatingColor;
378:            }
379:
380:            public void setFloating(final boolean b, final Point p) {
381:                Container parent = getToolBarMainParent();
382:                boolean wasFloating = isFloating();
383:                floating = b;
384:                final int saveFocusedCompIndex = focusedCompIndex;
385:                if (floating) {
386:                    Point fLocation;
387:                    if (p == null) {
388:                        fLocation = floatingLocation;
389:                    } else {
390:                        fLocation = p;
391:                        SwingUtilities.convertPointToScreen(fLocation, parent);
392:                    }
393:                    if (!wasFloating) {
394:                        if (parent.getLayout() instanceof  BorderLayout) {
395:                            Object constraint = ((BorderLayout) parent
396:                                    .getLayout()).getConstraints(toolBar);
397:                            if (constraint instanceof  String) {
398:                                constraintBeforeFloating = (String) constraint;
399:                            }
400:                        }
401:                        if (floatingWindow == null) {
402:                            floatingWindow = createFloatingWindow(toolBar);
403:                        }
404:                        setOrientation(HORIZONTAL);
405:                        parentBeforeFloating = toolBar.getParent();
406:                        floatingWindow.getContentPane().add(toolBar);
407:                        if (floatingWindow instanceof  Window) {
408:                            ((Window) floatingWindow).pack();
409:                        } else {
410:                            ((Container) floatingWindow)
411:                                    .setSize(((Container) floatingWindow)
412:                                            .getPreferredSize());
413:                            revalidateContainer((Container) floatingWindow);
414:                        }
415:                        revalidateContainer(parentBeforeFloating);
416:                    }
417:                    Point offset = SwingUtilities.convertPoint(toolBar,
418:                            dragWindow.getOffset(), SwingUtilities
419:                                    .getWindowAncestor(toolBar));
420:                    fLocation.translate(-offset.x, -offset.y);
421:                    ((Container) floatingWindow).setLocation(fLocation.x,
422:                            fLocation.y);
423:                    ((Container) floatingWindow).setVisible(true);
424:                } else {
425:                    String dockingConstraints = p != null ? getDockingConstraints(
426:                            parent, p)
427:                            : constraintBeforeFloating;
428:                    if (wasFloating) {
429:                        ((Container) floatingWindow).setVisible(false);
430:                    }
431:                    setOrientation(calculateOrientation(dockingConstraints));
432:                    parent.add(toolBar, dockingConstraints);
433:                    toolBar.revalidate();
434:                    toolBar.repaint();
435:                }
436:                if (saveFocusedCompIndex != -1) {
437:                    SwingUtilities.invokeLater(new Runnable() {
438:                        public void run() {
439:                            toolBar.getComponentAtIndex(saveFocusedCompIndex)
440:                                    .requestFocus();
441:                        }
442:                    });
443:                }
444:            }
445:
446:            public void setFloatingLocation(final int x, final int y) {
447:                floatingLocation.setLocation(x, y);
448:            }
449:
450:            public boolean isFloating() {
451:                return floating;
452:            }
453:
454:            public void setOrientation(final int orientation) {
455:                toolBar.setOrientation(orientation);
456:            }
457:
458:            public void setRolloverBorders(final boolean rollover) {
459:                isRolloverBorders = rollover;
460:
461:                if (rollover) {
462:                    installRolloverBorders(toolBar);
463:                } else {
464:                    installNonRolloverBorders(toolBar);
465:                }
466:            }
467:
468:            public boolean isRolloverBorders() {
469:                return isRolloverBorders;
470:            }
471:
472:            protected MouseInputListener createDockingListener() {
473:                return new DockingListener(toolBar);
474:            }
475:
476:            protected DragWindow createDragWindow(final JToolBar toolbar) {
477:                Window owner;
478:                if (floatingWindow == null) {
479:                    floatingWindow = createFloatingWindow(toolBar);
480:                }
481:                if (floatingWindow instanceof  Window) {
482:                    owner = (Window) floatingWindow;
483:                } else {
484:                    owner = SwingUtilities.getWindowAncestor(toolBar);
485:                    if (owner == null) {
486:                        if (defaultWindowOwner == null) {
487:                            defaultWindowOwner = new Frame();
488:                        }
489:                        owner = defaultWindowOwner;
490:                    }
491:                }
492:
493:                return new DragWindow(owner);
494:            }
495:
496:            protected JFrame createFloatingFrame(final JToolBar toolbar) {
497:                JFrame floatingFrame = new JFrame(toolbar.getName());
498:                floatingFrame.setResizable(false);
499:                floatingFrame.addWindowListener(createFrameListener());
500:                return floatingFrame;
501:            }
502:
503:            protected RootPaneContainer createFloatingWindow(
504:                    final JToolBar toolbar) {
505:                Window owner = SwingUtilities.getWindowAncestor(toolBar);
506:                while (owner instanceof  FloatingWindow) {
507:                    owner = owner.getOwner();
508:                }
509:                JDialog floatingFrame;
510:                if (owner instanceof  Dialog) {
511:                    floatingFrame = new FloatingWindow((Dialog) owner, toolbar
512:                            .getName());
513:                } else if (owner instanceof  Frame) {
514:                    floatingFrame = new FloatingWindow((Frame) owner, toolbar
515:                            .getName());
516:                } else {
517:                    floatingFrame = new FloatingWindow((Frame) null, toolbar
518:                            .getName());
519:                }
520:
521:                floatingFrame.setResizable(false);
522:                floatingFrame.addWindowListener(createFrameListener());
523:                return floatingFrame;
524:            }
525:
526:            protected WindowListener createFrameListener() {
527:                return new FrameListener();
528:            }
529:
530:            protected PropertyChangeListener createPropertyListener() {
531:                return new PropertyListener();
532:            }
533:
534:            protected Border createRolloverBorder() {
535:                Border buttonBorder = new BasicBorders.RolloverButtonBorder(
536:                        shadow, darkShadow, highlight, light);
537:                Border marginBorder = new BasicBorders.ToolBarButtonMarginBorder();
538:                return new BorderUIResource.CompoundBorderUIResource(
539:                        buttonBorder, marginBorder);
540:            }
541:
542:            protected Border createNonRolloverBorder() {
543:                Border buttonBorder = new BasicBorders.ButtonBorder(
544:                        buttonShadow, buttonDarkShadow, buttonHighlight,
545:                        buttonLight);
546:                Border marginBorder = new BasicBorders.ToolBarButtonMarginBorder();
547:                return new BorderUIResource.CompoundBorderUIResource(
548:                        buttonBorder, marginBorder);
549:            }
550:
551:            protected ContainerListener createToolBarContListener() {
552:                return new ToolBarContListener();
553:            }
554:
555:            protected FocusListener createToolBarFocusListener() {
556:                return new ToolBarFocusListener();
557:            }
558:
559:            protected void dragTo(final Point position, final Point origin) {
560:                if (!toolBar.isFloatable()) {
561:                    return;
562:                }
563:
564:                if (dragWindow == null) {
565:                    dragWindow = createDragWindow(toolBar);
566:                }
567:                if (!dragWindow.isVisible()) {
568:                    Dimension size = toolBar.getPreferredSize();
569:                    dragWindow.setSize(size);
570:                    if (dragWindow.getOffset() == null) {
571:                        dragWindow.setOffset(new Point(size.width / 2,
572:                                size.height / 2));
573:                    }
574:                    dragWindow.setOrientation(toolBar.getOrientation());
575:                    dragWindow.setVisible(true);
576:                }
577:
578:                Container parent = getToolBarMainParent();
579:                Point p = SwingUtilities
580:                        .convertPoint(toolBar, position, parent);
581:                boolean canDock = canDock(parent, p);
582:                dragWindow.setBackground(canDock ? getDockingColor()
583:                        : getFloatingColor());
584:                dragWindow.setBorderColor(canDock ? dockingBorderColor
585:                        : floatingBorderColor);
586:                int newOrientation = calculateOrientation(getDockingConstraints(
587:                        parent, p));
588:                SwingUtilities.convertPointToScreen(p, parent);
589:                p.translate(-dragWindow.getOffset().x,
590:                        -dragWindow.getOffset().y);
591:                dragWindow.updateBounds(newOrientation, p);
592:            }
593:
594:            protected void floatAt(final Point position, final Point origin) {
595:                if (!toolBar.isFloatable()) {
596:                    return;
597:                }
598:
599:                dragWindow.dispose();
600:
601:                Container parent = getToolBarMainParent();
602:                Point p = SwingUtilities
603:                        .convertPoint(toolBar, position, parent);
604:                String dockingConstraints = getDockingConstraints(parent, p);
605:                if (dockingConstraints == BorderLayout.CENTER) {
606:                    SwingUtilities.convertPointToScreen(p, parent);
607:                    setFloatingLocation(p.x, p.y);
608:                    setFloating(true, null);
609:                } else {
610:                    setFloating(false, p);
611:                }
612:            }
613:
614:            protected void installComponents() {
615:                // does nothing
616:            }
617:
618:            protected void uninstallComponents() {
619:                // does nothing
620:            }
621:
622:            protected void installDefaults() {
623:                LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
624:                        "ToolBar.foreground", "ToolBar.font");
625:                LookAndFeel.installBorder(toolBar, "ToolBar.border");
626:                LookAndFeel.installProperty(toolBar,
627:                        StringConstants.OPAQUE_PROPERTY, Boolean.TRUE);
628:
629:                dockingBorderColor = UIManager
630:                        .getColor("ToolBar.dockingForeground");
631:                dockingColor = UIManager.getColor("ToolBar.dockingBackground");
632:                floatingBorderColor = UIManager
633:                        .getColor("ToolBar.floatingForeground");
634:                floatingColor = UIManager
635:                        .getColor("ToolBar.floatingBackground");
636:                darkShadow = UIManager.getColor("ToolBar.darkShadow");
637:                highlight = UIManager.getColor("ToolBar.highlight");
638:                light = UIManager.getColor("ToolBar.light");
639:                shadow = UIManager.getColor("ToolBar.shadow");
640:                buttonDarkShadow = UIManager.getColor("Button.darkShadow");
641:                buttonHighlight = UIManager.getColor("Button.highlight");
642:                buttonLight = UIManager.getColor("Button.light");
643:                buttonShadow = UIManager.getColor("Button.shadow");
644:
645:                setRolloverBorders(toolBar.isRollover());
646:            }
647:
648:            protected void uninstallDefaults() {
649:                LookAndFeel.uninstallBorder(toolBar);
650:                installNormalBorders(toolBar);
651:            }
652:
653:            protected void installKeyboardActions() {
654:                SwingUtilities.replaceUIInputMap(toolBar,
655:                        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
656:                        (InputMap) UIManager.get("ToolBar.ancestorInputMap"));
657:
658:                SwingUtilities.replaceUIActionMap(toolBar, getUIActionMap());
659:            }
660:
661:            protected void uninstallKeyboardActions() {
662:                SwingUtilities.replaceUIInputMap(toolBar,
663:                        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
664:                SwingUtilities.replaceUIActionMap(toolBar, null);
665:            }
666:
667:            protected void installListeners() {
668:                if (toolBarContListener == null) {
669:                    toolBarContListener = createToolBarContListener();
670:                }
671:                toolBar.addContainerListener(toolBarContListener);
672:
673:                if (propertyListener == null) {
674:                    propertyListener = createPropertyListener();
675:                }
676:                toolBar.addPropertyChangeListener(propertyListener);
677:
678:                if (dockingListener == null) {
679:                    dockingListener = createDockingListener();
680:                }
681:                toolBar.addMouseListener(dockingListener);
682:                toolBar.addMouseMotionListener(dockingListener);
683:
684:                if (toolBarFocusListener == null) {
685:                    toolBarFocusListener = createToolBarFocusListener();
686:                }
687:                for (int i = 0; i < toolBar.getComponentCount(); i++) {
688:                    toolBar.getComponentAtIndex(i).addFocusListener(
689:                            toolBarFocusListener);
690:                }
691:            }
692:
693:            protected void uninstallListeners() {
694:                toolBar.removeContainerListener(toolBarContListener);
695:                toolBar.removePropertyChangeListener(propertyListener);
696:                toolBar.removeMouseListener(dockingListener);
697:                toolBar.removeMouseMotionListener(dockingListener);
698:                for (int i = 0; i < toolBar.getComponentCount(); i++) {
699:                    toolBar.getComponentAtIndex(i).removeFocusListener(
700:                            toolBarFocusListener);
701:                }
702:            }
703:
704:            protected void installNormalBorders(final JComponent c) {
705:                for (int i = 0; i < c.getComponentCount(); i++) {
706:                    setBorderToNormal(c.getComponent(i));
707:                }
708:            }
709:
710:            protected void installRolloverBorders(final JComponent c) {
711:                for (int i = 0; i < c.getComponentCount(); i++) {
712:                    setBorderToRollover(c.getComponent(i));
713:                }
714:            }
715:
716:            protected void installNonRolloverBorders(final JComponent c) {
717:                for (int i = 0; i < c.getComponentCount(); i++) {
718:                    setBorderToNonRollover(c.getComponent(i));
719:                }
720:            }
721:
722:            protected void navigateFocusedComp(final int direction) {
723:                if (focusedCompIndex == -1) {
724:                    return;
725:                }
726:
727:                int delta;
728:                if (direction == EAST || direction == SOUTH) {
729:                    delta = 1;
730:                } else {
731:                    delta = toolBar.getComponentCount() - 1;
732:                }
733:                int indexToFocus = focusedCompIndex;
734:                do {
735:                    indexToFocus = (indexToFocus + delta)
736:                            % toolBar.getComponentCount();
737:                } while ((!toolBar.getComponentAtIndex(indexToFocus)
738:                        .isFocusable() || !toolBar.getComponentAtIndex(
739:                        indexToFocus).isEnabled())
740:                        && indexToFocus != focusedCompIndex);
741:
742:                toolBar.getComponentAtIndex(indexToFocus).requestFocus();
743:            }
744:
745:            protected void paintDragWindow(final Graphics g) {
746:                g.setColor(dragWindow.getBackground());
747:                g.fillRect(0, 0, dragWindow.getWidth(), dragWindow.getHeight());
748:
749:                g.setColor(dragWindow.getBorderColor());
750:                g.drawRect(0, 0, dragWindow.getWidth() - 1, dragWindow
751:                        .getHeight() - 1);
752:            }
753:
754:            protected void setBorderToNonRollover(final Component c) {
755:                if (nonRolloverBorder == null) {
756:                    nonRolloverBorder = createNonRolloverBorder();
757:                }
758:                setCustomBorder(c, nonRolloverBorder, false);
759:            }
760:
761:            protected void setBorderToNormal(final Component c) {
762:                if (!(c instanceof  AbstractButton)) {
763:                    return;
764:                }
765:
766:                restoreBorderAndRolloverEnabled((AbstractButton) c);
767:            }
768:
769:            protected void setBorderToRollover(final Component c) {
770:                if (rolloverBorder == null) {
771:                    rolloverBorder = createRolloverBorder();
772:                }
773:                setCustomBorder(c, rolloverBorder, true);
774:            }
775:
776:            private void saveBorderAndRolloverEnabled(final AbstractButton b) {
777:                Boolean isRolloverEnabled = (Boolean) b
778:                        .getClientProperty(SAVED_ROLLOVER_ENABLED_PROPERTY);
779:                if (isRolloverEnabled == null) {
780:                    b.putClientProperty(SAVED_BORDER_PROPERTY, b.getBorder());
781:                    b.putClientProperty(SAVED_ROLLOVER_ENABLED_PROPERTY,
782:                            Boolean.valueOf(b.isRolloverEnabled()));
783:                }
784:            }
785:
786:            private void restoreBorderAndRolloverEnabled(final AbstractButton b) {
787:                Boolean isRolloverEnabled = (Boolean) b
788:                        .getClientProperty(SAVED_ROLLOVER_ENABLED_PROPERTY);
789:                if (isRolloverEnabled != null) {
790:                    LookAndFeel.installProperty(b, "rolloverEnabled",
791:                            isRolloverEnabled);
792:                    b.putClientProperty(SAVED_ROLLOVER_ENABLED_PROPERTY, null);
793:                    if (Utilities.isUIResource(b.getBorder())) {
794:                        b.setBorder((Border) b
795:                                .getClientProperty(SAVED_BORDER_PROPERTY));
796:                    }
797:                    b.putClientProperty(SAVED_BORDER_PROPERTY, null);
798:                }
799:            }
800:
801:            private void setCustomBorder(final Component c, final Border b,
802:                    final boolean rolloverEnabled) {
803:                if (!(c instanceof  AbstractButton)) {
804:                    return;
805:                }
806:
807:                AbstractButton button = (AbstractButton) c;
808:                if (Utilities.isUIResource(button.getBorder())) {
809:                    saveBorderAndRolloverEnabled(button);
810:                    button.setBorder(b);
811:                    button.setRolloverEnabled(rolloverEnabled);
812:                }
813:            }
814:
815:            private ActionMap getUIActionMap() {
816:                if (actionMap != null) {
817:                    return actionMap;
818:                }
819:
820:                actionMap = new ActionMapUIResource();
821:                actionMap.put("navigateRight", new NavigateAction(EAST));
822:                actionMap.put("navigateLeft", new NavigateAction(WEST));
823:                actionMap.put("navigateUp", new NavigateAction(NORTH));
824:                actionMap.put("navigateDown", new NavigateAction(SOUTH));
825:                return actionMap;
826:            }
827:
828:            /**
829:             * Returns position to dock the toolbar to: NORTH, SOUTH, EAST, WEST.
830:             * CENTER means that the toolbar cannot be docket at this position.
831:             */
832:            private String getDockingConstraints(final Component c,
833:                    final Point p) {
834:                Rectangle r = SwingUtilities.getLocalBounds(c);
835:                int delta = toolBar.getOrientation() == HORIZONTAL ? toolBar
836:                        .getHeight() : toolBar.getWidth();
837:
838:                if (!r.contains(p)) {
839:                    return BorderLayout.CENTER;
840:                }
841:
842:                if (p.x < delta) {
843:                    return BorderLayout.WEST;
844:                } else if (p.x > r.width - delta) {
845:                    return BorderLayout.EAST;
846:                } else if (p.y < delta) {
847:                    return BorderLayout.NORTH;
848:                } else if (p.y > r.height - delta) {
849:                    return BorderLayout.SOUTH;
850:                }
851:                return BorderLayout.CENTER;
852:            }
853:
854:            private int calculateOrientation(final String constraint) {
855:                if (constraint == BorderLayout.EAST
856:                        || constraint == BorderLayout.WEST) {
857:                    return VERTICAL;
858:                } else if (constraint == BorderLayout.NORTH
859:                        || constraint == BorderLayout.SOUTH
860:                        || constraint == BorderLayout.CENTER) {
861:                    return HORIZONTAL;
862:                }
863:                assert false : "invalid constraint";
864:                return HORIZONTAL;
865:            }
866:
867:            private Container getToolBarMainParent() {
868:                return isFloating() ? parentBeforeFloating : toolBar
869:                        .getParent();
870:            }
871:
872:            private void revalidateContainer(final Container c) {
873:                if (c instanceof  JComponent) {
874:                    ((JComponent) c).revalidate();
875:                } else {
876:                    c.doLayout();
877:                }
878:            }
879:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.