mammothkiosk.Bone.java Source code

Java tutorial

Introduction

Here is the source code for mammothkiosk.Bone.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mammothkiosk;

import java.awt.geom.Point2D;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * Class defining the Bone data structure.
 * 
 * @author Jaysen Spurlock
 * @author Paul Blasi
 * @author Joe Manke
 */
public class Bone {
    // Retrieved from initial element
    public final String id; // eg 89HS033
    public final String year; // eg 1989
    public final String objectnum; // eg 33
    public final String part; // eg B
    public final String previous; // eg P
    public final String taxon; // eg MAMMUTHUS COLUMBI
    public final String element; // eg 2
    public final String subelement; // eg SKULL UNDIFFERENTIATED
    public final String side; // eg LEFT
    public final String portion; // eg PROXIMAL
    public final String completeness; // eg CO
    public final String expside; // eg VENTRAL
    public final String crack; // *** no example found
    public final String articulate; // eg 92HS104
    public final String gender; // eg UNDESIGNATED
    public final String remarks; // eg Skull seems to be...
    public final String datefound; // eg 1989
    public final String foundby; // (???) eg `
    public final String elevation; // eg -1.491830
    public final String updates; // *** no example found
    public final String mappic; // *** no example found
    public final String fieldnotes; // *** no example found
    public final String removed; // eg 8/19/2008
    public final String removedby; // eg Scott Allen
    public final String azimuth; // *** no example found
    public final String incline; // *** no example found
    public final String labrec; // *** no example found
    public final String objectid; // eg 2349
    public final String shapelength; // eg 25.230144

    // Retrieved from bone shape file
    // nshapes is assumed to be 1
    private Point2D.Float min;
    private Point2D.Float max;
    // id is the same as this.id
    // we do not need npolylines or nvertices
    private List<PolyLine> polylines;

    // Retrieved from bone image file
    private ImageIcon boneImage;

    /**
     * Constructor that takes a JDOM element as an argument. Parses the data from
     * bones.xml found in the element, then parses its specific id.xml file for
     * further data.
     * 
     * @param boneRec The bone record to parse for information
     */
    public Bone(Element boneRec) {
        // Retrieve info from boneRec (bones.xml file)
        Element temp;
        id = (temp = boneRec.getChild("uniqueid")) != null ? temp.getTextTrim() : null;
        year = (temp = boneRec.getChild("year")) != null ? temp.getTextTrim() : null;
        objectnum = (temp = boneRec.getChild("objectnum")) != null ? temp.getTextTrim() : null;
        part = (temp = boneRec.getChild("part")) != null ? temp.getTextTrim() : null;
        previous = (temp = boneRec.getChild("previous")) != null ? temp.getTextTrim() : null;
        taxon = (temp = boneRec.getChild("taxon")) != null ? temp.getTextTrim() : null;
        element = (temp = boneRec.getChild("element")) != null ? temp.getTextTrim() : null;
        subelement = (temp = boneRec.getChild("subelement")) != null ? temp.getTextTrim() : null;
        side = (temp = boneRec.getChild("side")) != null ? temp.getTextTrim() : null;
        portion = (temp = boneRec.getChild("portion")) != null ? temp.getTextTrim() : null;
        completeness = (temp = boneRec.getChild("completeness")) != null ? temp.getTextTrim() : null;
        expside = (temp = boneRec.getChild("expside")) != null ? temp.getTextTrim() : null;
        crack = (temp = boneRec.getChild("crack")) != null ? temp.getTextTrim() : null;
        articulate = (temp = boneRec.getChild("articulate")) != null ? temp.getTextTrim() : null;
        gender = (temp = boneRec.getChild("gender")) != null ? temp.getTextTrim() : null;
        remarks = (temp = boneRec.getChild("remarks")) != null ? temp.getTextTrim() : null;
        datefound = (temp = boneRec.getChild("datefound")) != null ? temp.getTextTrim() : null;
        foundby = (temp = boneRec.getChild("foundby")) != null ? temp.getTextTrim() : null;
        elevation = (temp = boneRec.getChild("elevation")) != null ? temp.getTextTrim() : null;
        updates = (temp = boneRec.getChild("updates")) != null ? temp.getTextTrim() : null;
        mappic = (temp = boneRec.getChild("mappic")) != null ? temp.getTextTrim() : null;
        fieldnotes = (temp = boneRec.getChild("fieldnotes")) != null ? temp.getTextTrim() : null;
        removed = (temp = boneRec.getChild("removed")) != null ? temp.getTextTrim() : null;
        removedby = (temp = boneRec.getChild("removedby")) != null ? temp.getTextTrim() : null;
        azimuth = (temp = boneRec.getChild("azimuth")) != null ? temp.getTextTrim() : null;
        incline = (temp = boneRec.getChild("incline")) != null ? temp.getTextTrim() : null;
        labrec = (temp = boneRec.getChild("labrec")) != null ? temp.getTextTrim() : null;
        objectid = (temp = boneRec.getChild("objectid")) != null ? temp.getTextTrim() : null;
        shapelength = (temp = boneRec.getChild("shapelength")) != null ? temp.getTextTrim() : null;

        min = null;
        max = null;
        polylines = null;

        boneImage = null;

        // Retrieve info for drawing bone from respective bone-shape file
        SAXBuilder saxBuild = new SAXBuilder();
        Document doc;

        try {
            doc = saxBuild.build("../bonexml/" + id + ".xml");

            Element rootElement = doc.getRootElement();
            parseFile(rootElement);
        } catch (Exception ex) {
            System.out.println("Exception in Bone Constructor: " + ex.getMessage());
        }
    }

