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