Source for javax.swing.plaf.basic.BasicViewportUI

   1: /* BasicViewportUI.java --
   2:    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.plaf.basic;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Dimension;
  43: import java.awt.Graphics;
  44: import java.awt.Image;
  45: import java.awt.Point;
  46: import java.awt.Rectangle;
  47: import java.awt.image.ImageObserver;
  48: 
  49: import javax.swing.JComponent;
  50: import javax.swing.JViewport;
  51: import javax.swing.ViewportLayout;
  52: import javax.swing.event.ChangeEvent;
  53: import javax.swing.event.ChangeListener;
  54: import javax.swing.plaf.ComponentUI;
  55: import javax.swing.plaf.ViewportUI;
  56: 
  57: public class BasicViewportUI extends ViewportUI 
  58: {
  59: 
  60:   ChangeListener changeListener;
  61:   Image backingStoreImage;
  62:   int backingStoreWidth = -1;
  63:   int backingStoreHeight = -1;
  64:   
  65:   class ChangeHandler implements ChangeListener
  66:   {
  67:     public void stateChanged(ChangeEvent event)
  68:     {
  69:       JViewport v = (JViewport) event.getSource();
  70:       v.repaint();
  71:     }
  72:   }
  73: 
  74:   void installDefaults(JComponent c)
  75:   {    
  76:     c.setOpaque(true);
  77:   }
  78: 
  79:   void uninstallDefaults(JComponent c)
  80:   {
  81:   }
  82: 
  83:   void installListeners(JComponent c)
  84:   {
  85:     ((JViewport)c).addChangeListener(changeListener);
  86:   }
  87: 
  88:   void uninstallListeners(JComponent c)
  89:   {
  90:     ((JViewport)c).removeChangeListener(changeListener);
  91:   }
  92: 
  93:   public BasicViewportUI()
  94:   {
  95:     changeListener = new ChangeHandler();
  96:   }
  97: 
  98:   public static ComponentUI createUI(JComponent c)
  99:   {
 100:     return new BasicViewportUI();
 101:   }
 102: 
 103:   public void installUI(JComponent c) 
 104:   {
 105:     super.installUI(c);
 106:     installListeners(c);
 107:   }
 108: 
 109:   public void uninstallUI(JComponent c) 
 110:   {
 111:     uninstallListeners(c);
 112:   }
 113:     
 114: 
 115:   public Dimension getPreferredSize(JComponent c) 
 116:   {
 117:     // let the ViewportLayout decide
 118:     return null;
 119:   }
 120: 
 121:   public void paint(Graphics g, JComponent c)
 122:   {
 123:     JViewport port = (JViewport)c;
 124:     Component view = port.getView();
 125: 
 126:     if (view == null)
 127:       return;
 128: 
 129:     Point pos = port.getViewPosition();
 130:     Rectangle viewBounds = view.getBounds();
 131:     Rectangle portBounds = port.getBounds();
 132: 
 133:     if (viewBounds.width == 0 
 134:         || viewBounds.height == 0
 135:         || portBounds.width == 0
 136:         || portBounds.height == 0)
 137:       return;
 138: 
 139:     switch (port.getScrollMode())
 140:       {
 141: 
 142:       case JViewport.BACKINGSTORE_SCROLL_MODE:
 143:         paintBackingStore(g, port, view, pos, viewBounds, portBounds);
 144:         break;
 145: 
 146:       case JViewport.BLIT_SCROLL_MODE:
 147:         // FIXME: implement separate blit mode
 148: 
 149:       case JViewport.SIMPLE_SCROLL_MODE:
 150:       default:
 151:         paintSimple(g, port, view, pos, viewBounds, portBounds);
 152:         break;
 153:       }
 154:   }
 155: 
 156:   private void paintSimple(Graphics g, 
 157:                            JViewport v, 
 158:                            Component view, 
 159:                            Point pos, 
 160:                            Rectangle viewBounds, 
 161:                            Rectangle portBounds)
 162:   {
 163:     Rectangle oldClip = g.getClipBounds();
 164:     g.setClip(new Rectangle(0, 0, portBounds.width, portBounds.height));
 165:     g.translate (-pos.x, -pos.y);
 166:     try
 167:       {
 168:         view.paint(g);
 169:       } 
 170:     finally 
 171:       {
 172:         g.translate (pos.x, pos.y);
 173:         g.setClip (oldClip);
 174:       }
 175:   }
 176: 
 177:   private void paintBackingStore(Graphics g, 
 178:                                  JViewport v, 
 179:                                  Component view, 
 180:                                  Point pos, 
 181:                                  Rectangle viewBounds, 
 182:                                  Rectangle portBounds)
 183:   {      
 184:     if (backingStoreImage == null 
 185:         || backingStoreWidth != viewBounds.width
 186:         || backingStoreHeight != viewBounds.height)
 187:       {
 188:         backingStoreImage = v.createImage(viewBounds.width, viewBounds.height);
 189:         backingStoreWidth = viewBounds.width;
 190:         backingStoreHeight = viewBounds.height;
 191:       }
 192: 
 193:     Graphics g2 = backingStoreImage.getGraphics();
 194: 
 195:     if (v.getBackground() != null)
 196:       {
 197:         // fill the backing store background
 198:         java.awt.Color save = g2.getColor();
 199:         g2.setColor(v.getBackground());
 200:         g2.fillRect (0, 0, backingStoreWidth, backingStoreHeight);
 201:         g2.setColor(save);
 202: 
 203:         // fill the viewport background
 204:         save = g.getColor();
 205:         g.setColor(v.getBackground());
 206:         g.fillRect (0, 0, portBounds.width, portBounds.height);
 207:         g.setColor(save);
 208: 
 209:       }
 210:     else
 211:       {
 212:         // clear the backing store background
 213:         g2.clearRect(0, 0, backingStoreWidth, backingStoreHeight);
 214: 
 215:         // clear the viewport background
 216:         g.clearRect(0, 0, portBounds.width, portBounds.height);
 217:       }
 218: 
 219:     g2.setClip(g.getClipBounds());
 220:     g2.translate(-pos.x, -pos.y);
 221:     try 
 222:       {
 223:         view.paint(g2);
 224:       }
 225:     finally
 226:       {
 227:         g2.translate(pos.x, pos.y);
 228:       }
 229:     g2 = null;
 230:     g.drawImage(backingStoreImage, 
 231:                 0, 0, 
 232:                 (ImageObserver)null);
 233:   }
 234: }