//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.media;

import java.awt.Rectangle;

import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.threerings.media.util.MathUtil;

/**
 * Provides a {@link BoundedRangeModel} interface to a virtual media panel
 * so that it can easily be wired up to scroll bars or other scrolling
 * controls.
 */
public class VirtualRangeModel
    implements ChangeListener
{
    /**
     * Creates a virtual media panel range model that will interact with
     * the supplied virtual media panel.
     */
    public VirtualRangeModel (VirtualMediaPanel panel)
    {
        _panel = panel;

        // listen to our range models and scroll our badself
        _horizRange.addChangeListener(this);
        _vertRange.addChangeListener(this);
    }

    /**
     * Informs the virtual range model the extent of the area over which
     * we can scroll.
     */
    public void setScrollableArea (int x, int y, int width, int height)
    {
        Rectangle vbounds = _panel.getViewBounds();
        int hmax = x + width, vmax = y + height, value =
            MathUtil.bound(x, _horizRange.getValue(), hmax - vbounds.width);
        _horizRange.setRangeProperties(value, vbounds.width, x, hmax, false);
        value = MathUtil.bound(y, _vertRange.getValue(), vmax - vbounds.height);
        _vertRange.setRangeProperties(value, vbounds.height, y, vmax, false);
    }

    /**
     * Returns a range model that controls the scrollability of the scene
     * in the horizontal direction.
     */
    public BoundedRangeModel getHorizModel ()
    {
        return _horizRange;
    }

    /**
     * Returns a range model that controls the scrollability of the scene
     * in the vertical direction.
     */
    public BoundedRangeModel getVertModel ()
    {
        return _vertRange;
    }

    // documentation inherited from interface ChangeListener
    public void stateChanged (ChangeEvent e)
    {
        _panel.setViewLocation(_horizRange.getValue(), _vertRange.getValue());
    }

    protected VirtualMediaPanel _panel;
    protected BoundedRangeModel _horizRange = new DefaultBoundedRangeModel();
    protected BoundedRangeModel _vertRange = new DefaultBoundedRangeModel();
}

@@ -1695,29 +1695,25 @@
 f%0A        _h
-orizR
+r
 ange.addChan
@@ -1736,28 +1736,25 @@
 ;%0A        _v
-ertR
+r
 ange.addChan
@@ -1987,21 +1987,16 @@
 angle vb
-ounds
  = _pane
@@ -2068,18 +2068,50 @@
 t, value
- =
+;%0A%0A        if (width %3E vb.width) %7B
 %0A       
@@ -2114,16 +2114,24 @@
         
+ value =
  MathUti
@@ -2143,21 +2143,17 @@
 nd(x, _h
-orizR
+r
 ange.get
@@ -2170,21 +2170,16 @@
 max - vb
-ounds
 .width);
@@ -2187,23 +2187,23 @@
         
-_horizR
+    _hr
 ange.set
@@ -2227,21 +2227,16 @@
 alue, vb
-ounds
 .width, 
@@ -2248,24 +2248,162 @@
 ax, false);%0A
+        %7D else %7B%0A            _hrange.setRangeProperties(0, vb.width, 0, vb.width, false);%0A        %7D%0A        if (height %3E vb.height) %7B%0A    
         valu
@@ -2422,28 +2422,25 @@
 .bound(y, _v
-ertR
+r
 ange.getValu
@@ -2453,21 +2453,16 @@
 max - vb
-ounds
 .height)
@@ -2471,22 +2471,23 @@
         
+  
-_vertR
+  _vr
 ange.set
@@ -2511,21 +2511,16 @@
 alue, vb
-ounds
 .height,
@@ -2533,24 +2533,126 @@
 ax, false);%0A
+        %7D else %7B%0A            _vrange.setRangeProperties(0, vb.height, 0, vb.height, false);%0A        %7D%0A
     %7D%0A%0A    /
@@ -2841,21 +2841,17 @@
 eturn _h
-orizR
+r
 ange;%0A  
@@ -3047,20 +3047,17 @@
 eturn _v
-ertR
+r
 ange;%0A  
@@ -3206,21 +3206,17 @@
 ation(_h
-orizR
+r
 ange.get
@@ -3226,20 +3226,17 @@
 ue(), _v
-ertR
+r
 ange.get
@@ -3330,13 +3330,9 @@
 l _h
-orizR
+r
 ange
@@ -3404,12 +3404,9 @@
 l _v
-ertR
+r
 ange

Handle cases where one or the other direction does not scroll for lack of need.