Source for javax.swing.SizeSequence

   1: /* SizeSequence.java --
   2:    Copyright (C) 2002, 2006, 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: package javax.swing;
  39: 
  40: /**
  41:  * A sequence of values that represent the dimensions (widths or heights) of 
  42:  * some collection of items (for example, the widths of the columns in a table).
  43:  * 
  44:  * @author    Andrew Selkirk
  45:  */
  46: public class SizeSequence
  47: {
  48:   // TODO: Sun's API specification for this class contains an implementation
  49:   // note regarding the encoding for the element sizes.  We currently use the
  50:   // simple size encoding but we should look at improving this.
  51:   
  52:   /** Storage for the element sizes. */
  53:   private int[] sizes;
  54: 
  55:   /**
  56:    * Creates a new empty <code>SizeSequence</code> instance.
  57:    */
  58:   public SizeSequence()
  59:   {
  60:     sizes = new int[0];
  61:   }
  62: 
  63:   /**
  64:    * Creates a new <code>SizeSequence</code> instance with the specified number 
  65:    * of elements, each having a size of 0.
  66:    * 
  67:    * @param numEntries  the number of elements.
  68:    */
  69:   public SizeSequence(int numEntries)
  70:   {
  71:     this(numEntries, 0);
  72:   }
  73: 
  74:   /**
  75:    * Creates a new <code>SizeSequence</code> instance with the specified number
  76:    * of elements all having the same size (<code>value</code>).
  77:    * 
  78:    * @param numEntries  the number of elements.
  79:    * @param value  the value for each element.
  80:    */
  81:   public SizeSequence(int numEntries, int value)
  82:   {
  83:     sizes = new int[0];
  84:     insertEntries(0, numEntries, value);
  85:   }
  86: 
  87:   /**
  88:    * Creates a new <code>SizeSequence</code> instance using the specified 
  89:    * element sizes.
  90:    * 
  91:    * @param sizes  the element sizes (<code>null</code> not permitted).
  92:    */
  93:   public SizeSequence(int[] sizes)
  94:   {
  95:     this.sizes = (int[]) sizes.clone();
  96:   }
  97: 
  98:   /**
  99:    * Sets the size of the element at the specified index.
 100:    * 
 101:    * @param index  the index.
 102:    * @param size  the size.
 103:    */
 104:   public void setSize(int index, int size)
 105:   {
 106:     if (index >= 0 && index < sizes.length)
 107:       sizes[index] = size;
 108:   }
 109: 
 110:   /**
 111:    * Returns the index of the element that contains the specified position.
 112:    * 
 113:    * @param position  the position.
 114:    * 
 115:    * @return The index of the element that contains the specified position.
 116:    */
 117:   public int getIndex(int position)
 118:   {
 119:     int i = 0;
 120:     int runningTotal = 0;  
 121:     while (i < sizes.length && position >= runningTotal + sizes[i]) 
 122:       {
 123:         runningTotal += sizes[i];
 124:         i++;
 125:       }
 126:     return i;
 127:   }
 128: 
 129:   /**
 130:    * Returns the size of the specified element.
 131:    * 
 132:    * @param index  the element index.
 133:    * 
 134:    * @return The size of the specified element.
 135:    */
 136:   public int getSize(int index)
 137:   {
 138:     return sizes[index];
 139:   }
 140: 
 141:   /**
 142:    * Sets the sizes for the elements in the sequence.
 143:    * 
 144:    * @param sizes  the element sizes (<code>null</code> not permitted).
 145:    */
 146:   public void setSizes(int[] sizes)
 147:   {
 148:     this.sizes = (int[]) sizes.clone();
 149:   }
 150: 
 151:   /**
 152:    * Returns an array containing the sizes for all the elements in the sequence.
 153:    * 
 154:    * @return The element sizes.
 155:    */
 156:   public int[] getSizes()
 157:   {
 158:     return (int[]) sizes.clone();
 159:   }
 160: 
 161:   /**
 162:    * Returns the position of the specified element.
 163:    * 
 164:    * @param index  the element index.
 165:    * 
 166:    * @return The position.
 167:    */
 168:   public int getPosition(int index)
 169:   {
 170:     int position;
 171:     int loop;
 172:     position = 0;
 173:     for (loop = 0; loop < index; loop++)
 174:       position += sizes[loop];
 175:     return position;
 176: 
 177:   }
 178: 
 179:   /**
 180:    * Inserts new entries into the sequence at the <code>start</code> position.
 181:    * There are <code>length</code> new entries each having the specified
 182:    * <code>value</code>.
 183:    * 
 184:    * @param start  the start element.
 185:    * @param length  the number of elements to insert.
 186:    * @param value  the size for each of the new elements.
 187:    */
 188:   public void insertEntries(int start, int length, int value)
 189:   {
 190:     int[] newSizes = new int[sizes.length + length];
 191:     System.arraycopy(sizes, 0, newSizes, 0, start);
 192:     for (int i = start; i < start + length; i++)
 193:       newSizes[i] = value;
 194:     System.arraycopy(sizes, start, newSizes, start + length, 
 195:                      sizes.length - start);
 196:     sizes = newSizes;
 197:   }
 198: 
 199:   /**
 200:    * Removes the element(s) at index <code>start</code> (the number of elements
 201:    * removed is <code>length</code>).
 202:    * 
 203:    * @param start  the index of the first element to remove.
 204:    * @param length  the number of elements to remove.
 205:    */
 206:   public void removeEntries(int start, int length)
 207:   {
 208:     // Sanity check.
 209:     if ((start + length) > sizes.length)
 210:       throw new IllegalArgumentException("Specified start/length that "
 211:                                          + "is greater than available sizes");
 212: 
 213:     int[] newSizes = new int[sizes.length - length];
 214:     System.arraycopy(sizes, 0, newSizes, 0, start);
 215:     System.arraycopy(sizes, start + length, newSizes, start, 
 216:                      sizes.length - start - length);
 217:     sizes = newSizes;
 218:   }
 219: }