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