view StimulusPanel.java @ 47:be66ee2fe9fe

Added abstract class EndBlockPanel to deliver end of block messages (e.g. end of practice block). Added and implemented EndTestPanel which gives "thank you for participating" etc message at end of test.
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Thu, 13 Jun 2013 18:33:34 +0100
parents d3977e825d91
children
line wrap: on
line source
/*=============================================================================
 * File:       StimulusPanel.java
 * Author:     Marcus Pearce <m.pearce@gold.ac.uk>
 * Created:    <2007-12-05 11:24:00 marcusp>
 * Time-stamp: <2012-03-27 16:39:33 marcusp>
 *=============================================================================
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StimulusPanel extends JPanel { 
    
    /* the Experiment */ 
    private Experiment exp; 
    private ExperimentGui gui; 

    /* navigation buttons */ 
    private JButton playButton, nextButton; 

    /* The response buttons */ 
    private JButton[] responseButton;

    /* The song number label */ 
    private JLabel songNumberLabel; 
    
    /* The questions about each melody */ 
    private JComboBox q1Box; 
    private JComboBox q2Box;
    
    /* Message Panel Text */
    private JLabel msgPrompt;
    String defaultMessagePrompt = " ";

    /* accessors */ 
    public JButton getPlayButton() { return playButton; }
    public JButton getNextButton() { return nextButton; } 
    public JButton[] getResponseButtons() { return responseButton; }
    public JComboBox getQ1Box() { return q1Box; }
    public JComboBox getQ2Box() { return q2Box; }
    
    /* constructor */ 
    public StimulusPanel(ExperimentGui egui, Clock clock) { 

        gui = egui; 
        exp = gui.getExperiment(); 
        responseButton = new JButton[exp.getScaleLength()];

        // The display panel (Clock) 
        JPanel displayPanel = new JPanel(); 
        displayPanel.setBackground(Color.black); 
        displayPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        displayPanel.setLayout(new GridBagLayout()); 
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 2;
        c.gridy = 2;
        displayPanel.add(clock, c); 
        
        // Add the response buttons 
	/*
        JPanel responsePanel = new JPanel(); 
        responsePanel.setLayout (new BorderLayout());
        responsePanel.setBorder(BorderFactory.createRaisedBevelBorder());

        JPanel anchorsPanel = new JPanel(); 
        anchorsPanel.add(new JLabel(exp.getLowAnchor()), BorderLayout.WEST);
        anchorsPanel.add(new JLabel("                           "), 
                         BorderLayout.CENTER);
        anchorsPanel.add(new JLabel(exp.getHighAnchor()), BorderLayout.EAST);
	
        JPanel ratingsPanel = new JPanel(); 
        for (int i = 0; i < responseButton.length; i++) { 
            responseButton[i] = new JButton(Integer.toString(i+1)); 
            responseButton[i].setFocusPainted(false); 
            ratingsPanel.add(responseButton[i]); 
        }
        responsePanel.add(ratingsPanel, BorderLayout.NORTH); 
        responsePanel.add(anchorsPanel, BorderLayout.SOUTH); 
	*/

	JPanel responsePanel = new JPanel(); 
        responsePanel.setLayout(new FlowLayout());
        responsePanel.setBorder(BorderFactory.createRaisedBevelBorder());

	responsePanel.add(new JLabel(exp.getLowAnchor()));
        for (int i = 0; i < responseButton.length; i++) { 
            responseButton[i] = new JButton(Integer.toString(i+1)); 
            responseButton[i].setFocusPainted(false); 
            responsePanel.add(responseButton[i]); 
        }
        responsePanel.add(new JLabel(exp.getHighAnchor()));

        // Questions Panel 
        JPanel questionsPanel = new JPanel();
        GridLayout gl = new GridLayout(2,2); 
        gl.setHgap(50); 
        questionsPanel.setLayout(gl); 
        
        if (exp.getAskFamiliarity()) {
            String[] q1BoxOptions = { "", "Yes", "No" }; 
            q1Box = new JComboBox(q1BoxOptions); 
            q1Box.setSelectedIndex(0); 
            questionsPanel.add(new JLabel("Are you familiar with this melody?")); 
            questionsPanel.add(q1Box);
        }

        if (exp.getAskLiking()) {
            String[] q2BoxOptions = { "", "1", "2", "3", "4", "5"}; 
            q2Box = new JComboBox(q2BoxOptions); 
            q2Box.setSelectedIndex(0); 
            questionsPanel.add(new JLabel("How much do you like this melody as a whole? (1 = not at all; 5 = very much)"));
            questionsPanel.add(q2Box);
        }
        
        JPanel questionsPanel2 = new JPanel(); 
        questionsPanel2.setBorder(BorderFactory.createRaisedBevelBorder());
        questionsPanel2.add(questionsPanel, BorderLayout.CENTER); 
        
        // Navigation Panel 
        JPanel navPanel = new JPanel(); 
        playButton = new JButton(new ImageIcon("Icons/Play24.gif"));
        playButton.setText("Play"); 

        nextButton = new JButton(new ImageIcon("Icons/StepForward24.gif"));
        nextButton.setText("Next"); 
        
        songNumberLabel = new JLabel(""); 
        setSongNumberText(); 
        navPanel.add(songNumberLabel); 
        //navPanel.add(playButton); 
        navPanel.add(nextButton); 
        navPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        
        // Message Prompt Panel
        JPanel msgPanel = new JPanel();
        msgPrompt = new JLabel(defaultMessagePrompt);
        msgPanel.add(msgPrompt);
        msgPanel.setBorder(BorderFactory.createRaisedBevelBorder());
        
        // Add it all 
        JPanel southPanel = new JPanel(); 
        southPanel.setLayout (new BorderLayout());
        southPanel.add(msgPanel, BorderLayout.NORTH);
        southPanel.add(responsePanel, BorderLayout.CENTER);
        southPanel.add(questionsPanel2, BorderLayout.SOUTH);
        
        this.setLayout (new BorderLayout());
        this.add(navPanel, BorderLayout.NORTH); 
        this.add(displayPanel, BorderLayout.CENTER); 
        this.add(southPanel, BorderLayout.SOUTH); 
    }

    public void showDefaultMessagePrompt(){
        msgPrompt.setText(defaultMessagePrompt);
    }
    
    public void setMessagePrompt(String prompt){
        msgPrompt.setText(prompt);
    }
    
    //resets the background colour of the buttons
    public void resetButtonBackgrounds() {
        for(JButton button : responseButton) { 
            button.setBackground(UIManager.getColor( "Button.background" ));
        }
    }
    

    public void setSongNumberText() { 
        String m =
            "Block " + 
            exp.getCurrentBlockID() + 
            ", Melody " + 
            Integer.toString(exp.getCurrentBlock().getMelodyNumber()) + 
            ": "; 
        songNumberLabel.setText(m); 
    }

    public void defaultAnswers() { 
        if (exp.getAskFamiliarity())
            q1Box.setSelectedIndex(0); 
        if (exp.getAskLiking())
            q2Box.setSelectedIndex(0); 
    }

    public boolean unansweredQuestions() { 
        if ((exp.getAskFamiliarity() && q1Box.getSelectedItem().equals("")) || 
            (exp.getAskLiking() && q2Box.getSelectedItem().equals("")))
            return true; 
        else 
            return false; 
    }

    public void addAllListeners(ActionListener al) { 
        playButton.addActionListener(al);
        nextButton.addActionListener(al);
               
        for (int i = 0; i < responseButton.length; i++) { 
            responseButton[i].addActionListener(al); 
        }
    }

    public void addAllKeyListeners(KeyListener al) { 
        playButton.addKeyListener(al);
        nextButton.addKeyListener(al);
               
        for (int i = 0; i < responseButton.length; i++) { 
            responseButton[i].addKeyListener(al); 
        }
    }

    public void setResponseEnabled(boolean set) {
	for (int i = 0; i < responseButton.length; i++) {
	    responseButton[i].setEnabled(set);
	}
    }

    public void highlightResponse(int buttonIndex) {
	responseButton[buttonIndex].setBackground(Color.blue);
	responseButton[buttonIndex].setOpaque(true);
	responseButton[buttonIndex].revalidate();
    }

}