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