Thread: [Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/actions ImportFileReader.java, NONE, 1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2007-10-12 08:00:16
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv4080/src/net/sourceforge/bprocessor/gui/actions Modified Files: FileImportActionListener.java Added Files: ImportFileReader.java Removed Files: CsvReader.java Log Message: Refactored all readmechanisms to ImportFileReader and started on implementing obj reader --- CsvReader.java DELETED --- Index: FileImportActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileImportActionListener.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** FileImportActionListener.java 31 Jan 2007 20:45:54 -0000 1.6 --- FileImportActionListener.java 12 Oct 2007 08:00:13 -0000 1.7 *************** *** 20,23 **** --- 20,24 ---- import javax.swing.JLabel; import javax.swing.JPanel; + import javax.swing.filechooser.FileFilter; import net.sourceforge.bprocessor.model.Project; *************** *** 32,39 **** --- 33,62 ---- private static Logger log = Logger.getLogger(FileImportActionListener.class); + private JFileChooser loadChooser = null; + + private CVSinfo info = null; + /** * FileImportActionListener */ public FileImportActionListener() { + loadChooser = new JFileChooser(Project.getInstance().getDefaultPath()); + info = new CVSinfo(loadChooser); + loadChooser.setAccessory(info); + loadChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File f) { + String name = f.getName(); + if (f.isDirectory() || name.endsWith(".csv") || name.endsWith(".obj")) { + return true; + } + return false; + } + + @Override + public String getDescription() { + return "Legal import formats"; + } + }); } *************** *** 43,59 **** */ public void actionPerformed(ActionEvent e) { - JFileChooser loadChooser = new JFileChooser(Project.getInstance().getDefaultPath()); - CVSinfo info = new CVSinfo(loadChooser); - loadChooser.setAccessory(info); int lstate = loadChooser.showOpenDialog(null); File lfile = loadChooser.getSelectedFile(); if (lfile != null && lstate == JFileChooser.APPROVE_OPTION) { ! try { ! CsvReader read = new CsvReader(lfile, Project.getInstance().world()); ! read.importFloor(info.getSelectedFloor()); ! Project.getInstance().changed(Project.getInstance()); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); } } --- 66,87 ---- */ public void actionPerformed(ActionEvent e) { int lstate = loadChooser.showOpenDialog(null); File lfile = loadChooser.getSelectedFile(); if (lfile != null && lstate == JFileChooser.APPROVE_OPTION) { ! if (lfile.getName().endsWith(".csv")) { ! try { ! ImportFileReader.importCsvFile(lfile, Project.getInstance().world(), ! info.getSelectedFloor()); ! Project.getInstance().changed(Project.getInstance()); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); ! } ! } else if (lfile.getName().endsWith(".obj")) { ! try { ! ImportFileReader.importObjFile(lfile, Project.getInstance().world()); ! } catch (Exception ex) { ! log.error("Could not open file: " + lfile, ex); ! } } } *************** *** 64,68 **** * for the users pleasure */ ! public class CVSinfo extends JPanel implements PropertyChangeListener, ActionListener { private File file = null; --- 92,96 ---- * for the users pleasure */ ! private class CVSinfo extends JPanel implements PropertyChangeListener, ActionListener { private File file = null; *************** *** 78,82 **** * @param fc the filechooser to add the additional view to */ ! public CVSinfo(JFileChooser fc) { setPreferredSize(new Dimension(100, 50)); floor = new JComboBox(); --- 106,110 ---- * @param fc the filechooser to add the additional view to */ ! private CVSinfo(JFileChooser fc) { setPreferredSize(new Dimension(100, 50)); floor = new JComboBox(); *************** *** 93,97 **** * @return The chosen floor -1 if all are selected */ ! public int getSelectedFloor() { return selectedFloor; } --- 121,125 ---- * @return The chosen floor -1 if all are selected */ ! private int getSelectedFloor() { return selectedFloor; } *************** *** 114,158 **** update = true; } ! ! if (update && file != null && file.exists() && file.canRead()) { ! try { ! BufferedReader bf; ! FileInputStream fis = new FileInputStream(file); ! InputStreamReader isr = new InputStreamReader(fis); ! bf = new BufferedReader(isr); ! int lines = 0; ! long floors = 0; ! while (bf.ready()) { ! String line = bf.readLine(); ! if (line.charAt(0) == '#') { ! //skip comments ! continue; ! } else { ! lines++; ! //read info from file ! String[] tokens = line.split(";"); ! if (tokens.length < 15) { ! throw new Exception("Wrong file format"); } ! long f = Long.parseLong(tokens[2]); //read out the floor ! floors = floors | (long)Math.pow(2, f); ! } ! } ! ! resetView(); ! ! for (int i = 0; i < 64; i++) { ! long val = (floors >> i) & 1; ! if (val == 1) { ! floor.addItem("" + i); } } ! linesLabel.setText("Lines: " + lines); ! } catch (Exception exc) { ! log.error(exc); ! } ! if (isShowing()) { ! repaint(); } } else { resetView(); --- 142,192 ---- update = true; } ! if (update && file != null) { ! if (file.getName().endsWith(".csv")) { ! linesLabel.setEnabled(true); ! if (file.canRead()) { ! try { ! BufferedReader bf; ! FileInputStream fis = new FileInputStream(file); ! InputStreamReader isr = new InputStreamReader(fis); ! bf = new BufferedReader(isr); ! int lines = 0; ! long floors = 0; ! while (bf.ready()) { ! String line = bf.readLine(); ! if (line.charAt(0) == '#') { ! //skip comments ! continue; ! } else { ! lines++; ! //read info from file ! String[] tokens = line.split(";"); ! if (tokens.length < 15) { ! throw new Exception("Wrong file format"); ! } ! long f = Long.parseLong(tokens[2]); //read out the floor ! floors = floors | (long)Math.pow(2, f); ! } } ! ! resetView(); ! ! for (int i = 0; i < 64; i++) { ! long val = (floors >> i) & 1; ! if (val == 1) { ! floor.addItem("" + i); ! } ! } ! linesLabel.setText("Lines: " + lines); ! } catch (Exception exc) { ! log.error(exc); } } ! } else { ! linesLabel.setEnabled(false); } + } + if (isShowing()) { + repaint(); } else { resetView(); --- NEW FILE: ImportFileReader.java --- //--------------------------------------------------------------------------------- // $Id: ImportFileReader.java,v 1.1 2007/10/12 08:00:14 rimestad Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.actions; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; import org.apache.log4j.Logger; import net.sourceforge.bprocessor.model.Classification; import net.sourceforge.bprocessor.model.CoordinateSystem; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Space; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; /** * A singleton class with stitic methods for loading different file formats into the model */ public class ImportFileReader { private static Logger log = Logger.getLogger(ImportFileReader.class); /** * Importer for the obj format * @param f The file to load * @param into the space to paste it into * @throws IOException a possible file read error */ public static void importObjFile(File f, Space into) throws IOException { if (f.exists()) { FileInputStream stream = new FileInputStream(f); InputStreamReader reader = new InputStreamReader(stream); BufferedReader br = new BufferedReader(reader); LinkedList<Vertex> verts = new LinkedList<Vertex>(); LinkedList<Surface> surfaces = new LinkedList<Surface>(); int lineNum = 0; while (br.ready()) { String line = br.readLine(); lineNum++; String[] lineContent = line.split(" "); if (lineContent[0].equals("v")) { verts.add(new Vertex(Double.parseDouble(lineContent[1]), Double.parseDouble(lineContent[2]), Double.parseDouble(lineContent[3]))); } else if (lineContent[0].equals("f")) { LinkedList<Edge> edges = new LinkedList<Edge>(); Vertex cur = null; Vertex first = verts.get(Integer.parseInt(lineContent[1].split("/")[0])); Vertex prev = first; for (int i = 2; i < lineContent.length; i++) { cur = verts.get(Integer.parseInt(lineContent[i].split("/")[0])); edges.add(new Edge(prev, cur)); prev = cur; } edges.add(new Edge(prev, first)); surfaces.add(new Surface(edges)); } else if (lineContent[0].equals("#")) { continue; } else { log.error("Did not understand content on line " + lineNum + " containing " + line); } } } } /** * Importer for the csv file format * @param file The file to load * @param where The space to paste into * @param chosenFloor The level in the file to load * @throws IOException A possible file read exception */ public static void importCsvFile(File file, Space where, int chosenFloor) throws IOException { if (file.exists()) { BufferedReader bf; FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); bf = new BufferedReader(isr); String current = ""; double x = 0; double y = 0; double z = 0; int curId = 0; int prevId = 0; int spaceid = 0; int surfaceId = -1; int floor = -1; int previousSpaceId = spaceid; int classification = 0; Space currentSpace = null; Surface outerSurface = null; List<Vertex> verts = new ArrayList<Vertex>(); int line = 0; while (bf.ready()) { current = bf.readLine(); line++; StringTokenizer st = new StringTokenizer(current, ";"); if (st.countTokens() == 16) { int curSurfaceId = Integer.parseInt(st.nextToken()); // surface(with holes)id if (surfaceId == -1) { surfaceId = curSurfaceId; } st.nextToken(); // hus floor = Integer.parseInt(st.nextToken()); // etage st.nextToken(); // lejl st.nextToken(); // rum st.nextToken(); // kode y = Double.parseDouble(st.nextToken()); // y x = Double.parseDouble(st.nextToken()); // x z = Double.parseDouble(st.nextToken()); // z st.nextToken(); // tal curId = Integer.parseInt(st.nextToken()); // utal st.nextToken(); // type st.nextToken(); // material st.nextToken(); // billed spaceid = Integer.parseInt(st.nextToken()); // Object_id classification = Integer.parseInt(st.nextToken()); // Object_kode if (floor == chosenFloor || chosenFloor == -1) { if (curId < prevId && !verts.isEmpty()) { // Started on new surface Surface s = addToRead(verts, currentSpace, where, outerSurface); if (outerSurface == null) { // this is the first surface in a face outerSurface = s; } if (surfaceId != curSurfaceId) { // this were the last surface in a face outerSurface = null; surfaceId = curSurfaceId; } } if (spaceid != previousSpaceId) { currentSpace = new Space("Space with id " + spaceid, Space.CONSTRUCTION, Space.SPACE_LEVEL, true); where.add(currentSpace); currentSpace.setClassification(new Classification("Type " + classification, -1)); previousSpaceId = spaceid; } // add the just read vertex to the list of verts for the next surface verts.add(new Vertex(x, y, z)); } else if (!verts.isEmpty()) { // The previous vertex were from a wanted floor Surface s = addToRead(verts, currentSpace, where, outerSurface); if (outerSurface == null) { // this is the first surface in a face outerSurface = s; } if (surfaceId != curSurfaceId) { // this were the last surface in a face outerSurface = null; surfaceId = curSurfaceId; } } // last part of while loop prevId = curId; } else { throw new IOException("Problems in format at line " + line); } } //check if there is something left to read if (!verts.isEmpty()) { addToRead(verts, currentSpace, where, outerSurface); } } else { throw new FileNotFoundException(); } } /** * Make a list of edges out of a list of vertexes * @param verts the vertex list (have to be greater than 2) * @return The list of edges */ private static List<Edge> makeEdgesFromVertexes(List<Vertex> verts) { if (verts.size() > 2) { List<Edge> res = new ArrayList<Edge>(verts.size() + 1); Vertex first = null; Vertex previous = null; Vertex last = null; for (Vertex v : verts) { if (first == null) { first = v; previous = first; } else { res.add(new Edge(previous, v)); previous = v; } last = previous; } res.add(new Edge(last, first)); return res; } else { return null; } } /** * @param verts the vertex list * @param currentSpace the added space * @param where The space to insert into * @param outer If the given vertex list is inside a * surface pass it here otherwise pass null * @return the generated surface */ private static Surface addToRead(List<Vertex> verts, Space currentSpace, Space where, Surface outer) { List<Edge> es = makeEdgesFromVertexes(verts); Surface s = null; if (es != null) { es = where.insert(es); if (es.size() > 2) { s = new Surface(es); if (outer != null) { outer.addHole(s); } CoordinateSystem cs = new CoordinateSystem(new Vertex(1, 0, 0), new Vertex(0, 1, 0), new Vertex(0, 0, 1), new Vertex(0, 0, 0)); int dir = s.direction(cs); if (dir == Surface.LEFT) { s.setBackDomain(currentSpace); } else if (dir == Surface.RIGHT) { s.setFrontDomain(currentSpace); } else { cs = new CoordinateSystem(new Vertex(0, 0, 1), new Vertex(0, 1, 0), new Vertex(1, 0, 0), new Vertex(0, 0, 0)); dir = s.direction(cs); if (dir == Surface.LEFT) { s.setBackDomain(currentSpace); } else if (dir == Surface.RIGHT) { s.setFrontDomain(currentSpace); } else { cs = new CoordinateSystem(new Vertex(1, 0, 0), new Vertex(0, 0, 1), new Vertex(0, 1, 0), new Vertex(0, 0, 0)); dir = s.direction(cs); if (dir == Surface.LEFT) { s.setBackDomain(currentSpace); } else if (dir == Surface.RIGHT) { s.setFrontDomain(currentSpace); } else { log.info("Had to give up assigning"); } } } where.add(s); log.debug("Surface " + s + " had a direction of " + dir); } else { log.warn("There were a edge list with fewer than 3 edges"); } verts.clear(); return s; } else { log.warn("There were " + verts.size() + " vertexes which resulted in a empty edge list"); return s; } } } |