View Javadoc
1 package edu.psu.geovista.data.geog; 2 3 /* ------------------------------------------------------------------- 4 GeoVISTA Center (Penn State, Dept. of Geography) 5 Java source file for the class ShapeFileDataReader 6 Copyright (c), 2002, GeoVISTA Center 7 All Rights Reserved. 8 Original Author: Xiping Dai 9 $Author: hardisty $ 10 $Id: CSVFileDataReader.java,v 1.1 2003/04/25 17:44:57 hardisty Exp $ 11 $Date: 2003/04/25 17:44:57 $ 12 Reference: Document no: 13 ___ ___ 14 ------------------------------------------------------------------- * 15 */ 16 17 import java.awt.event.*; 18 import java.io.*; 19 20 import javax.swing.event.*; 21 22 import edu.psu.geovista.io.csv.*; 23 24 /*** 25 * Takes a file name and returns an Object[] with: 26 * Object[0] = names of variables 27 * 0bject[1] = data (double[], int[], or String[]) 28 * 0bject[1] = data (double[], int[], or String[]) 29 * ... 30 * Object[n-1] = the shapefile data 31 * 32 * also see DBaseFile, ShapeFile 33 * 34 */ 35 36 import edu.psu.geovista.ui.event.*; 37 38 public class CSVFileDataReader { 39 40 public static final String COMMAND_DATA_SET_MADE = "dataMade"; 41 42 private DataSetForApps dataForApps; 43 private String fileName; 44 private EventListenerList listenerList; 45 46 public CSVFileDataReader() { 47 listenerList = new EventListenerList(); 48 } 49 50 51 private DataSetForApps makeDataSetForApps(String fileName){ 52 53 Class cl = this.getClass(); 54 String csvFileName = fileName + ".csv"; 55 //InputStream isCSV = cl.getResourceAsStream("resources/states48.csv"); 56 Object[] csvData = null; 57 58 try { 59 60 GeogCSVReader csv = new GeogCSVReader(); 61 FileInputStream inStream = new FileInputStream(csvFileName); 62 csvData = csv.readFile(inStream); 63 //shpData = new Object[dbData.length + 1]; 64 //for (int i = 0; i < dbData.length; i++) { 65 // shpData[i] = dbData[i]; 66 //} 67 //shpData[dbData.length] = new ShapeFile(fileName + ".shp"); 68 } catch (Exception ex) { 69 ex.printStackTrace(); 70 } 71 72 //this.fireActionPerformed(COMMAND_DATA_SET_MADE); 73 this.dataForApps = new DataSetForApps(); 74 dataForApps.setDataObject(csvData); 75 return dataForApps; 76 } 77 78 private Object[] makeDataSet(String fileName){ 79 this.makeDataSetForApps(fileName); 80 return dataForApps.getDataObjectOriginal(); 81 } 82 83 private String removeExtension(String fileName){ 84 String removed = fileName; 85 int index = fileName.lastIndexOf("."); 86 if (index > -1) { //if it was found 87 int len = fileName.length(); 88 removed = fileName.substring(0,index); 89 } 90 return removed; 91 } 92 93 public void setDataForApps (DataSetForApps dataForApps) { 94 this.dataForApps = dataForApps; 95 } 96 public DataSetForApps getDataForApps() { 97 return this.dataForApps; 98 } 99 100 public Object[] getDataSet() { 101 //System.out.print("get data set..."); 102 return this.dataForApps.getDataObjectOriginal(); 103 } 104 105 public void setFileName (String fileName) { 106 this.fileName = fileName; 107 this.fileName = this.removeExtension(fileName); 108 //System.out.println(fileName); 109 this.dataForApps = this.makeDataSetForApps(this.fileName); 110 this.fireActionPerformed(COMMAND_DATA_SET_MADE); 111 this.fireDataSetChanged(this.dataForApps.getDataObjectOriginal()); 112 //for (int i = 0; i < dataSet.length; i++) { 113 // System.out.println(dataSet[i]); 114 //} 115 } 116 public String getFileName () { 117 return this.fileName; 118 } 119 120 private void writeObject(ObjectOutputStream oos) throws IOException { 121 oos.defaultWriteObject(); 122 } 123 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { 124 ois.defaultReadObject(); 125 } 126 /*** 127 * implements ActionListener 128 */ 129 public void addActionListener(ActionListener l) { 130 listenerList.add(ActionListener.class, l); 131 } 132 133 /*** 134 * removes an ActionListener from the button 135 */ 136 public void removeActionListener(ActionListener l) { 137 listenerList.remove(ActionListener.class, l); 138 } 139 140 /*** 141 * Notify all listeners that have registered interest for 142 * notification on this event type. The event instance 143 * is lazily created using the parameters passed into 144 * the fire method. 145 * @see EventListenerList 146 */ 147 protected void fireActionPerformed(String command) { 148 // Guaranteed to return a non-null array 149 Object[] listeners = listenerList.getListenerList(); 150 ActionEvent e = null; 151 // Process the listeners last to first, notifying 152 // those that are interested in this event 153 for (int i = listeners.length - 2; i >= 0; i -= 2) { 154 if (listeners[i] == ActionListener.class) { 155 // Lazily create the event: 156 if (e == null) { 157 e = new ActionEvent(this, 158 ActionEvent.ACTION_PERFORMED, 159 command); 160 } 161 ((ActionListener)listeners[i + 1]).actionPerformed(e); 162 } 163 } 164 } 165 166 /*** 167 * implements DataSetListener 168 */ 169 public void addDataSetListener(DataSetListener l) { 170 listenerList.add(DataSetListener.class, l); 171 } 172 173 /*** 174 * removes an DataSetListener from the button 175 */ 176 public void removeDataSetListener(DataSetListener l) { 177 listenerList.remove(DataSetListener.class, l); 178 } 179 180 /*** 181 * Notify all listeners that have registered interest for 182 * notification on this event type. The event instance 183 * is lazily created using the parameters passed into 184 * the fire method. 185 * @see EventListenerList 186 */ 187 protected void fireDataSetChanged(Object[] dataSet) { 188 //System.out.println("ShpToShp.fireDataSetChanged, Hi!!"); 189 // Guaranteed to return a non-null array 190 Object[] listeners = listenerList.getListenerList(); 191 DataSetEvent e = null; 192 // Process the listeners last to first, notifying 193 // those that are interested in this event 194 for (int i = listeners.length - 2; i >= 0; i -= 2) { 195 if (listeners[i] == DataSetListener.class) { 196 // Lazily create the event: 197 if (e == null) { 198 e = new DataSetEvent(this,dataSet); 199 200 } 201 ((DataSetListener)listeners[i + 1]).dataSetChanged(e); 202 } 203 } 204 } 205 206 }

This page was automatically generated by Maven