1. /*
  2. * @(#)BasicToolBarUI.java 1.62 01/02/09
  3. *
  4. * Copyright 1997-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.plaf.basic;
  11. import javax.swing.*;
  12. import javax.swing.event.*;
  13. import java.awt.BorderLayout;
  14. import java.awt.Color;
  15. import java.awt.Component;
  16. import java.awt.Container;
  17. import java.awt.Dimension;
  18. import java.awt.Frame;
  19. import java.awt.Graphics;
  20. import java.awt.Insets;
  21. import java.awt.Point;
  22. import java.awt.Rectangle;
  23. import java.awt.Window;
  24. import java.awt.event.*;
  25. import java.awt.IllegalComponentStateException;
  26. import java.beans.*;
  27. import javax.swing.border.*;
  28. import javax.swing.plaf.*;
  29. /**
  30. * A Basic L&F implementation of ToolBarUI. This implementation
  31. * is a "combined" view/controller.
  32. * <p>
  33. *
  34. * @version 1.62 02/09/01
  35. * @author Georges Saab
  36. * @author Jeff Shapiro
  37. */
  38. public class BasicToolBarUI extends ToolBarUI implements SwingConstants
  39. {
  40. protected JToolBar toolBar;
  41. private boolean floating;
  42. private int floatingX;
  43. private int floatingY;
  44. private JFrame floatingFrame;
  45. protected DragWindow dragWindow;
  46. private Container dockingSource;
  47. private int dockingSensitivity = 0;
  48. protected int focusedCompIndex = -1;
  49. protected Color dockingColor = null;
  50. protected Color floatingColor = null;
  51. protected Color dockingBorderColor = null;
  52. protected Color floatingBorderColor = null;
  53. protected MouseInputListener dockingListener;
  54. protected PropertyChangeListener propertyListener;
  55. protected ContainerListener toolBarContListener;
  56. protected FocusListener toolBarFocusListener;
  57. /**
  58. * As of Java 2 platform v1.3 this previously undocumented field is no
  59. * longer used.
  60. * Key bindings are now defined by the LookAndFeel, please refer to
  61. * the key bindings specification for further details.
  62. *
  63. * @deprecated As of Java 2 platform v1.3.
  64. */
  65. protected KeyStroke upKey;
  66. /**
  67. * As of Java 2 platform v1.3 this previously undocumented field is no
  68. * longer used.
  69. * Key bindings are now defined by the LookAndFeel, please refer to
  70. * the key bindings specification for further details.
  71. *
  72. * @deprecated As of Java 2 platform v1.3.
  73. */
  74. protected KeyStroke downKey;
  75. /**
  76. * As of Java 2 platform v1.3 this previously undocumented field is no
  77. * longer used.
  78. * Key bindings are now defined by the LookAndFeel, please refer to
  79. * the key bindings specification for further details.
  80. *
  81. * @deprecated As of Java 2 platform v1.3.
  82. */
  83. protected KeyStroke leftKey;
  84. /**
  85. * As of Java 2 platform v1.3 this previously undocumented field is no
  86. * longer used.
  87. * Key bindings are now defined by the LookAndFeel, please refer to
  88. * the key bindings specification for further details.
  89. *
  90. * @deprecated As of Java 2 platform v1.3.
  91. */
  92. protected KeyStroke rightKey;
  93. private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex";
  94. public static ComponentUI createUI( JComponent c )
  95. {
  96. return new BasicToolBarUI();
  97. }
  98. public void installUI( JComponent c )
  99. {
  100. toolBar = (JToolBar) c;
  101. // Set defaults
  102. installDefaults();
  103. installComponents();
  104. installListeners();
  105. installKeyboardActions();
  106. // Initialize instance vars
  107. dockingSensitivity = 0;
  108. floating = false;
  109. floatingX = floatingY = 0;
  110. floatingFrame = null;
  111. setOrientation( toolBar.getOrientation() );
  112. c.setOpaque(true);
  113. if ( c.getClientProperty( FOCUSED_COMP_INDEX ) != null )
  114. {
  115. focusedCompIndex = ( (Integer) ( c.getClientProperty( FOCUSED_COMP_INDEX ) ) ).intValue();
  116. }
  117. }
  118. public void uninstallUI( JComponent c )
  119. {
  120. // Clear defaults
  121. uninstallDefaults();
  122. uninstallComponents();
  123. uninstallListeners();
  124. uninstallKeyboardActions();
  125. // Clear instance vars
  126. if (isFloating() == true)
  127. setFloating(false, null);
  128. floatingFrame = null;
  129. dragWindow = null;
  130. dockingSource = null;
  131. c.putClientProperty( FOCUSED_COMP_INDEX, new Integer( focusedCompIndex ) );
  132. }
  133. protected void installDefaults( )
  134. {
  135. LookAndFeel.installBorder(toolBar,"ToolBar.border");
  136. LookAndFeel.installColorsAndFont(toolBar,
  137. "ToolBar.background",
  138. "ToolBar.foreground",
  139. "ToolBar.font");
  140. // Toolbar specific defaults
  141. if ( dockingColor == null || dockingColor instanceof UIResource )
  142. dockingColor = UIManager.getColor("ToolBar.dockingBackground");
  143. if ( floatingColor == null || floatingColor instanceof UIResource )
  144. floatingColor = UIManager.getColor("ToolBar.floatingBackground");
  145. if ( dockingBorderColor == null ||
  146. dockingBorderColor instanceof UIResource )
  147. dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
  148. if ( floatingBorderColor == null ||
  149. floatingBorderColor instanceof UIResource )
  150. floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
  151. }
  152. protected void uninstallDefaults( )
  153. {
  154. LookAndFeel.uninstallBorder(toolBar);
  155. dockingColor = null;
  156. floatingColor = null;
  157. dockingBorderColor = null;
  158. floatingBorderColor = null;
  159. }
  160. protected void installComponents( )
  161. {
  162. }
  163. protected void uninstallComponents( )
  164. {
  165. }
  166. protected void installListeners( )
  167. {
  168. dockingListener = createDockingListener( );
  169. if ( dockingListener != null )
  170. {
  171. toolBar.addMouseMotionListener( dockingListener );
  172. toolBar.addMouseListener( dockingListener );
  173. }
  174. propertyListener = createPropertyListener(); // added in setFloating
  175. toolBarContListener = createToolBarContListener();
  176. if ( toolBarContListener != null )
  177. {
  178. toolBar.addContainerListener( toolBarContListener );
  179. }
  180. toolBarFocusListener = createToolBarFocusListener();
  181. if ( toolBarFocusListener != null )
  182. {
  183. // Put focus listener on all components in toolbar
  184. Component[] components = toolBar.getComponents();
  185. for ( int i = 0; i < components.length; ++i )
  186. {
  187. components[ i ].addFocusListener( toolBarFocusListener );
  188. }
  189. }
  190. }
  191. protected void uninstallListeners( )
  192. {
  193. if ( dockingListener != null )
  194. {
  195. toolBar.removeMouseMotionListener(dockingListener);
  196. toolBar.removeMouseListener(dockingListener);
  197. dockingListener = null;
  198. }
  199. if ( propertyListener != null )
  200. {
  201. propertyListener = null; // removed in setFloating
  202. }
  203. if ( toolBarContListener != null )
  204. {
  205. toolBar.removeContainerListener( toolBarContListener );
  206. toolBarContListener = null;
  207. }
  208. if ( toolBarFocusListener != null )
  209. {
  210. // Remove focus listener from all components in toolbar
  211. Component[] components = toolBar.getComponents();
  212. for ( int i = 0; i < components.length; ++i )
  213. {
  214. components[ i ].removeFocusListener( toolBarFocusListener );
  215. }
  216. toolBarFocusListener = null;
  217. }
  218. }
  219. protected void installKeyboardActions( )
  220. {
  221. InputMap km = getInputMap(JComponent.
  222. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  223. SwingUtilities.replaceUIInputMap(toolBar, JComponent.
  224. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  225. km);
  226. ActionMap am = getActionMap();
  227. if (am != null) {
  228. SwingUtilities.replaceUIActionMap(toolBar, am);
  229. }
  230. }
  231. InputMap getInputMap(int condition) {
  232. if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
  233. return (InputMap)UIManager.get("ToolBar.ancestorInputMap");
  234. }
  235. return null;
  236. }
  237. ActionMap getActionMap() {
  238. ActionMap map = (ActionMap)UIManager.get("ToolBar.actionMap");
  239. if (map == null) {
  240. map = createActionMap();
  241. if (map != null) {
  242. UIManager.put("ToolBar.actionMap", map);
  243. }
  244. }
  245. return map;
  246. }
  247. ActionMap createActionMap() {
  248. ActionMap map = new ActionMapUIResource();
  249. map.put("navigateRight", new RightAction());
  250. map.put("navigateLeft", new LeftAction());
  251. map.put("navigateUp", new UpAction());
  252. map.put("navigateDown", new DownAction());
  253. return map;
  254. }
  255. protected void uninstallKeyboardActions( )
  256. {
  257. SwingUtilities.replaceUIActionMap(toolBar, null);
  258. SwingUtilities.replaceUIInputMap(toolBar, JComponent.
  259. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  260. null);
  261. }
  262. protected void navigateFocusedComp( int direction )
  263. {
  264. int nComp = toolBar.getComponentCount();
  265. int j;
  266. switch ( direction )
  267. {
  268. case EAST:
  269. case SOUTH:
  270. if ( focusedCompIndex < 0 || focusedCompIndex >= nComp ) break;
  271. j = focusedCompIndex + 1;
  272. while ( j != focusedCompIndex )
  273. {
  274. if ( j >= nComp ) j = 0;
  275. Component comp = toolBar.getComponentAtIndex( j++ );
  276. if ( comp != null && comp.isFocusTraversable() )
  277. {
  278. comp.requestFocus();
  279. break;
  280. }
  281. }
  282. break;
  283. case WEST:
  284. case NORTH:
  285. if ( focusedCompIndex < 0 || focusedCompIndex >= nComp ) break;
  286. j = focusedCompIndex - 1;
  287. while ( j != focusedCompIndex )
  288. {
  289. if ( j < 0 ) j = nComp - 1;
  290. Component comp = toolBar.getComponentAtIndex( j-- );
  291. if ( comp != null && comp.isFocusTraversable() )
  292. {
  293. comp.requestFocus();
  294. break;
  295. }
  296. }
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. protected JFrame createFloatingFrame(JToolBar toolbar) {
  303. Window window = SwingUtilities.getWindowAncestor(toolbar);
  304. JFrame frame = new JFrame(toolbar.getName(), window == null ?
  305. (java.awt.GraphicsConfiguration)null : window.getGraphicsConfiguration());
  306. frame.setResizable(false);
  307. WindowListener wl = createFrameListener();
  308. frame.addWindowListener(wl);
  309. return frame;
  310. }
  311. protected DragWindow createDragWindow(JToolBar toolbar) {
  312. Frame frame = null;
  313. if(toolBar != null) {
  314. Container p;
  315. for(p = toolBar.getParent() ; p != null && !(p instanceof Frame) ;
  316. p = p.getParent());
  317. if(p != null && p instanceof Frame)
  318. frame = (Frame) p;
  319. }
  320. if(floatingFrame == null) {
  321. floatingFrame = createFloatingFrame(toolBar);
  322. }
  323. frame = floatingFrame;
  324. DragWindow dragWindow = new DragWindow(frame);
  325. return dragWindow;
  326. }
  327. public Dimension getMinimumSize(JComponent c) {
  328. return getPreferredSize(c);
  329. }
  330. public Dimension getPreferredSize(JComponent c) {
  331. return null;
  332. }
  333. public Dimension getMaximumSize(JComponent c) {
  334. return getPreferredSize(c);
  335. }
  336. public void setFloatingLocation(int x, int y) {
  337. floatingX = x;
  338. floatingY = y;
  339. }
  340. public boolean isFloating() {
  341. return floating;
  342. }
  343. public void setFloating(boolean b, Point p) {
  344. if (toolBar.isFloatable() == true) {
  345. if (dragWindow != null)
  346. dragWindow.setVisible(false);
  347. this.floating = b;
  348. if (b == true)
  349. {
  350. if (dockingSource == null)
  351. {
  352. dockingSource = toolBar.getParent();
  353. dockingSource.remove(toolBar);
  354. }
  355. if ( propertyListener != null )
  356. UIManager.addPropertyChangeListener( propertyListener );
  357. if (floatingFrame == null)
  358. floatingFrame = createFloatingFrame(toolBar);
  359. floatingFrame.getContentPane().add(toolBar,BorderLayout.CENTER);
  360. setOrientation( JToolBar.HORIZONTAL );
  361. floatingFrame.pack();
  362. floatingFrame.setLocation(floatingX, floatingY);
  363. floatingFrame.show();
  364. } else {
  365. if (floatingFrame == null)
  366. floatingFrame = createFloatingFrame(toolBar);
  367. floatingFrame.setVisible(false);
  368. floatingFrame.getContentPane().remove(toolBar);
  369. String constraint = getDockingConstraint(dockingSource,
  370. p);
  371. int orientation = mapConstraintToOrientation(constraint);
  372. setOrientation(orientation);
  373. if (dockingSource== null)
  374. dockingSource = toolBar.getParent();
  375. if ( propertyListener != null )
  376. UIManager.removePropertyChangeListener( propertyListener );
  377. dockingSource.add(constraint, toolBar);
  378. }
  379. dockingSource.invalidate();
  380. Container dockingSourceParent = dockingSource.getParent();
  381. if (dockingSourceParent != null)
  382. dockingSourceParent.validate();
  383. dockingSource.repaint();
  384. }
  385. }
  386. private int mapConstraintToOrientation(String constraint)
  387. {
  388. int orientation = toolBar.getOrientation();
  389. if ( constraint != null )
  390. {
  391. if ( constraint.equals(BorderLayout.EAST) || constraint.equals(BorderLayout.WEST) )
  392. orientation = JToolBar.VERTICAL;
  393. else if ( constraint.equals(BorderLayout.NORTH) || constraint.equals(BorderLayout.SOUTH) )
  394. orientation = JToolBar.HORIZONTAL;
  395. }
  396. return orientation;
  397. }
  398. public void setOrientation(int orientation)
  399. {
  400. toolBar.setOrientation( orientation );
  401. if (dragWindow !=null)
  402. dragWindow.setOrientation(orientation);
  403. }
  404. /**
  405. * Gets the color displayed when over a docking area
  406. */
  407. public Color getDockingColor() {
  408. return dockingColor;
  409. }
  410. /**
  411. * Sets the color displayed when over a docking area
  412. */
  413. public void setDockingColor(Color c) {
  414. this.dockingColor = c;
  415. }
  416. /**
  417. * Gets the color displayed when over a floating area
  418. */
  419. public Color getFloatingColor() {
  420. return floatingColor;
  421. }
  422. /**
  423. * Sets the color displayed when over a floating area
  424. */
  425. public void setFloatingColor(Color c) {
  426. this.floatingColor = c;
  427. }
  428. public boolean canDock(Component c, Point p) {
  429. // System.out.println("Can Dock: " + p);
  430. boolean b = false;
  431. if (c.contains(p)) {
  432. if (dockingSensitivity == 0)
  433. dockingSensitivity = toolBar.getSize().height;
  434. // North
  435. if (p.y < dockingSensitivity)
  436. b = true;
  437. // South
  438. if (p.y > c.getSize().height-dockingSensitivity)
  439. b = true;
  440. // West (Base distance on height for now!)
  441. if (p.x < dockingSensitivity)
  442. b = true;
  443. // East (Base distance on height for now!)
  444. if (p.x > c.getSize().width-dockingSensitivity)
  445. b = true;
  446. }
  447. return b;
  448. }
  449. private String getDockingConstraint(Component c, Point p) {
  450. // System.out.println("Docking Constraint: " + p);
  451. String s = BorderLayout.NORTH;
  452. if ((p != null) && (c.contains(p))) {
  453. if (dockingSensitivity == 0)
  454. dockingSensitivity = toolBar.getSize().height;
  455. if (p.y > c.getSize().height-dockingSensitivity)
  456. s = BorderLayout.SOUTH;
  457. // West (Base distance on height for now!)
  458. if (p.x < dockingSensitivity)
  459. s = BorderLayout.WEST;
  460. // East (Base distance on height for now!)
  461. if (p.x > c.getSize().width-dockingSensitivity)
  462. s = BorderLayout.EAST;
  463. // North (Base distance on height for now!)
  464. if (p.y < dockingSensitivity)
  465. s = BorderLayout.NORTH;
  466. }
  467. return s;
  468. }
  469. protected void dragTo(Point position, Point origin)
  470. {
  471. if (toolBar.isFloatable() == true)
  472. {
  473. try
  474. {
  475. if (dragWindow == null)
  476. dragWindow = createDragWindow(toolBar);
  477. Point offset = dragWindow.getOffset();
  478. if (offset == null) {
  479. Dimension size = toolBar.getPreferredSize();
  480. offset = new Point(size.width2, size.height2);
  481. dragWindow.setOffset(offset);
  482. }
  483. Point global = new Point(origin.x+ position.x,
  484. origin.y+position.y);
  485. Point dragPoint = new Point(global.x- offset.x,
  486. global.y- offset.y);
  487. if (dockingSource == null)
  488. dockingSource = toolBar.getParent();
  489. Point dockingPosition = dockingSource.getLocationOnScreen();
  490. Point comparisonPoint = new Point(global.x-dockingPosition.x,
  491. global.y-dockingPosition.y);
  492. if (canDock(dockingSource, comparisonPoint)) {
  493. dragWindow.setBackground(getDockingColor());
  494. String constraint = getDockingConstraint(dockingSource,
  495. comparisonPoint);
  496. int orientation = mapConstraintToOrientation(constraint);
  497. dragWindow.setOrientation(orientation);
  498. dragWindow.setBorderColor(dockingBorderColor);
  499. } else {
  500. dragWindow.setBackground(getFloatingColor());
  501. dragWindow.setOrientation( JToolBar.HORIZONTAL );
  502. dragWindow.setBorderColor(floatingBorderColor);
  503. }
  504. dragWindow.setLocation(dragPoint.x, dragPoint.y);
  505. if (dragWindow.isVisible() == false) {
  506. Dimension size = toolBar.getPreferredSize();
  507. dragWindow.setSize(size.width, size.height);
  508. dragWindow.show();
  509. }
  510. }
  511. catch ( IllegalComponentStateException e )
  512. {
  513. }
  514. }
  515. }
  516. protected void floatAt(Point position, Point origin)
  517. {
  518. if(toolBar.isFloatable() == true)
  519. {
  520. try
  521. {
  522. Point offset = dragWindow.getOffset();
  523. if (offset == null) {
  524. offset = position;
  525. dragWindow.setOffset(offset);
  526. }
  527. Point global = new Point(origin.x+ position.x,
  528. origin.y+position.y);
  529. setFloatingLocation(global.x-offset.x,
  530. global.y-offset.y);
  531. if (dockingSource != null) {
  532. Point dockingPosition = dockingSource.getLocationOnScreen();
  533. Point comparisonPoint = new Point(global.x-dockingPosition.x,
  534. global.y-dockingPosition.y);
  535. if (canDock(dockingSource, comparisonPoint)) {
  536. setFloating(false, comparisonPoint);
  537. } else {
  538. setFloating(true, null);
  539. }
  540. } else {
  541. setFloating(true, null);
  542. }
  543. dragWindow.setOffset(null);
  544. }
  545. catch ( IllegalComponentStateException e )
  546. {
  547. }
  548. }
  549. }
  550. protected ContainerListener createToolBarContListener( )
  551. {
  552. return new ToolBarContListener( );
  553. }
  554. protected FocusListener createToolBarFocusListener( )
  555. {
  556. return new ToolBarFocusListener( );
  557. }
  558. protected PropertyChangeListener createPropertyListener()
  559. {
  560. return new PropertyListener();
  561. }
  562. protected MouseInputListener createDockingListener( ) {
  563. return new DockingListener(toolBar);
  564. }
  565. protected WindowListener createFrameListener() {
  566. return new FrameListener();
  567. }
  568. // The private inner classes below should be changed to protected the
  569. // next time API changes are allowed.
  570. private static abstract class KeyAction extends AbstractAction {
  571. public boolean isEnabled() {
  572. return true;
  573. }
  574. };
  575. private static class RightAction extends KeyAction {
  576. public void actionPerformed(ActionEvent e) {
  577. JToolBar toolBar = (JToolBar)e.getSource();
  578. BasicToolBarUI ui = (BasicToolBarUI)toolBar.getUI();
  579. ui.navigateFocusedComp(EAST);
  580. }
  581. };
  582. private static class LeftAction extends KeyAction {
  583. public void actionPerformed(ActionEvent e) {
  584. JToolBar toolBar = (JToolBar)e.getSource();
  585. BasicToolBarUI ui = (BasicToolBarUI)toolBar.getUI();
  586. ui.navigateFocusedComp(WEST);
  587. }
  588. };
  589. private static class UpAction extends KeyAction {
  590. public void actionPerformed(ActionEvent e) {
  591. JToolBar toolBar = (JToolBar)e.getSource();
  592. BasicToolBarUI ui = (BasicToolBarUI)toolBar.getUI();
  593. ui.navigateFocusedComp(NORTH);
  594. }
  595. };
  596. private static class DownAction extends KeyAction {
  597. public void actionPerformed(ActionEvent e) {
  598. JToolBar toolBar = (JToolBar)e.getSource();
  599. BasicToolBarUI ui = (BasicToolBarUI)toolBar.getUI();
  600. ui.navigateFocusedComp(SOUTH);
  601. }
  602. };
  603. protected class FrameListener extends WindowAdapter {
  604. public void windowClosing(WindowEvent w) {
  605. setFloating(false, null);
  606. }
  607. }
  608. protected class ToolBarContListener implements ContainerListener
  609. {
  610. public void componentAdded( ContainerEvent e )
  611. {
  612. Component c = e.getChild();
  613. if ( toolBarFocusListener != null )
  614. {
  615. c.addFocusListener( toolBarFocusListener );
  616. }
  617. }
  618. public void componentRemoved( ContainerEvent e )
  619. {
  620. Component c = e.getChild();
  621. if ( toolBarFocusListener != null )
  622. {
  623. c.removeFocusListener( toolBarFocusListener );
  624. }
  625. }
  626. } // end class ToolBarContListener
  627. protected class ToolBarFocusListener implements FocusListener
  628. {
  629. public void focusGained( FocusEvent e )
  630. {
  631. Component c = e.getComponent();
  632. focusedCompIndex = toolBar.getComponentIndex( c );
  633. }
  634. public void focusLost( FocusEvent e )
  635. {
  636. }
  637. } // end class ToolBarFocusListener
  638. protected class PropertyListener implements PropertyChangeListener
  639. {
  640. public void propertyChange( PropertyChangeEvent e )
  641. {
  642. if ( e.getPropertyName().equals("lookAndFeel") )
  643. {
  644. toolBar.updateUI();
  645. }
  646. }
  647. }
  648. /**
  649. * This inner class is marked "public" due to a compiler bug.
  650. * This class should be treated as a "protected" inner class.
  651. * Instantiate it only within subclasses of BasicToolBarUI.
  652. */
  653. public class DockingListener implements MouseInputListener {
  654. protected JToolBar toolBar;
  655. protected boolean isDragging = false;
  656. protected Point origin = null;
  657. public DockingListener(JToolBar t) {
  658. this.toolBar = t;
  659. }
  660. public void mouseClicked(MouseEvent e) {}
  661. public void mousePressed(MouseEvent e) {
  662. if (!toolBar.isEnabled()) {
  663. return;
  664. }
  665. isDragging = false;
  666. }
  667. public void mouseReleased(MouseEvent e) {
  668. if (!toolBar.isEnabled()) {
  669. return;
  670. }
  671. if (isDragging == true) {
  672. Point position = e.getPoint();
  673. if (origin == null)
  674. origin = e.getComponent().getLocationOnScreen();
  675. floatAt(position, origin);
  676. }
  677. origin = null;
  678. isDragging = false;
  679. }
  680. public void mouseEntered(MouseEvent e) { }
  681. public void mouseExited(MouseEvent e) { }
  682. public void mouseDragged(MouseEvent e) {
  683. if (!toolBar.isEnabled()) {
  684. return;
  685. }
  686. isDragging = true;
  687. Point position = e.getPoint();
  688. if (origin == null)
  689. origin = e.getComponent().getLocationOnScreen();
  690. dragTo(position, origin);
  691. }
  692. public void mouseMoved(MouseEvent e) {
  693. }
  694. }
  695. protected class DragWindow extends Window
  696. {
  697. Color borderColor = Color.gray;
  698. int orientation = toolBar.getOrientation();
  699. Point offset; // offset of the mouse cursor inside the DragWindow
  700. DragWindow(Frame f) {
  701. super(f);
  702. }
  703. public void setOrientation(int o) {
  704. if(isShowing()) {
  705. if (o == this.orientation)
  706. return;
  707. this.orientation = o;
  708. Dimension size = getSize();
  709. setSize(new Dimension(size.height, size.width));
  710. if (offset!=null) {
  711. if( BasicGraphicsUtils.isLeftToRight(toolBar) ) {
  712. setOffset(new Point(offset.y, offset.x));
  713. } else if( o == JToolBar.HORIZONTAL ) {
  714. setOffset(new Point( size.height-offset.y, offset.x));
  715. } else {
  716. setOffset(new Point(offset.y, size.width-offset.x));
  717. }
  718. }
  719. repaint();
  720. }
  721. }
  722. public Point getOffset() {
  723. return offset;
  724. }
  725. public void setOffset(Point p) {
  726. this.offset = p;
  727. }
  728. public void setBorderColor(Color c) {
  729. if (this.borderColor == c)
  730. return;
  731. this.borderColor = c;
  732. repaint();
  733. }
  734. public Color getBorderColor() {
  735. return this.borderColor;
  736. }
  737. public void paint(Graphics g) {
  738. Color temp = g.getColor();
  739. g.setColor(getBackground());
  740. Dimension size = getSize();
  741. g.fillRect(0,0,size.width, size.height);
  742. g.setColor(getBorderColor());
  743. g.drawRect(0,0,size.width-1, size.height-1);
  744. g.setColor(temp);
  745. super.paint(g);
  746. }
  747. public Insets getInsets() {
  748. return new Insets(1,1,1,1);
  749. }
  750. }
  751. }