home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / plaf / basic / BasicSplitPaneUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  10.2 KB  |  503 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.JComponent;
  4. import com.sun.java.swing.JSplitPane;
  5. import com.sun.java.swing.border.Border;
  6. import com.sun.java.swing.plaf.ComponentUI;
  7. import com.sun.java.swing.plaf.SplitPaneUI;
  8. import java.awt.Color;
  9. import java.awt.Component;
  10. import java.awt.Dimension;
  11. import java.awt.Graphics;
  12. import java.awt.Insets;
  13. import java.awt.LayoutManager;
  14. import java.awt.Rectangle;
  15. import java.awt.peer.ComponentPeer;
  16. import java.awt.peer.LightweightPeer;
  17. import java.beans.PropertyChangeEvent;
  18. import java.beans.PropertyChangeListener;
  19. import java.io.Serializable;
  20. import java.util.EventObject;
  21.  
  22. public class BasicSplitPaneUI extends SplitPaneUI implements PropertyChangeListener, Serializable {
  23.    protected static final String NON_CONTINUOUS_DIVIDER = "nonContinuousDivider";
  24.    protected JSplitPane splitPane;
  25.    private int orientation;
  26.    protected BasicHorizontalLayoutManager layoutManager;
  27.    private boolean continuousLayout;
  28.    protected BasicSplitPaneDivider divider;
  29.    private int lastDragLocation;
  30.    protected int dividerSize;
  31.    protected boolean createdBorder;
  32.    protected Component nonContinuousLayoutDivider;
  33.    protected boolean draggingHW;
  34.    protected int beginDragDividerLocation;
  35.  
  36.    public int getOrientation() {
  37.       return this.orientation;
  38.    }
  39.  
  40.    public void setOrientation(int var1) {
  41.       this.orientation = var1;
  42.    }
  43.  
  44.    public boolean isContinuousLayout() {
  45.       return this.continuousLayout;
  46.    }
  47.  
  48.    public void setContinuousLayout(boolean var1) {
  49.       this.continuousLayout = var1;
  50.    }
  51.  
  52.    public int getLastDragLocation() {
  53.       return this.lastDragLocation;
  54.    }
  55.  
  56.    public void setLastDragLocation(int var1) {
  57.       this.lastDragLocation = var1;
  58.    }
  59.  
  60.    public static ComponentUI createUI(JComponent var0) {
  61.       return new BasicSplitPaneUI();
  62.    }
  63.  
  64.    public BasicSplitPaneUI() {
  65.       this.setLastDragLocation(-1);
  66.    }
  67.  
  68.    public void installUI(JComponent var1) {
  69.       this.installDefaults(var1);
  70.       this.installListeners(var1);
  71.    }
  72.  
  73.    protected void installDefaults(JComponent var1) {
  74.       this.splitPane = (JSplitPane)var1;
  75.       this.setOrientation(this.splitPane.getOrientation());
  76.       this.setContinuousLayout(this.splitPane.isContinuousLayout());
  77.       Border var2 = this.splitPane.getBorder();
  78.       if (var2 == null) {
  79.          var2 = this.createDefaultBorder();
  80.          if (var2 != null) {
  81.             this.splitPane.setBorder(var2);
  82.             this.createdBorder = true;
  83.          }
  84.       } else {
  85.          this.createdBorder = false;
  86.       }
  87.  
  88.       if (this.divider == null) {
  89.          this.divider = this.createDefaultDivider();
  90.       }
  91.  
  92.       this.divider.setBasicSplitPaneUI(this);
  93.       this.divider.setDividerSize(this.splitPane.getDividerSize());
  94.       this.splitPane.add(this.divider, "divider");
  95.       this.resetLayoutManager();
  96.       if (this.nonContinuousLayoutDivider == null) {
  97.          this.setNonContinuousLayoutDivider(this.createDefaultNonContinuousLayoutDivider(), true);
  98.       } else {
  99.          this.setNonContinuousLayoutDivider(this.nonContinuousLayoutDivider, true);
  100.       }
  101.    }
  102.  
  103.    protected void installListeners(JComponent var1) {
  104.       this.splitPane.addPropertyChangeListener(this);
  105.    }
  106.  
  107.    public void uninstallUI(JComponent var1) {
  108.       this.uninstallListeners(var1);
  109.       this.uninstallDefaults(var1);
  110.    }
  111.  
  112.    protected void uninstallListeners(JComponent var1) {
  113.       this.splitPane.removePropertyChangeListener(this);
  114.    }
  115.  
  116.    protected void uninstallDefaults(JComponent var1) {
  117.       if (this.splitPane.getLayout() == this.layoutManager) {
  118.          this.splitPane.setLayout((LayoutManager)null);
  119.       }
  120.  
  121.       if (this.createdBorder) {
  122.          this.splitPane.setBorder((Border)null);
  123.       }
  124.  
  125.       if (this.nonContinuousLayoutDivider != null) {
  126.          this.splitPane.remove(this.nonContinuousLayoutDivider);
  127.       }
  128.  
  129.       this.splitPane.remove(this.divider);
  130.       this.divider.setBasicSplitPaneUI((BasicSplitPaneUI)null);
  131.       this.splitPane = null;
  132.       this.layoutManager = null;
  133.       this.setNonContinuousLayoutDivider((Component)null);
  134.    }
  135.  
  136.    protected Border createDefaultBorder() {
  137.       return new BasicSplitPaneBorder(this);
  138.    }
  139.  
  140.    public void propertyChange(PropertyChangeEvent var1) {
  141.       if (((EventObject)var1).getSource() == this.splitPane) {
  142.          String var2 = var1.getPropertyName();
  143.          if (var2.equals("orientation")) {
  144.             this.orientation = this.splitPane.getOrientation();
  145.             this.resetLayoutManager();
  146.             return;
  147.          }
  148.  
  149.          if (var2.equals("continuousLayout")) {
  150.             this.setContinuousLayout(this.splitPane.isContinuousLayout());
  151.             if (!this.isContinuousLayout()) {
  152.                if (this.nonContinuousLayoutDivider == null) {
  153.                   this.setNonContinuousLayoutDivider(this.createDefaultNonContinuousLayoutDivider(), true);
  154.                   return;
  155.                }
  156.  
  157.                if (this.nonContinuousLayoutDivider.getParent() == null) {
  158.                   this.setNonContinuousLayoutDivider(this.nonContinuousLayoutDivider, true);
  159.                   return;
  160.                }
  161.             }
  162.          } else {
  163.             if (var2.equals("border")) {
  164.                this.createdBorder = false;
  165.                return;
  166.             }
  167.  
  168.             if (var2.equals("dividerSize")) {
  169.                this.divider.setDividerSize(this.splitPane.getDividerSize());
  170.                this.layoutManager.resetSizeAt(2);
  171.                this.splitPane.invalidate();
  172.                this.splitPane.validate();
  173.             }
  174.          }
  175.       }
  176.  
  177.    }
  178.  
  179.    public void setDivider(BasicSplitPaneDivider var1) {
  180.       if (var1 != null) {
  181.          if (this.splitPane != null) {
  182.             if (this.divider != null) {
  183.                this.splitPane.remove(this.divider);
  184.             }
  185.  
  186.             this.divider = var1;
  187.             this.splitPane.add(this.divider, "divider");
  188.             this.layoutManager.resetSizeAt(2);
  189.             this.splitPane.invalidate();
  190.             this.splitPane.validate();
  191.          } else {
  192.             this.divider = var1;
  193.          }
  194.  
  195.          this.divider.setBasicSplitPaneUI(this);
  196.       }
  197.  
  198.    }
  199.  
  200.    public BasicSplitPaneDivider getDivider() {
  201.       return this.divider;
  202.    }
  203.  
  204.    protected Component createDefaultNonContinuousLayoutDivider() {
  205.       return new 1(this);
  206.    }
  207.  
  208.    protected void setNonContinuousLayoutDivider(Component var1) {
  209.       this.setNonContinuousLayoutDivider(var1, true);
  210.    }
  211.  
  212.    protected void setNonContinuousLayoutDivider(Component var1, boolean var2) {
  213.       if (this.nonContinuousLayoutDivider != null && this.splitPane != null) {
  214.          this.splitPane.remove(this.nonContinuousLayoutDivider);
  215.       }
  216.  
  217.       this.nonContinuousLayoutDivider = var1;
  218.       if (this.nonContinuousLayoutDivider != null && this.splitPane != null) {
  219.          this.nonContinuousLayoutDivider.setLocation(-1000, -1000);
  220.          Component var3 = this.splitPane.getLeftComponent();
  221.          Component var4 = this.splitPane.getRightComponent();
  222.          int[] var5 = this.layoutManager.getSizes();
  223.          if (var3 != null) {
  224.             this.splitPane.setLeftComponent((Component)null);
  225.          }
  226.  
  227.          if (var4 != null) {
  228.             this.splitPane.setRightComponent((Component)null);
  229.          }
  230.  
  231.          this.splitPane.remove(this.divider);
  232.          this.splitPane.add(this.nonContinuousLayoutDivider, "nonContinuousDivider", this.splitPane.getComponentCount());
  233.          this.splitPane.setLeftComponent(var3);
  234.          this.splitPane.setRightComponent(var4);
  235.          this.splitPane.add(this.divider, "divider");
  236.          if (var2) {
  237.             this.layoutManager.setSizes(var5);
  238.          }
  239.  
  240.          this.splitPane.validate();
  241.          this.splitPane.paintImmediately(this.splitPane.getX(), this.splitPane.getY(), this.splitPane.getWidth(), this.splitPane.getHeight());
  242.       }
  243.  
  244.    }
  245.  
  246.    public Component getNonContinuousLayoutDivider() {
  247.       return this.nonContinuousLayoutDivider;
  248.    }
  249.  
  250.    public JSplitPane getSplitPane() {
  251.       return this.splitPane;
  252.    }
  253.  
  254.    public BasicSplitPaneDivider createDefaultDivider() {
  255.       return new BasicSplitPaneDivider(this);
  256.    }
  257.  
  258.    public void resetToPreferredSizes() {
  259.       if (this.splitPane != null) {
  260.          this.layoutManager.resetToPreferredSizes();
  261.          this.splitPane.validate();
  262.          this.layoutManager.layoutContainer(this.splitPane);
  263.       }
  264.  
  265.    }
  266.  
  267.    public void setDividerLocation(int var1) {
  268.       Component var2 = this.splitPane.getLeftComponent();
  269.       Component var3 = this.splitPane.getRightComponent();
  270.       if (var2 != null && var3 != null) {
  271.          Insets var4 = this.splitPane.getInsets();
  272.          if (var4 != null) {
  273.             if (this.orientation == 1) {
  274.                var2.setSize(Math.max(0, var1 - var4.left), 10);
  275.             } else {
  276.                var2.setSize(10, Math.max(0, var1 - var4.top));
  277.             }
  278.          } else if (this.orientation == 1) {
  279.             var2.setSize(Math.max(0, var1), 10);
  280.          } else {
  281.             var2.setSize(10, Math.max(0, var1));
  282.          }
  283.  
  284.          this.splitPane.validate();
  285.          this.splitPane.repaint();
  286.       }
  287.  
  288.    }
  289.  
  290.    public int getDividerLocation() {
  291.       return this.orientation == 1 ? this.divider.getLocation().x : this.divider.getLocation().y;
  292.    }
  293.  
  294.    public int getMinimumDividerLocation() {
  295.       int var1 = 0;
  296.       Component var2 = this.splitPane.getLeftComponent();
  297.       if (var2 != null) {
  298.          Insets var3 = this.splitPane.getInsets();
  299.          Dimension var4 = var2.getMinimumSize();
  300.          if (this.orientation == 1) {
  301.             var1 = var4.width;
  302.          } else {
  303.             var1 = var4.height;
  304.          }
  305.  
  306.          var1 += this.getDividerBorderSize();
  307.          if (var3 != null) {
  308.             if (this.orientation == 1) {
  309.                var1 += var3.left;
  310.             } else {
  311.                var1 += var3.top;
  312.             }
  313.          }
  314.       }
  315.  
  316.       return var1;
  317.    }
  318.  
  319.    public int getMaximumDividerLocation() {
  320.       Dimension var1 = this.splitPane.getSize();
  321.       int var2 = 0;
  322.       Component var3 = this.splitPane.getRightComponent();
  323.       if (var3 != null) {
  324.          Insets var4 = this.splitPane.getInsets();
  325.          Dimension var5 = var3.getMinimumSize();
  326.          if (this.orientation == 1) {
  327.             var2 = var1.width - var5.width;
  328.          } else {
  329.             var2 = var1.height - var5.height;
  330.          }
  331.  
  332.          var2 -= this.dividerSize + this.getDividerBorderSize();
  333.          if (var4 != null) {
  334.             if (this.orientation == 1) {
  335.                var2 += var4.left;
  336.             } else {
  337.                var2 += var4.top;
  338.             }
  339.          }
  340.       }
  341.  
  342.       return Math.max(this.getMinimumDividerLocation(), var2);
  343.    }
  344.  
  345.    public void finishedPaintingChildren(JSplitPane var1, Graphics var2) {
  346.       if (var1 == this.splitPane && this.getLastDragLocation() != -1 && !this.isContinuousLayout() && !this.draggingHW) {
  347.          Dimension var3 = this.splitPane.getSize();
  348.          var2.setColor(Color.darkGray);
  349.          if (this.orientation == 1) {
  350.             var2.fillRect(this.getLastDragLocation(), 0, this.dividerSize - 1, var3.height - 1);
  351.             return;
  352.          }
  353.  
  354.          var2.fillRect(0, this.lastDragLocation, var3.width - 1, this.dividerSize - 1);
  355.       }
  356.  
  357.    }
  358.  
  359.    public void paint(Graphics var1, JComponent var2) {
  360.    }
  361.  
  362.    public Dimension getPreferredSize(JComponent var1) {
  363.       return this.splitPane != null ? this.layoutManager.preferredLayoutSize(this.splitPane) : new Dimension(0, 0);
  364.    }
  365.  
  366.    public Dimension getMinimumSize(JComponent var1) {
  367.       return this.splitPane != null ? this.layoutManager.minimumLayoutSize(this.splitPane) : new Dimension(0, 0);
  368.    }
  369.  
  370.    public Dimension getMaximumSize(JComponent var1) {
  371.       return this.splitPane != null ? this.layoutManager.maximumLayoutSize(this.splitPane) : new Dimension(0, 0);
  372.    }
  373.  
  374.    public Insets getInsets(JComponent var1) {
  375.       return null;
  376.    }
  377.  
  378.    protected void resetLayoutManager() {
  379.       if (this.orientation == 1) {
  380.          this.layoutManager = new BasicHorizontalLayoutManager(this);
  381.       } else {
  382.          this.layoutManager = new BasicVerticalLayoutManager(this);
  383.       }
  384.  
  385.       this.splitPane.setLayout(this.layoutManager);
  386.       this.layoutManager.updateComponents();
  387.       this.splitPane.validate();
  388.       this.splitPane.repaint();
  389.    }
  390.  
  391.    protected void startDragging() {
  392.       Component var1 = this.splitPane.getLeftComponent();
  393.       Component var2 = this.splitPane.getRightComponent();
  394.       this.beginDragDividerLocation = this.getDividerLocation();
  395.       this.draggingHW = false;
  396.       ComponentPeer var3;
  397.       if (var1 != null && (var3 = var1.getPeer()) != null && !(var3 instanceof LightweightPeer)) {
  398.          this.draggingHW = true;
  399.       } else if (var2 != null && (var3 = var2.getPeer()) != null && !(var3 instanceof LightweightPeer)) {
  400.          this.draggingHW = true;
  401.       }
  402.  
  403.       if (this.orientation == 1) {
  404.          this.setLastDragLocation(this.divider.getBounds().x);
  405.          this.dividerSize = this.divider.getSize().width + 2 * this.getDividerBorderSize();
  406.          if (!this.isContinuousLayout() && this.draggingHW) {
  407.             this.nonContinuousLayoutDivider.setBounds(this.getLastDragLocation(), 0, this.dividerSize, this.splitPane.getHeight());
  408.             return;
  409.          }
  410.       } else {
  411.          this.setLastDragLocation(this.divider.getBounds().y);
  412.          this.dividerSize = this.divider.getSize().height + 2 * this.getDividerBorderSize();
  413.          if (!this.isContinuousLayout() && this.draggingHW) {
  414.             this.nonContinuousLayoutDivider.setBounds(0, this.getLastDragLocation(), this.splitPane.getWidth(), this.dividerSize);
  415.          }
  416.       }
  417.  
  418.    }
  419.  
  420.    protected void dragDividerTo(int var1) {
  421.       if (this.getLastDragLocation() != var1) {
  422.          if (this.isContinuousLayout()) {
  423.             Component var4 = this.splitPane.getLeftComponent();
  424.             Rectangle var6 = var4.getBounds();
  425.             if (this.orientation == 1) {
  426.                var4.setSize(var1 - var6.x, var6.height);
  427.             } else {
  428.                var4.setSize(var6.width, var1 - var6.y);
  429.             }
  430.  
  431.             this.splitPane.validate();
  432.             this.splitPane.repaint();
  433.             this.setLastDragLocation(var1);
  434.             return;
  435.          }
  436.  
  437.          int var2 = this.getLastDragLocation();
  438.          this.setLastDragLocation(var1);
  439.          if (this.orientation == 1) {
  440.             int var5 = this.splitPane.getSize().height;
  441.             if (this.draggingHW) {
  442.                this.nonContinuousLayoutDivider.setLocation(this.getLastDragLocation(), 0);
  443.                return;
  444.             }
  445.  
  446.             this.splitPane.repaint(var2, 0, this.dividerSize, var5);
  447.             this.splitPane.repaint(var1, 0, this.dividerSize, var5);
  448.             return;
  449.          }
  450.  
  451.          int var3 = this.splitPane.getSize().width;
  452.          if (this.draggingHW) {
  453.             this.nonContinuousLayoutDivider.setLocation(0, this.getLastDragLocation());
  454.             return;
  455.          }
  456.  
  457.          this.splitPane.repaint(0, var2, var3, this.dividerSize);
  458.          this.splitPane.repaint(0, var1, var3, this.dividerSize);
  459.       }
  460.  
  461.    }
  462.  
  463.    protected void finishDraggingTo(int var1) {
  464.       this.dragDividerTo(var1);
  465.       this.setLastDragLocation(-1);
  466.       if (!this.isContinuousLayout()) {
  467.          Component var2 = this.splitPane.getLeftComponent();
  468.          Rectangle var3 = var2.getBounds();
  469.          if (this.orientation == 1) {
  470.             int var4 = this.splitPane.getSize().height;
  471.             var2.setSize(var1 - var3.x, var3.height);
  472.             if (this.draggingHW) {
  473.                this.nonContinuousLayoutDivider.setLocation(-this.dividerSize, 0);
  474.             }
  475.  
  476.             this.splitPane.paintImmediately(var1, 0, this.dividerSize, var4);
  477.          } else {
  478.             int var5 = this.splitPane.getSize().width;
  479.             var2.setSize(var3.width, var1 - var3.y);
  480.             if (this.draggingHW) {
  481.                this.nonContinuousLayoutDivider.setLocation(0, -this.dividerSize);
  482.             }
  483.  
  484.             this.splitPane.paintImmediately(0, var1, var5, this.dividerSize);
  485.          }
  486.  
  487.          this.splitPane.validate();
  488.          this.splitPane.repaint();
  489.       }
  490.  
  491.       this.splitPane.setLastDividerLocation(this.beginDragDividerLocation);
  492.    }
  493.  
  494.    protected int getDividerBorderSize() {
  495.       return 1;
  496.    }
  497.  
  498.    // $FF: synthetic method
  499.    static int access$0(BasicSplitPaneUI var0) {
  500.       return var0.orientation;
  501.    }
  502. }
  503.