prog2.Mainwindow.java Source code

Java tutorial

Introduction

Here is the source code for prog2.Mainwindow.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 prog2;

import java.awt.Graphics;
import java.io.IOException;
import org.jdom2.*;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.IOException;
import java.util.*;
import java.io.*;

/**
 * This is the main window of the application.  
 * @author Scott Samson
 */
public class Mainwindow extends javax.swing.JFrame {
    Vector BoneRecs = new Vector();

    /**
     * Creates new form Mainwindow
     */
    public Mainwindow() {
        initComponents();
        BoneRec bone = new BoneRec();
        bone.setPoints(xmlRead("bonexml/walkway.xml"));
        BoneRecs.add(bone);

        fileParse(BoneRecs);

        plotPoints2.SetComponents(BoneRecs, 1026.526813 - 961.478445, 1023.727136 - 974.766754, 0.0, 0, 0, 0.0);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane4 = new javax.swing.JScrollPane();
        plotPoints2 = new prog2.PlotPoints();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPaneTools = new javax.swing.JScrollPane();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        ZoomScrollBar = new javax.swing.JScrollBar();
        DetailScrollBar = new javax.swing.JScrollBar();
        jScrollPaneSearch = new javax.swing.JScrollPane();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        MainwindowMenu1 = new javax.swing.JMenuBar();
        jMenu1File = new javax.swing.JMenu();
        jMenuItemClose = new javax.swing.JMenuItem();
        jMenu3View = new javax.swing.JMenu();
        jMenuItemZoomin = new javax.swing.JMenuItem();
        jMenuItemZoomout = new javax.swing.JMenuItem();
        jMenu4Bookmarks = new javax.swing.JMenu();
        jMenu5Insert = new javax.swing.JMenu();
        jMenu6Selection = new javax.swing.JMenu();
        jMenu7Tools = new javax.swing.JMenu();
        jMenu8Window = new javax.swing.JMenu();
        jMenu9Help = new javax.swing.JMenu();
        jMenu2Edit = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(500, 500));
        setName("mainFrame"); // NOI18N

