1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 07/11/2001
10   *
11   *  $Id: TabHighlighter.java,v 1.8 2005/01/11 13:51:34 ian Exp $
12   */
13  package gate.gui;
14  
15  
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.event.MouseAdapter;
19  import java.awt.event.MouseEvent;
20  
21  import javax.swing.JTabbedPane;
22  import javax.swing.event.ChangeEvent;
23  import javax.swing.event.ChangeListener;
24  
25  /**
26   * Highligts a tab in a JTabbedPane. Removes the highlight automatically when
27   * the highlighted tab is selected.
28   */
29  public class TabHighlighter {
30    public TabHighlighter(JTabbedPane pane, Component comp,
31                          Color highlightColour){
32      this.tPane = pane;
33      this.tab = tPane.indexOfComponent(comp);
34      this.highlightColour = highlightColour;
35      tPane.getModel().addChangeListener(new ChangeListener() {
36        public void stateChanged(ChangeEvent e) {
37          if(tPane.getSelectedIndex() == tab) removeHighlight();
38        }
39      });
40  
41      tPane.addMouseListener(new MouseAdapter() {
42        public void mouseClicked(MouseEvent e) {
43          if(tPane.getSelectedIndex() == tab) removeHighlight();
44        }
45      });
46  
47    }// TabBlinker(JTabbedPane pane, Component comp, Color blinkColor)
48  
49    /**
50     * Highlights the tab unless is selected
51     */
52    public void highlight(){
53      if(tPane.getSelectedIndex() != tab){
54        if(tPane.getBackgroundAt(tab).equals(highlightColour)) return;
55  
56        oldColour = tPane.getBackgroundAt(tab);
57        tPane.setBackgroundAt(tab, highlightColour);
58      }
59    }//public void highlight()
60  
61  
62    /**
63     * Restores the tab to the normal colour
64     */
65    public void removeHighlight(){
66      tPane.setBackgroundAt(tab, oldColour);
67    }// public void removeHighlight()
68  
69    JTabbedPane tPane;
70    int tab;
71    Color highlightColour;
72    Color oldColour;
73  }