View Javadoc
1 /* 2 * SimpleParallelSpaceModel.java 3 * 4 * Created on 18. November 2001, 20:27 5 * 6 * Copyright 2001 by Flo Ledermann flo@subnet.at 7 * 8 * Licensed under GNU General Public License (GPL). 9 * See http://www.gnu.org/copyleft/gpl.html 10 * 11 */ 12 13 package edu.psu.geovista.app.parvis.model; 14 15 import java.util.*; 16 17 import javax.swing.event.*; 18 19 /*** 20 * Provides a basic implementation of ParallelSpaceModel. Values are stored in a 21 * two-dimensionsl array, labels are stored in arrays. 22 * 23 * @author Flo Ledermann flo@subnet.at 24 * @version 0.1 25 */ 26 public class SimpleParallelSpaceModel implements ParallelSpaceModel { 27 28 /*** Contains the float[] values. */ 29 protected Vector values = new Vector(); 30 31 /*** Contains the record label Strings. */ 32 protected Vector recordLabels = new Vector(); 33 /*** Contains the axis label Strings. */ 34 protected String axisLabels[]; 35 /*** Contains the value label Hashtables. */ 36 protected Hashtable valueLabels[]; 37 38 /*** Number of dimensions of the model. */ 39 protected int numDimensions = 0; 40 41 /*** List to store our event subscribers. */ 42 protected EventListenerList listeners = new EventListenerList(); 43 44 /*** 45 * Default Constructor. 46 */ 47 public SimpleParallelSpaceModel(){ 48 49 } 50 51 /*** 52 * Initializes the model with a given float[][] array of values. 53 * 54 * @param values A float[][] containing records (first index) with float values for each dimension (second index).<br> 55 * All records must have the same number of dimensions! 56 */ 57 public SimpleParallelSpaceModel(float values[][]){ 58 int i; 59 60 int len = values[0].length; 61 62 for (i=0; i<values.length; i++){ 63 if (values[i].length != len) { 64 throw new IllegalArgumentException("Recordsets must have same number of dimensions"); 65 } 66 } 67 68 for (i=0; i<values.length; i++){ 69 this.values.addElement(values[i]); 70 } 71 72 initNumDimensions(len); 73 } 74 75 /*** 76 * Adds a record. The record must have the same number of dimensions as the currently 77 * stored records. 78 * 79 * @param values The float values of the record. 80 * @param label A String label for the record. 81 */ 82 public void addRecord(float values[], String label){ 83 84 if (numDimensions == 0){ 85 initNumDimensions(values.length); 86 } 87 else if (values.length != numDimensions){ 88 throw new IllegalArgumentException("Recordsets must have same number of dimensions (" + numDimensions + ")"); 89 } 90 91 this.values.addElement(values); 92 recordLabels.addElement(label); 93 94 } 95 96 /*** 97 * Adds a record. The record must have the same number of dimensions as the currently 98 * stored records. 99 * 100 * @param values The float values of the record. 101 */ 102 public void addRecord(float values[]){ 103 addRecord(values, null); 104 } 105 106 /*** 107 * Sets up all internal variables for the given number of dimensions. 108 * This must be done only once, usually in the constructor or by the first 109 * addRecord(). After the first call the number of dimensions is fixed and 110 * cannot be changed! 111 * 112 * @param num The number of dimensions. 113 */ 114 protected void initNumDimensions(int num){ 115 116 if (numDimensions != 0){ 117 throw new IllegalArgumentException("Number of Dimensions already set to " + numDimensions + "!"); 118 } 119 120 numDimensions = num; 121 122 axisLabels = new String[num]; 123 valueLabels = new Hashtable[num]; 124 125 for (int i=0; i<num; i++){ 126 axisLabels[i] = null; 127 valueLabels[i] = null; 128 } 129 130 } 131 132 /*** 133 * Returns the number of dimnesions. 134 * 135 * @return The number of dimensions of the records in this model. 136 */ 137 public int getNumDimensions() { 138 return numDimensions; 139 } 140 141 /*** 142 * Returns the number of records. 143 * 144 * @return The number of records currently stored in the model. 145 */ 146 public int getNumRecords() { 147 return values.size(); 148 } 149 150 /*** 151 * Returns the maximum value for the given dimension. 152 * 153 * @return Maximum value of all records for the given dimension. 154 */ 155 public float getMaxValue(int dimension) { 156 157 float maxval = ((float[])values.firstElement())[dimension]; 158 if (Float.isNaN(maxval)){ 159 maxval = -1 * Float.MAX_VALUE; 160 } 161 for (int i=0; i<values.size(); i++){ 162 if (((float[])values.elementAt(i))[dimension] > maxval) maxval = ((float[])values.elementAt(i))[dimension]; 163 } 164 165 return maxval; 166 167 } 168 169 /*** 170 * Returns the minimum value for the given dimension. 171 * 172 * @return Minimum value of all records for the given dimension. 173 */ 174 public float getMinValue(int dimension) { 175 176 float minval = ((float[])values.firstElement())[dimension]; 177 if (Float.isNaN(minval)){ 178 minval = Float.MAX_VALUE; 179 } 180 for (int i=0; i<values.size(); i++){ 181 if (((float[])values.elementAt(i))[dimension] < minval) minval = ((float[])values.elementAt(i))[dimension]; 182 } 183 184 return minval; 185 186 } 187 188 /*** 189 * Returns a specific value of the dataset. 190 * 191 * @param record The number of the record to be queried. 192 * @param dimension The value of the record to be returned. 193 * 194 * @return The value specified by record, dimension. 195 */ 196 public float getValue(int record, int dimension) { 197 return ((float[])values.elementAt(record))[dimension]; 198 } 199 200 /*** 201 * Returns a String label for a specific dimension. 202 * 203 * @param dimension The dimension. 204 * 205 * @return A Human-readable label for the dimension. 206 */ 207 public String getAxisLabel(int dimension) { 208 return axisLabels[dimension]; 209 } 210 211 /*** 212 * Sets the labels for all axes. 213 * Note that this method is not included in the ParallelSpaceModel interface, 214 * which defines only read-only methods. It is used for filling the model 215 * before passing it on to a consumer. 216 * 217 * @param labels An Array of Strings to be used as human-readable labels for the axes. 218 */ 219 public void setAxisLabels(String labels[]){ 220 for (int i=0; i<labels.length; i++){ 221 axisLabels[i] = labels[i]; 222 } 223 } 224 225 /*** 226 * Sets the label of a single axis. 227 * 228 * @param dimension The dimension this label is for. 229 * @String The label. 230 */ 231 public void setAxisLabel(int dimension, String label){ 232 axisLabels[dimension] = label; 233 } 234 235 /*** 236 * Returns a Hashtable with labels for specific values. This is provided for 237 * ordinal values, which might be added as keys to the Hashtable, with the 238 * corresponding human-readable labels as values. 239 * 240 * @param dimension The dimension to retrieve value labels for. 241 * 242 * @return A Hashtable containing value-label pairs. 243 */ 244 public Hashtable getValueLabels(int dimension) { 245 return valueLabels[dimension]; 246 } 247 248 /*** 249 * Returns the label for a single value in a specific dimension, if present. 250 * 251 * @param dimension The dimension. 252 * @param value The value to look up a label for. 253 * 254 * @return A String with the label, null if no label is set. 255 */ 256 public String getValueLabel(int dimension, float value){ 257 if (valueLabels[dimension] != null){ 258 return (String) (valueLabels[dimension].get(new Float(value))); 259 } 260 else { 261 return null; 262 } 263 } 264 265 /*** 266 * Sets the value labels for a dimension. 267 * Note that this method is not included in the ParallelSpaceModel interface, 268 * which defines only read-only methods. It is used for filling the model 269 * before passing it on to a consumer. 270 * 271 * @param dimension The dimension the labels are to be set for. 272 * @param values The values to assign labels to. Note that the number of labels an values must match. 273 * @param labels The String labels for the values. Note that the number of labels an values must match. 274 */ 275 public void setValueLabels(int dimension, float values[], String labels[]){ 276 if (values.length != labels.length) { 277 throw new IllegalArgumentException("number of values and labels do not match!"); 278 } 279 280 if (valueLabels[dimension] == null){ 281 valueLabels[dimension] = new Hashtable(); 282 } 283 284 for (int i=0; i<values.length; i++){ 285 valueLabels[dimension].put(new Float(values[i]), labels[i]); 286 } 287 } 288 289 /*** 290 * Sets a single value label for a specific axis. 291 * 292 * @param dimension The dimension to set the label for. 293 * @param value The value to set the label for. 294 * @param label The label to set. 295 */ 296 public void setValueLabel(int dimension, float value, String label){ 297 298 if (valueLabels[dimension] == null){ 299 valueLabels[dimension] = new Hashtable(); 300 } 301 302 valueLabels[dimension].put(new Float(value), label); 303 304 } 305 306 /*** 307 * Returns all values of a specific record. 308 * 309 * @param record The number of the record to be returned. 310 * 311 * @return All values of the specified record.. 312 */ 313 public float[] getValues(int recordnum) { 314 return (float[])values.elementAt(recordnum); 315 } 316 317 /*** 318 * Subscribes a ChangeListener with the model. 319 * 320 * @param l The ChangeListener to be notified when values change. 321 */ 322 public void addChangeListener(ChangeListener l) { 323 listeners.add(ChangeListener.class, l); 324 } 325 326 /*** 327 * Removes a previously subscribed changeListener. 328 * 329 * @param l The ChangeListener to be removed from the model. 330 */ 331 public void removeChangeListener(ChangeListener l) { 332 listeners.remove(ChangeListener.class, l); 333 } 334 335 /*** 336 * Returns a human-readable label for a specific record. 337 * 338 * @param num The record number. 339 * 340 * @retrun A human-readable label for the record. 341 */ 342 public String getRecordLabel(int num) { 343 return (String)recordLabels.elementAt(num); 344 } 345 346 }

This page was automatically generated by Maven