// --------------------------------------- // Restricted Focus Viewer (RFV) Program // Version 2.1 // Language: Java 2 // November, 2000 // Copyright (C) 2000 Anthony R. Jansen // --------------------------------------- // 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // --------------------------------------- // Replayer_Selection_Window.java // // Class for setting up the window in // which the experimenter selects the // stimulus block to be replayed. // --------------------------------------- import java.util.LinkedList; import java.awt.*; import java.awt.event.*; public class Replayer_Selection_Window extends Frame implements ItemListener { // Graphical elements needed // ------------------------- private List component_list; private List block_list; private Label subject_label; private Label component_label; private Label block_label; private Label status_label; private Button run_button; private Font font; private Font list_font; // File I/O variables needed // ------------------------- private Replayer_Data_File data_file; private LinkedList component_indexes; private LinkedList block_indexes; private RFV_File_Index selected_block; private int num_data_points; // Constants // --------- private static final int LIST_SIZE = 12; private static final String TITLE = "Replayer Version 2.1"; // Constructor, which sets up graphics // ----------------------------------- public Replayer_Selection_Window(Replayer replayer, String data_filename) { data_file = new Replayer_Data_File(data_filename); num_data_points = 0; this.setTitle(TITLE); this.setBackground(Color.white); this.setLayout(new GridBagLayout()); font = new Font("Helvetica", Font.BOLD, 16); list_font = new Font("Helvetica", Font.PLAIN, 16); generateLabelsAndButtons(); generateLists(); layoutWindow(); this.pack(); // Add key listeners so user can quit at any time // ---------------------------------------------- this.addKeyListener(replayer); component_list.addKeyListener(replayer); block_list.addKeyListener(replayer); run_button.addKeyListener(replayer); // Load in the components // ---------------------- showComponents(replayer); } // Method which reads in the component names, and displays the window // ------------------------------------------------------------------ private void showComponents(Replayer replayer) { RFV_File_Index index; component_indexes = data_file.readComponents(); try { for (int i = 0; i < component_indexes.size(); i++) { index = (RFV_File_Index) component_indexes.get(i); component_list.add(index.label); } } catch(IndexOutOfBoundsException e) { RFV_Error.die(5); } this.show(); // Add listeners for interaction // ----------------------------- component_list.addItemListener(this); block_list.addItemListener(this); run_button.addActionListener(replayer); } // Method which reads in the blocks of the selected component // and displays in place of any other blocks previously loaded // ----------------------------------------------------------- private void showBlocks() { RFV_File_Index index = null; String component; try { block_list.removeAll(); component = component_list.getSelectedItem(); for (int i = 0; i < component_indexes.size(); i++) { index = (RFV_File_Index) component_indexes.get(i); if ((index.label).equals(component)) break; } block_indexes = data_file.readBlocks(index); for (int i = 0; i < block_indexes.size(); i++) { index = (RFV_File_Index) block_indexes.get(i); block_list.add(index.label); } } catch(IndexOutOfBoundsException e) { RFV_Error.die(85); } status_label.setText(" "); this.show(); num_data_points = 0; } // Return true if settings are valid, false otherwise // -------------------------------------------------- public boolean isValidSettings() { if (num_data_points == 0) { status_label.setText("A block with data points must be selected"); this.show(); return false; } else return true; } // Returns the data points // ----------------------- public Replayer_Stimulus_Data [] getDataPoints() { return data_file.readDataPoints(selected_block, num_data_points); } // Returns the input filename // -------------------------- public String getInputFilename() { return data_file.getInputFilename(); } // Returns the selected component label // ------------------------------------ public String getComponent() { return component_list.getSelectedItem(); } // Returns the selected block label // -------------------------------- public String getBlock() { return block_list.getSelectedItem(); } // Layout the graphical components of the window // --------------------------------------------- private void layoutWindow() { constrain(this, subject_label, 0, 0, 2, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 15, 10, 15, 10); constrain(this, component_label, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.5, 0.5, 10, 10, 10, 10); constrain(this, block_label, 1, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.5, 0.5, 10, 10, 10, 10); constrain(this, component_list, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.5, 0.5, 10, 10, 10, 10); constrain(this, block_list, 1, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.5, 0.5, 10, 10, 10, 10); constrain(this, run_button, 0, 3, 2, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 15, 10, 15, 10); constrain(this, status_label, 0, 4, 2, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 15, 5, 15, 5); } // Generates the Lists needed // -------------------------- private void generateLists() { component_list = new List(LIST_SIZE, false); block_list = new List(LIST_SIZE, false); // Set list font and colours // ------------------------- component_list.setForeground(Color.black); component_list.setBackground(new Color(210, 210, 210)); component_list.setFont(list_font); block_list.setForeground(Color.black); block_list.setBackground(new Color(210, 210, 210)); block_list.setFont(list_font); } // Generates the Labels and Buttons needed // --------------------------------------- private void generateLabelsAndButtons() { subject_label = new Label("Subject ID: " + data_file.getSubjectID()); component_label = new Label("Available Components", Label.CENTER); block_label = new Label("Available Blocks", Label.CENTER); status_label = new Label(TITLE, Label.CENTER); // Set label font and colour // ------------------------- subject_label.setFont(font); component_label.setFont(font); block_label.setFont(font); status_label.setFont(font); subject_label.setForeground(Color.black); component_label.setForeground(Color.black); block_label.setForeground(Color.black); status_label.setForeground(Color.black); run_button = new Button(" Run Replayer "); // Set button fonts and colours // ---------------------------- run_button.setFont(font); run_button.setForeground(Color.black); run_button.setBackground(new Color(210, 210, 210)); } // Constraints for Grid Bag Layout // ------------------------------- public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int fill, int anchor, double weight_x, double weight_y, int top, int left, int bottom, int right) { GridBagConstraints c = new GridBagConstraints(); c.gridx = grid_x; c.gridy = grid_y; c.gridwidth = grid_width; c.gridheight = grid_height; c.fill = fill; c.anchor = anchor; c.weightx = weight_x; c.weighty = weight_y; if (top+left+bottom+right > 0) c.insets = new Insets(top, left, bottom, right); ((GridBagLayout)container.getLayout()).setConstraints(component, c); container.add(component); } // Deal with list item selections // ------------------------------ public void itemStateChanged(ItemEvent ie) { String block; RFV_File_Index index = null; status_label.setText(" "); this.show(); if (ie.getSource() == component_list && component_list.getSelectedIndex() != -1) showBlocks(); else if (ie.getSource() == block_list && block_list.getSelectedIndex() != -1) { try { block = block_list.getSelectedItem(); for (int i = 0; i < block_indexes.size(); i++) { index = (RFV_File_Index) block_indexes.get(i); if ((index.label).equals(block)) break; } num_data_points = data_file.readNumberDataPoints(index); selected_block = index; } catch(IndexOutOfBoundsException e) { RFV_Error.die(85); } status_label.setText("Number of data points: " + num_data_points); this.show(); } } }