mammothkiosk.MainWindow.java Source code

Java tutorial

Introduction

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

import java.util.ArrayList;
import static java.util.Collections.sort;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

/**
 * The main container form for the application. Holds a Toolbar and the Mammoth
 * Kiosk map. This class is also responsible for parsing bones.xml and creating 
 * all of the individual Bone objects.
 * 
 * @author Jaysen Spurlock
 * @author Paul Blasi
 * @author Joe Manke
 */
public class MainWindow extends javax.swing.JFrame {

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();

        setFilterLists(getBones());

        toolbar1.addToolbarListener(mammothKiosk1);
    }

    /**
     * A method to construct the filter options for the toolbar.
     * 
     * @param bones The list of bones from which to get the filter options.
     */
    private void setFilterLists(List<Bone> bones) {
        mammothKiosk1.setBones(bones);
        List<String> types = toolbar1.TypeElements;
        List<String> genders = toolbar1.GenderElements;
        List<String> sizes = toolbar1.SizeElements;

        for (Bone bone : bones) {
            if (bone.taxon != null && !types.contains(bone.taxon))
                types.add(bone.taxon);
            if (bone.shapelength != null && !sizes.contains(bone.shapelength))
                sizes.add(bone.shapelength);
            if (bone.gender != null && !genders.contains(bone.gender))
                genders.add(bone.gender);
        }

        sort(types);
        sort(sizes);
        sort(genders);

        types.add(0, "<Type>");
        sizes.add(0, "<Size>");
        genders.add(0, "<Gender>");

        toolbar1.setFilterElements();
    }

    /**
     * Parses bones.xml and creates an instance of the Bone class for each bone
     * record found within.
     * 
     * @return the list of Bone objects specified by bones.xml 
     */
    private List<Bone> getBones() {
        List<Bone> temp = new ArrayList<>();

        SAXBuilder saxBuild = new SAXBuilder();
        Document doc;

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

            Element rootElement = doc.getRootElement();
            for (Element bone : rootElement.getChildren("bonerec")) {
                temp.add(new Bone(bone));
            }
        } catch (Exception ex) {
            System.out.println("Exception in Get Bones: " + ex.getMessage());
        }

        return temp;
    }

    /**
     * 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() {

        jSplitPane1 = new javax.swing.JSplitPane();
        toolbar1 = new mammothkiosk.Toolbar();
        jScrollPane1 = new javax.swing.JScrollPane();
        mammothKiosk1 = new mammothkiosk.MammothKiosk();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                onComponentResized(evt);
            }
        });

        jSplitPane1.setEnabled(false);
        jSplitPane1.setLeftComponent(toolbar1);

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setAutoscrolls(true);

        javax.swing.GroupLayout mammothKiosk1Layout = new javax.swing.GroupLayout(mammothKiosk1);
        mammothKiosk1.setLayout(mammothKiosk1Layout);
        mammothKiosk1Layout.setHorizontalGroup(mammothKiosk1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 527, Short.MAX_VALUE));
        mammothKiosk1Layout.setVerticalGroup(mammothKiosk1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

        jScrollPane1.setViewportView(mammothKiosk1);

        jSplitPane1.setRightComponent(jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jSplitPane1));

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

    private void onComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_onComponentResized
        // TODO add your handling code here:
    }//GEN-LAST:event_onComponentResized

    /**
     * Starting point for the program. Creates a new MainWindow which then builds
     * the rest of the user interface.
     * 
     * @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);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private mammothkiosk.MammothKiosk mammothKiosk1;
    private mammothkiosk.Toolbar toolbar1;
    // End of variables declaration//GEN-END:variables
}