Projet

Général

Profil

Télécharger (11,7 ko) Statistiques
| Branche: | Tag: | Révision:
/*
* This program is a part of the IoTa project.
*
* Copyright © 2008-2012 Université de Caen Basse-Normandie, GREYC
*
* This program 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, or
* (at your option) any later version.
*
* This program 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.
* <http://www.gnu.org/licenses/>
*
* See AUTHORS for a list of contributors.
*/
package fr.unicaen.iota.validator.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.List;
import javax.swing.*;

/**
*
*/
public class ChartDialog extends JDialog implements ActionListener {

public static final int YES_OPTION = 0;
public static final int CANCEL_OPTION = 1;
private int responseCode = ChartDialog.CANCEL_OPTION;
private JComboBox jComboBox;
private int width = 420;
private int xyHight = 200;
private int bawHight = 200;
private Component configPanel = null;
private List<String> values;
private ChartType chartType = ChartType.XY_CHART;

/** Creates new form ChartDialog */
public ChartDialog(java.awt.Frame parent, boolean modal, List<String> values) {
super(parent, modal);
initComponents();
this.values = values;
initChartTypes();
this.setTitle("Chart configuration");
this.setSize(width, xyHight);
}

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

jPanel3 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
jPanel3.setLayout(new java.awt.BorderLayout());

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chart type:"));
jPanel1.setPreferredSize(new java.awt.Dimension(400, 60));
jPanel1.setLayout(new java.awt.BorderLayout());

jLabel1.setText(" Select a chart Type: ");
jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 5, 1));
jPanel1.add(jLabel1, java.awt.BorderLayout.LINE_START);

jPanel3.add(jPanel1, java.awt.BorderLayout.PAGE_START);

jButton2.setText("Cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel2.add(jButton2);

jButton1.setText("Ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.add(jButton1);

jPanel3.add(jPanel2, java.awt.BorderLayout.PAGE_END);

getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
this.responseCode = ChartDialog.YES_OPTION;
dispose();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
this.responseCode = ChartDialog.CANCEL_OPTION;
dispose();
}//GEN-LAST:event_jButton2ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
// End of variables declaration//GEN-END:variables

/**
* @return the responseCode
*/
public int getResponseCode() {
return responseCode;
}

private void initChartTypes() {
jComboBox = new JComboBox(new String[]{"XY chart", "Bar Chart", "Box and Whisker"});
jComboBox.addActionListener(this);
jComboBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 5, 5));
jPanel1.add(jComboBox, BorderLayout.CENTER);
configPanel = new XYChartPanel(this, values.size());
jPanel3.add(configPanel, BorderLayout.CENTER);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jComboBox)) {
jPanel3.remove(configPanel);
switch (((JComboBox) (e.getSource())).getSelectedIndex()) {
case 0:
configPanel = new XYChartPanel(this, values.size());
jPanel3.add(configPanel, BorderLayout.CENTER);
this.setSize(width, xyHight);
chartType = ChartType.XY_CHART;
break;
case 1:
configPanel = new BarChartPanel(this);
jPanel3.add(configPanel, BorderLayout.CENTER);
this.setSize(width, bawHight);
chartType = ChartType.BAR_CHART;
break;
case 2:
this.setSize(width, bawHight);
chartType = ChartType.BOX_AND_WISKER;
break;
default:
break;
}
jPanel3.updateUI();

}
}

/**
* @return the chartType
*/
public ChartType getChartType() {
return chartType;
}

public int getXYThreshold() {
if (!(configPanel instanceof XYChartPanel)) {
return -1;
}
return ((XYChartPanel) configPanel).getValue();
}

public String getXYParam() {
if (!(configPanel instanceof XYChartPanel)) {
return null;
}
return ((XYChartPanel) configPanel).getParam();
}

public String getIntervals() {
if (!(configPanel instanceof BarChartPanel)) {
return null;
}
return ((BarChartPanel) configPanel).getValue();
}

