home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / BasicToolBarUI.java < prev    next >
Text File  |  1998-02-26  |  15KB  |  526 lines

  1. /*
  2.  * @(#)BasicToolBarUI.java    1.36 98/02/06
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import java.awt.BorderLayout;
  25. import java.awt.Color;
  26. import java.awt.Component;
  27. import java.awt.Container;
  28. import java.awt.Dimension;
  29. import java.awt.Frame;
  30. import java.awt.Graphics;
  31. import java.awt.Insets;
  32. import java.awt.Point;
  33. import java.awt.Rectangle;
  34. import java.awt.Window;
  35. import java.awt.event.*;
  36. import java.beans.*;
  37. import java.io.Serializable;
  38.  
  39. import com.sun.java.swing.border.*;
  40. import com.sun.java.swing.plaf.*;
  41.  
  42. /**
  43.  * A Windows L&F implementation of ToolBarUI.  This implementation 
  44.  * is a "combined" view/controller.
  45.  * <p>
  46.  * Warning: serialized objects of this class will not be compatible with
  47.  * future swing releases.  The current serialization support is appropriate
  48.  * for short term storage or RMI between Swing1.0 applications.  It will
  49.  * not be possible to load serialized Swing1.0 objects with future releases
  50.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  51.  * baseline for the serialized form of Swing objects.
  52.  *
  53.  * @version 1.36 02/06/98
  54.  * @author Georges Saab
  55.  */
  56. public class BasicToolBarUI extends ToolBarUI implements Serializable {
  57.     protected JToolBar toolBar;
  58.     private boolean floating;
  59.     private boolean floatable;
  60.     private int floatingX;
  61.     private int floatingY;
  62.     private JFrame floatingFrame;
  63.     private DragWindow dragWindow;
  64.     private Container dockingSource;
  65.     private DockingListener dockingListener;
  66.     private int dockingSensitivity = 0;
  67.  
  68.     protected Color dockingColor = null;
  69.     protected Color floatingColor = null;
  70.     protected Color dockingBorderColor = null;
  71.     protected Color floatingBorderColor = null;
  72.  
  73.     public static final int HORIZONTAL = 0;
  74.     public static final int VERTICAL = 1;
  75.  
  76.     public static ComponentUI createUI(JComponent x) {
  77.     return new BasicToolBarUI();
  78.     }
  79.  
  80.     public void installUI(JComponent c) {
  81.     toolBar = (JToolBar) c;
  82.  
  83.     // Set defaults
  84.         installDefaults(c);
  85.  
  86.         // Initialize instance vars
  87.         dockingSensitivity = 0;
  88.         floating = floatable = false;
  89.         floatingX = floatingY = 0;
  90.      floatingFrame = createFloatingFrame(toolBar);
  91.  
  92.     setOrientation(HORIZONTAL);
  93.  
  94.         // Set-up docking behavior
  95.         installListeners(c);
  96.  
  97.     c.setOpaque(true);
  98.     }
  99.     
  100.     public void uninstallUI(JComponent c) {
  101.  
  102.         // Clear defaults
  103.         uninstallDefaults(c);
  104.  
  105.         // Clear instance vars
  106.     setFloating(false, null);
  107.         floatingFrame = null;
  108.         dragWindow = null;
  109.         dockingSource = null;
  110.  
  111.         // Remove docking behavior
  112.         uninstallListeners(c);
  113.     }
  114.  
  115.     protected void installDefaults(JComponent c) {
  116.      LookAndFeel.installBorder(c,"ToolBar.border");    
  117.     LookAndFeel.installColorsAndFont(c,
  118.                           "ToolBar.background",
  119.                           "ToolBar.foreground",
  120.                           "ToolBar.font");
  121.     // Toolbar specific defaults
  122.     if ( dockingColor == null || dockingColor instanceof UIResource )
  123.         dockingColor = UIManager.getColor("ToolBar.dockingColor");
  124.     if ( floatingColor == null || floatingColor instanceof UIResource )
  125.         floatingColor = UIManager.getColor("ToolBar.floatingColor");
  126.     if ( dockingBorderColor == null || 
  127.          dockingBorderColor instanceof UIResource )
  128.         dockingBorderColor = UIManager.getColor("ToolBar.dockingBorderColor");
  129.     if ( floatingBorderColor == null || 
  130.          floatingBorderColor instanceof UIResource )
  131.         floatingBorderColor = UIManager.getColor("ToolBar.floatingBorderColor");
  132.     }
  133.  
  134.     protected void uninstallDefaults(JComponent c) {
  135.     LookAndFeel.uninstallBorder(c);
  136.         dockingColor = null;
  137.         floatingColor = null;
  138.         dockingBorderColor = null;
  139.         floatingBorderColor = null;
  140.     }
  141.  
  142.     protected void installListeners(JComponent c) {
  143.         dockingListener = createDockingListener((JToolBar)c);
  144.         setFloatable(true);    
  145.     }
  146.  
  147.     protected void uninstallListeners(JComponent c) {
  148.         setFloatable(false);
  149.         if (dockingListener != null) {
  150.             dockingListener = null;
  151.         }
  152.     }
  153.  
  154.     protected JFrame createFloatingFrame(JToolBar toolbar) {
  155.     JFrame frame = new JFrame(toolbar.getName());
  156.     WindowListener wl = createFrameListener();
  157.     frame.addWindowListener(wl);
  158.         return frame;
  159.     }
  160.  
  161.     protected DragWindow createDragWindow(JToolBar toolbar) {
  162.     Frame frame = null;
  163.     if(toolBar != null) {
  164.         Container p;
  165.         for(p = toolBar.getParent() ; p != null && !(p instanceof Frame) ;
  166.         p = p.getParent());
  167.         if(p != null && p instanceof Frame)
  168.         frame = (Frame) p;
  169.     }
  170.     if(frame == null) {
  171.         floatingFrame = createFloatingFrame(toolBar);
  172.         frame = floatingFrame;
  173.     }
  174.  
  175.     DragWindow dragWindow = new DragWindow(frame);
  176.     return dragWindow;
  177.     }
  178.  
  179.     public Dimension getMinimumSize(JComponent c) {
  180.         return getPreferredSize(c);
  181.     }
  182.  
  183.     public Dimension getPreferredSize(JComponent c) {
  184.     return null;
  185.     }
  186.  
  187.     public Dimension getMaximumSize(JComponent c) {
  188.         return getPreferredSize(c);
  189.     }
  190.  
  191.     public void setFloatingLocation(int x, int y) {
  192.     floatingX = x;
  193.     floatingY = y;
  194.     }
  195.     
  196.     public boolean isFloating() {
  197.     return floating;
  198.     }
  199.  
  200.     public void setFloating(boolean b, Point p) {
  201.      if (toolBar.isFloatable() == true) {
  202.         if (dragWindow != null)
  203.         dragWindow.setVisible(false);
  204.         this.floating = b;
  205.         if (b == true) {
  206.         if (dockingSource == null) {
  207.             dockingSource = toolBar.getParent();
  208.             dockingSource.remove(toolBar);
  209.         }
  210.         if (floatingFrame == null)
  211.             floatingFrame = createFloatingFrame(toolBar);
  212.         floatingFrame.getContentPane().add(toolBar,BorderLayout.CENTER);
  213.         setOrientation(HORIZONTAL);
  214.         floatingFrame.pack();
  215.         floatingFrame.setLocation(floatingX, floatingY);
  216.         floatingFrame.show();
  217.         } else {
  218.         floatingFrame.setVisible(false);
  219.         floatingFrame.getContentPane().remove(toolBar);
  220.         String constraint = getDockingConstraint(dockingSource,
  221.                              p);
  222.         int orientation = mapConstraintToOrientation(constraint);
  223.         setOrientation(orientation);
  224.         if (dockingSource== null)
  225.             dockingSource = toolBar.getParent();
  226.         dockingSource.add(constraint, toolBar);
  227.         }
  228.          dockingSource.invalidate();
  229.          Container dockingSourceParent = dockingSource.getParent();
  230.         if (dockingSourceParent != null) 
  231.         dockingSourceParent.validate();
  232.         dockingSource.repaint();
  233.     }
  234.     }
  235.  
  236.     private int mapConstraintToOrientation(String constraint) {
  237.     int orientation = HORIZONTAL;
  238.     if ((constraint != null) &&
  239.         (constraint.equals(BorderLayout.EAST) ||
  240.          constraint.equals(BorderLayout.WEST))) {
  241.         orientation = VERTICAL;
  242.     }
  243.     return orientation;
  244.     }
  245.     
  246.     public void setOrientation(int orientation) {    
  247.     if (orientation == VERTICAL) {
  248.         toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.Y_AXIS));
  249.     } else {
  250.         toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
  251.     }
  252.     if (dragWindow !=null)
  253.         dragWindow.setOrientation(orientation);
  254.     }
  255.     
  256.     /**
  257.      * Gets the color displayed when over a docking area
  258.      */
  259.     public Color getDockingColor() {
  260.     return dockingColor;
  261.     }
  262.     
  263.     /**
  264.      * Sets the color displayed when over a docking area
  265.      */
  266.    public void setDockingColor(Color c) {
  267.     this.dockingColor = c;
  268.     }
  269.  
  270.     /**
  271.      * Gets the color displayed when over a floating area
  272.      */
  273.     public Color getFloatingColor() {
  274.     return floatingColor;
  275.     }
  276.  
  277.     /**
  278.      * Sets the color displayed when over a floating area
  279.      */
  280.     public void setFloatingColor(Color c) {
  281.     this.floatingColor = c;
  282.     }
  283.  
  284.     public void propertyChange(PropertyChangeEvent e) {
  285.     String propertyName = e.getPropertyName();
  286.     if (e.getPropertyName().equals("floatable")) {
  287.         Boolean b = (Boolean) e.getNewValue();
  288.         setFloatable(b.booleanValue());
  289.     }
  290.     }
  291.     
  292.     public void setFloatable(boolean b) {
  293.     if (b == true) {
  294.         toolBar.addMouseMotionListener(dockingListener);
  295.         toolBar.addMouseListener(dockingListener);
  296.     } else {
  297.         toolBar.removeMouseMotionListener(dockingListener);
  298.         toolBar.removeMouseListener(dockingListener);
  299.     }
  300.     }
  301.     
  302.     public boolean canDock(Component c, Point p) {
  303.     // System.out.println("Can Dock: " + p);
  304.     boolean b = false;
  305.     if (c.contains(p)) {
  306.         if (dockingSensitivity == 0)
  307.         dockingSensitivity = toolBar.getSize().height;
  308.         // North
  309.         if (p.y < dockingSensitivity)
  310.         b = true;
  311.         // South
  312.         if (p.y > c.getSize().height-dockingSensitivity)
  313.         b = true;
  314.         // West  (Base distance on height for now!)
  315.         if (p.x < dockingSensitivity)
  316.         b = true;
  317.         // East  (Base distance on height for now!)
  318.         if (p.x > c.getSize().width-dockingSensitivity)
  319.         b = true;
  320.     }
  321.     return b;
  322.     }
  323.  
  324.     private String getDockingConstraint(Component c, Point p) {
  325.     // System.out.println("Docking Constraint: " + p);
  326.     String s = BorderLayout.NORTH;
  327.     if ((p != null) && (c.contains(p))) {
  328.         if (dockingSensitivity == 0)
  329.         dockingSensitivity = toolBar.getSize().height;
  330.         if (p.y > c.getSize().height-dockingSensitivity)
  331.         s = BorderLayout.SOUTH;
  332.         // West  (Base distance on height for now!)
  333.         if (p.x < dockingSensitivity)
  334.         s = BorderLayout.WEST;
  335.         // East  (Base distance on height for now!)
  336.         if (p.x > c.getSize().width-dockingSensitivity)
  337.         s = BorderLayout.EAST;
  338.         // North  (Base distance on height for now!)
  339.         if (p.y < dockingSensitivity)
  340.         s = BorderLayout.NORTH;
  341.     }
  342.     return s;
  343.     }
  344.  
  345.     protected void dragTo(Point position, Point origin) {
  346.     if (toolBar.isFloatable() == true) {
  347.         if (dragWindow == null)
  348.         dragWindow = createDragWindow(toolBar);
  349.         Point offset = dragWindow.getOffset();
  350.         if (offset == null) {
  351.         Dimension size = toolBar.getPreferredSize();
  352.         offset = new Point(size.width/2, size.height/2);
  353.         dragWindow.setOffset(offset);
  354.         }
  355.         Point global = new Point(origin.x+ position.x,
  356.                      origin.y+position.y);
  357.         Point dragPoint = new Point(global.x- offset.x, 
  358.                     global.y- offset.y);
  359.         if (dockingSource == null)
  360.         dockingSource = toolBar.getParent();
  361.         
  362.         Point dockingPosition = dockingSource.getLocationOnScreen();
  363.         Point comparisonPoint = new Point(global.x-dockingPosition.x,
  364.                           global.y-dockingPosition.y);
  365.         if (canDock(dockingSource, comparisonPoint)) {
  366.         dragWindow.setBackground(getDockingColor());    
  367.         String constraint = getDockingConstraint(dockingSource,
  368.                              comparisonPoint);
  369.         int orientation = mapConstraintToOrientation(constraint);
  370.         dragWindow.setOrientation(orientation);
  371.         dragWindow.setBorderColor(dockingBorderColor);
  372.         } else {
  373.         dragWindow.setBackground(getFloatingColor());
  374.         dragWindow.setOrientation(HORIZONTAL);
  375.         dragWindow.setBorderColor(floatingBorderColor);
  376.         }
  377.         
  378.         dragWindow.setLocation(dragPoint.x, dragPoint.y);
  379.         if (dragWindow.isVisible() == false) {
  380.         Dimension size = toolBar.getPreferredSize();
  381.         dragWindow.setSize(size.width, size.height);
  382.         dragWindow.show();
  383.         }
  384.     }
  385.     }
  386.  
  387.     protected void floatAt(Point position, Point origin) {
  388.     if(toolBar.isFloatable() == true) {
  389.         Point offset = dragWindow.getOffset();
  390.         if (offset == null) {
  391.         offset = position;
  392.         dragWindow.setOffset(offset);
  393.         }
  394.         Point global = new Point(origin.x+ position.x,
  395.                      origin.y+position.y);
  396.         setFloatingLocation(global.x-offset.x, 
  397.                 global.y-offset.y);
  398.         if (dockingSource != null) { 
  399.         Point dockingPosition = dockingSource.getLocationOnScreen();
  400.         Point comparisonPoint = new Point(global.x-dockingPosition.x,
  401.                           global.y-dockingPosition.y);
  402.         if (canDock(dockingSource, comparisonPoint)) {
  403.             setFloating(false, comparisonPoint);
  404.         } else {
  405.             setFloating(true, null);
  406.         }
  407.         } else {
  408.         setFloating(true, null);
  409.         }
  410.         dragWindow.setOffset(null);
  411.     }
  412.     }
  413.     
  414.     protected DockingListener createDockingListener(JToolBar toolbar) {
  415.     return new DockingListener(toolbar);
  416.     }
  417.     
  418.     protected WindowListener createFrameListener() {
  419.     return new FrameListener();
  420.     }
  421.  
  422.     class FrameListener extends WindowAdapter {
  423.     public void windowClosing(WindowEvent w) {        
  424.         setFloating(false, null);
  425.     }
  426.  
  427.     } 
  428.     class DockingListener implements MouseMotionListener, MouseListener,
  429.     Serializable {
  430.     JToolBar toolBar;
  431.     private boolean isDragging = false;
  432.     private Point origin = null;
  433.  
  434.     DockingListener(JToolBar t) {
  435.         this.toolBar = t;
  436.     } 
  437.  
  438.     public void mouseClicked(MouseEvent e) {}
  439.     public void mousePressed(MouseEvent e) { 
  440.         isDragging = false;
  441.     }
  442.     public void mouseReleased(MouseEvent e) {
  443.         if (isDragging == true) {
  444.         Point position = e.getPoint();
  445.         floatAt(position, origin);
  446.         }
  447.         origin = null;
  448.     }
  449.     public void mouseEntered(MouseEvent e) { }
  450.     public void mouseExited(MouseEvent e) { }
  451.  
  452.     public void mouseDragged(MouseEvent e) {
  453.         isDragging = true;
  454.         Point position = e.getPoint();
  455.         if (origin == null)
  456.         origin = e.getComponent().getLocationOnScreen();
  457.         dragTo(position, origin);
  458.     }
  459.     public void mouseMoved(MouseEvent e) {
  460.     }
  461.     }
  462.  
  463.     static class DragWindow extends Window {
  464.     Color borderColor = Color.gray;
  465.     int orientation = HORIZONTAL;
  466.     Point offset; // offset of the mouse cursor inside the DragWindow
  467.  
  468.     DragWindow(Frame f) {
  469.         super(f);
  470.     }
  471.  
  472.     public void setOrientation(int o) {
  473.         if(isShowing()) {
  474.         if (o == this.orientation)
  475.             return;        
  476.         this.orientation = o;
  477.         Dimension size = getSize();
  478.         setSize(new Dimension(size.height, size.width));
  479.         if (offset!=null)
  480.             setOffset(new Point(offset.y, offset.x));
  481.         repaint();
  482.         }
  483.     }
  484.  
  485.     public Point getOffset() {
  486.         return offset;
  487.     }
  488.  
  489.     public void setOffset(Point p) {
  490.         this.offset = p;
  491.     }
  492.     
  493.     public void setBorderColor(Color c) {
  494.         if (this.borderColor == c)
  495.         return;
  496.         this.borderColor = c;
  497.         repaint();
  498.     }
  499.  
  500.     public Color getBorderColor() {
  501.         return this.borderColor;
  502.     }
  503.  
  504.     public void paint(Graphics g) {
  505.         Color temp = g.getColor();
  506.         g.setColor(getBackground());        
  507.         Dimension size = getSize();
  508.         g.fillRect(0,0,size.width, size.height);        
  509.         g.setColor(getBorderColor());
  510.         g.drawRect(0,0,size.width-1, size.height-1);        
  511.         g.setColor(temp);
  512.         super.paint(g);
  513.     }
  514.     public Insets getInsets() {
  515.         return new Insets(1,1,1,1);
  516.     }
  517.     }
  518. }
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.