package edu.calpoly.cpe205.fetter; import javax.swing.*; import java.util.*; import java.io.*; import java.lang.reflect.*; import java.lang.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; /** * This class listens for changes in the contents of a JTextArea and makes * the tab containing the JTextArea highlight red until it is selected again. */ // Author: Phillip Hansen // Last Updated By: Jonathon Lee // Version History: // Nov 20, 2000 - comments/pseudocode added // Nov 30, 2000 - (Mike Hebron) updated class description, field description // Jan 17, 2001 - added ETA.out lines to all methods // Feb 27, 2001 - (Phillip Hansen) implemented constructor, changedUpdate(), // insertUpdate(), and run() // Mar 08, 2001 - (Phillip Hansen) changed so that it no longer makes tab blink, // just makes it highlight until the user clicks there. public class ChangeBackgroundDocumentListener implements DocumentListener, MouseListener { /** * Creates a new instance of ChangeBackgroundDocumentListener *
* Post-conditions: new instance has been created * @param text JTextArea to instantiate text with * @param tab JTabbedPane to instantiate tab with */ public ChangeBackgroundDocumentListener(JTextArea text, JTabbedPane tab, int index) { // SET tabIndex to index // SET this.tab to tab // CALL getDocument of text // CALL addDocumentListener of the returned document with this // CALL getBackgroundAt of tab with index // SET originalColor to the returned color tabIndex = index; this.tab = tab; text.getDocument().addDocumentListener(this); originalColor = this.tab.getBackgroundAt(tabIndex); } /** * method is called when a change occurs *
* Pre-conditions: change has occured
* Post-conditions: none
* @param evt the document event
*/
public void changedUpdate(DocumentEvent evt)
{
}
/**
* method is called when an insert occurs
*
* Pre-conditions: insert has occurs
* Post-conditions: none
* @param evt the document event
*/
public void insertUpdate(DocumentEvent evt)
{
// CALL getSelectedIndex of tab
// IF returned index is not equal to tabIndex
// CALL setBackgroundAt of tab with tabIndex, Color.red
// ENDIF
if (tab.getSelectedIndex() != tabIndex)
{
tab.setBackgroundAt(tabIndex, Color.red);
tab.repaint();
}
}
/**
* method is called when a remove occurs
*
* Pre-conditions: remove has occurs
* Post-conditions: none
* @param evt the document event
*/
public void removeUpdate(DocumentEvent evt)
{ // NOTHING
}
public void mouseEntered(MouseEvent evt)
{ //not needed
}
public void mouseExited(MouseEvent evt)
{ //not needed
}
public void mousePressed(MouseEvent evt)
{ //not needed
}
public void mouseReleased(MouseEvent evt)
{ //not needed
}
/**
* it switches the background color back if it was red
*
* Pre-conditions: the MouseListener has been triggered
* Post-conditions: none
* @param evt the MouseEvent
*/
public void mouseClicked(MouseEvent evt)
{
// CALL getSelectedIndex of tab
// CALL getBackgroundAt of tab with returned index
// IF returned background color is equal to Color.red
// CALL getSelectedIndex of tab
// CALL setBackgroundAt of tab with returned index and originalColor
// ENDIF
if (tab.getBackgroundAt(tab.getSelectedIndex()) == Color.red)
{
tab.setBackgroundAt(tab.getSelectedIndex(), originalColor);
tab.repaint();
}
}
/**
* The JTabbedPane that holds all of the JTextAreas
*/
protected JTabbedPane tab;
/**
* The original color when switching the tabs back
*/
protected Color originalColor;
/**
* Stores the index of the tab this listener is listening to
*/
protected int tabIndex;
}