        jScrollPane4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout plotPoints2Layout = new javax.swing.GroupLayout(plotPoints2);
        plotPoints2.setLayout(plotPoints2Layout);
        plotPoints2Layout.setHorizontalGroup(plotPoints2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 529, Short.MAX_VALUE));
        plotPoints2Layout.setVerticalGroup(plotPoints2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 541, Short.MAX_VALUE));

        jScrollPane4.setViewportView(plotPoints2);

        jTabbedPane1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jTabbedPane1.setMaximumSize(new java.awt.Dimension(300, 500));

        ZoomScrollBar.setMaximum(200);
        ZoomScrollBar.setMinimum(-200);
        ZoomScrollBar.setOrientation(javax.swing.JScrollBar.HORIZONTAL);
        ZoomScrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                ZoomScrollBarAdjustmentValueChanged(evt);
            }
        });

        DetailScrollBar.setOrientation(javax.swing.JScrollBar.HORIZONTAL);
        DetailScrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                DetailScrollBarAdjustmentValueChanged(evt);
            }
        });

        javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
        jLayeredPane1.setLayout(jLayeredPane1Layout);
        jLayeredPane1Layout.setHorizontalGroup(
                jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jLayeredPane1Layout.createSequentialGroup().addContainerGap()
                                .addGroup(jLayeredPane1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(ZoomScrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, 105,
                                                Short.MAX_VALUE)
                                        .addComponent(DetailScrollBar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addContainerGap(68, Short.MAX_VALUE)));
        jLayeredPane1Layout.setVerticalGroup(jLayeredPane1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jLayeredPane1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(ZoomScrollBar, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(DetailScrollBar, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(423, Short.MAX_VALUE)));

        jScrollPaneTools.setViewportView(jLayeredPane1);

        jTabbedPane1.addTab("Tools", jScrollPaneTools);

        javax.swing.GroupLayout jLayeredPane2Layout = new javax.swing.GroupLayout(jLayeredPane2);
        jLayeredPane2.setLayout(jLayeredPane2Layout);
        jLayeredPane2Layout.setHorizontalGroup(jLayeredPane2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 152, Short.MAX_VALUE));
        jLayeredPane2Layout.setVerticalGroup(jLayeredPane2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 533, Short.MAX_VALUE));

        jScrollPaneSearch.setViewportView(jLayeredPane2);

        jTabbedPane1.addTab("Search", jScrollPaneSearch);

        MainwindowMenu1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jMenu1File.setText("File");

        jMenuItemClose.setText("Close");
        jMenu1File.add(jMenuItemClose);

        MainwindowMenu1.add(jMenu1File);

        jMenu3View.setText("View");

        jMenuItemZoomin.setText("Zoom In");
        jMenu3View.add(jMenuItemZoomin);

        jMenuItemZoomout.setText("Zoom Out");
        jMenu3View.add(jMenuItemZoomout);

        MainwindowMenu1.add(jMenu3View);

        jMenu4Bookmarks.setText("Bookmarks");
        MainwindowMenu1.add(jMenu4Bookmarks);

        jMenu5Insert.setText("Insert");
        MainwindowMenu1.add(jMenu5Insert);

        jMenu6Selection.setText("Selection");
        MainwindowMenu1.add(jMenu6Selection);

        jMenu7Tools.setText("Tools");
        MainwindowMenu1.add(jMenu7Tools);

        jMenu8Window.setText("Window");
        MainwindowMenu1.add(jMenu8Window);

        jMenu9Help.setText("Help");
        MainwindowMenu1.add(jMenu9Help);

        jMenu2Edit.setText("Edit");
        MainwindowMenu1.add(jMenu2Edit);

        setJMenuBar(MainwindowMenu1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane4)
                        .addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Gets a new zoom value and then sets up the new components to be drawn.
     * The zoom value is read in from the zoomScrollBar.
     * 
     * @author Scott Samson
     * 
     * @param evt 
     */
    private void ZoomScrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {//GEN-FIRST:event_ZoomScrollBarAdjustmentValueChanged

        if (!ZoomScrollBar.getValueIsAdjusting()) {
            // create a temporary BoneRec
            BoneRec bone = new BoneRec();

            //read in walkway points
            bone.setPoints(xmlRead("bonexml/walkway.xml"));

            // add walkway to BoneRecs list
            BoneRecs.add(bone);

            // get the rest of the BoneRecs from xml
            fileParse(BoneRecs);

            // set new component values 
            plotPoints2.SetComponents(BoneRecs, 1026.526813 - 961.478445, 1023.727136 - 974.766754,
                    evt.getValue() / 10, 0, 0, 0.0);
            plotPoints2.repaint();
        }
    }//GEN-LAST:event_ZoomScrollBarAdjustmentValueChanged

    /**
     * Gets a new detail value and sets up the new components to be drawn.
     * 
     * @author Scott Samson
     * 
     * @param evt 
     */
    private void DetailScrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {//GEN-FIRST:event_DetailScrollBarAdjustmentValueChanged

        if (!DetailScrollBar.getValueIsAdjusting()) {
            // create new BoneRec object
            BoneRec bone = new BoneRec();

            //read in walkway points
            bone.setPoints(xmlRead("bonexml/walkway.xml"));

            // add walkway to list of BoneRecs
            BoneRecs.add(bone);

            // add the rest of the BoneRecs to the list from xml
            fileParse(BoneRecs);

            // set the new components to be displayed
            plotPoints2.SetComponents(BoneRecs, 1026.526813 - 961.478445, 1023.727136 - 974.766754, 0.0, 0, 0,
                    evt.getValue() / 10.0);
            plotPoints2.repaint();
        }
    }//GEN-LAST:event_DetailScrollBarAdjustmentValueChanged

    /**
     * Sets up and runs the main window 
     *
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Mainwindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Mainwindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Mainwindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Mainwindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Mainwindow().setVisible(true);
            }
        });
    }

    /**
     * This function reads the xml files for each BoneRec.  It will insert the 
     * x and y ranges (max - min) in the front of the Points list of the BoneRec.
     * The rest of the points defining BoneRec will then follow.  This function
     * is adapted from example code on the class web site.
     * 
     * @author Scott Samson
     * 
     * @param fileName file of the BoneRec to read from
     * @return Vector list of points to be returned
     */
    public static Vector xmlRead(String fileName) {
        SAXBuilder builder = new SAXBuilder();
        Vector pointsList = new Vector();
        try {
            Document doc = builder.build(fileName); // parse XML tags
            Element root = doc.getRootElement(); // get root of XML tree

            List<Element> children = root.getChildren();

            Iterator iterator = children.iterator();
            List<Element> subChildren = null;
            List<Element> points = null;
            String value = null;
            float x1, y1, x2, y2;
            int a;

            for (int i = 0; i < children.size(); i++) {
                if (children.get(i).getName().equals("shape"))
                    subChildren = children.get(i).getChildren();
            }

            Vector xymin = new Vector();
            Vector xymax = new Vector();

            // loop across subchildren of shape level
            for (int i = 0; i < subChildren.size(); i++) {
                if (subChildren.get(i).getName().equals("polyline")) {
                    points = subChildren.get(i).getChildren();

                    Vector point = new Vector();

                    for (int j = 0; j < points.size(); j++) {
                        value = points.get(j).getValue();
                        a = value.indexOf(" ", 1);

                        point.add(Double.parseDouble(value.substring(0, a)));
                        point.add(Double.parseDouble(value.substring(a, value.length())));
                    }

                    pointsList.add(point);
                } else if (subChildren.get(i).getName().equals("xymin")) {
                    Element point = subChildren.get(i);//.getChildren();
                    value = point.getValue();
                    a = value.indexOf(" ", 1);

                    xymin.add(Double.parseDouble(value.substring(0, a)));
                    xymin.add(Double.parseDouble(value.substring(a, value.length())));
                } else if (subChildren.get(i).getName().equals("xymax")) {
                    Vector xyrange = new Vector();
                    Element point = subChildren.get(i);//.getChildren();
                    value = point.getValue();
                    a = value.indexOf(" ", 1);

                    xymax.add(Double.parseDouble(value.substring(0, a)));
                    xymax.add(Double.parseDouble(value.substring(a, value.length())));

                    xyrange.add((double) xymax.get(0) - (double) xymin.get(0));
                    xyrange.add((double) xymax.get(1) - (double) xymin.get(1));

                    pointsList.add(xyrange);

                }

            }

        }
        // JDOMException indicates a well-formedness error
        catch (JDOMException e) {
            System.out.println("walkway.xml" + " is not well-formed.");
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e);
        }
        return pointsList;
        //
    }

    /**
     * @author Scott Samson
     * 
     * @param BoneRecs 
     */
    public static void fileParse(Vector BoneRecs) {

        // read and parse XML document
        SAXBuilder builder = new SAXBuilder();

        try {
            Document doc = builder.build("bonexml/bones.xml");// parse XML tags
            Element root = doc.getRootElement(); // get root of XML tree
            listChildren(root, 0, BoneRecs); // print info in XML tree
        } catch (JDOMException e) {
            System.out.println("bones.xml" + " is not well-formed.");
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    /**
     * @author Scott Samson
     * 
     * @param current
     * @param depth
     * @param boneRecs 
     */
    public static void listChildren(Element current, int depth, Vector boneRecs) {
        List children = current.getChildren();
        Iterator iterator = children.iterator();
        BoneRec bone = new BoneRec();

        // get next file name
        if (current.getName().equals("uniqueid")) {
            bone.setUID(current.getValue().trim());
            String filename = "bonexml/" + current.getValue().trim() + ".xml";
            // add iterator to get elevation
            // switch on elevation to determine color
            // add color/name to points list ahead of xmlRead call
            //System.out.println(boneRecs.get(boneRecs.size()-1));
            try {
                bone.setPoints(xmlRead(filename));
                boneRecs.add(bone);
            } catch (Exception e) {
                System.out.println(e);
            }

        } else if (current.getName().equals("year")) {
            bone = (BoneRec) boneRecs.elementAt(boneRecs.size() - 1);
            bone.setYear(Integer.parseInt(current.getValue().trim()));
            boneRecs.setElementAt(bone, boneRecs.size() - 1);
        } else if (current.getName().equals("elevation")) {
            bone = (BoneRec) boneRecs.elementAt(boneRecs.size() - 1);
            bone.setElev(Double.parseDouble(current.getValue().trim()));
            boneRecs.setElementAt(bone, boneRecs.size() - 1);
        } else if (current.getName().equals("taxon")) {
            bone = (BoneRec) boneRecs.elementAt(boneRecs.size() - 1);
            bone.setTaxon(current.getValue().trim());
            boneRecs.setElementAt(bone, boneRecs.size() - 1);
        } else if (current.getName().equals("subelement")) {
            bone = (BoneRec) boneRecs.elementAt(boneRecs.size() - 1);
            bone.setSubElement(current.getValue().trim());
            boneRecs.setElementAt(bone, boneRecs.size() - 1);
        } else if (current.getName().equals("completeness")) {
            bone = (BoneRec) boneRecs.elementAt(boneRecs.size() - 1);
            bone.setCompleteness(current.getValue().trim());
            boneRecs.setElementAt(bone, boneRecs.size() - 1);
        } else if (current.getName().equals("gender")) {
            bone = (BoneRec) boneRecs.elementAt(boneRecs.size() - 1);
            bone.setGender(current.getValue().trim());
            boneRecs.setElementAt(bone, boneRecs.size() - 1);
        }

        //bone.print();
        // recursively process each child node
        while (iterator.hasNext()) {
            Element child = (Element) iterator.next();
            listChildren(child, depth + 1, boneRecs);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollBar DetailScrollBar;
    private javax.swing.JMenuBar MainwindowMenu1;
    private javax.swing.JScrollBar ZoomScrollBar;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JMenu jMenu1File;
    private javax.swing.JMenu jMenu2Edit;
    private javax.swing.JMenu jMenu3View;
    private javax.swing.JMenu jMenu4Bookmarks;
    private javax.swing.JMenu jMenu5Insert;
    private javax.swing.JMenu jMenu6Selection;
    private javax.swing.JMenu jMenu7Tools;
    private javax.swing.JMenu jMenu8Window;
    private javax.swing.JMenu jMenu9Help;
    private javax.swing.JMenuItem jMenuItemClose;
    private javax.swing.JMenuItem jMenuItemZoomin;
    private javax.swing.JMenuItem jMenuItemZoomout;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPaneSearch;
    private javax.swing.JScrollPane jScrollPaneTools;
    private javax.swing.JTabbedPane jTabbedPane1;
    private static prog2.PlotPoints plotPoints2;
    // End of variables declaration//GEN-END:variables
}