1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
|
/*
* ToolBarHandler.java
* 2004-01-10
*/
package org.tigris.toolbar.layouts;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.plaf.*;
/**
* Utilitiy class that replaces JToolBar's look and feel mouse listeners
* to manage drag and drop behavior. Also provides a drag outline window
* and a floating frame for the associated toolbar.
* @author Christopher Bach
*/
// Package access only...
class ToolBarHandler
{
private DockingConstraints ourConstraints = new DockingConstraints();
private JToolBar ourToolBar = null;
private DockLayout ourDockLayout = null;
private JDialog ourFloatFrame = null;
private DraggingWindow ourDraggingWindow = null;
private ToolBarDragListener ourDragListener = null;
private UIChangeListener ourUIListener = null;
private boolean ourToolBarIsDragging = false;
private boolean ourToolBarShouldFloat = false;
private static boolean ourVersionIsCompatible = false;
public static final String TOOL_BAR_HANDLER_KEY = "ToolBarHandler";
/**
* Creates a ToolBarHandler for the specified toolbar
* to be docked within the specified container.
*/
public ToolBarHandler(JToolBar toolbar, DockLayout layout)
{
ourToolBar = toolbar;
ourDockLayout = layout;
// Initially the value of the static variable ourVersionIsCompatible is
// false so that for compatible versions (1.3 and above) this check is
// only performed once...
if (!ourVersionIsCompatible)
{
String specVersion = System.getProperty("java.specification.version");
try
{
float ver = Float.parseFloat(specVersion);
if (ver > 1.2f) ourVersionIsCompatible = true;
}
catch(Exception ex)
{
// Assume the version is not high enough,
// leave ourVersionIsCompatible = false
}
}
if (ourVersionIsCompatible)
{
ourDragListener = new ToolBarDragListener();
ourUIListener = new UIChangeListener();
installListeners();
}
}
/**
* Sets the DockingConstraints for this handler representing
* the edge and indicies at which this handler's toolbar
* should be docked.
*/
public void setConstraints(DockingConstraints constraints)
{
ourConstraints = constraints;
}
/**
* Returns this handler's DockingConstraints representing
* the edge and indicies at which this handler's toolbar
* should be docked.
*/
public DockingConstraints getConstraints()
{
return ourConstraints;
}
/**
* Gets the edge in which the assocated toolbar should be docked.
*/
public int getDockEdge()
{
return ourConstraints.getEdge();
}
/**
* Sets the edge in which the associated toolbar should be docked.
*/
public void setDockEdge(int edge)
{
ourConstraints.setEdge(edge);
}
/**
* Gets the index at which the associated toolbar
* should be docked within its edge.
*/
public int getDockIndex()
{
return ourConstraints.getIndex();
}
/**
* Sets the index at which the associated toolbar
* should be docked within its edge.
*/
public void setDockIndex(int index)
{
ourConstraints.setIndex(index);
}
/**
* Gets the row index at which the associated toolbar
* should be docked within its edge. Not all dock boundary
* styles will support control over the row index.
*/
public int getRowIndex()
{
return ourConstraints.getRow();
}
/**
* Sets the row index at which the associated toolbar
* should be docked within its edge. Not all dock boundary
* styles will support control over the row index.
*/
public void setRowIndex(int index)
{
ourConstraints.setRow(index);
}
/**
* Hides the associated toolbar by removing it from its dock
* or by closing its client floating frame.
*/
public void hideToolBar()
{
Container target = ourDockLayout.getTargetContainer();
target.remove(ourToolBar);
JDialog floatFrame = getFloatingFrame();
if (floatFrame != null)
{
floatFrame.setVisible(false);
floatFrame.getContentPane().remove(ourToolBar);
}
target.validate();
target.repaint();
}
/**
* Docks the associated toolbar at its last visible location.
*/
public void dockToolBar()
{
dockToolBar(getDockEdge());
}
/**
* Docks the associated toolbar at the specified edge.
*/
public void dockToolBar(int edge)
{
dockToolBar(edge, getRowIndex(), getDockIndex());
}
/**
* Docks the associated toolbar at the secified edge and indicies.
*/
public void dockToolBar(int edge, int row, int index)
{
Container target = ourDockLayout.getTargetContainer();
if (target == null) return;
target.remove(ourToolBar);
JDialog floatFrame = getFloatingFrame();
if (floatFrame != null)
{
floatFrame.setVisible(false);
floatFrame.getContentPane().remove(ourToolBar);
}
ourConstraints.setEdge(edge);
ourConstraints.setRow(row);
ourConstraints.setIndex(index);
target.add(ourToolBar, ourConstraints);
ourToolBarShouldFloat = false;
target.validate();
target.repaint();
}
/**
* Floats the associated toolbar at its natural location.
*/
public void floatToolBar()
{
Point p = getFloatingLocation();
floatToolBar(p.x, p.y);
}
/**
* Floats the associated toolbar at the specified screen location.
*/
public void floatToolBar(int x, int y)
{
floatToolBar(x, y, false);
}
/**
* Floats the associated toolbar at the specified screen location,
* optionally centering the floating frame on this point.
*/
public void floatToolBar(int x, int y, boolean center)
{
JDialog floatFrame = getFloatingFrame();
if (floatFrame == null) return;
Container target = ourDockLayout.getTargetContainer();
if (target != null) target.remove(ourToolBar);
floatFrame.setVisible(false);
floatFrame.getContentPane().remove(ourToolBar);
ourToolBar.setOrientation(DockLayout.HORIZONTAL);
floatFrame.getContentPane().add(ourToolBar, BorderLayout.CENTER);
floatFrame.pack();
if (center)
{
x -= floatFrame.getWidth() / 2;
y -= floatFrame.getHeight() / 2;
}
// x and y are given relative to screen
floatFrame.setLocation(x, y);
floatFrame.setTitle(ourToolBar.getName());
floatFrame.setVisible(true);
ourToolBarShouldFloat = true;
if (target != null)
{
target.validate();
target.repaint();
}
}
/**
* Gets the screen location of the associated
* toolbar's floating frame.
*/
public Point getFloatingLocation()
{
JDialog floatFrame = getFloatingFrame();
if (floatFrame != null) return floatFrame.getLocation();
else return new Point(0, 0);
}
/**
* Sets the screen location of the associated
* toolbar's floating frame.
*/
public void setFloatingLocation(int x, int y)
{
JDialog floatFrame = getFloatingFrame();
if (floatFrame != null) floatFrame.setLocation(x, y);
}
/**
* Determines whether or not the associated toolbar should be floated.
*/
public boolean shouldFloat()
{
return ourToolBarShouldFloat;
}
/**
* Stores whether or not the associated toolbar should be floated.
*/
public void setShouldFloat(boolean shouldFloat)
{
ourToolBarShouldFloat = shouldFloat;
}
/**
* Gets the nearest dockable DockBoundary for the specified point,
* or returns null if the point is not a dockable location.
*/
private DockBoundary getDockableBoundary(Point point)
{
return ourDockLayout.getDockableBoundary(point);
}
/**
* Checks to see if the DockBoundary containing the provided
* toolbar wishes to veto the drag operation at the provided
* point. Some DockBoundaries may manipulate the positions
* of the toolbars without this handler having to undock and
* redock the toolbar.
*/
private boolean isDraggable(Point point, JToolBar toolbar)
{
if (toolbar == null) return false;
DockBoundary boundary = ourDockLayout.getBoundary(point);
if (boundary == null) return true;
else if (boundary.containsToolBar(toolbar))
return boundary.isDraggablePoint(point, toolbar);
else return true;
}
/**
* Returns the client frame for supporting the floating toolbar.
*/
private JDialog getFloatingFrame()
{
if (ourFloatFrame == null)
{
Window w = SwingUtilities.getWindowAncestor(
ourDockLayout.getTargetContainer());
if (w == null) return null;
Frame fr = null;
if (w instanceof Frame) fr = (Frame)w;
ourFloatFrame = new JDialog(fr);
ourFloatFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
ourFloatFrame.addWindowListener(new FloatFrameCloseListener());
ourFloatFrame.getContentPane().setLayout(new BorderLayout());
ourFloatFrame.setTitle(ourToolBar.getName());
ourFloatFrame.setResizable(false);
}
return ourFloatFrame;
}
/**
* Returns a temporary window to display which represents the estimated
* bounds of the toolbar while it is being dragged.
*/
private DraggingWindow getDraggingWindow()
{
if (ourDraggingWindow == null)
{
Window w = SwingUtilities.getWindowAncestor(
ourDockLayout.getTargetContainer());
if (w != null) ourDraggingWindow = new DraggingWindow(w);
}
return ourDraggingWindow;
}
/**
* Releases this handler's listeners from the associated toolbar.
*/
public void uninstallListeners()
{
// May want to restore the original listeners that were
// stripped from the toolbar when this handler was created.
ourToolBar.removeMouseListener(ourDragListener);
ourToolBar.removeMouseMotionListener(ourDragListener);
ourToolBar.removePropertyChangeListener(ourUIListener);
}
/**
* Strips off the UI's mouse listeners attached to the associated toolbar
* and replaces them with this handler's listeners.
*/
private void installListeners()
{
if (!ourVersionIsCompatible) return;
ourToolBar.removePropertyChangeListener("UI", ourUIListener);
// Uninstall the current ui, collect the remaining listeners
// on the toolbar, and reinstall the ui...
ComponentUI ui = ourToolBar.getUI();
ui.uninstallUI(ourToolBar);
java.util.List mList = Arrays.asList(
ourToolBar.getListeners(
MouseListener.class));
java.util.List mmList = Arrays.asList(
ourToolBar.getListeners(
MouseMotionListener.class));
ui.installUI(ourToolBar);
// ...then remove the listeners that were added by the ui...
MouseListener[] ml
= (MouseListener[])ourToolBar.getListeners(
MouseListener.class);
MouseMotionListener[] mml
= (MouseMotionListener[])ourToolBar.getListeners(
MouseMotionListener.class);
for (int i=0; i < ml.length; i++)
{
if (!mList.contains(ml[i]))
ourToolBar.removeMouseListener(ml[i]);
}
for (int i=0; i < mml.length; i++)
{
if (!mmList.contains(mml[i]))
ourToolBar.removeMouseMotionListener(mml[i]);
}
// ...and add our listeners to the toolbar.
ourToolBar.addMouseListener(ourDragListener);
ourToolBar.addMouseMotionListener(ourDragListener);
ourToolBar.addPropertyChangeListener("UI", ourUIListener);
}
/**
* Inner class that monitors the float frame for closing.
*/
private class FloatFrameCloseListener extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
dockToolBar(getDockEdge(), getRowIndex(), getDockIndex());
}
}
/**
* Inner class that listens for changes in the UI on the
* associated toolbar. Insures that when a new UI is installed,
* its listeners are replaced.
*/
private class UIChangeListener implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent pce)
{
installListeners();
}
}
/**
* Inner class that replaces the associated toolbar's mouse
* listeners and handles the drag and drop behavior.
*/
private class ToolBarDragListener extends MouseInputAdapter
{
public void mouseDragged(MouseEvent me)
{
ourDraggingWindow = getDraggingWindow();
if (ourDraggingWindow == null) return;
// Only allow Button 1 to perform the drag...
if ((me.getModifiers() & MouseEvent.BUTTON1_MASK) != MouseEvent.BUTTON1_MASK)
{
hideDraggingWindow();
return;
}
Container target = ourDockLayout.getTargetContainer();
if (target == null) return;
Point p = me.getPoint();
p = SwingUtilities.convertPoint(ourToolBar, p, target);
// Make sure the DockBoundary containing this point
// and this toolbar wishes to allow the drag operation
// to commence or continue...
if (!isDraggable(p, ourToolBar))
{
hideDraggingWindow();
return;
}
// Determine if this point lies within a
// DockBoundary's dockable range...
int orient = DockLayout.HORIZONTAL;
boolean dockable = false;
if (!me.isControlDown())
{
DockBoundary dock = getDockableBoundary(p);
if (dock != null)
{
dockable = true;
orient = dock.getOrientation();
}
}
// Present the dragging window at this point on the screen...
SwingUtilities.convertPointToScreen(p, target);
ourDraggingWindow.presentWindow(p, dockable, orient);
ourToolBarIsDragging = true;
}
public void mouseReleased(MouseEvent me)
{
if (!ourToolBarIsDragging) return;
if ((me.getModifiers() & MouseEvent.BUTTON1_MASK) != MouseEvent.BUTTON1_MASK)
{
return;
}
hideDraggingWindow();
Container target = ourDockLayout.getTargetContainer();
if (target == null) return;
Point p = me.getPoint();
p = SwingUtilities.convertPoint(ourToolBar, p, target);
DockBoundary dock = null;
if (!me.isControlDown())
{
dock = getDockableBoundary(p);
if (dock != null)
{
setDockIndex(dock.getDockIndex(p));
setRowIndex(dock.getRowIndex(p));
setDockEdge(dock.getEdge());
}
}
if (dock != null)
{
dockToolBar(getDockEdge(), getRowIndex(), getDockIndex());
}
else
{
SwingUtilities.convertPointToScreen(p, target);
floatToolBar(p.x, p.y, true);
}
}
private void hideDraggingWindow()
{
if (ourDraggingWindow != null)
{
ourDraggingWindow.hideWindow();
ourToolBarIsDragging = false;
}
}
}
/**
* Inner class that represents the bounds of the associated
* toolbar while dragging.
*/
private class DraggingWindow extends JWindow
{
private Border myFloatBorder = null;
private Border myDockBorder = null;
private Color myFloatColor = null;
private Color myDockColor = null;
private JPanel myContent = null;
public DraggingWindow(Window ancestor)
{
super(ancestor);
Color myFloatColor = UIManager.getColor("ToolBar.floatingForeground");
Color myDockColor = UIManager.getColor("ToolBar.dockingForeground");
if (myFloatColor == null) myFloatColor = Color.darkGray;
if (myDockColor == null) myDockColor = Color.yellow;
myFloatBorder = BorderFactory.createLineBorder(myFloatColor, 3);
myDockBorder = BorderFactory.createLineBorder(myDockColor, 3);
myContent = new JPanel();
myContent.setOpaque(true);
myFloatColor = UIManager.getColor("ToolBar.floatingBackground");
myDockColor = UIManager.getColor("ToolBar.dockingBackground");
if (myFloatColor == null) myFloatColor = myContent.getBackground();
if (myDockColor == null) myDockColor = myContent.getBackground();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(myContent, BorderLayout.CENTER);
}
// Present the window at the given point for the given dockability
// at the given dock orientation...
public void presentWindow(Point screenPoint,
boolean dockable,
int dockOrientation)
{
setCentroidLocation(screenPoint);
//int tbOrientation = ourToolBar.getOrientation();
int orientation = (dockable ? dockOrientation
: DockLayout.HORIZONTAL);
setSize(getPreferredToolBarSize(orientation));
myContent.setBorder( dockable ? myDockBorder : myFloatBorder );
myContent.setBackground( dockable ? myDockColor : myFloatColor );
setCentroidLocation(screenPoint);
validate();
if (!isVisible()) setVisible(true);
}
public void hideWindow()
{
setVisible(false);
}
private void setCentroidLocation(Point screenPoint)
{
setLocation(screenPoint.x - getWidth() / 2,
screenPoint.y - getHeight() / 2);
}
private Point getCentroidLocation()
{
Point p = getLocation();
p.x += (getWidth() / 2);
p.y += (getHeight() / 2);
return p;
}
private Dimension getPreferredToolBarSize(int orientation)
{
Component[] comps = ourToolBar.getComponents();
int w = 0, h = 0;
for (int i=0; i < comps.length; i++)
{
Dimension d = comps[i].getPreferredSize();
if (orientation == DockLayout.HORIZONTAL)
{
w += d.width;
h = Math.max(h, d.height);
}
else
{
w = Math.max(w, d.width);
h += d.height;
}
}
return new Dimension(w, h);
}
}
}
|