// --------------------------------------- // Restricted Focus Viewer (RFV) Program // Version 2.1 // Language: Java 2 // October, 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. // --------------------------------------- // RFV_Setup_Window.java // // Class for setting up the window in // which the experimenter selects the // subject label and the order of the // experiment items. // --------------------------------------- import java.util.LinkedList; import java.util.Random; import java.awt.*; import java.awt.event.*; public class RFV_Setup_Window extends Frame implements ActionListener, ItemListener, TextListener { // Graphical elements needed // ------------------------- private Panel subject_panel; private Panel order_panel; private Panel available_panel; private Panel presentation_panel; private List available_list; private List presentation_list; private TextField subject_field; private Label subject_label; private Label available_label; private Label presentation_label; private Label status_label; private Button normal_order_button; private Button reverse_order_button; private Button random_order_button; private Button clear_selections_button; private Button select_all_button; private Button remove_component_button; private Button remove_all_button; private Button run_button; private Font font; private Font list_font; // Constants // --------- private static final int LIST_SIZE = 12; private static final int SUBJECT_FIELD_WIDTH = 25; private static final String TITLE = "RFV Version 2.1"; public static final String EOE = "End of Experiment"; // Constructor, which sets up graphics // ----------------------------------- public RFV_Setup_Window(RFV rfv) { 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(); generateListsAndField(); generatePanels(); this.pack(); // Add key listeners so user can quit at any time // ---------------------------------------------- this.addKeyListener(rfv); available_list.addKeyListener(rfv); presentation_list.addKeyListener(rfv); normal_order_button.addKeyListener(rfv); reverse_order_button.addKeyListener(rfv); random_order_button.addKeyListener(rfv); clear_selections_button.addKeyListener(rfv); select_all_button.addKeyListener(rfv); remove_component_button.addKeyListener(rfv); remove_all_button.addKeyListener(rfv); run_button.addKeyListener(rfv); } // Method which accepts the component names, and displays the window // ----------------------------------------------------------------- public void showComponents(LinkedList component_list, RFV rfv) { RFV_File_Index index; try { for (int i = 0; i < component_list.size(); i++) { index = (RFV_File_Index) component_list.get(i); available_list.add(index.label); } } catch(IndexOutOfBoundsException e) { RFV_Error.die(5); } this.show(); // Add listeners for interaction // ----------------------------- available_list.addItemListener(this); presentation_list.addItemListener(this); subject_field.addTextListener(this); run_button.addActionListener(rfv); } // Return true if settings are valid, false otherwise // -------------------------------------------------- public boolean isValidSettings() { String subject_label; boolean valid = true; // Check number of presentation items // ---------------------------------- if (presentation_list.getItemCount() <= 1) { status_label.setText("No components selected for presentation"); this.show(); return false; } // Check subject ID // ---------------- subject_label = subject_field.getText(); if (subject_label.length() == 0) valid = false; else { try { for (int i = 0; i < subject_label.length(); i++) { if (!isValidCharacter(subject_label.charAt(i))) { valid = false; break; } } } catch(IndexOutOfBoundsException e) { RFV_Error.die(69); } } if (!valid) { status_label.setText("Invalid Subject ID"); this.show(); } return valid; } // Returns true if character is valid, false otherwise // --------------------------------------------------- private boolean isValidCharacter(char c) { if (Character.isLetterOrDigit(c)) return true; if (c == '_') return true; return false; } // Returns the subject ID string // ----------------------------- public String getSubjectID() { return subject_field.getText(); } // Returns the component presentation list // --------------------------------------- public LinkedList getPresentationOrder(LinkedList component_list) { RFV_File_Index index; LinkedList new_list; String component; new_list = new LinkedList(); try { for (int i = 0; i < presentation_list.getItemCount() - 1; i++) { component = presentation_list.getItem(i); for (int j = 0; j < component_list.size(); j++) { index = (RFV_File_Index) component_list.get(j); if ((index.label).equals(component)) { new_list.add(index); break; } } } } catch(IndexOutOfBoundsException e) { RFV_Error.die(5); } return new_list; } // Generates the Panels needed // --------------------------- private void generatePanels() { subject_panel = new Panel(new GridBagLayout()); order_panel = new Panel(new GridBagLayout()); available_panel = new Panel(new GridBagLayout()); presentation_panel = new Panel(new GridBagLayout()); subject_panel.setBackground(Color.white); order_panel.setBackground(Color.white); available_panel.setBackground(Color.white); presentation_panel.setBackground(Color.white); // Set up subject panel // -------------------- constrain(subject_panel, subject_label, 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 0.0, 0.0, 5, 5, 5, 0); constrain(subject_panel, subject_field, 1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.CENTER, 0.0, 0.0, 5, 1, 5, 5); // Set up available panel // ---------------------- constrain(available_panel, available_label, 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(available_panel, available_list, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(available_panel, clear_selections_button, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(available_panel, select_all_button, 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); // Set up order panel // ------------------ constrain(order_panel, normal_order_button, 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(order_panel, reverse_order_button, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(order_panel, random_order_button, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); // Set up presentation panel // ------------------------- constrain(presentation_panel, presentation_label, 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(presentation_panel, presentation_list, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(presentation_panel, remove_component_button, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(presentation_panel, remove_all_button, 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); // Set up frame layout // ------------------- constrain(this, subject_panel, 0, 0, 3, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 15, 5, 5, 5); constrain(this, available_panel, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.5, 0.5, 5, 5, 5, 5); constrain(this, order_panel, 1, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(this, presentation_panel, 2, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.5, 0.5, 5, 5, 5, 5); constrain(this, run_button, 0, 2, 3, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 5, 5, 5, 5); constrain(this, status_label, 0, 3, 3, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 15, 5, 15, 5); } // Generates the Lists and the TextField needed // -------------------------------------------- private void generateListsAndField() { available_list = new List(LIST_SIZE, true); presentation_list = new List(LIST_SIZE, false); presentation_list.add(EOE); presentation_list.select(0); // Set list font and colours // ------------------------- available_list.setForeground(Color.black); available_list.setBackground(new Color(210, 210, 210)); available_list.setFont(list_font); presentation_list.setForeground(Color.black); presentation_list.setBackground(new Color(210, 210, 210)); presentation_list.setFont(list_font); subject_field = new TextField(SUBJECT_FIELD_WIDTH); subject_field.setForeground(Color.black); subject_field.setBackground(new Color(210, 210, 210)); subject_field.setFont(list_font); } // Generates the Labels and Buttons needed // --------------------------------------- private void generateLabelsAndButtons() { subject_label = new Label("Subject ID:"); available_label = new Label("Available Components", Label.CENTER); presentation_label = new Label("Presentation Order", Label.CENTER); status_label = new Label(TITLE, Label.CENTER); // Set label font and colour // ------------------------- subject_label.setFont(font); available_label.setFont(font); presentation_label.setFont(font); status_label.setFont(font); subject_label.setForeground(Color.black); available_label.setForeground(Color.black); presentation_label.setForeground(Color.black); status_label.setForeground(Color.black); normal_order_button = new Button("Insert in Order Listed ->"); reverse_order_button = new Button("Insert in Reverse Order ->"); random_order_button = new Button("Insert in Random Order ->"); clear_selections_button = new Button("Clear Selections"); select_all_button = new Button("Select All"); remove_component_button = new Button("Remove Component"); remove_all_button = new Button("Remove All"); run_button = new Button(" Run Experiment "); // Set button fonts and colours // ---------------------------- normal_order_button.setFont(font); reverse_order_button.setFont(font); random_order_button.setFont(font); clear_selections_button.setFont(font); select_all_button.setFont(font); remove_component_button.setFont(font); remove_all_button.setFont(font); run_button.setFont(font); normal_order_button.setForeground(Color.black); normal_order_button.setBackground(new Color(210, 210, 210)); reverse_order_button.setForeground(Color.black); reverse_order_button.setBackground(new Color(210, 210, 210)); random_order_button.setForeground(Color.black); random_order_button.setBackground(new Color(210, 210, 210)); clear_selections_button.setForeground(Color.black); clear_selections_button.setBackground(new Color(210, 210, 210)); select_all_button.setForeground(Color.black); select_all_button.setBackground(new Color(210, 210, 210)); remove_component_button.setForeground(Color.black); remove_component_button.setBackground(new Color(210, 210, 210)); remove_all_button.setForeground(Color.black); remove_all_button.setBackground(new Color(210, 210, 210)); run_button.setForeground(Color.black); run_button.setBackground(new Color(210, 210, 210)); // Add listeners for interaction // ----------------------------- normal_order_button.addActionListener(this); reverse_order_button.addActionListener(this); random_order_button.addActionListener(this); clear_selections_button.addActionListener(this); select_all_button.addActionListener(this); remove_component_button.addActionListener(this); remove_all_button.addActionListener(this); } // 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) { status_label.setText(" "); this.show(); } // Deal with text modification // --------------------------- public void textValueChanged(TextEvent te) { status_label.setText(" "); this.show(); } // Deal with button presses // ------------------------ public void actionPerformed(ActionEvent ae) { status_label.setText(" "); this.show(); if (ae.getSource() == clear_selections_button) { for (int i = 0; i < available_list.getItemCount(); i++) available_list.deselect(i); } else if (ae.getSource() == select_all_button) { for (int i = 0; i < available_list.getItemCount(); i++) if (!available_list.isIndexSelected(i)) available_list.select(i); } else if (ae.getSource() == remove_component_button) removeComponentButtonPress(); else if (ae.getSource() == remove_all_button) removeAllButtonPress(); else if (ae.getSource() == normal_order_button) normalButtonPress(); else if (ae.getSource() == reverse_order_button) reverseButtonPress(); else if (ae.getSource() == random_order_button) randomButtonPress(); } // Removes all components from the presentation order list // ------------------------------------------------------- private void removeAllButtonPress() { while (presentation_list.getItemCount() > 1) { try { presentation_list.remove(0); } catch(ArrayIndexOutOfBoundsException e) { ; } } presentation_list.select(0); } // Removes a component from the presentation order list // ---------------------------------------------------- private void removeComponentButtonPress() { int index; index = getPresentationIndex(); if (index < presentation_list.getItemCount() - 1) { try { presentation_list.remove(index); presentation_list.select(index); } catch(ArrayIndexOutOfBoundsException e) { ; } } else { status_label.setText("End of Experiment cannot be removed"); this.show(); } } // Checks to make sure that none of the items to be // inserted into the presentation list are already there // ----------------------------------------------------- private boolean checkDuplication() { String components[]; boolean duplication = false; components = available_list.getSelectedItems(); for (int i = 0; i < components.length; i++) { for (int j = 0; j < presentation_list.getItemCount(); j++) { if (components[i].equals(presentation_list.getItem(j))) { duplication = true; break; } } } if (duplication) { status_label.setText("Each component may only appear once in the" + " Presentation Order list"); this.show(); } return duplication; } // Inserts items in presntation list in normal order // ------------------------------------------------- private void normalButtonPress() { if (checkDuplication()) return; for (int i = 0; i < available_list.getItemCount(); i++) { if (available_list.isIndexSelected(i)) presentation_list.add(available_list.getItem(i), getPresentationIndex()); } } // Inserts items in presentation list in reverse order // --------------------------------------------------- private void reverseButtonPress() { if (checkDuplication()) return; for (int i = available_list.getItemCount() - 1; i >= 0; i--) { if (available_list.isIndexSelected(i)) presentation_list.add(available_list.getItem(i), getPresentationIndex()); } } // Insert items in presentation list in random order // ------------------------------------------------- private void randomButtonPress() { boolean flags[]; int count; int index; Random random; if (checkDuplication()) return; random = new Random(); count = available_list.getItemCount(); flags = new boolean[count]; for (int i = 0; i < count; i++) flags[i] = false; while (count != 0) { index = Math.abs(random.nextInt()) % available_list.getItemCount(); if (flags[index] == false) { flags[index] = true; count--; if (available_list.isIndexSelected(index)) presentation_list.add(available_list.getItem(index), getPresentationIndex()); } } } // Get selected item from presentation list // ---------------------------------------- private int getPresentationIndex() { int selected_index; selected_index = presentation_list.getSelectedIndex(); if (selected_index == -1) { presentation_list.select(presentation_list.getItemCount() - 1); selected_index = presentation_list.getItemCount() - 1; } return selected_index; } }