package uk.ac.leeds.ccg.cluster; import java.io.*; import java.awt.*; import java.awt.event.*; /** Class: KnnParametersViewer * Function: To display and modify KnnParameters */ public class KnnParametersViewer extends ParametersViewer implements ActionListener { // Members Container knnParametersContainer; // These Doubles and TextFields allow for the setting of // KnnParameters from the KnnParametersViewer Double k; TextField kTextField = new TextField(""+((KnnParameters)parameters).getK()); // Constructors public KnnParametersViewer(Parameters p){ super(p); } // Methods public static void main(String args[]) throws IOException,FileNotFoundException{ KnnParametersViewer knnParametersViewer = new KnnParametersViewer (new KnnParameters()); knnParametersViewer.run(); } public void run(){ initComponents(); setDisplay(); } public void initComponents(){ // Parameters come first as ParametersViewer instantiates the // gridBagLayout gridBagLayout and gridBagConstraints gridBagConstraints super.initComponents(); knnParametersContainer = new Container(); knnParametersContainer.setLayout(gridBagLayout); // KnnParameter k label = new Label("Number of nearest neighbours"); gridBagConstraints.anchor=GridBagConstraints.EAST; gridBagConstraints.gridwidth=GridBagConstraints.RELATIVE; gridBagLayout.setConstraints(label,gridBagConstraints); knnParametersContainer.add(label); kTextField.setText(""+((KnnParameters)parameters).getK()); gridBagConstraints.anchor=GridBagConstraints.WEST; gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(kTextField,gridBagConstraints); knnParametersContainer.add(kTextField); } public void setDisplay(){ super.setDisplay(); gridBagConstraints.gridwidth=GridBagConstraints.REMAINDER; gridBagLayout.setConstraints(knnParametersContainer,gridBagConstraints); frame.add(knnParametersContainer,frame.getComponentCount()); frame.pack(); } public void parseValues(){ super.parseValues(); k = new Double(kTextField.getText()); ((KnnParameters)parameters).setK((int)k.doubleValue()); } public void setText(){ super.setText(); kTextField.setText(""+((KnnParameters)parameters).getK()); } public void actionPerformed(ActionEvent actionEvent){ String actionCommand = new String(actionEvent.getActionCommand()); if(actionCommand.equals("load parameters")){ System.out.println("Load Parameters File"); fileDialog = new FileDialog(new Frame()); fileFil = new FileFil(fileDialog,".pars"); fileDialog.setTitle("Load Knn Parameters File"); fileDialog.setFilenameFilter(fileFil); fileDialog.setVisible(true); if (fileDialog.getFile()==null) {return;} file = new File(fileDialog.getDirectory(),fileDialog.getFile()); try{ parameters = new KnnParameters(file); setText(); parseValues(); //dbase.load(file); } catch(IOException error){ System.err.println("Error loading file "+error); return; } } else{ // This works because calling super.actionPerformed(actionEvent) from this class // uses the methods in this class first if they exist. super.actionPerformed(actionEvent); } } }