home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / plaf / basic / BasicToolBarUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  11.8 KB  |  532 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Frame;
  8. import java.awt.IllegalComponentStateException;
  9. import java.awt.Point;
  10. import java.awt.Window;
  11. import java.awt.event.ContainerListener;
  12. import java.awt.event.FocusListener;
  13. import java.awt.event.WindowListener;
  14. import java.beans.PropertyChangeListener;
  15. import javax.swing.JComponent;
  16. import javax.swing.JFrame;
  17. import javax.swing.JToolBar;
  18. import javax.swing.KeyStroke;
  19. import javax.swing.LookAndFeel;
  20. import javax.swing.SwingConstants;
  21. import javax.swing.UIManager;
  22. import javax.swing.event.MouseInputListener;
  23. import javax.swing.plaf.ComponentUI;
  24. import javax.swing.plaf.ToolBarUI;
  25. import javax.swing.plaf.UIResource;
  26.  
  27. public class BasicToolBarUI extends ToolBarUI implements SwingConstants {
  28.    protected JToolBar toolBar;
  29.    private boolean floating;
  30.    private int floatingX;
  31.    private int floatingY;
  32.    private JFrame floatingFrame;
  33.    protected DragWindow dragWindow;
  34.    private Container dockingSource;
  35.    private int dockingSensitivity = 0;
  36.    protected int focusedCompIndex = -1;
  37.    protected Color dockingColor = null;
  38.    protected Color floatingColor = null;
  39.    protected Color dockingBorderColor = null;
  40.    protected Color floatingBorderColor = null;
  41.    protected MouseInputListener dockingListener;
  42.    protected PropertyChangeListener propertyListener;
  43.    protected ContainerListener toolBarContListener;
  44.    protected FocusListener toolBarFocusListener;
  45.    protected KeyStroke upKey;
  46.    protected KeyStroke downKey;
  47.    protected KeyStroke leftKey;
  48.    protected KeyStroke rightKey;
  49.    private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex";
  50.  
  51.    public boolean canDock(Component var1, Point var2) {
  52.       boolean var3 = false;
  53.       if (var1.contains(var2)) {
  54.          if (this.dockingSensitivity == 0) {
  55.             this.dockingSensitivity = this.toolBar.getSize().height;
  56.          }
  57.  
  58.          if (var2.y < this.dockingSensitivity) {
  59.             var3 = true;
  60.          }
  61.  
  62.          if (var2.y > var1.getSize().height - this.dockingSensitivity) {
  63.             var3 = true;
  64.          }
  65.  
  66.          if (var2.x < this.dockingSensitivity) {
  67.             var3 = true;
  68.          }
  69.  
  70.          if (var2.x > var1.getSize().width - this.dockingSensitivity) {
  71.             var3 = true;
  72.          }
  73.       }
  74.  
  75.       return var3;
  76.    }
  77.  
  78.    protected MouseInputListener createDockingListener() {
  79.       return new DockingListener(this, this.toolBar);
  80.    }
  81.  
  82.    protected DragWindow createDragWindow(JToolBar var1) {
  83.       Object var2 = null;
  84.       if (this.toolBar != null) {
  85.          Container var3;
  86.          for(var3 = this.toolBar.getParent(); var3 != null && !(var3 instanceof Frame); var3 = ((Component)var3).getParent()) {
  87.          }
  88.  
  89.          if (var3 != null && var3 instanceof Frame) {
  90.             var2 = (Frame)var3;
  91.          }
  92.       }
  93.  
  94.       if (var2 == null) {
  95.          this.floatingFrame = this.createFloatingFrame(this.toolBar);
  96.          var2 = this.floatingFrame;
  97.       }
  98.  
  99.       DragWindow var4 = new DragWindow(this, (Frame)var2);
  100.       return var4;
  101.    }
  102.  
  103.    protected JFrame createFloatingFrame(JToolBar var1) {
  104.       JFrame var2 = new JFrame(((Component)var1).getName());
  105.       WindowListener var3 = this.createFrameListener();
  106.       ((Window)var2).addWindowListener(var3);
  107.       return var2;
  108.    }
  109.  
  110.    protected WindowListener createFrameListener() {
  111.       return new FrameListener(this);
  112.    }
  113.  
  114.    protected PropertyChangeListener createPropertyListener() {
  115.       return new PropertyListener(this);
  116.    }
  117.  
  118.    protected ContainerListener createToolBarContListener() {
  119.       return new ToolBarContListener(this);
  120.    }
  121.  
  122.    protected FocusListener createToolBarFocusListener() {
  123.       return new ToolBarFocusListener(this);
  124.    }
  125.  
  126.    public static ComponentUI createUI(JComponent var0) {
  127.       return new BasicToolBarUI();
  128.    }
  129.  
  130.    protected void dragTo(Point var1, Point var2) {
  131.       if (this.toolBar.isFloatable()) {
  132.          try {
  133.             if (this.dragWindow == null) {
  134.                this.dragWindow = this.createDragWindow(this.toolBar);
  135.             }
  136.  
  137.             Point var3 = this.dragWindow.getOffset();
  138.             if (var3 == null) {
  139.                Dimension var4 = this.toolBar.getPreferredSize();
  140.                var3 = new Point(var4.width / 2, var4.height / 2);
  141.                this.dragWindow.setOffset(var3);
  142.             }
  143.  
  144.             Point var11 = new Point(var2.x + var1.x, var2.y + var1.y);
  145.             Point var5 = new Point(var11.x - var3.x, var11.y - var3.y);
  146.             if (this.dockingSource == null) {
  147.                this.dockingSource = this.toolBar.getParent();
  148.             }
  149.  
  150.             Point var6 = this.dockingSource.getLocationOnScreen();
  151.             Point var7 = new Point(var11.x - var6.x, var11.y - var6.y);
  152.             if (this.canDock(this.dockingSource, var7)) {
  153.                this.dragWindow.setBackground(this.getDockingColor());
  154.                String var8 = this.getDockingConstraint(this.dockingSource, var7);
  155.                int var9 = this.mapConstraintToOrientation(var8);
  156.                this.dragWindow.setOrientation(var9);
  157.                this.dragWindow.setBorderColor(this.dockingBorderColor);
  158.             } else {
  159.                this.dragWindow.setBackground(this.getFloatingColor());
  160.                this.dragWindow.setOrientation(0);
  161.                this.dragWindow.setBorderColor(this.floatingBorderColor);
  162.             }
  163.  
  164.             this.dragWindow.setLocation(var5.x, var5.y);
  165.             if (!this.dragWindow.isVisible()) {
  166.                Dimension var12 = this.toolBar.getPreferredSize();
  167.                this.dragWindow.setSize(var12.width, var12.height);
  168.                this.dragWindow.show();
  169.             }
  170.          } catch (IllegalComponentStateException var10) {
  171.          }
  172.       }
  173.  
  174.    }
  175.  
  176.    protected void floatAt(Point var1, Point var2) {
  177.       if (this.toolBar.isFloatable()) {
  178.          try {
  179.             Point var3 = this.dragWindow.getOffset();
  180.             if (var3 == null) {
  181.                var3 = var1;
  182.                this.dragWindow.setOffset(var1);
  183.             }
  184.  
  185.             Point var4 = new Point(var2.x + var1.x, var2.y + var1.y);
  186.             this.setFloatingLocation(var4.x - var3.x, var4.y - var3.y);
  187.             if (this.dockingSource != null) {
  188.                Point var5 = this.dockingSource.getLocationOnScreen();
  189.                Point var6 = new Point(var4.x - var5.x, var4.y - var5.y);
  190.                if (this.canDock(this.dockingSource, var6)) {
  191.                   this.setFloating(false, var6);
  192.                } else {
  193.                   this.setFloating(true, (Point)null);
  194.                }
  195.             } else {
  196.                this.setFloating(true, (Point)null);
  197.             }
  198.  
  199.             this.dragWindow.setOffset((Point)null);
  200.          } catch (IllegalComponentStateException var7) {
  201.          }
  202.       }
  203.  
  204.    }
  205.  
  206.    public Color getDockingColor() {
  207.       return this.dockingColor;
  208.    }
  209.  
  210.    private String getDockingConstraint(Component var1, Point var2) {
  211.       String var3 = "North";
  212.       if (var2 != null && var1.contains(var2)) {
  213.          if (this.dockingSensitivity == 0) {
  214.             this.dockingSensitivity = this.toolBar.getSize().height;
  215.          }
  216.  
  217.          if (var2.y > var1.getSize().height - this.dockingSensitivity) {
  218.             var3 = "South";
  219.          }
  220.  
  221.          if (var2.x < this.dockingSensitivity) {
  222.             var3 = "West";
  223.          }
  224.  
  225.          if (var2.x > var1.getSize().width - this.dockingSensitivity) {
  226.             var3 = "East";
  227.          }
  228.  
  229.          if (var2.y < this.dockingSensitivity) {
  230.             var3 = "North";
  231.          }
  232.       }
  233.  
  234.       return var3;
  235.    }
  236.  
  237.    public Color getFloatingColor() {
  238.       return this.floatingColor;
  239.    }
  240.  
  241.    public Dimension getMaximumSize(JComponent var1) {
  242.       return this.getPreferredSize(var1);
  243.    }
  244.  
  245.    public Dimension getMinimumSize(JComponent var1) {
  246.       return this.getPreferredSize(var1);
  247.    }
  248.  
  249.    public Dimension getPreferredSize(JComponent var1) {
  250.       return null;
  251.    }
  252.  
  253.    protected void installComponents() {
  254.    }
  255.  
  256.    protected void installDefaults() {
  257.       LookAndFeel.installBorder(this.toolBar, "ToolBar.border");
  258.       LookAndFeel.installColorsAndFont(this.toolBar, "ToolBar.background", "ToolBar.foreground", "ToolBar.font");
  259.       if (this.dockingColor == null || this.dockingColor instanceof UIResource) {
  260.          this.dockingColor = UIManager.getColor("ToolBar.dockingBackground");
  261.       }
  262.  
  263.       if (this.floatingColor == null || this.floatingColor instanceof UIResource) {
  264.          this.floatingColor = UIManager.getColor("ToolBar.floatingBackground");
  265.       }
  266.  
  267.       if (this.dockingBorderColor == null || this.dockingBorderColor instanceof UIResource) {
  268.          this.dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
  269.       }
  270.  
  271.       if (this.floatingBorderColor == null || this.floatingBorderColor instanceof UIResource) {
  272.          this.floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
  273.       }
  274.  
  275.    }
  276.  
  277.    protected void installKeyboardActions() {
  278.       this.upKey = KeyStroke.getKeyStroke(38, 0);
  279.       this.downKey = KeyStroke.getKeyStroke(40, 0);
  280.       this.leftKey = KeyStroke.getKeyStroke(37, 0);
  281.       this.rightKey = KeyStroke.getKeyStroke(39, 0);
  282.       this.toolBar.registerKeyboardAction(new 1(this), this.upKey, 1);
  283.       this.toolBar.registerKeyboardAction(new 2(this), this.downKey, 1);
  284.       this.toolBar.registerKeyboardAction(new 3(this), this.leftKey, 1);
  285.       this.toolBar.registerKeyboardAction(new 4(this), this.rightKey, 1);
  286.    }
  287.  
  288.    protected void installListeners() {
  289.       this.dockingListener = this.createDockingListener();
  290.       if (this.dockingListener != null) {
  291.          this.toolBar.addMouseMotionListener(this.dockingListener);
  292.          this.toolBar.addMouseListener(this.dockingListener);
  293.       }
  294.  
  295.       this.propertyListener = this.createPropertyListener();
  296.       this.toolBarContListener = this.createToolBarContListener();
  297.       if (this.toolBarContListener != null) {
  298.          this.toolBar.addContainerListener(this.toolBarContListener);
  299.       }
  300.  
  301.       this.toolBarFocusListener = this.createToolBarFocusListener();
  302.       if (this.toolBarFocusListener != null) {
  303.          Component[] var1 = this.toolBar.getComponents();
  304.  
  305.          for(int var2 = 0; var2 < var1.length; ++var2) {
  306.             var1[var2].addFocusListener(this.toolBarFocusListener);
  307.          }
  308.       }
  309.  
  310.    }
  311.  
  312.    public void installUI(JComponent var1) {
  313.       this.toolBar = (JToolBar)var1;
  314.       this.installDefaults();
  315.       this.installComponents();
  316.       this.installListeners();
  317.       this.installKeyboardActions();
  318.       this.dockingSensitivity = 0;
  319.       this.floating = false;
  320.       this.floatingX = this.floatingY = 0;
  321.       this.floatingFrame = this.createFloatingFrame(this.toolBar);
  322.       this.setOrientation(this.toolBar.getOrientation());
  323.       var1.setOpaque(true);
  324.       if (var1.getClientProperty(FOCUSED_COMP_INDEX) != null) {
  325.          this.focusedCompIndex = (Integer)var1.getClientProperty(FOCUSED_COMP_INDEX);
  326.       }
  327.  
  328.    }
  329.  
  330.    public boolean isFloating() {
  331.       return this.floating;
  332.    }
  333.  
  334.    private int mapConstraintToOrientation(String var1) {
  335.       int var2 = this.toolBar.getOrientation();
  336.       if (var1 != null) {
  337.          if (!var1.equals("East") && !var1.equals("West")) {
  338.             if (var1.equals("North") || var1.equals("South")) {
  339.                var2 = 0;
  340.             }
  341.          } else {
  342.             var2 = 1;
  343.          }
  344.       }
  345.  
  346.       return var2;
  347.    }
  348.  
  349.    protected void navigateFocusedComp(int var1) {
  350.       int var2 = this.toolBar.getComponentCount();
  351.       switch (var1) {
  352.          case 1:
  353.          case 7:
  354.             if (this.focusedCompIndex >= 0 && this.focusedCompIndex < var2) {
  355.                int var5 = this.focusedCompIndex - 1;
  356.  
  357.                while(var5 != this.focusedCompIndex) {
  358.                   if (var5 < 0) {
  359.                      var5 = var2 - 1;
  360.                   }
  361.  
  362.                   Component var6 = this.toolBar.getComponentAtIndex(var5--);
  363.                   if (var6 != null && var6.isFocusTraversable()) {
  364.                      var6.requestFocus();
  365.                      break;
  366.                   }
  367.                }
  368.             }
  369.          case 2:
  370.          case 4:
  371.          case 6:
  372.          default:
  373.             break;
  374.          case 3:
  375.          case 5:
  376.             if (this.focusedCompIndex >= 0 && this.focusedCompIndex < var2) {
  377.                int var3 = this.focusedCompIndex + 1;
  378.  
  379.                while(var3 != this.focusedCompIndex) {
  380.                   if (var3 >= var2) {
  381.                      var3 = 0;
  382.                   }
  383.  
  384.                   Component var4 = this.toolBar.getComponentAtIndex(var3++);
  385.                   if (var4 != null && var4.isFocusTraversable()) {
  386.                      var4.requestFocus();
  387.                      break;
  388.                   }
  389.                }
  390.             }
  391.       }
  392.  
  393.    }
  394.  
  395.    public void setDockingColor(Color var1) {
  396.       this.dockingColor = var1;
  397.    }
  398.  
  399.    public void setFloating(boolean var1, Point var2) {
  400.       if (this.toolBar.isFloatable()) {
  401.          if (this.dragWindow != null) {
  402.             this.dragWindow.setVisible(false);
  403.          }
  404.  
  405.          this.floating = var1;
  406.          if (var1) {
  407.             if (this.dockingSource == null) {
  408.                this.dockingSource = this.toolBar.getParent();
  409.                this.dockingSource.remove(this.toolBar);
  410.             }
  411.  
  412.             if (this.propertyListener != null) {
  413.                UIManager.addPropertyChangeListener(this.propertyListener);
  414.             }
  415.  
  416.             if (this.floatingFrame == null) {
  417.                this.floatingFrame = this.createFloatingFrame(this.toolBar);
  418.             }
  419.  
  420.             this.floatingFrame.getContentPane().add(this.toolBar, "Center");
  421.             this.setOrientation(0);
  422.             this.floatingFrame.pack();
  423.             this.floatingFrame.setLocation(this.floatingX, this.floatingY);
  424.             this.floatingFrame.show();
  425.          } else {
  426.             this.floatingFrame.setVisible(false);
  427.             this.floatingFrame.getContentPane().remove(this.toolBar);
  428.             String var3 = this.getDockingConstraint(this.dockingSource, var2);
  429.             int var4 = this.mapConstraintToOrientation(var3);
  430.             this.setOrientation(var4);
  431.             if (this.dockingSource == null) {
  432.                this.dockingSource = this.toolBar.getParent();
  433.             }
  434.  
  435.             if (this.propertyListener != null) {
  436.                UIManager.removePropertyChangeListener(this.propertyListener);
  437.             }
  438.  
  439.             this.dockingSource.add(var3, this.toolBar);
  440.          }
  441.  
  442.          this.dockingSource.invalidate();
  443.          Container var5 = this.dockingSource.getParent();
  444.          if (var5 != null) {
  445.             var5.validate();
  446.          }
  447.  
  448.          this.dockingSource.repaint();
  449.       }
  450.  
  451.    }
  452.  
  453.    public void setFloatingColor(Color var1) {
  454.       this.floatingColor = var1;
  455.    }
  456.  
  457.    public void setFloatingLocation(int var1, int var2) {
  458.       this.floatingX = var1;
  459.       this.floatingY = var2;
  460.    }
  461.  
  462.    public void setOrientation(int var1) {
  463.       this.toolBar.setOrientation(var1);
  464.       if (this.dragWindow != null) {
  465.          this.dragWindow.setOrientation(var1);
  466.       }
  467.  
  468.    }
  469.  
  470.    protected void uninstallComponents() {
  471.    }
  472.  
  473.    protected void uninstallDefaults() {
  474.       LookAndFeel.uninstallBorder(this.toolBar);
  475.       this.dockingColor = null;
  476.       this.floatingColor = null;
  477.       this.dockingBorderColor = null;
  478.       this.floatingBorderColor = null;
  479.    }
  480.  
  481.    protected void uninstallKeyboardActions() {
  482.       this.toolBar.unregisterKeyboardAction(this.upKey);
  483.       this.toolBar.unregisterKeyboardAction(this.downKey);
  484.       this.toolBar.unregisterKeyboardAction(this.leftKey);
  485.       this.toolBar.unregisterKeyboardAction(this.rightKey);
  486.       this.upKey = this.downKey = this.rightKey = this.leftKey = null;
  487.    }
  488.  
  489.    protected void uninstallListeners() {
  490.       if (this.dockingListener != null) {
  491.          this.toolBar.removeMouseMotionListener(this.dockingListener);
  492.          this.toolBar.removeMouseListener(this.dockingListener);
  493.          this.dockingListener = null;
  494.       }
  495.  
  496.       if (this.propertyListener != null) {
  497.          this.propertyListener = null;
  498.       }
  499.  
  500.       if (this.toolBarContListener != null) {
  501.          this.toolBar.removeContainerListener(this.toolBarContListener);
  502.          this.toolBarContListener = null;
  503.       }
  504.  
  505.       if (this.toolBarFocusListener != null) {
  506.          Component[] var1 = this.toolBar.getComponents();
  507.  
  508.          for(int var2 = 0; var2 < var1.length; ++var2) {
  509.             var1[var2].removeFocusListener(this.toolBarFocusListener);
  510.          }
  511.  
  512.          this.toolBarFocusListener = null;
  513.       }
  514.  
  515.    }
  516.  
  517.    public void uninstallUI(JComponent var1) {
  518.       this.uninstallDefaults();
  519.       this.uninstallComponents();
  520.       this.uninstallListeners();
  521.       this.uninstallKeyboardActions();
  522.       if (this.isFloating()) {
  523.          this.setFloating(false, (Point)null);
  524.       }
  525.  
  526.       this.floatingFrame = null;
  527.       this.dragWindow = null;
  528.       this.dockingSource = null;
  529.       var1.putClientProperty(FOCUSED_COMP_INDEX, new Integer(this.focusedCompIndex));
  530.    }
  531. }
  532.