/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.hwbot.bench.ui.swing;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JOptionPane;
import org.hwbot.bench.Benchmark;
import org.hwbot.bench.service.BenchService;
import org.hwbot.bench.ui.BenchUI;
/**
*
* @author frederik
*/
public class BenchSwingUI extends javax.swing.JPanel implements BenchUI {
private static final long serialVersionUID = 1L;
private final BenchService benchService;
private JFileChooser fc;
private String title = "HWBOT Benchmark";
private String subtitle = "Multithreaded Prime Bench - ARM/x86 - Win/Mac/Linux";
/**
* Creates new form BenchUI
*
* @param benchBot
*/
public BenchSwingUI(BenchService benchService, String title, String subtitle) {
this.benchService = benchService;
this.title = title;
this.subtitle = subtitle;
initComponents();
}
/**
* 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
startButton = new javax.swing.JButton();
jProgressBar1 = new javax.swing.JProgressBar();
compareButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
processor = new javax.swing.JLabel();
frequency = new javax.swing.JLabel();
threads = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
score = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
startButtonStability = new javax.swing.JButton();
memory = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
fc = new JFileChooser();
jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("HWBOT Prime Benchmark");
jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 12)); // NOI18N
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText(subtitle);
jLabel3.setToolTipText("");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap()));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
jPanel1Layout.createSequentialGroup().addContainerGap()
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
startButton.setBackground(new java.awt.Color(166, 94, 44));
startButton.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
startButton.setForeground(new java.awt.Color(240, 240, 240));
startButton.setText("Quick Benchmark");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonActionPerformed(evt);
}
});
jProgressBar1.setBackground(new java.awt.Color(208, 127, 59));
compareButton.setBackground(new java.awt.Color(19, 89, 122));
compareButton.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
compareButton.setForeground(new java.awt.Color(240, 240, 240));
compareButton.setText("Analyze on HWBOT");
compareButton.setEnabled(false);
compareButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
compareButtonActionPerformed(evt);
}
});
saveButton.setText("Save");
saveButton.setEnabled(false);
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
jLabel2.setText("Processor");
jLabel4.setText("Frequency");
processor.setText("unknown");
processor.setToolTipText("processor identification");
processor.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
processor.setPreferredSize(new java.awt.Dimension(100, 20));
frequency.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
frequency.setText(" ");
frequency.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
frequency.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
threads.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
threads.setText("1");
threads.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
jLabel9.setText("MHz");
jLabel11.setText("Score");
score.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
score.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
score.setText(" ");
score.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
score.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel10.setText("Primes per second");
jLabel5.setText("x");
startButtonStability.setBackground(new java.awt.Color(166, 94, 44));
startButtonStability.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
startButtonStability.setForeground(new java.awt.Color(240, 240, 240));
startButtonStability.setText("Stability Benchmark");
startButtonStability.setToolTipText("");
startButtonStability.setEnabled(false);
startButtonStability.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonStabilityActionPerformed(evt);
}
});
memory.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
memory.setText(" ");
memory.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
memory.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jLabel12.setText("MB mem");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
layout.createSequentialGroup()
.addContainerGap()
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout
.createSequentialGroup()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE).addContainerGap())
.add(layout
.createSequentialGroup()
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, jProgressBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING,
layout.createSequentialGroup()
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jLabel4).add(jLabel11))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(processor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(layout
.createSequentialGroup()
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(layout
.createSequentialGroup()
.add(threads, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(frequency, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(score, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel10)
.add(layout
.createSequentialGroup()
.add(jLabel9)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(memory, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel12))).add(0, 1, Short.MAX_VALUE))))).add(5, 5, 5))
.add(org.jdesktop.layout.GroupLayout.TRAILING,
layout.createSequentialGroup()
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, startButtonStability,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.add(startButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(compareButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
.add(saveButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));
layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
org.jdesktop.layout.GroupLayout.TRAILING,
layout.createSequentialGroup()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout
.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(processor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel4).add(frequency).add(threads).add(jLabel9)
.add(jLabel5).add(memory).add(jLabel12))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel11).add(score).add(jLabel10))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(startButton).add(compareButton))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(startButtonStability).add(saveButton))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
}// </editor-fold>//GEN-END:initComponents
private void startButtonStabilityActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_startButtonStabilityActionPerformed
// TODO add your handling code here:
}// GEN-LAST:event_startButtonStabilityActionPerformed
private void compareButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_compareButtonActionPerformed
benchService.submit();
}// GEN-LAST:event_compareButtonActionPerformed
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
this.startButton.setEnabled(false);
this.compareButton.setEnabled(false);
this.saveButton.setEnabled(false);
this.startButtonStability.setEnabled(false);
this.benchService.benchmark();
}// GEN-LAST:event_jButton1ActionPerformed
private void saveButtonActionPerformed(ActionEvent evt) {
//set filechooser defaults
FileNameExtensionFilter extensions = new FileNameExtensionFilter("HWBOT Scorefiles", "hwbot");
fc.setFileFilter(extensions);
fc.setSelectedFile(new File(score.getText()));
//
int returnVal = fc.showSaveDialog(saveButton);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
// sleep so that filechooser is not screencaped
try {
Thread.sleep(100); //find an appropiate value
} catch(InterruptedException ex) {
//System.out.println("Exception caught");
}
// write xml to this file
try {
this.benchService.saveToFile(file);
JOptionPane.showMessageDialog(this, "Date file saved to: " + file.getAbsolutePath());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Failed to save the file. Sorry! Error: " + e.getMessage());
}
} else {
// cancelled
System.out.println("cancelled save: " + returnVal);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton compareButton;
private javax.swing.JLabel frequency;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JLabel memory;
private javax.swing.JLabel processor;
private javax.swing.JButton saveButton;
private javax.swing.JLabel score;
private javax.swing.JButton startButton;
private javax.swing.JButton startButtonStability;
private javax.swing.JLabel threads;
// End of variables declaration//GEN-END:variables
public javax.swing.JButton getCompareButton() {
return compareButton;
}
public void setCompareButton(javax.swing.JButton compareButton) {
this.compareButton = compareButton;
}
public javax.swing.JLabel getjLabel1() {
return jLabel1;
}
public void setjLabel1(javax.swing.JLabel jLabel1) {
this.jLabel1 = jLabel1;
}
public javax.swing.JLabel getjLabel3() {
return jLabel3;
}
public void setjLabel3(javax.swing.JLabel jLabel3) {
this.jLabel3 = jLabel3;
}
public javax.swing.JPanel getjPanel1() {
return jPanel1;
}
public void setjPanel1(javax.swing.JPanel jPanel1) {
this.jPanel1 = jPanel1;
}
public javax.swing.JProgressBar getjProgressBar1() {
return jProgressBar1;
}
public void setjProgressBar1(javax.swing.JProgressBar jProgressBar1) {
this.jProgressBar1 = jProgressBar1;
}
public javax.swing.JButton getStartButton() {
return startButton;
}
public void setStartButton(javax.swing.JButton startButton) {
this.startButton = startButton;
}
public javax.swing.JLabel getScore() {
return score;
}
public void setScore(javax.swing.JLabel score) {
this.score = score;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public javax.swing.JLabel getFrequency() {
return frequency;
}
public void setFrequency(javax.swing.JLabel frequency) {
this.frequency = frequency;
}
public javax.swing.JLabel getMemory() {
return memory;
}
public void setMemory(javax.swing.JLabel memory) {
this.memory = memory;
}
public javax.swing.JLabel getProcessor() {
return processor;
}
public void setProcessor(javax.swing.JLabel processor) {
this.processor = processor;
}
public javax.swing.JLabel getThreads() {
return threads;
}
public void setThreads(javax.swing.JLabel threads) {
this.threads = threads;
}
public void notifyBenchmarkFinished(Benchmark benchmark) {
this.score.setText(this.benchService.formatScore(benchmark.getScore()));
this.compareButton.setEnabled(true);
this.saveButton.setEnabled(true);
this.startButton.setEnabled(true);
}
public void waitForCommands() {
try {
System.in.read();
} catch (IOException e) {
}
}
@Override
public void notifyError(String message) {
JOptionPane.showMessageDialog(this, message);
}
}