Java tutorial
/* * Copyright 2012 Devoteam http://www.devoteam.com * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * * This file is part of Multi-Protocol Test Suite (MTS). * * Multi-Protocol Test Suite (MTS) is free software: you can redistribute * it and/or modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 of the * License. * * Multi-Protocol Test Suite (MTS) is distributed in the hope that it will * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Multi-Protocol Test Suite (MTS). * If not, see <http://www.gnu.org/licenses/>. * */ package com.devoteam.srit.xmlloader.gui.frames; import com.devoteam.srit.xmlloader.core.RunProfile; import com.devoteam.srit.xmlloader.core.RunProfile.Point; import java.util.ArrayList; import java.util.HashMap; import org.dom4j.tree.DefaultElement; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.RectangleInsets; /** * * @author gpasquiers */ public class JFrameRunProfile extends javax.swing.JFrame { private static HashMap<RunProfile, JFrameRunProfile> map = new HashMap();; public static JFrameRunProfile getJFrame(RunProfile runProfile) { JFrameRunProfile frame = null; frame = map.get(runProfile); if (null == frame) { frame = new JFrameRunProfile(runProfile); map.put(runProfile, frame); } else { frame.setVisible(false); } return frame; } private RunProfile runProfile; /** Creates new form JFrameRunProfile */ public JFrameRunProfile() { initComponents(); } /** Creates new form JFrameRunProfile */ public JFrameRunProfile(RunProfile runProfile) { initComponents(); init(runProfile); } private void init(RunProfile runProfile) { // special DEV case if (null == runProfile) { try { runProfile = new RunProfile(new DefaultElement("RunProfile")); } catch (Exception e) { e.printStackTrace(); } } this.runProfile = runProfile; refreshGraph(jComboBoxType.getSelectedIndex()); } // 0 is delay // 1 is frequency private void refreshGraph(int type) { ArrayList<Point> vector = runProfile.getPoints(); this.jPanelGraph.removeAll(); if (type == 1) // delay graph { XYSeries series = new XYSeries("Delay"); for (Point point : vector) series.add(point.date / 1000, 1 / point.frequency); XYDataset xyDataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart(null, "date (s)", "delay (s)", xyDataset, PlotOrientation.VERTICAL, false, true, false); ChartPanel panel = new ChartPanel(chart); panel.setPreferredSize(jPanelGraph.getSize()); this.jPanelGraph.add(panel); this.jPanelGraph.validate(); } else // frequency graph { XYSeries series = new XYSeries("Frequency"); for (Point point : vector) series.add(point.date / 1000, point.frequency); XYDataset xyDataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart(null, "date (s)", "frequency (hz)", xyDataset, PlotOrientation.VERTICAL, false, true, false); chart.setPadding(new RectangleInsets(0, 0, 0, 0)); ChartPanel panel = new ChartPanel(chart); panel.setPreferredSize(jPanelGraph.getSize()); this.jPanelGraph.add(panel); this.jPanelGraph.validate(); } } /** 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() { buttonGroupBeginning = new javax.swing.ButtonGroup(); buttonGroupEnding = new javax.swing.ButtonGroup(); jLabel3 = new javax.swing.JLabel(); jComboBoxType = new javax.swing.JComboBox(); jPanelGraph = new javax.swing.JPanel(); jLabel4 = new javax.swing.JLabel(); jTextPane1 = new javax.swing.JTextPane(); jButtonIncrease = new javax.swing.JButton(); jButtonDecrease = new javax.swing.JButton(); jTextFieldIncreasePercent = new javax.swing.JTextField(); jTextFieldDecreasePercent = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButtonReset = new javax.swing.JButton(); jLabel3.setText("jLabel3"); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("run profile editor default title"); setLocationByPlatform(true); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } }); jComboBoxType.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Frequency (hertz)", "Delay (seconds)" })); jComboBoxType.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBoxTypeActionPerformed(evt); } }); jPanelGraph.setPreferredSize(new java.awt.Dimension(300, 190)); jLabel4.setText("Representation of run profile :"); jTextPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background")); jTextPane1.setBorder(null); jTextPane1.setEditable(false); jTextPane1.setText( "This frame allows the editing of the current test or testcase run profile.\n\nBy editing the run profile you can adjust the testcase execution's frequency by a given value.\n"); jButtonIncrease.setLabel("Increase freq. by"); jButtonIncrease.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonIncreaseActionPerformed(evt); } }); jButtonDecrease.setLabel("Decrease freq. by"); jButtonDecrease.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonDecreaseActionPerformed(evt); } }); jTextFieldIncreasePercent.setText("1"); jTextFieldDecreasePercent.setText("1"); jLabel1.setText("tc/s"); jLabel2.setText("tc/s"); jButtonReset.setText("Reset"); jButtonReset.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonResetActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup().addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(layout.createSequentialGroup().add(jLabel4) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jComboBoxType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 157, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanelGraph, 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(org.jdesktop.layout.GroupLayout.TRAILING, jTextPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(jButtonReset, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup().add(layout .createParallelGroup( org.jdesktop.layout.GroupLayout.TRAILING, false) .add(org.jdesktop.layout.GroupLayout.LEADING, jButtonIncrease, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, jButtonDecrease)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout .createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING, false) .add(jTextFieldIncreasePercent) .add(jTextFieldDecreasePercent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout .createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING) .add(jLabel1).add(jLabel2))))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout .createSequentialGroup().addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout .createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel4).add( jComboBoxType, 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(jPanelGraph, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)) .add(layout.createSequentialGroup() .add(jTextPane1, 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(jButtonIncrease) .add(jTextFieldIncreasePercent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel1)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jButtonDecrease) .add(jTextFieldDecreasePercent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel2)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jButtonReset))) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButtonIncreaseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonIncreaseActionPerformed {//GEN-HEADEREND:event_jButtonIncreaseActionPerformed int incr = Integer.parseInt(this.jTextFieldIncreasePercent.getText()); if (incr < 0) incr = 0; this.runProfile.add(incr); refreshGraph(jComboBoxType.getSelectedIndex()); }//GEN-LAST:event_jButtonIncreaseActionPerformed private void jButtonDecreaseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonDecreaseActionPerformed {//GEN-HEADEREND:event_jButtonDecreaseActionPerformed int decr = Integer.parseInt(this.jTextFieldDecreasePercent.getText()); if (decr < 0) decr = 0; this.runProfile.add(-decr); refreshGraph(jComboBoxType.getSelectedIndex()); }//GEN-LAST:event_jButtonDecreaseActionPerformed private void jComboBoxTypeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jComboBoxTypeActionPerformed {//GEN-HEADEREND:event_jComboBoxTypeActionPerformed refreshGraph(jComboBoxType.getSelectedIndex()); }//GEN-LAST:event_jComboBoxTypeActionPerformed private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonResetActionPerformed {//GEN-HEADEREND:event_jButtonResetActionPerformed this.runProfile.reset(); refreshGraph(jComboBoxType.getSelectedIndex()); }//GEN-LAST:event_jButtonResetActionPerformed private void formWindowClosed(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosed {//GEN-HEADEREND:event_formWindowClosed map.remove(this.runProfile); }//GEN-LAST:event_formWindowClosed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup buttonGroupBeginning; private javax.swing.ButtonGroup buttonGroupEnding; private javax.swing.JButton jButtonDecrease; private javax.swing.JButton jButtonIncrease; private javax.swing.JButton jButtonReset; private javax.swing.JComboBox jComboBoxType; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanelGraph; private javax.swing.JTextField jTextFieldDecreasePercent; private javax.swing.JTextField jTextFieldIncreasePercent; private javax.swing.JTextPane jTextPane1; // End of variables declaration//GEN-END:variables }