package uk.ac.leeds.ccg.cluster; /** Class: TimeParametersViewer * Function: To display and modify TimeParameters */ import java.io.*; import java.awt.*; import java.awt.event.*; import uk.ac.leeds.ccg.geotools.misc.*; public class TimeParametersViewer extends ParametersViewer implements ActionListener{ // Members File file; //FilenameFilter filenameFilter; FileFil fileFil; FileDialog fileDialog; TimeParameters parameters; Label label; // These Textfield, Double Choice and checkboxes allow for the // setting of TimeParameters from the TimeParametersViewer Double thresh,numBin,minTime,maxTime; TextField threshTextField,numBinTextField,minTimeTextField,maxTimeTextField; Checkbox animateCheckbox; MenuBar menuBar; Menu fileMenu; MenuItem loadTimeParametersMenuItem,saveTimeParametersMenuItem,closeMenuItem; Container parametersContainer,buttonContainer; GridBagLayout gridBagLayout; GridBagConstraints gridBagConstraints; Button applyButton,defaultButton,significanceTestTimeParametersButton; Frame frame; String actionCommand; boolean alternator=true; // Constructors public TimeParametersViewer(TimeParameters parameters){ this.parameters = parameters; animateCheckbox = new Checkbox(); animateCheckbox.setState(parameters.getAnimate()); animateCheckbox.setLabel("Animate"); //threshTextField = new TextField(""+parameters.getSignificanceThreshold()); threshTextField = new TextField(""+FormatedString.format(""+parameters.getSignificanceThreshold(),4).trim()); numBinTextField = new TextField(""+parameters.getNumBin()); minTimeTextField = new TextField(""+parameters.getStartTime()); maxTimeTextField = new TextField(""+parameters.getEndTime()); } // Methods public static void main(String args[]) throws IOException,FileNotFoundException{ TimeParametersViewer pv = new TimeParametersViewer(new TimeParameters()); pv.run(); } public void run(){ initComponents(); setDisplay(); } public void initComponents(){ // Set menu bar menuBar = new MenuBar(); fileMenu = new Menu(); fileMenu.setLabel("File"); loadTimeParametersMenuItem = new MenuItem(); loadTimeParametersMenuItem.addActionListener(this); loadTimeParametersMenuItem.setActionCommand("load parameters"); loadTimeParametersMenuItem.setLabel("Load TimeParameters"); fileMenu.add(loadTimeParametersMenuItem); saveTimeParametersMenuItem = new MenuItem(); saveTimeParametersMenuItem.addActionListener(this); saveTimeParametersMenuItem.setActionCommand("save parameters"); saveTimeParametersMenuItem.setLabel("Save TimeParameters"); fileMenu.add(saveTimeParametersMenuItem); closeMenuItem = new MenuItem(); closeMenuItem.addActionListener(this); closeMenuItem.setActionCommand("close"); closeMenuItem.setLabel("Close"); fileMenu.add(closeMenuItem); menuBar.add(fileMenu); // Display parametersContainer gridBagLayout = new GridBagLayout(); gridBagConstraints = new GridBagConstraints(); parametersContainer = new Container(); parametersContainer.setLayout(gridBagLayout); // TimeParameter thresh label = new Label("Significance Threshold"); gridBagConstraints.anchor=GridBagConstraints.EAST; gridBagConstraints.gridwidth=GridBagConstraints.RELATIVE; gridBagLayout.setConstraints(label,gridBagConstraints); parametersContainer.add(label); threshTextField.setText(""+FormatedString.format(""+parameters.getSignificanceThreshold(),4)); gridBagConstraints.anchor=GridBagConstraints.WEST; gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(threshTextField,gridBagConstraints); parametersContainer.add(threshTextField); // TimeParameter numBin label = new Label("Number of bins"); gridBagConstraints.anchor=GridBagConstraints.EAST; gridBagConstraints.gridwidth=GridBagConstraints.RELATIVE; gridBagLayout.setConstraints(label,gridBagConstraints); parametersContainer.add(label); numBinTextField.setText(""+parameters.getNumBin()); gridBagConstraints.anchor=GridBagConstraints.WEST; gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(numBinTextField,gridBagConstraints); parametersContainer.add(numBinTextField); // TimeParameter minTime label = new Label("Earliest (start) time"); gridBagConstraints.anchor=GridBagConstraints.EAST; gridBagConstraints.gridwidth=GridBagConstraints.RELATIVE; gridBagLayout.setConstraints(label,gridBagConstraints); parametersContainer.add(label); minTimeTextField.setText(""+parameters.getStartTime()); gridBagConstraints.anchor=GridBagConstraints.WEST; gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(minTimeTextField,gridBagConstraints); parametersContainer.add(minTimeTextField); // TimeParameter maxTime label = new Label("Latest (finish) time"); gridBagConstraints.anchor=GridBagConstraints.EAST; gridBagConstraints.gridwidth=GridBagConstraints.RELATIVE; gridBagLayout.setConstraints(label,gridBagConstraints); parametersContainer.add(label); maxTimeTextField.setText(""+parameters.getEndTime()); gridBagConstraints.anchor=GridBagConstraints.WEST; gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(maxTimeTextField,gridBagConstraints); parametersContainer.add(maxTimeTextField); // Set buttonContainer and checkbox buttonContainer = new Container(); buttonContainer.setLayout(gridBagLayout); applyButton = new Button(); applyButton.setLabel("Apply"); applyButton.addActionListener(this); applyButton.setActionCommand("apply"); buttonContainer.add(applyButton); defaultButton = new Button(); defaultButton.setLabel("Default"); defaultButton.addActionListener(this); defaultButton.setActionCommand("default"); buttonContainer.add(defaultButton); // significanceTestTimeParametersButton = new Button(); // significanceTestTimeParametersButton.setLabel("Time Series Parameters"); // significanceTestTimeParametersButton.addActionListener(this); // significanceTestTimeParametersButton.setActionCommand("significance test parameters"); // buttonContainer.add(significanceTestTimeParametersButton); animateCheckbox.setState(parameters.getAnimate()); buttonContainer.add(animateCheckbox); } public void setDisplay(){ // Set frame frame = new Frame("TimeParameters"); frame.setMenuBar(getMenuBar()); frame.setLayout(gridBagLayout); // Add buttonContainer gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(buttonContainer,gridBagConstraints); frame.add(buttonContainer,frame.getComponentCount()); gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(parametersContainer,gridBagConstraints ); frame.add(parametersContainer); frame.pack(); frame.setVisible(true); } public MenuBar getMenuBar(){return menuBar;} public void parseValues(){ parameters.setAnimate(animateCheckbox.getState()); thresh = new Double(threshTextField.getText()); parameters.setSignificanceThreshold(thresh.doubleValue()); numBin = new Double(numBinTextField.getText()); parameters.setNumBin((int)numBin.doubleValue()); minTime = new Double(minTimeTextField.getText()); parameters.setStartTime(minTime.doubleValue()); maxTime = new Double(maxTimeTextField.getText()); parameters.setEndTime(maxTime.doubleValue()); } public void setText(){ animateCheckbox.setState(parameters.getAnimate()); threshTextField.setText(""+FormatedString.format(""+parameters.getSignificanceThreshold(),4).trim()); numBinTextField.setText(""+parameters.getNumBin()); minTimeTextField.setText(""+parameters.getStartTime()); maxTimeTextField.setText(""+parameters.getEndTime()); } public void actionPerformed(ActionEvent actionEvent){ actionCommand = new String(actionEvent.getActionCommand()); // Action command name // Menu actions if(actionCommand.equals("load parameters")){ System.out.println("Load TimeParameters File"); fileDialog = new FileDialog(new Frame()); fileFil = new FileFil(fileDialog,".parameters"); fileDialog.setTitle("Load TimeParameters File"); fileDialog.setFilenameFilter(fileFil); fileDialog.setVisible(true); if (fileDialog.getFile()==null) {return;} file = new File(fileDialog.getDirectory(),fileDialog.getFile()); try{ parameters = new TimeParameters(file); System.out.println(parameters.getAnimate()); setText(); parseValues(); //dbase.load(file); } catch(IOException error){ System.err.println("Error "+error+ "loading file "+file); return; } } if(actionCommand.equals("save parameters")){ System.out.println("Save TimeParameters File"); fileDialog = new FileDialog(new Frame()); fileFil = new FileFil(fileDialog,".parameters"); fileDialog.setTitle("Save TimeParameters File"); fileDialog.setFilenameFilter(fileFil); fileDialog.setVisible(true); if (fileDialog.getFile()==null) {return;} file = new File(fileDialog.getDirectory(),fileDialog.getFile()); try{ parameters.writeTimeParameters(file); //dbase.save(file); } catch(IOException error){ System.err.println("Error saving file "+error); return; } } if(actionCommand.equals("close")){ frame.dispose(); } // Button actions if(actionCommand.equals("apply")){ parseValues(); setText(); parameters.notifyTimeParametersChangedListeners(); System.out.println("TimeParameters\n"+parameters); } if(actionCommand.equals("default")){ parameters.setDefaultTimeParameters(); setText(); parameters.notifyTimeParametersChangedListeners(); System.out.println("TimeParameters\n"+parameters); } if(actionCommand.equals("significance test parameters")){ if(alternator==true){ gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(parametersContainer,gridBagConstraints); frame.add(parametersContainer); frame.pack(); alternator=false; } else{ frame.remove(parametersContainer); frame.pack(); alternator=true; } } } }