home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / netscape / application / ScrollView.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  9.2 KB  |  425 lines

  1. package netscape.application;
  2.  
  3. import netscape.util.ClassInfo;
  4. import netscape.util.CodingException;
  5. import netscape.util.Decoder;
  6. import netscape.util.Encoder;
  7. import netscape.util.Vector;
  8.  
  9. public class ScrollView extends View implements Scrollable {
  10.    View contentView;
  11.    Color backgroundColor;
  12.    Rect clipRect;
  13.    Vector scrollBars;
  14.    boolean transparent;
  15.    private boolean scrollBarUpdatesEnabled;
  16.    static final String CONTENTVIEW_KEY = "contentView";
  17.    static final String BACKGROUNDC_KEY = "backgroundColor";
  18.    static final String SCROLLERS_KEY = "scrollBars";
  19.    static final String SCROLLERUPDATES_KEY = "scrollBarUpdatesEnabed";
  20.    static final String TRANSPARENT_KEY = "transparent";
  21.  
  22.    public ScrollView() {
  23.       this(0, 0, 0, 0);
  24.    }
  25.  
  26.    public ScrollView(Rect var1) {
  27.       this(var1.x, var1.y, var1.width, var1.height);
  28.    }
  29.  
  30.    public ScrollView(int var1, int var2, int var3, int var4) {
  31.       super(var1, var2, var3, var4);
  32.       this.transparent = false;
  33.       this.scrollBarUpdatesEnabled = true;
  34.       this.scrollBars = new Vector();
  35.       this.backgroundColor = Color.lightGray;
  36.       ((View)this).setHorizResizeInstruction(2);
  37.       ((View)this).setVertResizeInstruction(16);
  38.    }
  39.  
  40.    public void addSubview(View var1) {
  41.       ((View)this).subviews().removeAllElements();
  42.       super.addSubview(var1);
  43.    }
  44.  
  45.    public void setContentView(View var1) {
  46.       if (this.contentView != null) {
  47.          this.contentView.removeFromSuperview();
  48.       }
  49.  
  50.       this.contentView = var1;
  51.       if (this.contentView != null) {
  52.          this.contentView.moveTo(0, 0);
  53.          this.addSubview(this.contentView);
  54.       }
  55.  
  56.       this.updateScrollBars();
  57.    }
  58.  
  59.    public View contentView() {
  60.       return this.contentView;
  61.    }
  62.  
  63.    public int cursorForPoint(int var1, int var2) {
  64.       if (this.contentView == null) {
  65.          return 0;
  66.       } else {
  67.          Point var3 = Point.newPoint(var1, var2);
  68.          ((View)this).convertPointToView(this.contentView, var3, var3);
  69.          int var4 = this.contentView.cursorForPoint(var3.x, var3.y);
  70.          Point.returnPoint(var3);
  71.          return var4;
  72.       }
  73.    }
  74.  
  75.    public void setBackgroundColor(Color var1) {
  76.       if (var1 != null) {
  77.          this.backgroundColor = var1;
  78.       }
  79.  
  80.    }
  81.  
  82.    public Color backgroundColor() {
  83.       return this.backgroundColor;
  84.    }
  85.  
  86.    public void setTransparent(boolean var1) {
  87.       this.transparent = var1;
  88.    }
  89.  
  90.    public boolean isTransparent() {
  91.       return this.transparent;
  92.    }
  93.  
  94.    public void addScrollBar(Target var1) {
  95.       this.scrollBars.addElementIfAbsent(var1);
  96.    }
  97.  
  98.    public void removeScrollBar(Target var1) {
  99.       this.scrollBars.removeElement(var1);
  100.    }
  101.  
  102.    public void setScrollBarUpdatesEnabled(boolean var1) {
  103.       this.scrollBarUpdatesEnabled = var1;
  104.    }
  105.  
  106.    public boolean scrollBarUpdatesEnabled() {
  107.       return this.scrollBarUpdatesEnabled;
  108.    }
  109.  
  110.    public void updateScrollBars() {
  111.       if (this.scrollBarUpdatesEnabled) {
  112.          int var3 = this.scrollBars.count();
  113.  
  114.          for(int var2 = 0; var2 < var3; ++var2) {
  115.             Target var1 = (Target)this.scrollBars.elementAt(var2);
  116.             var1.performCommand("updateScrollValue", this);
  117.          }
  118.  
  119.       }
  120.    }
  121.  
  122.    View scrollingView() {
  123.       return this;
  124.    }
  125.  
  126.    public void scrollRectToVisible(Rect var1) {
  127.       int var2 = 0;
  128.       int var3 = 0;
  129.       if (var1 != null && this.contentView != null) {
  130.          var2 = this.positionAdjustment(super.bounds.width, var1.width, var1.x);
  131.          var3 = this.positionAdjustment(super.bounds.height, var1.height, var1.y);
  132.          if (var2 != 0 || var3 != 0) {
  133.             this.scrollBy(var2, var3);
  134.          }
  135.  
  136.       }
  137.    }
  138.  
  139.    private int positionAdjustment(int var1, int var2, int var3) {
  140.       if (var3 >= 0 && var2 + var3 <= var1) {
  141.          return 0;
  142.       } else if (var3 <= 0 && var2 + var3 >= var1) {
  143.          return 0;
  144.       } else if (var3 > 0 && var2 <= var1) {
  145.          return -var3 + var1 - var2;
  146.       } else if (var3 >= 0 && var2 >= var1) {
  147.          return -var3;
  148.       } else if (var3 <= 0 && var2 <= var1) {
  149.          return -var3;
  150.       } else {
  151.          return var3 < 0 && var2 >= var1 ? -var3 + var1 - var2 : 0;
  152.       }
  153.    }
  154.  
  155.    public void scrollTo(int var1, int var2) {
  156.       Rect var3 = null;
  157.       if (this.contentView != null) {
  158.          this.setClipRect((Rect)null);
  159.          if (var1 <= 0 && super.bounds.width < this.contentView.bounds.width) {
  160.             if (var1 < super.bounds.width - this.contentView.bounds.width) {
  161.                var1 = super.bounds.width - this.contentView.bounds.width;
  162.             }
  163.          } else {
  164.             var1 = 0;
  165.          }
  166.  
  167.          if (var2 <= 0 && super.bounds.height < this.contentView.bounds.height) {
  168.             if (var2 < super.bounds.height - this.contentView.bounds.height) {
  169.                var2 = super.bounds.height - this.contentView.bounds.height;
  170.             }
  171.          } else {
  172.             var2 = 0;
  173.          }
  174.  
  175.          int var4 = var1 - this.contentView.bounds.x;
  176.          int var5 = var2 - this.contentView.bounds.y;
  177.          if (var4 == 0 && var5 == 0) {
  178.             this.updateScrollBars();
  179.          } else {
  180.             boolean var6 = ((View)this).isBuffered() && super.drawingBufferValid;
  181.             if (var6) {
  182.                if (var4 != 0 && var5 == 0 && Math.abs(var4) < super.bounds.width) {
  183.                   if (var4 < 0) {
  184.                      var3 = Rect.newRect(super.bounds.width + var4, 0, -var4, super.bounds.height);
  185.                   } else {
  186.                      var3 = Rect.newRect(0, 0, var4, super.bounds.height);
  187.                   }
  188.                } else if (var4 == 0 && var5 != 0 && Math.abs(var5) < super.bounds.height) {
  189.                   if (var5 < 0) {
  190.                      var3 = Rect.newRect(0, super.bounds.height + var5, super.bounds.width, -var5);
  191.                   } else {
  192.                      var3 = Rect.newRect(0, 0, super.bounds.width, var5);
  193.                   }
  194.                }
  195.  
  196.                if (var3 != null) {
  197.                   this.contentView.moveTo(var1, var2);
  198.                   this.setClipRect(var3);
  199.                   Rect.returnRect(var3);
  200.                } else {
  201.                   this.contentView.moveTo(var1, var2);
  202.                }
  203.             } else {
  204.                this.contentView.moveTo(var1, var2);
  205.             }
  206.  
  207.             if (this.scrollBarUpdatesEnabled) {
  208.                this.updateScrollBars();
  209.             }
  210.  
  211.             ((View)this).setDirty(true);
  212.          }
  213.       }
  214.    }
  215.  
  216.    public void scrollBy(int var1, int var2) {
  217.       if (this.contentView != null) {
  218.          this.scrollTo(this.contentView.bounds.x + var1, this.contentView.bounds.y + var2);
  219.       }
  220.  
  221.    }
  222.  
  223.    public void subviewDidResize(View var1) {
  224.       if (var1 == this.contentView) {
  225.          this.scrollBy(0, 0);
  226.          this.drawBackground();
  227.       }
  228.    }
  229.  
  230.    public void didSizeBy(int var1, int var2) {
  231.       super.didSizeBy(var1, var2);
  232.       this.scrollBy(0, 0);
  233.    }
  234.  
  235.    void setClipRect(Rect var1) {
  236.       if (this.clipRect != null) {
  237.          Rect.returnRect(this.clipRect);
  238.       }
  239.  
  240.       if (var1 != null) {
  241.          this.clipRect = Rect.newRect(var1);
  242.       } else {
  243.          this.clipRect = null;
  244.       }
  245.    }
  246.  
  247.    void updateDrawingBuffer(Rect var1) {
  248.       if (this.clipRect != null && ((View)this).isBuffered() && !this.isTransparent()) {
  249.          Graphics var2 = super.drawingBuffer.createGraphics();
  250.          var2.setDebugOptions(((View)this).shouldDebugGraphics());
  251.          if (this.clipRect.height != super.bounds.height) {
  252.             if (this.clipRect.y == 0) {
  253.                var2.copyArea(this.clipRect.x, this.clipRect.y, this.clipRect.width, super.bounds.height - this.clipRect.height, this.clipRect.x, this.clipRect.maxY());
  254.             } else {
  255.                var2.copyArea(0, this.clipRect.height, this.clipRect.width, super.bounds.height - this.clipRect.height, 0, 0);
  256.             }
  257.          } else if (this.clipRect.x == 0) {
  258.             var2.copyArea(this.clipRect.x, this.clipRect.y, super.bounds.width - this.clipRect.width, this.clipRect.height, this.clipRect.maxX(), this.clipRect.y);
  259.          } else {
  260.             var2.copyArea(this.clipRect.width, 0, super.bounds.width - this.clipRect.width, this.clipRect.height, 0, 0);
  261.          }
  262.  
  263.          var2.dispose();
  264.          Object var4 = null;
  265.          var1 = new Rect(var1);
  266.          Rect var3 = new Rect(0, 0, 0, 0);
  267.          this.contentView.getDirtyRect(var3);
  268.          if (var3.isEmpty()) {
  269.             var1.intersectWith(this.clipRect);
  270.          } else {
  271.             var3.unionWith(this.clipRect);
  272.             var1.intersectWith(var3);
  273.          }
  274.  
  275.          this.setClipRect((Rect)null);
  276.       }
  277.  
  278.       super.updateDrawingBuffer(var1);
  279.    }
  280.  
  281.    public void computeVisibleRect(Rect var1) {
  282.       super.computeVisibleRect(var1);
  283.       if (this.clipRect != null) {
  284.          var1.intersectWith(this.clipRect);
  285.       }
  286.  
  287.    }
  288.  
  289.    public void drawView(Graphics var1) {
  290.       if (!this.isTransparent()) {
  291.          if (this.contentView != null && this.contentView.isTransparent()) {
  292.             var1.setColor(this.backgroundColor);
  293.             Rect var2 = var1.clipRect();
  294.             var1.fillRect(var2.x, var2.y, var2.width, var2.height);
  295.          } else {
  296.             int var3;
  297.             int var4;
  298.             if (this.contentView != null) {
  299.                var3 = super.bounds.width - this.contentView.bounds.width;
  300.                var4 = super.bounds.height - this.contentView.bounds.height;
  301.             } else {
  302.                var3 = super.bounds.width;
  303.                var4 = super.bounds.height;
  304.             }
  305.  
  306.             if (var3 > 0) {
  307.                var1.setColor(this.backgroundColor);
  308.                var1.fillRect(super.bounds.width - var3, 0, var3, super.bounds.height);
  309.             }
  310.  
  311.             if (var4 > 0) {
  312.                var1.setColor(this.backgroundColor);
  313.                var1.fillRect(0, super.bounds.height - var4, super.bounds.width, var4);
  314.             }
  315.  
  316.             if (this.clipRect != null && ((View)this).isBuffered()) {
  317.                if (this.clipRect.y == super.bounds.y) {
  318.                   super.drawingBuffer.drawAt(var1, this.clipRect.x, this.clipRect.maxY());
  319.                   return;
  320.                }
  321.  
  322.                super.drawingBuffer.drawAt(var1, this.clipRect.x, -this.clipRect.height);
  323.             }
  324.  
  325.          }
  326.       }
  327.    }
  328.  
  329.    public void drawSubviews(Graphics var1) {
  330.       super.drawSubviews(var1);
  331.       this.setClipRect((Rect)null);
  332.    }
  333.  
  334.    void drawBackground() {
  335.       int var2;
  336.       int var3;
  337.       if (this.contentView != null) {
  338.          var2 = super.bounds.width - this.contentView.bounds.width;
  339.          var3 = super.bounds.height - this.contentView.bounds.height;
  340.       } else {
  341.          var2 = super.bounds.width;
  342.          var3 = super.bounds.height;
  343.       }
  344.  
  345.       Rect var1 = Rect.newRect();
  346.       if (var2 > 0) {
  347.          var1.setBounds(super.bounds.width - var2, 0, var2, super.bounds.height);
  348.          ((View)this).addDirtyRect(var1);
  349.       }
  350.  
  351.       if (var3 > 0) {
  352.          var1.setBounds(0, super.bounds.height - var3, super.bounds.width, var3);
  353.          ((View)this).addDirtyRect(var1);
  354.       }
  355.  
  356.       Rect.returnRect(var1);
  357.    }
  358.  
  359.    public boolean mouseDown(MouseEvent var1) {
  360.       if (this.contentView != null) {
  361.          boolean var2 = this.contentView.mouseDown(((View)this).convertEventToView(this.contentView, var1));
  362.          if (var2) {
  363.             ((View)this).rootView().setMouseView(this.contentView);
  364.             return true;
  365.          } else {
  366.             return false;
  367.          }
  368.       } else {
  369.          return false;
  370.       }
  371.    }
  372.  
  373.    public DragDestination acceptsDrag(DragSession var1, int var2, int var3) {
  374.       return this.contentView != null ? this.contentView.acceptsDrag(var1, var2 - this.contentView.bounds.x, var3 - this.contentView.bounds.y) : null;
  375.    }
  376.  
  377.    public int lengthOfScrollViewForAxis(int var1) {
  378.       return var1 == 0 ? super.bounds.width : super.bounds.height;
  379.    }
  380.  
  381.    public int lengthOfContentViewForAxis(int var1) {
  382.       if (this.contentView == null) {
  383.          return 0;
  384.       } else {
  385.          return var1 == 0 ? this.contentView.bounds.width : this.contentView.bounds.height;
  386.       }
  387.    }
  388.  
  389.    public int positionOfContentViewForAxis(int var1) {
  390.       if (this.contentView == null) {
  391.          return 0;
  392.       } else {
  393.          return var1 == 0 ? this.contentView.bounds.x : this.contentView.bounds.y;
  394.       }
  395.    }
  396.  
  397.    public void describeClassInfo(ClassInfo var1) {
  398.       super.describeClassInfo(var1);
  399.       var1.addClass("netscape.application.ScrollView", 1);
  400.       var1.addField("contentView", (byte)18);
  401.       var1.addField("backgroundColor", (byte)18);
  402.       var1.addField("scrollBars", (byte)18);
  403.       var1.addField("scrollBarUpdatesEnabed", (byte)0);
  404.       var1.addField("transparent", (byte)0);
  405.    }
  406.  
  407.    public void encode(Encoder var1) throws CodingException {
  408.       super.encode(var1);
  409.       var1.encodeObject("contentView", this.contentView);
  410.       var1.encodeObject("backgroundColor", this.backgroundColor);
  411.       var1.encodeObject("scrollBars", this.scrollBars);
  412.       var1.encodeBoolean("scrollBarUpdatesEnabed", this.scrollBarUpdatesEnabled);
  413.       var1.encodeBoolean("transparent", this.transparent);
  414.    }
  415.  
  416.    public void decode(Decoder var1) throws CodingException {
  417.       super.decode(var1);
  418.       this.contentView = (View)var1.decodeObject("contentView");
  419.       this.backgroundColor = (Color)var1.decodeObject("backgroundColor");
  420.       this.scrollBars = (Vector)var1.decodeObject("scrollBars");
  421.       this.scrollBarUpdatesEnabled = var1.decodeBoolean("scrollBarUpdatesEnabed");
  422.       this.transparent = var1.decodeBoolean("transparent");
  423.    }
  424. }
  425.