home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / plaf / basic / BasicScrollBarUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  11.5 KB  |  521 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.Graphics;
  8. import java.awt.Insets;
  9. import java.awt.LayoutManager;
  10. import java.awt.Rectangle;
  11. import java.beans.PropertyChangeListener;
  12. import javax.swing.ActionMap;
  13. import javax.swing.InputMap;
  14. import javax.swing.JButton;
  15. import javax.swing.JComponent;
  16. import javax.swing.JScrollBar;
  17. import javax.swing.LookAndFeel;
  18. import javax.swing.SwingConstants;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.Timer;
  21. import javax.swing.UIManager;
  22. import javax.swing.plaf.ActionMapUIResource;
  23. import javax.swing.plaf.ComponentUI;
  24. import javax.swing.plaf.ScrollBarUI;
  25.  
  26. public class BasicScrollBarUI extends ScrollBarUI implements LayoutManager, SwingConstants {
  27.    private static final int POSITIVE_SCROLL = 1;
  28.    private static final int NEGATIVE_SCROLL = -1;
  29.    private static final int MIN_SCROLL = 2;
  30.    private static final int MAX_SCROLL = 3;
  31.    protected Dimension minimumThumbSize;
  32.    protected Dimension maximumThumbSize;
  33.    protected Color thumbHighlightColor;
  34.    protected Color thumbLightShadowColor;
  35.    protected Color thumbDarkShadowColor;
  36.    protected Color thumbColor;
  37.    protected Color trackColor;
  38.    protected Color trackHighlightColor;
  39.    protected JScrollBar scrollbar;
  40.    protected JButton incrButton;
  41.    protected JButton decrButton;
  42.    protected boolean isDragging;
  43.    protected TrackListener trackListener;
  44.    protected ArrowButtonListener buttonListener;
  45.    protected ModelListener modelListener;
  46.    protected Rectangle thumbRect;
  47.    protected Rectangle trackRect;
  48.    protected int trackHighlight;
  49.    protected static final int NO_HIGHLIGHT = 0;
  50.    protected static final int DECREASE_HIGHLIGHT = 1;
  51.    protected static final int INCREASE_HIGHLIGHT = 2;
  52.    protected ScrollListener scrollListener;
  53.    protected PropertyChangeListener propertyChangeListener;
  54.    protected Timer scrollTimer;
  55.    private static final int scrollSpeedThrottle = 60;
  56.    private boolean supportsAbsolutePositioning;
  57.  
  58.    public static ComponentUI createUI(JComponent var0) {
  59.       return new BasicScrollBarUI();
  60.    }
  61.  
  62.    protected void configureScrollBarColors() {
  63.       this.thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
  64.       this.thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbLightShadow");
  65.       this.thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow");
  66.       this.thumbColor = UIManager.getColor("ScrollBar.thumb");
  67.       this.trackColor = UIManager.getColor("ScrollBar.track");
  68.       this.trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
  69.    }
  70.  
  71.    public void installUI(JComponent var1) {
  72.       this.scrollbar = (JScrollBar)var1;
  73.       this.thumbRect = new Rectangle(0, 0, 0, 0);
  74.       this.trackRect = new Rectangle(0, 0, 0, 0);
  75.       this.installDefaults();
  76.       this.installComponents();
  77.       this.installListeners();
  78.       this.installKeyboardActions();
  79.    }
  80.  
  81.    public void uninstallUI(JComponent var1) {
  82.       this.scrollbar = (JScrollBar)var1;
  83.       this.uninstallDefaults();
  84.       this.uninstallComponents();
  85.       this.uninstallListeners();
  86.       this.uninstallKeyboardActions();
  87.       ((Container)var1).remove(this.incrButton);
  88.       ((Container)var1).remove(this.decrButton);
  89.       ((Container)var1).setLayout((LayoutManager)null);
  90.       this.thumbRect = null;
  91.       this.scrollbar = null;
  92.       this.incrButton = null;
  93.       this.decrButton = null;
  94.    }
  95.  
  96.    protected void installDefaults() {
  97.       this.minimumThumbSize = (Dimension)UIManager.get("ScrollBar.minimumThumbSize");
  98.       this.maximumThumbSize = (Dimension)UIManager.get("ScrollBar.maximumThumbSize");
  99.       Boolean var1 = (Boolean)UIManager.get("ScrollBar.allowsAbsolutePositioning");
  100.       this.supportsAbsolutePositioning = var1 != null ? var1 : false;
  101.       this.trackHighlight = 0;
  102.       switch (this.scrollbar.getOrientation()) {
  103.          case 0:
  104.             this.incrButton = this.createIncreaseButton(3);
  105.             this.decrButton = this.createDecreaseButton(7);
  106.             break;
  107.          case 1:
  108.             this.incrButton = this.createIncreaseButton(5);
  109.             this.decrButton = this.createDecreaseButton(1);
  110.       }
  111.  
  112.       this.scrollbar.setLayout(this);
  113.       this.scrollbar.add(this.incrButton);
  114.       this.scrollbar.add(this.decrButton);
  115.       this.scrollbar.setEnabled(this.scrollbar.isEnabled());
  116.       this.scrollbar.setOpaque(true);
  117.       this.configureScrollBarColors();
  118.       LookAndFeel.installBorder(this.scrollbar, "ScrollBar.border");
  119.    }
  120.  
  121.    protected void installComponents() {
  122.    }
  123.  
  124.    protected void uninstallComponents() {
  125.    }
  126.  
  127.    protected void installListeners() {
  128.       this.trackListener = this.createTrackListener();
  129.       this.buttonListener = this.createArrowButtonListener();
  130.       this.modelListener = this.createModelListener();
  131.       this.propertyChangeListener = this.createPropertyChangeListener();
  132.       this.scrollbar.addMouseListener(this.trackListener);
  133.       this.scrollbar.addMouseMotionListener(this.trackListener);
  134.       this.scrollbar.getModel().addChangeListener(this.modelListener);
  135.       this.scrollbar.addPropertyChangeListener(this.propertyChangeListener);
  136.       if (this.incrButton != null) {
  137.          this.incrButton.addMouseListener(this.buttonListener);
  138.       }
  139.  
  140.       if (this.decrButton != null) {
  141.          this.decrButton.addMouseListener(this.buttonListener);
  142.       }
  143.  
  144.       this.scrollListener = this.createScrollListener();
  145.       this.scrollTimer = new Timer(60, this.scrollListener);
  146.       this.scrollTimer.setInitialDelay(300);
  147.    }
  148.  
  149.    protected void installKeyboardActions() {
  150.       ActionMap var1 = this.getActionMap();
  151.       SwingUtilities.replaceUIActionMap(this.scrollbar, var1);
  152.       InputMap var2 = this.getInputMap(0);
  153.       SwingUtilities.replaceUIInputMap(this.scrollbar, 0, var2);
  154.    }
  155.  
  156.    protected void uninstallKeyboardActions() {
  157.       SwingUtilities.replaceUIInputMap(this.scrollbar, 0, (InputMap)null);
  158.       SwingUtilities.replaceUIActionMap(this.scrollbar, (ActionMap)null);
  159.    }
  160.  
  161.    private InputMap getInputMap(int var1) {
  162.       return var1 == 0 ? (InputMap)UIManager.get("ScrollBar.focusInputMap") : null;
  163.    }
  164.  
  165.    private ActionMap getActionMap() {
  166.       ActionMap var1 = (ActionMap)UIManager.get("ScrollBar.actionMap");
  167.       if (var1 == null) {
  168.          var1 = this.createActionMap();
  169.          if (var1 != null) {
  170.             UIManager.put("ScrollBar.actionMap", var1);
  171.          }
  172.       }
  173.  
  174.       return var1;
  175.    }
  176.  
  177.    private ActionMap createActionMap() {
  178.       ActionMapUIResource var1 = new ActionMapUIResource();
  179.       ((ActionMap)var1).put("positiveUnitIncrement", new SharedActionScroller(1, false));
  180.       ((ActionMap)var1).put("positiveBlockIncrement", new SharedActionScroller(1, true));
  181.       ((ActionMap)var1).put("negativeUnitIncrement", new SharedActionScroller(-1, false));
  182.       ((ActionMap)var1).put("negativeBlockIncrement", new SharedActionScroller(-1, true));
  183.       ((ActionMap)var1).put("minScroll", new SharedActionScroller(2, true));
  184.       ((ActionMap)var1).put("maxScroll", new SharedActionScroller(3, true));
  185.       return var1;
  186.    }
  187.  
  188.    protected void uninstallListeners() {
  189.       this.scrollTimer.stop();
  190.       this.scrollTimer = null;
  191.       if (this.decrButton != null) {
  192.          this.decrButton.removeMouseListener(this.buttonListener);
  193.       }
  194.  
  195.       if (this.incrButton != null) {
  196.          this.incrButton.removeMouseListener(this.buttonListener);
  197.       }
  198.  
  199.       this.scrollbar.getModel().removeChangeListener(this.modelListener);
  200.       this.scrollbar.removeMouseListener(this.trackListener);
  201.       this.scrollbar.removeMouseMotionListener(this.trackListener);
  202.       this.scrollbar.removePropertyChangeListener(this.propertyChangeListener);
  203.    }
  204.  
  205.    protected void uninstallDefaults() {
  206.       LookAndFeel.uninstallBorder(this.scrollbar);
  207.    }
  208.  
  209.    protected TrackListener createTrackListener() {
  210.       return new TrackListener(this);
  211.    }
  212.  
  213.    protected ArrowButtonListener createArrowButtonListener() {
  214.       return new ArrowButtonListener(this);
  215.    }
  216.  
  217.    protected ModelListener createModelListener() {
  218.       return new ModelListener(this);
  219.    }
  220.  
  221.    protected ScrollListener createScrollListener() {
  222.       return new ScrollListener(this);
  223.    }
  224.  
  225.    protected PropertyChangeListener createPropertyChangeListener() {
  226.       return new PropertyChangeHandler(this);
  227.    }
  228.  
  229.    public void paint(Graphics var1, JComponent var2) {
  230.       this.paintTrack(var1, var2, this.getTrackBounds());
  231.       this.paintThumb(var1, var2, this.getThumbBounds());
  232.    }
  233.  
  234.    public Dimension getPreferredSize(JComponent var1) {
  235.       return this.scrollbar.getOrientation() == 1 ? new Dimension(16, 48) : new Dimension(48, 16);
  236.    }
  237.  
  238.    public Dimension getMinimumSize(JComponent var1) {
  239.       return this.getPreferredSize(var1);
  240.    }
  241.  
  242.    public Dimension getMaximumSize(JComponent var1) {
  243.       return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  244.    }
  245.  
  246.    protected JButton createDecreaseButton(int var1) {
  247.       return new BasicArrowButton(var1);
  248.    }
  249.  
  250.    protected JButton createIncreaseButton(int var1) {
  251.       return new BasicArrowButton(var1);
  252.    }
  253.  
  254.    protected void paintDecreaseHighlight(Graphics var1) {
  255.       Insets var2 = this.scrollbar.getInsets();
  256.       Rectangle var3 = this.getThumbBounds();
  257.       var1.setColor(this.trackHighlightColor);
  258.       if (this.scrollbar.getOrientation() == 1) {
  259.          int var4 = var2.left;
  260.          int var5 = this.decrButton.getY() + this.decrButton.getHeight();
  261.          int var6 = this.scrollbar.getWidth() - (var2.left + var2.right);
  262.          int var7 = var3.y - var5;
  263.          var1.fillRect(var4, var5, var6, var7);
  264.       } else {
  265.          int var8 = this.decrButton.getX() + this.decrButton.getHeight();
  266.          int var9 = var2.top;
  267.          int var10 = var3.x - var8;
  268.          int var11 = this.scrollbar.getHeight() - (var2.top + var2.bottom);
  269.          var1.fillRect(var8, var9, var10, var11);
  270.       }
  271.  
  272.    }
  273.  
  274.    protected void paintIncreaseHighlight(Graphics var1) {
  275.       Insets var2 = this.scrollbar.getInsets();
  276.       Rectangle var3 = this.getThumbBounds();
  277.       var1.setColor(this.trackHighlightColor);
  278.       if (this.scrollbar.getOrientation() == 1) {
  279.          int var4 = var2.left;
  280.          int var5 = var3.y + var3.height;
  281.          int var6 = this.scrollbar.getWidth() - (var2.left + var2.right);
  282.          int var7 = this.incrButton.getY() - var5;
  283.          var1.fillRect(var4, var5, var6, var7);
  284.       } else {
  285.          int var8 = var3.x + var3.width;
  286.          int var9 = var2.top;
  287.          int var10 = this.incrButton.getX() - var8;
  288.          int var11 = this.scrollbar.getHeight() - (var2.top + var2.bottom);
  289.          var1.fillRect(var8, var9, var10, var11);
  290.       }
  291.  
  292.    }
  293.  
  294.    protected void paintTrack(Graphics var1, JComponent var2, Rectangle var3) {
  295.       var1.setColor(this.trackColor);
  296.       var1.fillRect(var3.x, var3.y, var3.width, var3.height);
  297.       if (this.trackHighlight == 1) {
  298.          this.paintDecreaseHighlight(var1);
  299.       } else if (this.trackHighlight == 2) {
  300.          this.paintIncreaseHighlight(var1);
  301.       }
  302.  
  303.    }
  304.  
  305.    protected void paintThumb(Graphics var1, JComponent var2, Rectangle var3) {
  306.       if (!var3.isEmpty() && this.scrollbar.isEnabled()) {
  307.          int var4 = var3.width;
  308.          int var5 = var3.height;
  309.          var1.translate(var3.x, var3.y);
  310.          var1.setColor(this.thumbDarkShadowColor);
  311.          var1.drawRect(0, 0, var4 - 1, var5 - 1);
  312.          var1.setColor(this.thumbColor);
  313.          var1.fillRect(0, 0, var4 - 1, var5 - 1);
  314.          var1.setColor(this.thumbHighlightColor);
  315.          var1.drawLine(1, 1, 1, var5 - 2);
  316.          var1.drawLine(2, 1, var4 - 3, 1);
  317.          var1.setColor(this.thumbLightShadowColor);
  318.          var1.drawLine(2, var5 - 2, var4 - 2, var5 - 2);
  319.          var1.drawLine(var4 - 2, 1, var4 - 2, var5 - 3);
  320.          var1.translate(-var3.x, -var3.y);
  321.       }
  322.    }
  323.  
  324.    protected Dimension getMinimumThumbSize() {
  325.       return this.minimumThumbSize;
  326.    }
  327.  
  328.    protected Dimension getMaximumThumbSize() {
  329.       return this.maximumThumbSize;
  330.    }
  331.  
  332.    public void addLayoutComponent(String var1, Component var2) {
  333.    }
  334.  
  335.    public void removeLayoutComponent(Component var1) {
  336.    }
  337.  
  338.    public Dimension preferredLayoutSize(Container var1) {
  339.       return this.getPreferredSize((JComponent)var1);
  340.    }
  341.  
  342.    public Dimension minimumLayoutSize(Container var1) {
  343.       return this.getMinimumSize((JComponent)var1);
  344.    }
  345.  
  346.    protected void layoutVScrollbar(JScrollBar var1) {
  347.       Dimension var2 = ((Component)var1).getSize();
  348.       Insets var3 = ((JComponent)var1).getInsets();
  349.       int var4 = var2.width - (var3.left + var3.right);
  350.       int var5 = var3.left;
  351.       int var6 = this.decrButton.getPreferredSize().height;
  352.       int var7 = var3.top;
  353.       int var8 = this.incrButton.getPreferredSize().height;
  354.       int var9 = var2.height - (var3.bottom + var8);
  355.       int var10 = var3.top + var3.bottom;
  356.       int var11 = var6 + var8;
  357.       float var12 = (float)(var2.height - (var10 + var11));
  358.       float var13 = (float)var1.getMinimum();
  359.       float var14 = (float)var1.getVisibleAmount();
  360.       float var15 = (float)var1.getMaximum() - var13;
  361.       float var16 = (float)var1.getValue();
  362.       int var17 = var15 <= 0.0F ? this.getMaximumThumbSize().height : (int)(var12 * (var14 / var15));
  363.       var17 = Math.max(var17, this.getMinimumThumbSize().height);
  364.       var17 = Math.min(var17, this.getMaximumThumbSize().height);
  365.       int var18 = var9 - var17;
  366.       if (var1.getValue() < var1.getMaximum() - var1.getVisibleAmount()) {
  367.          float var19 = var12 - (float)var17;
  368.          var18 = (int)(0.5F + var19 * ((var16 - var13) / (var15 - var14)));
  369.          var18 += var7 + var6;
  370.       }
  371.  
  372.       int var25 = var2.height - var10;
  373.       if (var25 < var11) {
  374.          var8 = var6 = var25 / 2;
  375.          var9 = var2.height - (var3.bottom + var8);
  376.       }
  377.  
  378.       this.decrButton.setBounds(var5, var7, var4, var6);
  379.       this.incrButton.setBounds(var5, var9, var4, var8);
  380.       int var20 = var7 + var6;
  381.       int var21 = var9 - var20;
  382.       this.trackRect.setBounds(var5, var20, var4, var21);
  383.       if (var17 >= (int)var12) {
  384.          this.setThumbBounds(0, 0, 0, 0);
  385.       } else {
  386.          if (var18 + var17 > var9) {
  387.             var18 = var9 - var17;
  388.          }
  389.  
  390.          if (var18 < var7 + var6) {
  391.             var18 = var7 + var6 + 1;
  392.          }
  393.  
  394.          this.setThumbBounds(var5, var18, var4, var17);
  395.       }
  396.  
  397.    }
  398.  
  399.    protected void layoutHScrollbar(JScrollBar var1) {
  400.       Dimension var2 = ((Component)var1).getSize();
  401.       Insets var3 = ((JComponent)var1).getInsets();
  402.       int var4 = var2.height - (var3.top + var3.bottom);
  403.       int var5 = var3.top;
  404.       int var6 = this.decrButton.getPreferredSize().width;
  405.       int var7 = var3.left;
  406.       int var8 = this.incrButton.getPreferredSize().width;
  407.       int var9 = var2.width - (var3.right + var8);
  408.       int var10 = var3.left + var3.right;
  409.       int var11 = var6 + var8;
  410.       float var12 = (float)(var2.width - (var10 + var11));
  411.       float var13 = (float)var1.getMinimum();
  412.       float var14 = (float)var1.getVisibleAmount();
  413.       float var15 = (float)var1.getMaximum() - var13;
  414.       float var16 = (float)var1.getValue();
  415.       int var17 = var15 <= 0.0F ? this.getMaximumThumbSize().width : (int)(var12 * (var14 / var15));
  416.       var17 = Math.max(var17, this.getMinimumThumbSize().width);
  417.       var17 = Math.min(var17, this.getMaximumThumbSize().width);
  418.       int var18 = var9 - var17;
  419.       if (var1.getValue() < var1.getMaximum() - var1.getVisibleAmount()) {
  420.          float var19 = var12 - (float)var17;
  421.          var18 = (int)(0.5F + var19 * ((var16 - var13) / (var15 - var14)));
  422.          var18 += var7 + var6;
  423.       }
  424.  
  425.       int var25 = var2.width - var10;
  426.       if (var25 < var11) {
  427.          var8 = var6 = var25 / 2;
  428.          var9 = var2.width - (var3.right + var8);
  429.       }
  430.  
  431.       this.decrButton.setBounds(var7, var5, var6, var4);
  432.       this.incrButton.setBounds(var9, var5, var8, var4);
  433.       int var20 = var7 + var6;
  434.       int var21 = var9 - var20;
  435.       this.trackRect.setBounds(var20, var5, var21, var4);
  436.       if (var17 >= (int)var12) {
  437.          this.setThumbBounds(0, 0, 0, 0);
  438.       } else {
  439.          if (var18 + var17 > var9) {
  440.             var18 = var9 - var17;
  441.          }
  442.  
  443.          if (var18 < var7 + var6) {
  444.             var18 = var7 + var6 + 1;
  445.          }
  446.  
  447.          this.setThumbBounds(var18, var5, var17, var4);
  448.       }
  449.  
  450.    }
  451.  
  452.    public void layoutContainer(Container var1) {
  453.       if (!this.isDragging) {
  454.          JScrollBar var2 = (JScrollBar)var1;
  455.          switch (var2.getOrientation()) {
  456.             case 0:
  457.                this.layoutHScrollbar(var2);
  458.                break;
  459.             case 1:
  460.                this.layoutVScrollbar(var2);
  461.          }
  462.  
  463.       }
  464.    }
  465.  
  466.    protected void setThumbBounds(int var1, int var2, int var3, int var4) {
  467.       if (this.thumbRect.x != var1 || this.thumbRect.y != var2 || this.thumbRect.width != var3 || this.thumbRect.height != var4) {
  468.          int var5 = Math.min(var1, this.thumbRect.x);
  469.          int var6 = Math.min(var2, this.thumbRect.y);
  470.          int var7 = Math.max(var1 + var3, this.thumbRect.x + this.thumbRect.width);
  471.          int var8 = Math.max(var2 + var4, this.thumbRect.y + this.thumbRect.height);
  472.          this.thumbRect.setBounds(var1, var2, var3, var4);
  473.          this.scrollbar.repaint(var5, var6, var7 - var5, var8 - var6);
  474.       }
  475.    }
  476.  
  477.    protected Rectangle getThumbBounds() {
  478.       return this.thumbRect;
  479.    }
  480.  
  481.    protected Rectangle getTrackBounds() {
  482.       return this.trackRect;
  483.    }
  484.  
  485.    protected void scrollByBlock(int var1) {
  486.       JScrollBar var2 = this.scrollbar;
  487.       synchronized(var2) {
  488.          int var3 = this.scrollbar.getValue();
  489.          int var4 = this.scrollbar.getBlockIncrement(var1);
  490.          int var5 = var4 * (var1 > 0 ? 1 : -1);
  491.          this.scrollbar.setValue(var3 + var5);
  492.          this.trackHighlight = var1 > 0 ? 2 : 1;
  493.          Rectangle var6 = this.getTrackBounds();
  494.          this.scrollbar.repaint(var6.x, var6.y, var6.width, var6.height);
  495.       }
  496.    }
  497.  
  498.    protected void scrollByUnit(int var1) {
  499.       JScrollBar var2 = this.scrollbar;
  500.       synchronized(var2) {
  501.          int var3;
  502.          if (var1 > 0) {
  503.             var3 = this.scrollbar.getUnitIncrement(var1);
  504.          } else {
  505.             var3 = -this.scrollbar.getUnitIncrement(var1);
  506.          }
  507.  
  508.          this.scrollbar.setValue(var3 + this.scrollbar.getValue());
  509.       }
  510.    }
  511.  
  512.    private boolean getSupportsAbsolutePositioning() {
  513.       return this.supportsAbsolutePositioning;
  514.    }
  515.  
  516.    // $FF: synthetic method
  517.    static boolean access$000(BasicScrollBarUI var0) {
  518.       return var0.getSupportsAbsolutePositioning();
  519.    }
  520. }
  521.