Java tutorial
/* * 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 org.evors.rs.ui.frames; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.RowSorter; import javax.swing.SortOrder; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; import org.apache.commons.io.FileUtils; import org.evors.genesis.Genotype; import org.evors.rs.genesis.RobotExperiment; import org.evors.rs.sim.robot.ctrnn.io.JSONCTRNNLayout; import org.evors.rs.ui.frames.TrialViewerFrame; import org.evors.rs.ui.utils.UIUtils; import org.evors.genesis.JSONPopulation; import org.evors.rs.ui.utils.PopulationTM; /** * * @author miles */ public class PopulationViewer extends javax.swing.JInternalFrame { private final JPopupMenu menu; private String currentFileName = ""; private RobotExperiment exp; JMenuItem miTrial; /** * Creates new form PopulationViewer */ public PopulationViewer() { initComponents(); menu = new JPopupMenu(); miTrial = new JMenuItem("View trial..."); miTrial.setEnabled(false); miTrial.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { TrialViewerFrame tvf = new TrialViewerFrame(); tvf.setVisible(true); JSONPopulation pop = ((PopulationTM) jTable1.getModel()).getPop(); tvf.loadSimulation(exp, Genotype.withGenes(pop.getIndividuals()[jTable1.getSelectedRow()].getGenes())); getParent().add(tvf); } }); menu.add(miTrial); } public String getCurrentFileName() { return currentFileName; } public void setCurrentFileName(String currentFileName) { this.currentFileName = currentFileName; } public void loadJSONPopulation(RobotExperiment exp, JSONPopulation jpop) { this.exp = exp; jTable1.setModel(new PopulationTM(jpop)); sortByFitness(); miTrial.setEnabled(true); } public void showOpenDialog() { JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(UIUtils.getUserDir("/user/")); int result = fc.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { try { loadJSONPopulation(RobotExperiment.fromDirectory(fc.getSelectedFile().getParent()), JSONPopulation.fromJSONFile(fc.getSelectedFile())); } catch (IOException ex) { System.out.println(ex.toString()); } } else { this.dispose(); } } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); setClosable(true); setMaximizable(true); setResizable(true); setTitle("View population (right click a row for options)"); setDoubleBuffered(true); addInternalFrameListener(new javax.swing.event.InternalFrameListener() { public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) { formInternalFrameOpened(evt); } public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) { } }); jTable1.setAutoCreateRowSorter(true); jTable1.setModel(new PopulationTM()); jTable1.setRowHeight(30); jTable1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTable1MouseClicked(evt); } }); jScrollPane1.setViewportView(jTable1); jMenu1.setText("File"); jMenuItem1.setText("Open..."); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuItem2.setText("Save..."); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu1.add(jMenuItem2); jMenuBar1.add(jMenu1); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened }//GEN-LAST:event_formInternalFrameOpened private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked int row = jTable1.rowAtPoint(evt.getPoint()); if (!jTable1.isRowSelected(row)) { jTable1.changeSelection(row, 1, false, false); } menu.show(jTable1, evt.getX(), evt.getY()); }//GEN-LAST:event_jTable1MouseClicked private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed showOpenDialog(); }//GEN-LAST:event_jMenuItem1ActionPerformed private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(UIUtils.getUserDir("/user/")); int result = fc.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { PrintWriter out = null; try { FileUtils.copyFile(new File(exp.getLayout().getFilename()), new File(fc.getSelectedFile().getParent() + "layout.json")); FileUtils.copyFile(new File(exp.getWorld().getFilename()), new File(fc.getSelectedFile().getParent() + "world.json")); JSONPopulation pop = ((PopulationTM) jTable1.getModel()).getPop(); out = new PrintWriter(fc.getSelectedFile()); out.print(pop.toJSONString()); } catch (FileNotFoundException ex) { Logger.getLogger(PopulationViewer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PopulationViewer.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } } }//GEN-LAST:event_jMenuItem2ActionPerformed private void sortByFitness() { TableRowSorter<TableModel> sorter = new TableRowSorter<>(jTable1.getModel()); jTable1.setRowSorter(sorter); List<RowSorter.SortKey> sortKeys = new ArrayList<>(); int columnIndexToSort = 1; sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); sorter.sort(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenu jMenu1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }