001package edu.pdx.cs410J.grader.gradebook.ui;
002
003import edu.pdx.cs410J.grader.gradebook.Notable;
004
005import javax.swing.*;
006import javax.swing.border.Border;
007import javax.swing.event.ListSelectionEvent;
008import javax.swing.event.ListSelectionListener;
009import java.awt.*;
010import java.awt.event.ActionEvent;
011import java.awt.event.ActionListener;
012import java.util.Vector;
013
014/**
015 * This panel displays and edits notes.
016 *
017 * @author David Whitlock
018 * @version $Revision: 1.6 $
019 * @since Fall 2000
020 */
021@SuppressWarnings("serial")
022public class NotesPanel extends JPanel {
023
024  private Notable notable;
025
026  // GUI components we care about
027  private JList notesList;
028  private JButton add;
029
030  /**
031   * Creates a new <code>NotePanel</code> for displaying the notes of
032   * some <code>Notable</code> object.
033   */
034  public NotesPanel() {
035    this.setLayout(new BorderLayout());
036
037    Border notesBorder = BorderFactory.createTitledBorder("Notes");
038    this.setBorder(notesBorder);
039
040    this.notesList = new JList();
041    this.add(new JScrollPane(notesList), BorderLayout.CENTER);
042
043    JPanel panel = new JPanel();
044    panel.setLayout(new FlowLayout());
045
046    final JTextField field = new JTextField(20);
047    panel.add(field);
048
049    ActionListener action = new ActionListener() {
050        public void actionPerformed(ActionEvent e) {
051          String note = field.getText();
052          if (note != null && !note.equals("") && notable != null) {
053            notable.addNote(note);
054            setNotable(notable);
055          }
056
057          field.setText("");
058        }
059      };
060    field.addActionListener(action);
061
062    this.add = new JButton("Add");
063    this.add.setEnabled(false);
064    this.add.addActionListener(action);
065    panel.add(this.add);
066
067    final JButton delete = new JButton("Delete");
068    delete.setEnabled(false);
069    delete.addActionListener(new ActionListener() {
070        public void actionPerformed(ActionEvent e) {
071          String note = (String) notesList.getSelectedValue();
072          if (note == null) {
073            return;
074          }
075
076          if (notable != null) {
077            notable.removeNote(note);
078            setNotable(notable);
079          }
080        }
081      });
082    notesList.addListSelectionListener(new ListSelectionListener() {
083        public void valueChanged(ListSelectionEvent e) {
084          if (notesList.isSelectionEmpty()) {
085            delete.setEnabled(false);
086
087          } else {
088            delete.setEnabled(true);
089          }
090        }
091      });
092    panel.add(delete);
093
094    this.add(panel, BorderLayout.SOUTH);
095  }
096
097  /**
098   * Sets the <code>Notable</code> that is being displayed/edited by
099   * this <code>NotesPanel</code>.
100   */
101  public void setNotable(Notable notable) {
102    clearNotes();   // Start from scratch
103    this.notable = notable;
104    this.notesList.setListData(notable.getNotes().toArray());
105    this.add.setEnabled(true);
106  }
107
108  /**
109   * Returns the <code>Notable</code> edited by this
110   * <code>NotesPanel</code>. 
111   */
112  public Notable getNotable() {
113    return this.notable;
114  }
115
116  /**
117   * Clears the contents of the notes list
118   */
119  public void clearNotes() {
120    this.notesList.setListData(new Vector());
121    this.notable = null;
122  }
123
124}