class XYChartPanel extends JPanel implements ActionListener, ItemListener {

private JRadioButton rb1;
private JRadioButton rb2;
private JComboBox jComboBox1;
private JPanel confPanel;
private HashMap<String, JPanel> typePans = new HashMap<String, JPanel>();
private JPanel linearPanel;
private JPanel adaptivePanel;
private JDialog parent;
private JSpinner threshold;
private JSpinner nbPoints;

public String getParam() {
return (String) (jComboBox1.getSelectedItem());
}

public int getValue() {
return (Integer) ("linear".equals(jComboBox1.getSelectedItem()) ? nbPoints.getValue() : threshold.getValue());
}

public XYChartPanel(JDialog parent, int nbPoints) {
this.setBorder(javax.swing.BorderFactory.createTitledBorder("Chart configuration:"));
this.parent = parent;
this.setLayout(new BorderLayout());
rb1 = new JRadioButton(": print all points");
rb1.setSelected(true);
rb2 = new JRadioButton(": custom points ");
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
rb1.addActionListener(this);
rb2.addActionListener(this);
JPanel pan = new JPanel();
pan.setLayout(new FlowLayout());
pan.add(rb1);
pan.add(rb2);
jComboBox1 = new JComboBox(new String[]{"linear", "adaptive"});
jComboBox1.setEnabled(false);
jComboBox1.addItemListener(this);
pan.add(jComboBox1);

SpinnerModel model = new SpinnerNumberModel(nbPoints, 1, nbPoints, 1);
this.nbPoints = new JSpinner(model);
this.nbPoints.setSize(50, 20);
JLabel label1 = new JLabel("number of points: ");
linearPanel = new JPanel(new FlowLayout());
linearPanel.add(label1);
linearPanel.add(this.nbPoints);
confPanel = new JPanel(new BorderLayout());
confPanel.add(linearPanel, BorderLayout.CENTER);
this.add(pan, BorderLayout.NORTH);

adaptivePanel = new JPanel(new FlowLayout());
SpinnerModel model2 = new SpinnerNumberModel(1, 0, 1000, 1);
threshold = new JSpinner(model2);
threshold.setSize(50, 20);
JLabel label2 = new JLabel("Threshold (ms): ");
adaptivePanel.add(label2);
adaptivePanel.add(threshold);
typePans.put("linear", linearPanel);
typePans.put("adaptive", adaptivePanel);
}
JComponent chk = rb1;

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == rb1) {
if (chk == rb1) {
return;
}
jComboBox1.setEnabled(false);
this.remove(confPanel);
parent.setSize(parent.getWidth(), parent.getHeight() - 32);
this.updateUI();
chk = rb1;
} else if (e.getSource() == rb2) {
if (chk == rb2) {
return;
}
jComboBox1.setEnabled(true);
this.add(confPanel, BorderLayout.CENTER);
parent.setSize(parent.getWidth(), parent.getHeight() + 32);
this.updateUI();
chk = rb2;
}
}

String currentItem = "linear";

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == jComboBox1) {
if (e.getItem().equals(currentItem)) {
return;
}
currentItem = (String) e.getItem();
confPanel.removeAll();
confPanel.add(typePans.get(currentItem), BorderLayout.CENTER);
confPanel.updateUI();
}
}
}

class BarChartPanel extends JPanel {

private JDialog parent;
private JTextField intervals;

public String getValue() {
return intervals.getText();
}

public BarChartPanel(JDialog parent) {
this.setBorder(javax.swing.BorderFactory.createTitledBorder("Chart configuration:"));
this.parent = parent;
this.setLayout(new BorderLayout());
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("intervals: "), BorderLayout.WEST);
intervals = new JTextField();
panel.add(intervals, BorderLayout.CENTER);
panel.add(new JLabel("*"), BorderLayout.EAST);
panel.add(new JLabel("*: values separated with \",\""), BorderLayout.SOUTH);
this.add(panel);
}
}
}
(4-4/30)