    /**
     * Parses an xml file for the data not contained in the bones.xml bonerec.
     * This includes the xy min and max coordinates and coordinates for the 
     * polylines to draw the bone.
     * 
     * @param root The root element to parse  
     */
    private void parseFile(Element root) {
        // Grab minimum X/Y values
        String parseMe = root.getChild("xymin").getValue();
        int space = parseMe.substring(1).indexOf(" ");
        min = new Point2D.Float(Float.parseFloat(parseMe.substring(0, space)),
                Float.parseFloat(parseMe.substring(space + 1)));

        // Grab maximum X/Y values
        parseMe = root.getChild("xymax").getValue();
        space = parseMe.substring(1).indexOf(" ");
        max = new Point2D.Float(Float.parseFloat(parseMe.substring(0, space)),
                Float.parseFloat(parseMe.substring(space + 1)));

        // Parse the shape
        Element shape = root.getChild("shape");

        polylines = new ArrayList<>();

        for (Element curElement : shape.getChildren("polyline")) {
            PolyLine polyline = new PolyLine();

            for (Element coord : curElement.getChildren("xy")) {
                space = coord.getValue().substring(1).indexOf(" ");

                polyline.addX(Float.parseFloat(coord.getValue().substring(0, space)));
                polyline.addY(Float.parseFloat(coord.getValue().substring(space + 1)));
            }

            polylines.add(polyline);
        }
    }

    /**
     * Getter for the bone's polylines member.
     * 
     * @return The bone's shape, defined as a list of polylines 
     */
    public List<PolyLine> getShape() {
        return polylines;
    }

    /**
     * Getter for the bone's min member.
     * 
     * @return The minimum (top-left) xy coordinate of the rectangle surrounding
     * the bone.
     */
    public Point2D.Float getMin() {
        return min;
    }

    /**
     * Getter for the bone's max member.
     * 
     * @return The maximum (bottom-right) xy coordinate of the rectangle 
     * surrounding the bone. 
     */
    public Point2D.Float getMax() {
        return max;
    }

    /**
     * Getter for a photograph of the bone. Will attempt to get the image from 
     * a JPEG file if it has not yet been retrieved.
     * 
     * @return A photograph of the bone or an Unavailble icon if the photo cannot
     * be found
     */
    public ImageIcon getImage() {
        if (boneImage == null) {
            try {
                boneImage = new ImageIcon(ImageIO.read(new URL("../bonexml/" + id + ".jpg")));
            } catch (MalformedURLException ex) {
                boneImage = new ImageIcon(getClass().getResource("/images/Unavailable.png"));
            } catch (IOException ex) {
                System.out.println("Exception in Bone.getImage: " + ex.getMessage());
            }
        }

        return boneImage;
    }
}