📄 inputpanel.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* 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 2 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.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.engine.jwat.workloadAnalysis.panels;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;
import javax.swing.filechooser.FileFilter;
import jmt.engine.jwat.ProgressStatusListener;
import jmt.engine.jwat.workloadAnalysis.tables.JWatVariableInputTable;
import jmt.engine.jwat.workloadAnalysis.tables.JWatVariableInputTableModel;
import jmt.engine.jwat.workloadAnalysis.tables.listeners.RowDeleteListener;
import jmt.engine.jwat.workloadAnalysis.utils.FormatFileReader;
import jmt.engine.jwat.workloadAnalysis.utils.FormatFileWriter;
import jmt.engine.jwat.workloadAnalysis.utils.ModelWorkloadAnalysis;
import jmt.engine.jwat.workloadAnalysis.utils.SteppedComboBox;
import jmt.engine.jwat.workloadAnalysis.wizard.WorkloadAnalysisWizard;
import jmt.gui.common.CommonConstants;
import jmt.gui.help.HoverHelp;
import jmt.gui.jwat.JWATConstants;
import jmt.gui.jwat.JWatWizard;
import jmt.gui.jwat.input.EventFinishAbort;
import jmt.gui.jwat.input.EventFinishLoad;
import jmt.gui.jwat.input.EventStatus;
import jmt.gui.jwat.input.Loader;
import jmt.gui.jwat.input.Parameter;
import jmt.gui.jwat.input.ProgressMonitorShow;
import jmt.gui.wizard.WizardPanel;
/**
* Description: This class rapresents workload analysis tool input panel
*
* @author Brambilla Davide Matr 667986, Fumagalli Claudio 667971
* Class created 30-lug-2006 10.54.14 Darksch
*/
public class InputPanel extends WizardPanel implements CommonConstants,JWATConstants{
//Help strings
private static String ADD_NEW_VARIABLE = "Add new variable";
private static String SPINNER_VARIABLE = "Enter the number of variables";
private static String COMBO_FILE_INPUT = "Select input file";
private static String COMBO_FILTER_INPUT = "Select workload sampling method";
private static String COMBO_FORMAT_INPUT = "Select format for input file";
private static String PANEL_INFORMATION = "Name and observations of selected file";
private static String INPUT_TABLE = "Click or drag to select classes; to edit data single-click and start typing. Right-click for a list of available operations";
// JComboBox filter section texts
private String FILTER_TEXT_COMPLETE = "Complete file";
private String FILTER_TEXT_RANDOM = "Random Sampling";
private String FILTER_TEXT_INTERVAL = "By interval";
// Description of the panel
private final static String INPUT_DESCRIPTION = HTML_START
+ HTML_FONT_TITLE + "Inputs" + HTML_FONT_TIT_END + HTML_FONT_NORM
+ "Define a new input format or open a saved format" + HTML_FONT_NOR_END
+ HTML_END;
// Text of label of log file input
private final static String LOAD_FILE_LABEL = HTML_START + HTML_FONT_NORM
+ "Load file" + HTML_FONT_NOR_END + HTML_END;
// Text of label of format input file
public final static String LOAD_FORMAT_LABEL = HTML_START + HTML_FONT_NORM
+ "Load saved format " + HTML_FONT_NOR_END + HTML_END;
// Text of label of filter on input file
public final static String LOAD_FILTER_LABEL = HTML_START + HTML_FONT_NORM
+ "Choose sampling method " + HTML_FONT_NOR_END + HTML_END;
// Text button save new format
public final static String BUTTON_SAVE_FORMAT = HTML_START + HTML_FONT_NORM
+ "Save format" + HTML_FONT_NOR_END + HTML_END;
//Text button save new format
public final static String LOAD_LOG = HTML_START + HTML_FONT_NORM
+ "LOAD" + HTML_FONT_NOR_END + HTML_END;
// Text information about filtering on variable
public final static String FILTER_NEXT_STEP = HTML_START + HTML_FONT_NORM
+ "<CENTER>For filtering on variable see next step</CENTER>" + HTML_FONT_NOR_END + HTML_END;
// String of browsing for a new file used in input file panel
private static String browseInputPanelText = "Browse...";
// Table used to define or show existing format of a log file ( variables information )
private JWatVariableInputTable inputTable = null;
// Reference to main startscreen
private ModelWorkloadAnalysis model = null;
// Input File panel
private JPanel inputPanel = null;
// File Format panel
private JPanel formatPanel = null;
// Main filtering panel
private JPanel filterPanel = null;
// Information panel
private JPanel infoPanel = null;
private JLabel observationNumb = null;
private JLabel fileName = null;
private static String obsNumbString = "Number of observations: ";
private static String obsFileName = "File name: ";
private int obsNumb = -1;
// Combobox http log files retrived from default directory
//private JComboBoxHorizScroll filechooser = new JComboBoxHorizScroll();
private SteppedComboBox filechooser = new SteppedComboBox(new String[]{browseInputPanelText});
// List of log files in save directory and opened by user
//private ArrayList<File> nameFile = new ArrayList<File>();
private File nameFile;
// ComboBox file format retrived from default format directory
private JComboBox formatchooser = new JComboBox();
private JButton saveNewFormat = null;
// ComboBox filtering possibilities
private JComboBox filterchooser = new JComboBox();
// Main filtering options panel
private JPanel optionFilterPanel;
// Random option panel and spinner element
private JPanel randomOptionPanel = null;
private JSpinner randomNObs = null;
// Interval option panel and Spinner elements from / to observation
private JPanel intervalOptionPanel = null;
private JSpinner intervalFromObs = null;
private JSpinner intervalToObs = null;
// Panel used to organize filter option panel and others organizer panels
private JPanel centerFilterOpt = new JPanel(new BorderLayout(0,5));
private boolean canGoOn=false;
private JButton loadFileBtn = null;
private boolean loadOnRun = false;
//Aggiunta 19/10/2006
private JSpinner numOfVars = null;
private JButton addVar = null;
private HoverHelp help = null;
private ChangeListener spinnerListener = new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
/** Azzero format combo box **/
if(((Integer)numOfVars.getValue()).intValue() == 0 || inputTable.getRowCount() == 0){
formatchooser.setSelectedIndex(-1);
}
/** Rimozione variabile da spinner **/
if(((Integer)numOfVars.getValue()).intValue() < inputTable.getRowCount() && inputTable.getRowCount() > 0){
((JWatVariableInputTableModel) inputTable.getModel()).deleteRow(inputTable.getRowCount()-1);
inputTable.tableChanged(new TableModelEvent(inputTable.getModel()));
// Set formatchooser to none
formatchooser.setSelectedIndex(-1);
return;
}
/** Aggiunta variabile da spinner **/
if(((Integer)numOfVars.getValue()).intValue() > inputTable.getRowCount()){
addVariable();
}
}
};
protected AbstractAction loadFile = new AbstractAction(LOAD_LOG) {
{ putValue(Action.SHORT_DESCRIPTION, "Load Data from file"); }
// Adds a new row to inputTable and update varchooser comboBox used to show list of variables on which can be applied a filter
public void actionPerformed(ActionEvent arg0) {
if (!((JWatVariableInputTableModel) inputTable.getModel()).checkInfos()) {
JOptionPane.showMessageDialog(InputPanel.this,"Some fileds of variable table are not correctly setted or are left blank");
return;
}
if(nameFile == null){
JOptionPane.showMessageDialog(InputPanel.this,"You have no selected any input file, please select one and then retry");
return;
}
if(!loadOnRun){
loadOnRun = true;
//Recupero il table model di inut
JWatVariableInputTableModel tableModel = ((JWatVariableInputTableModel) inputTable.getModel());
Parameter fileParameter = tableModel.getParameter();
//Controllo se necessario creare il filtering
if(((String)filterchooser.getModel().getSelectedItem()).equals(FILTER_TEXT_COMPLETE)){
try {
fileParameter.setOption(new int[]{Loader.calcNumOfObs((nameFile.getAbsolutePath())),((SpinnerNumberModel)randomNObs.getModel()).getNumber().intValue()});
} catch (Exception e) {
e.printStackTrace();
}
}
if(((String)filterchooser.getModel().getSelectedItem()).equals(FILTER_TEXT_RANDOM)){
// OPTION PER RANDOM
fileParameter.setSampleMethod(Parameter.RANDOM_INPUT);
try {
fileParameter.setOption(new int[]{Loader.calcNumOfObs(nameFile.getAbsolutePath()),((SpinnerNumberModel)randomNObs.getModel()).getNumber().intValue()});
} catch (Exception e) { e.printStackTrace(); }
}
if(((String)filterchooser.getModel().getSelectedItem()).equals(FILTER_TEXT_INTERVAL)){
// OPTION PER INTERVAL
fileParameter.setSampleMethod(Parameter.INTERVAL_INPUT);
fileParameter.setOption(new int[]{((SpinnerNumberModel)intervalFromObs.getModel()).getNumber().intValue(),((SpinnerNumberModel)intervalToObs.getModel()).getNumber().intValue()});
}
//Chiamata a loader
try {
Loader.readData(nameFile.getAbsolutePath(),fileParameter,new ProgressMonitorShow(InputPanel.this,"Loading Data...",1000),new InputStatusListener());
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
// Action applied to add new variable button used to add a variable definition to inputTable
protected AbstractAction addNewVariable = new AbstractAction("New var") {
{
putValue(Action.SHORT_DESCRIPTION, ADD_NEW_VARIABLE);
}
// Adds a new row to inputTable and update varchooser comboBox used to show list of variables on which can be applied a filter
public void actionPerformed(ActionEvent arg0) {
addVariable();
}
};
private void addVariable(){
// Adds new row to table
((JWatVariableInputTableModel) inputTable.getModel()).addNewRow();
// Notify to inputTable changes
inputTable.tableChanged(new TableModelEvent(inputTable.getModel()));
// Update Spinner
numOfVars.setValue(new Integer(inputTable.getRowCount()));
// Refresh values in varchooser comboBox reset value to null and clean option panel
centerFilterOpt.removeAll();
// Set formatchooser to none
formatchooser.setSelectedIndex(-1);
}
// Add save new format Action, this action saves new format in format directory
protected AbstractAction saveFormat = new AbstractAction(BUTTON_SAVE_FORMAT) {
{ putValue(Action.SHORT_DESCRIPTION, "Save current defined format"); }
// Removes all rows from inputTable and update varchooser comboBox and option panel
public void actionPerformed(ActionEvent arg0) {
// Se esistono dei valori li salvo
if(((JWatVariableInputTableModel) inputTable.getModel()).getSize() > 0){
if(formatchooser.getSelectedIndex() >= 0){
int response = JOptionPane.showConfirmDialog (null,
"Overwrite existing file?","Confirm Overwrite",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(response == JOptionPane.YES_OPTION){
File file = new File(absolutePath + "jwatFormats/" + formatchooser.getSelectedItem().toString());
JWatVariableInputTableModel mod = ((JWatVariableInputTableModel) inputTable.getModel());
new FormatFileWriter(mod.getNames(),mod.getComments(),mod.getDelimiters(),mod.getRegExpr(),
mod.getTypes(),file.getAbsolutePath(),mod.getDefaults(),mod.getReplaces());
refreshComboFormatPanel();
formatchooser.setSelectedItem(file.getName());
}else{
refreshComboFormatPanel();
}
}else{
JFileChooser file = new JFileChooser(new File(absolutePath + "jwatFormats"));
//file.setFileSystemView(new SingleDirFileSystem(new File(absolutePath + "jwatFormats")));
file.setAcceptAllFileFilterUsed(false);
file.setFileFilter(new FileFilter(){
public boolean accept(File f) {
if(f.getName().indexOf(".jwatformat") != -1){
return true;
}
return false;
}
public String getDescription() {
return "jWAT format";
}
});
file.setApproveButtonText("Save");
file.setFileSelectionMode(JFileChooser.FILES_ONLY);
if(file.showOpenDialog(InputPanel.this) == JFileChooser.APPROVE_OPTION){
File fFile = file.getSelectedFile ();
if (fFile.exists ()) {
int response = JOptionPane.showConfirmDialog (null,
"Overwrite existing file?","Confirm Overwrite",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION){
return;
}
}
JWatVariableInputTableModel mod = ((JWatVariableInputTableModel) inputTable.getModel());
if(fFile.getName().indexOf(".jwatformat") == -1){
fFile = new File(fFile.getAbsolutePath() + ".jwatformat");
}
new FormatFileWriter(mod.getNames(),mod.getComments(),mod.getDelimiters(),mod.getRegExpr(),
mod.getTypes(),fFile.getAbsolutePath(),mod.getDefaults(),mod.getReplaces());
refreshComboFormatPanel();
formatchooser.setSelectedItem(file.getSelectedFile().getName());
}else{
JOptionPane.showMessageDialog(null,"No file choosen. Please try again",
"Information",JOptionPane.INFORMATION_MESSAGE);
refreshComboFormatPanel();
}
}
}else{
JOptionPane.showMessageDialog(null,"There are no variables defined. Please choose format and then clcik save button",
"Information",JOptionPane.INFORMATION_MESSAGE);
refreshComboFormatPanel();
}
}
};
private WorkloadAnalysisWizard parent;
/**
* Creates and sets up a workload analysis input panel. This panels contains all necessary information to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -