1. /*
  2. * @(#)SynthToolBarUI.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.gtk;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.beans.*;
  13. import javax.swing.border.*;
  14. import javax.swing.plaf.*;
  15. /**
  16. * A Basic L&F implementation of ToolBarUI. This implementation
  17. * is a "combined" view/controller.
  18. * <p>
  19. *
  20. * @version 1.8, 01/23/03 (based on BasicToolBarUI v 1.83)
  21. * @author Georges Saab
  22. * @author Jeff Shapiro
  23. */
  24. class SynthToolBarUI extends ToolBarUI implements SwingConstants, SynthUI {
  25. protected JToolBar toolBar;
  26. private boolean floating;
  27. private int floatingX;
  28. private int floatingY;
  29. private JFrame floatingFrame;
  30. private RootPaneContainer floatingToolBar;
  31. protected DragWindow dragWindow;
  32. private Container dockingSource;
  33. private int dockingSensitivity = 0;
  34. protected int focusedCompIndex = -1;
  35. protected Icon handleIcon = null;
  36. protected Rectangle contentRect = null;
  37. protected MouseInputListener dockingListener;
  38. protected PropertyChangeListener propertyListener;
  39. protected ContainerListener toolBarContListener;
  40. protected FocusListener toolBarFocusListener;
  41. protected String constraintBeforeFloating = BorderLayout.NORTH;
  42. private SynthStyle style;
  43. private SynthStyle contentStyle;
  44. private SynthStyle dragWindowStyle;
  45. private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex";
  46. public static ComponentUI createUI(JComponent c) {
  47. return new SynthToolBarUI();
  48. }
  49. protected String getPropertyPrefix() {
  50. return "ToolBar";
  51. }
  52. public static void loadActionMap(ActionMap map) {
  53. // NOTE: this needs to remain static. If you have a need to
  54. // have Actions that reference the UI in the ActionMap,
  55. // then you'll also need to change the registeration of the
  56. // ActionMap.
  57. map.put("navigateRight", new RightAction());
  58. map.put("navigateLeft", new LeftAction());
  59. map.put("navigateUp", new UpAction());
  60. map.put("navigateDown", new DownAction());
  61. }
  62. public void installUI(JComponent c) {
  63. toolBar = (JToolBar)c;
  64. contentRect = new Rectangle();
  65. // Set defaults
  66. installDefaults();
  67. installComponents();
  68. installListeners();
  69. installKeyboardActions();
  70. // Initialize instance vars
  71. dockingSensitivity = 0;
  72. floating = false;
  73. floatingX = floatingY = 0;
  74. floatingToolBar = null;
  75. setOrientation(toolBar.getOrientation());
  76. c.setOpaque(true);
  77. if (c.getClientProperty(FOCUSED_COMP_INDEX) != null) {
  78. focusedCompIndex =((Integer)(c.getClientProperty(
  79. FOCUSED_COMP_INDEX))).intValue();
  80. }
  81. }
  82. public void uninstallUI(JComponent c) {
  83. // Clear defaults
  84. uninstallDefaults();
  85. uninstallComponents();
  86. uninstallListeners();
  87. uninstallKeyboardActions();
  88. // Clear instance vars
  89. if (isFloating() == true)
  90. setFloating(false, null);
  91. floatingToolBar = null;
  92. dragWindow = null;
  93. dockingSource = null;
  94. c.putClientProperty(FOCUSED_COMP_INDEX, new Integer(focusedCompIndex));
  95. }
  96. protected void installDefaults() {
  97. toolBar.setLayout(createLayout());
  98. fetchStyle(toolBar);
  99. }
  100. private void fetchStyle(JToolBar c) {
  101. SynthContext context = getContext(c, ENABLED);
  102. SynthStyle oldStyle = style;
  103. style = SynthLookAndFeel.updateStyle(context, this);
  104. if (oldStyle != style) {
  105. handleIcon =
  106. style.getIcon(context, getPropertyPrefix() + ".handleIcon");
  107. }
  108. context.dispose();
  109. context = getContext(c, Region.TOOL_BAR_CONTENT, ENABLED);
  110. contentStyle = SynthLookAndFeel.updateStyle(context, this);
  111. context.dispose();
  112. context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
  113. dragWindowStyle = SynthLookAndFeel.updateStyle(context, this);
  114. context.dispose();
  115. }
  116. protected void uninstallDefaults() {
  117. SynthContext context = getContext(toolBar, ENABLED);
  118. style.uninstallDefaults(context);
  119. context.dispose();
  120. style = null;
  121. handleIcon = null;
  122. context = getContext(toolBar, Region.TOOL_BAR_CONTENT, ENABLED);
  123. contentStyle.uninstallDefaults(context);
  124. context.dispose();
  125. contentStyle = null;
  126. context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
  127. dragWindowStyle.uninstallDefaults(context);
  128. context.dispose();
  129. dragWindowStyle = null;
  130. toolBar.setLayout(null);
  131. }
  132. protected void installComponents() {
  133. }
  134. protected void uninstallComponents() {
  135. }
  136. protected void installListeners() {
  137. dockingListener = createDockingListener();
  138. if (dockingListener != null) {
  139. toolBar.addMouseMotionListener( dockingListener );
  140. toolBar.addMouseListener( dockingListener );
  141. }
  142. propertyListener = createPropertyListener(); // added in setFloating
  143. if (propertyListener != null) {
  144. toolBar.addPropertyChangeListener(propertyListener);
  145. }
  146. toolBarContListener = createToolBarContListener();
  147. if ( toolBarContListener != null ) {
  148. toolBar.addContainerListener( toolBarContListener );
  149. }
  150. toolBarFocusListener = createToolBarFocusListener();
  151. if ( toolBarFocusListener != null )
  152. {
  153. // Put focus listener on all components in toolbar
  154. Component[] components = toolBar.getComponents();
  155. for ( int i = 0; i < components.length; ++i )
  156. {
  157. components[ i ].addFocusListener( toolBarFocusListener );
  158. }
  159. }
  160. }
  161. protected void uninstallListeners( )
  162. {
  163. if ( dockingListener != null )
  164. {
  165. toolBar.removeMouseMotionListener(dockingListener);
  166. toolBar.removeMouseListener(dockingListener);
  167. dockingListener = null;
  168. }
  169. if ( propertyListener != null )
  170. {
  171. toolBar.removePropertyChangeListener(propertyListener);
  172. propertyListener = null; // removed in setFloating
  173. }
  174. if ( toolBarContListener != null )
  175. {
  176. toolBar.removeContainerListener( toolBarContListener );
  177. toolBarContListener = null;
  178. }
  179. if ( toolBarFocusListener != null )
  180. {
  181. // Remove focus listener from all components in toolbar
  182. Component[] components = toolBar.getComponents();
  183. for ( int i = 0; i < components.length; ++i )
  184. {
  185. components[ i ].removeFocusListener( toolBarFocusListener );
  186. }
  187. toolBarFocusListener = null;
  188. }
  189. }
  190. protected void installKeyboardActions() {
  191. InputMap km = getInputMap(JComponent.
  192. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  193. SwingUtilities.replaceUIInputMap(toolBar, JComponent.
  194. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  195. km);
  196. LazyActionMap.installLazyActionMap(toolBar, SynthToolBarUI.class,
  197. "ToolBar.actionMap");
  198. }
  199. InputMap getInputMap(int condition) {
  200. if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
  201. SynthContext context = getContext(toolBar, ENABLED);
  202. InputMap map = (InputMap)context.getStyle().get(context,
  203. "ToolBar.ancestorInputMap");
  204. context.dispose();
  205. return map;
  206. }
  207. return null;
  208. }
  209. protected void uninstallKeyboardActions() {
  210. SwingUtilities.replaceUIActionMap(toolBar, null);
  211. SwingUtilities.replaceUIInputMap(toolBar, JComponent.
  212. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  213. null);
  214. }
  215. protected LayoutManager createLayout() {
  216. return new SynthToolBarLayoutManager();
  217. }
  218. public SynthContext getContext(JComponent c) {
  219. return getContext(c, getComponentState(c));
  220. }
  221. private SynthContext getContext(JComponent c, int state) {
  222. return SynthContext.getContext(SynthContext.class, c,
  223. SynthLookAndFeel.getRegion(c), style, state);
  224. }
  225. private SynthContext getContext(JComponent c, Region region) {
  226. return getContext(c, region, getComponentState(c, region));
  227. }
  228. private SynthContext getContext(JComponent c, Region region, int state) {
  229. return SynthContext.getContext(SynthContext.class, c, region,
  230. dragWindowStyle, state);
  231. }
  232. private Region getRegion(JComponent c) {
  233. return SynthLookAndFeel.getRegion(c);
  234. }
  235. private int getComponentState(JComponent c) {
  236. return SynthLookAndFeel.getComponentState(c);
  237. }
  238. private int getComponentState(JComponent c, Region region) {
  239. return SynthLookAndFeel.getComponentState(c);
  240. }
  241. public void update(Graphics g, JComponent c) {
  242. SynthContext context = getContext(c);
  243. SynthLookAndFeel.update(context, g);
  244. paint(context, g);
  245. context.dispose();
  246. }
  247. public void paint(Graphics g, JComponent c) {
  248. SynthContext context = getContext(c);
  249. paint(context, g);
  250. context.dispose();
  251. }
  252. protected void paint(SynthContext context, Graphics g) {
  253. if (handleIcon != null && toolBar.isFloatable()) {
  254. int startX = toolBar.getComponentOrientation().isLeftToRight() ?
  255. 0 : toolBar.getWidth() -
  256. SynthIcon.getIconWidth(handleIcon, context);
  257. SynthIcon.paintIcon(handleIcon, context, g, startX, 0,
  258. SynthIcon.getIconWidth(handleIcon, context),
  259. SynthIcon.getIconHeight(handleIcon, context));
  260. }
  261. SynthContext subcontext = getContext(toolBar, Region.TOOL_BAR_CONTENT);
  262. paintContent(subcontext, g, contentRect);
  263. subcontext.dispose();
  264. }
  265. public void paintContent(SynthContext context, Graphics g,
  266. Rectangle bounds) {
  267. SynthLookAndFeel.updateSubregion(context, g, bounds);
  268. }
  269. protected void navigateFocusedComp(int direction) {
  270. int nComp = toolBar.getComponentCount();
  271. int j;
  272. switch (direction) {
  273. case EAST:
  274. case SOUTH:
  275. if (focusedCompIndex < 0 || focusedCompIndex >= nComp) break;
  276. j = focusedCompIndex + 1;
  277. while (j != focusedCompIndex) {
  278. if (j >= nComp) j = 0;
  279. Component comp = toolBar.getComponentAtIndex(j++);
  280. if (comp != null && comp.isFocusTraversable()) {
  281. comp.requestFocus();
  282. break;
  283. }
  284. }
  285. break;
  286. case WEST:
  287. case NORTH:
  288. if (focusedCompIndex < 0 || focusedCompIndex >= nComp) break;
  289. j = focusedCompIndex - 1;
  290. while (j != focusedCompIndex) {
  291. if (j < 0) j = nComp - 1;
  292. Component comp = toolBar.getComponentAtIndex(j--);
  293. if (comp != null && comp.isFocusTraversable()) {
  294. comp.requestFocus();
  295. break;
  296. }
  297. }
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. /**
  304. * No longer used, use BasicToolBarUI.createFloatingWindow(JToolBar)
  305. * @see #createFloatingWindow
  306. */
  307. protected JFrame createFloatingFrame(JToolBar toolbar) {
  308. Window window = SwingUtilities.getWindowAncestor(toolbar);
  309. JFrame frame = new JFrame(toolbar.getName(),
  310. (window != null) ? window.getGraphicsConfiguration() : null) {
  311. // Override createRootPane() to automatically resize
  312. // the frame when contents change
  313. protected JRootPane createRootPane() {
  314. JRootPane rootPane = new JRootPane() {
  315. private boolean packing = false;
  316. public void validate() {
  317. super.validate();
  318. if (!packing) {
  319. packing = true;
  320. pack();
  321. packing = false;
  322. }
  323. }
  324. };
  325. rootPane.setOpaque(true);
  326. return rootPane;
  327. }
  328. };
  329. frame.getRootPane().setName("ToolBar.FloatingFrame");
  330. frame.setResizable(false);
  331. WindowListener wl = createFrameListener();
  332. frame.addWindowListener(wl);
  333. return frame;
  334. }
  335. /**
  336. * Creates a window which contains the toolbar after it has been
  337. * dragged out from its container
  338. * @returns a <code>RootPaneContainer</code> object, containing the toolbar.
  339. */
  340. protected RootPaneContainer createFloatingWindow(JToolBar toolbar) {
  341. class ToolBarDialog extends JDialog {
  342. public ToolBarDialog(Frame owner, String title, boolean modal) {
  343. super(owner, title, modal);
  344. }
  345. public ToolBarDialog(Dialog owner, String title, boolean modal) {
  346. super(owner, title, modal);
  347. }
  348. // Override createRootPane() to automatically resize
  349. // the frame when contents change
  350. protected JRootPane createRootPane() {
  351. JRootPane rootPane = new JRootPane() {
  352. private boolean packing = false;
  353. public void validate() {
  354. super.validate();
  355. if (!packing) {
  356. packing = true;
  357. pack();
  358. packing = false;
  359. }
  360. }
  361. };
  362. rootPane.setOpaque(true);
  363. return rootPane;
  364. }
  365. }
  366. JDialog dialog;
  367. Window window = SwingUtilities.getWindowAncestor(toolbar);
  368. if (window instanceof Frame) {
  369. dialog = new ToolBarDialog((Frame)window, toolbar.getName(), false);
  370. } else if (window instanceof Dialog) {
  371. dialog = new ToolBarDialog((Dialog)window, toolbar.getName(), false);
  372. } else {
  373. dialog = new ToolBarDialog((Frame)null, toolbar.getName(), false);
  374. }
  375. dialog.getRootPane().setName("ToolBar.FloatingWindow");
  376. dialog.setTitle(toolbar.getName());
  377. dialog.setResizable(false);
  378. WindowListener wl = createFrameListener();
  379. dialog.addWindowListener(wl);
  380. return dialog;
  381. }
  382. protected DragWindow createDragWindow(JToolBar toolbar) {
  383. Window frame = null;
  384. if(toolBar != null) {
  385. Container p;
  386. for(p = toolBar.getParent() ; p != null && !(p instanceof Window) ;
  387. p = p.getParent());
  388. if(p != null && p instanceof Window)
  389. frame = (Window) p;
  390. }
  391. if(floatingToolBar == null) {
  392. floatingToolBar = createFloatingWindow(toolBar);
  393. }
  394. if (floatingToolBar instanceof Window) frame = (Window) floatingToolBar;
  395. DragWindow dragWindow = new DragWindow(frame);
  396. return dragWindow;
  397. }
  398. public Dimension getMinimumSize(JComponent c) {
  399. return getPreferredSize(c);
  400. }
  401. public Dimension getPreferredSize(JComponent c) {
  402. return null;
  403. }
  404. public Dimension getMaximumSize(JComponent c) {
  405. return getPreferredSize(c);
  406. }
  407. public void setFloatingLocation(int x, int y) {
  408. floatingX = x;
  409. floatingY = y;
  410. }
  411. public boolean isFloating() {
  412. return floating;
  413. }
  414. public void setFloating(boolean b, Point p) {
  415. if (toolBar.isFloatable() == true) {
  416. if (dragWindow != null)
  417. dragWindow.setVisible(false);
  418. this.floating = b;
  419. if (b == true)
  420. {
  421. if (dockingSource == null)
  422. {
  423. dockingSource = toolBar.getParent();
  424. dockingSource.remove(toolBar);
  425. }
  426. Point l = new Point();
  427. toolBar.getLocation(l);
  428. constraintBeforeFloating = calculateConstraint(dockingSource, l);
  429. if ( propertyListener != null )
  430. UIManager.addPropertyChangeListener( propertyListener );
  431. if (floatingToolBar == null)
  432. floatingToolBar = createFloatingWindow(toolBar);
  433. floatingToolBar.getContentPane().add(toolBar,BorderLayout.CENTER);
  434. setOrientation( JToolBar.HORIZONTAL );
  435. if (floatingToolBar instanceof Window) ((Window)floatingToolBar).pack();
  436. if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setLocation(floatingX, floatingY);
  437. if (floatingToolBar instanceof Window) ((Window)floatingToolBar).show();
  438. } else {
  439. if (floatingToolBar == null)
  440. floatingToolBar = createFloatingWindow(toolBar);
  441. if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setVisible(false);
  442. floatingToolBar.getContentPane().remove(toolBar);
  443. String constraint = getDockingConstraint(dockingSource,
  444. p);
  445. int orientation = mapConstraintToOrientation(constraint);
  446. setOrientation(orientation);
  447. if (dockingSource== null)
  448. dockingSource = toolBar.getParent();
  449. if ( propertyListener != null )
  450. UIManager.removePropertyChangeListener( propertyListener );
  451. dockingSource.add(constraint, toolBar);
  452. }
  453. dockingSource.invalidate();
  454. Container dockingSourceParent = dockingSource.getParent();
  455. if (dockingSourceParent != null)
  456. dockingSourceParent.validate();
  457. dockingSource.repaint();
  458. }
  459. }
  460. private int mapConstraintToOrientation(String constraint) {
  461. int orientation = toolBar.getOrientation();
  462. if (constraint != null) {
  463. if (constraint.equals(BorderLayout.EAST) ||
  464. constraint.equals(BorderLayout.WEST))
  465. orientation = JToolBar.VERTICAL;
  466. else if (constraint.equals(BorderLayout.NORTH) ||
  467. constraint.equals(BorderLayout.SOUTH))
  468. orientation = JToolBar.HORIZONTAL;
  469. }
  470. return orientation;
  471. }
  472. public void setOrientation(int orientation) {
  473. toolBar.setOrientation(orientation);
  474. if (dragWindow != null)
  475. dragWindow.setOrientation(orientation);
  476. }
  477. public boolean canDock(Component c, Point p) {
  478. boolean b = false;
  479. if (c.contains(p)) {
  480. dockingSensitivity = (toolBar.getOrientation() == JToolBar.HORIZONTAL) ? toolBar.getSize().height : toolBar.getSize().width;
  481. // North
  482. if (p.y < dockingSensitivity)
  483. b = true;
  484. // South
  485. if (p.y > c.getSize().height-dockingSensitivity)
  486. b = true;
  487. // West (Base distance on height for now!)
  488. if (p.x < dockingSensitivity)
  489. b = true;
  490. // East (Base distance on height for now!)
  491. if (p.x > c.getSize().width-dockingSensitivity)
  492. b = true;
  493. }
  494. return b;
  495. }
  496. private String calculateConstraint(Component c, Point p) {
  497. if (p == null) return constraintBeforeFloating;
  498. String s = BorderLayout.NORTH;
  499. if (c.contains(p)) {
  500. dockingSensitivity = (toolBar.getOrientation() == JToolBar.HORIZONTAL) ? toolBar.getSize().height : toolBar.getSize().width;
  501. if (p.y >= dockingSource.getSize().height-dockingSensitivity)
  502. s = BorderLayout.SOUTH;
  503. // West (Base distance on height for now!)
  504. else if (p.x < dockingSensitivity && (toolBar.getOrientation() == JToolBar.VERTICAL))
  505. s = BorderLayout.WEST;
  506. // East (Base distance on height for now!)
  507. else if (p.x >= dockingSource.getSize().width-dockingSensitivity && (toolBar.getOrientation() == JToolBar.VERTICAL))
  508. s = BorderLayout.EAST;
  509. // North (Base distance on height for now!)
  510. else if (p.y < dockingSensitivity)
  511. s = BorderLayout.NORTH;
  512. }
  513. return s;
  514. }
  515. private String getDockingConstraint(Component c, Point p) {
  516. if (p == null) return constraintBeforeFloating;
  517. String s = BorderLayout.NORTH;
  518. if (c.contains(p)) {
  519. dockingSensitivity = (toolBar.getOrientation() == JToolBar.HORIZONTAL) ? toolBar.getSize().height : toolBar.getSize().width;
  520. if (p.y >= dockingSource.getSize().height-dockingSensitivity)
  521. s = BorderLayout.SOUTH;
  522. // West (Base distance on height for now!)
  523. if (p.x < dockingSensitivity)
  524. s = BorderLayout.WEST;
  525. // East (Base distance on height for now!)
  526. if (p.x >= dockingSource.getSize().width-dockingSensitivity)
  527. s = BorderLayout.EAST;
  528. // North (Base distance on height for now!)
  529. if (p.y < dockingSensitivity)
  530. s = BorderLayout.NORTH;
  531. }
  532. return s;
  533. }
  534. protected void dragTo(Point position, Point origin)
  535. {
  536. if (toolBar.isFloatable() == true)
  537. {
  538. try
  539. {
  540. if (dragWindow == null)
  541. dragWindow = createDragWindow(toolBar);
  542. Point offset = dragWindow.getOffset();
  543. if (offset == null) {
  544. Dimension size = toolBar.getPreferredSize();
  545. offset = new Point(size.width2, size.height2);
  546. dragWindow.setOffset(offset);
  547. }
  548. Point global = new Point(origin.x+ position.x,
  549. origin.y+position.y);
  550. Point dragPoint = new Point(global.x- offset.x,
  551. global.y- offset.y);
  552. if (dockingSource == null)
  553. dockingSource = toolBar.getParent();
  554. Point p = new Point(origin);
  555. SwingUtilities.convertPointFromScreen(p,toolBar.getParent());
  556. constraintBeforeFloating = calculateConstraint(dockingSource, p);
  557. Point dockingPosition = dockingSource.getLocationOnScreen();
  558. Point comparisonPoint = new Point(global.x-dockingPosition.x,
  559. global.y-dockingPosition.y);
  560. if (canDock(dockingSource, comparisonPoint)) {
  561. String constraint = getDockingConstraint(dockingSource,
  562. comparisonPoint);
  563. int orientation = mapConstraintToOrientation(constraint);
  564. dragWindow.setOrientation(orientation);
  565. } else {
  566. dragWindow.setOrientation( JToolBar.HORIZONTAL );
  567. }
  568. dragWindow.setLocation(dragPoint.x, dragPoint.y);
  569. if (dragWindow.isVisible() == false) {
  570. Dimension size = toolBar.getPreferredSize();
  571. dragWindow.setSize(size.width, size.height);
  572. dragWindow.show();
  573. }
  574. }
  575. catch ( IllegalComponentStateException e )
  576. {
  577. }
  578. }
  579. }
  580. protected void floatAt(Point position, Point origin)
  581. {
  582. if(toolBar.isFloatable() == true)
  583. {
  584. try
  585. {
  586. Point offset = dragWindow.getOffset();
  587. if (offset == null) {
  588. offset = position;
  589. dragWindow.setOffset(offset);
  590. }
  591. Point global = new Point(origin.x+ position.x,
  592. origin.y+position.y);
  593. setFloatingLocation(global.x-offset.x,
  594. global.y-offset.y);
  595. if (dockingSource != null) {
  596. Point dockingPosition = dockingSource.getLocationOnScreen();
  597. Point comparisonPoint = new Point(global.x-dockingPosition.x,
  598. global.y-dockingPosition.y);
  599. if (canDock(dockingSource, comparisonPoint)) {
  600. setFloating(false, comparisonPoint);
  601. } else {
  602. setFloating(true, null);
  603. }
  604. } else {
  605. setFloating(true, null);
  606. }
  607. dragWindow.setOffset(null);
  608. }
  609. catch ( IllegalComponentStateException e )
  610. {
  611. }
  612. }
  613. }
  614. protected ContainerListener createToolBarContListener( )
  615. {
  616. return new ToolBarContListener( );
  617. }
  618. protected FocusListener createToolBarFocusListener( )
  619. {
  620. return new ToolBarFocusListener( );
  621. }
  622. protected PropertyChangeListener createPropertyListener()
  623. {
  624. return new PropertyListener();
  625. }
  626. protected MouseInputListener createDockingListener( ) {
  627. return new DockingListener(toolBar);
  628. }
  629. protected WindowListener createFrameListener() {
  630. return new FrameListener();
  631. }
  632. // The private inner classes below should be changed to protected the
  633. // next time API changes are allowed.
  634. private static abstract class KeyAction extends AbstractAction {
  635. public boolean isEnabled() {
  636. return true;
  637. }
  638. };
  639. private static class RightAction extends KeyAction {
  640. public void actionPerformed(ActionEvent e) {
  641. JToolBar toolBar = (JToolBar)e.getSource();
  642. // PENDING:
  643. SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();
  644. ui.navigateFocusedComp(EAST);
  645. }
  646. };
  647. private static class LeftAction extends KeyAction {
  648. public void actionPerformed(ActionEvent e) {
  649. JToolBar toolBar = (JToolBar)e.getSource();
  650. SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();
  651. ui.navigateFocusedComp(WEST);
  652. }
  653. };
  654. private static class UpAction extends KeyAction {
  655. public void actionPerformed(ActionEvent e) {
  656. JToolBar toolBar = (JToolBar)e.getSource();
  657. SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();
  658. ui.navigateFocusedComp(NORTH);
  659. }
  660. };
  661. private static class DownAction extends KeyAction {
  662. public void actionPerformed(ActionEvent e) {
  663. JToolBar toolBar = (JToolBar)e.getSource();
  664. SynthToolBarUI ui = (SynthToolBarUI)toolBar.getUI();
  665. ui.navigateFocusedComp(SOUTH);
  666. }
  667. };
  668. protected class FrameListener extends WindowAdapter {
  669. public void windowClosing(WindowEvent w) {
  670. if (toolBar.isFloatable() == true) {
  671. if (dragWindow != null)
  672. dragWindow.setVisible(false);
  673. floating = false;
  674. if (floatingToolBar == null)
  675. floatingToolBar = createFloatingWindow(toolBar);
  676. if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setVisible(false);
  677. floatingToolBar.getContentPane().remove(toolBar);
  678. String constraint = constraintBeforeFloating;
  679. int orientation = mapConstraintToOrientation(constraint);
  680. setOrientation(orientation);
  681. if (dockingSource == null)
  682. dockingSource = toolBar.getParent();
  683. if (propertyListener != null)
  684. UIManager.removePropertyChangeListener(propertyListener);
  685. dockingSource.add(constraint, toolBar);
  686. dockingSource.invalidate();
  687. Container dockingSourceParent = dockingSource.getParent();
  688. if (dockingSourceParent != null)
  689. dockingSourceParent.validate();
  690. dockingSource.repaint();
  691. setFloating(false,null);
  692. }
  693. }
  694. }
  695. protected class ToolBarContListener implements ContainerListener
  696. {
  697. public void componentAdded( ContainerEvent e ) {
  698. Component c = e.getChild();
  699. if ( toolBarFocusListener != null ) {
  700. c.addFocusListener( toolBarFocusListener );
  701. }
  702. }
  703. public void componentRemoved( ContainerEvent e ) {
  704. Component c = e.getChild();
  705. if ( toolBarFocusListener != null ) {
  706. c.removeFocusListener( toolBarFocusListener );
  707. }
  708. }
  709. } // end class ToolBarContListener
  710. protected class ToolBarFocusListener implements FocusListener
  711. {
  712. public void focusGained( FocusEvent e )
  713. {
  714. Component c = e.getComponent();
  715. focusedCompIndex = toolBar.getComponentIndex( c );
  716. }
  717. public void focusLost( FocusEvent e )
  718. {
  719. }
  720. } // end class ToolBarFocusListener
  721. protected class PropertyListener implements PropertyChangeListener
  722. {
  723. public void propertyChange( PropertyChangeEvent e ) {
  724. String propertyName = e.getPropertyName();
  725. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  726. fetchStyle(toolBar);
  727. }
  728. if ( propertyName.equals("lookAndFeel") ) {
  729. toolBar.updateUI();
  730. } else if (propertyName.equals("orientation")) {
  731. // Search for JSeparator components and change it's orientation
  732. // to match the toolbar and flip it's orientation.
  733. Component[] components = toolBar.getComponents();
  734. int orientation = ((Integer)e.getNewValue()).intValue();
  735. JToolBar.Separator separator;
  736. for( int i = 0; i < components.length; ++i ) {
  737. if (components[i] instanceof JToolBar.Separator) {
  738. separator = (JToolBar.Separator)components[i];
  739. separator.setOrientation(orientation);
  740. Dimension size = separator.getSize();
  741. if (size.width != size.height) {
  742. // Flip the orientation.
  743. Dimension newSize = new Dimension(size.height, size.width);
  744. separator.setSeparatorSize(newSize);
  745. }
  746. }
  747. }
  748. }
  749. }
  750. }
  751. class SynthToolBarLayoutManager implements LayoutManager {
  752. public void addLayoutComponent(String name, Component comp) {}
  753. public void removeLayoutComponent(Component comp) {}
  754. public Dimension minimumLayoutSize(Container parent) {
  755. JToolBar tb = (JToolBar)parent;
  756. Dimension dim = new Dimension();
  757. SynthContext context = getContext(tb);
  758. if (tb.getOrientation() == JToolBar.HORIZONTAL) {
  759. dim.width = SynthIcon.getIconWidth(handleIcon, context);
  760. Dimension compDim;
  761. for (int i = 0; i < tb.getComponentCount(); i++) {
  762. compDim = tb.getComponent(i).getMinimumSize();
  763. dim.width += compDim.width;
  764. dim.height = Math.max(dim.height, compDim.height);
  765. }
  766. } else {
  767. dim.height =
  768. SynthIcon.getIconHeight(handleIcon, context);
  769. Dimension compDim;
  770. for (int i = 0; i < tb.getComponentCount(); i++) {
  771. compDim = tb.getComponent(i).getMinimumSize();
  772. dim.width = Math.max(dim.width, compDim.width);
  773. dim.height += compDim.height;
  774. }
  775. }
  776. context.dispose();
  777. return dim;
  778. }
  779. public Dimension preferredLayoutSize(Container parent) {
  780. JToolBar tb = (JToolBar)parent;
  781. Dimension dim = new Dimension();
  782. SynthContext context = getContext(tb);
  783. if (tb.getOrientation() == JToolBar.HORIZONTAL) {
  784. dim.width = SynthIcon.getIconWidth(handleIcon, context);
  785. Dimension compDim;
  786. for (int i = 0; i < tb.getComponentCount(); i++) {
  787. compDim = tb.getComponent(i).getPreferredSize();
  788. dim.width += compDim.width;
  789. dim.height = Math.max(dim.height, compDim.height);
  790. }
  791. } else {
  792. dim.height =
  793. SynthIcon.getIconHeight(handleIcon, context);
  794. Dimension compDim;
  795. for (int i = 0; i < tb.getComponentCount(); i++) {
  796. compDim = tb.getComponent(i).getPreferredSize();
  797. dim.width = Math.max(dim.width, compDim.width);
  798. dim.height += compDim.height;
  799. }
  800. }
  801. context.dispose();
  802. return dim;
  803. }
  804. public void layoutContainer(Container parent) {
  805. JToolBar tb = (JToolBar)parent;
  806. boolean ltr = tb.getComponentOrientation().isLeftToRight();
  807. SynthContext context = getContext(tb);
  808. int handleWidth = SynthIcon.getIconWidth(handleIcon, context);
  809. Component c;
  810. Dimension d;
  811. if (tb.getOrientation() == JToolBar.HORIZONTAL) {
  812. int x = ltr ? handleWidth : tb.getWidth() - handleWidth;
  813. for (int i = 0; i < tb.getComponentCount(); i++) {
  814. c = tb.getComponent(i);
  815. d = c.getPreferredSize();
  816. c.setBounds(ltr ? x : x - d.width, 0, d.width, d.height);
  817. x = ltr ? x + d.width : x - d.width;
  818. }
  819. contentRect.x = ltr ?
  820. SynthIcon.getIconWidth(handleIcon, context) : 0;
  821. contentRect.y = 0;
  822. contentRect.width = tb.getWidth() - contentRect.x;
  823. contentRect.height = tb.getHeight();
  824. } else {
  825. int y = SynthIcon.getIconHeight(handleIcon, context);
  826. for (int i = 0; i < tb.getComponentCount(); i++) {
  827. c = tb.getComponent(i);
  828. d = c.getPreferredSize();
  829. c.setBounds(0, y, d.width, d.height);
  830. y += d.height;
  831. }
  832. contentRect.x = 0;
  833. contentRect.y =
  834. SynthIcon.getIconHeight(handleIcon, context);
  835. contentRect.width = tb.getWidth();
  836. contentRect.height = tb.getHeight() - contentRect.y;
  837. }
  838. context.dispose();
  839. }
  840. }
  841. class DockingListener implements MouseInputListener {
  842. protected JToolBar toolBar;
  843. protected boolean isDragging = false;
  844. protected Point origin = null;
  845. public DockingListener(JToolBar t) {
  846. this.toolBar = t;
  847. }
  848. public void mouseClicked(MouseEvent e) {}
  849. public void mousePressed(MouseEvent e) {
  850. if (!toolBar.isEnabled()) {
  851. return;
  852. }
  853. isDragging = false;
  854. }
  855. public void mouseReleased(MouseEvent e) {
  856. if (!toolBar.isEnabled()) {
  857. return;
  858. }
  859. if (isDragging == true) {
  860. Point position = e.getPoint();
  861. if (origin == null)
  862. origin = e.getComponent().getLocationOnScreen();
  863. floatAt(position, origin);
  864. }
  865. origin = null;
  866. isDragging = false;
  867. }
  868. public void mouseEntered(MouseEvent e) { }
  869. public void mouseExited(MouseEvent e) { }
  870. public void mouseDragged(MouseEvent e) {
  871. if (!toolBar.isEnabled()) {
  872. return;
  873. }
  874. isDragging = true;
  875. Point position = e.getPoint();
  876. if (origin == null)
  877. origin = e.getComponent().getLocationOnScreen();
  878. dragTo(position, origin);
  879. }
  880. public void mouseMoved(MouseEvent e) {
  881. }
  882. }
  883. protected class DragWindow extends Window
  884. {
  885. int orientation = toolBar.getOrientation();
  886. Point offset; // offset of the mouse cursor inside the DragWindow
  887. DragWindow(Window w) {
  888. super(w);
  889. }
  890. public void setOrientation(int o) {
  891. if(isShowing()) {
  892. if (o == this.orientation)
  893. return;
  894. this.orientation = o;
  895. Dimension size = getSize();
  896. setSize(new Dimension(size.height, size.width));
  897. if (offset!=null) {
  898. if( SynthLookAndFeel.isLeftToRight(toolBar) ) {
  899. setOffset(new Point(offset.y, offset.x));
  900. } else if( o == JToolBar.HORIZONTAL ) {
  901. setOffset(new Point( size.height-offset.y, offset.x));
  902. } else {
  903. setOffset(new Point(offset.y, size.width-offset.x));
  904. }
  905. }
  906. repaint();
  907. }
  908. }
  909. public Point getOffset() {
  910. return offset;
  911. }
  912. public void setOffset(Point p) {
  913. this.offset = p;
  914. }
  915. public void paint(Graphics g) {
  916. SynthContext context = getContext(toolBar,
  917. Region.TOOL_BAR_DRAG_WINDOW);
  918. SynthLookAndFeel.updateSubregion(context, g, new Rectangle(
  919. 0, 0, getWidth(), getHeight()));
  920. context.dispose();
  921. }
  922. public Insets getInsets() {
  923. return new Insets(1,1,1,1);
  924. }
  925. }
  926. }