001     
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006  Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    /*****************************************************************************
021    
022    /*
023     * ToolTipManager.java
024     *
025     * Created on September 18, 2006, 10:42 PM
026     */
027    
028    package org.jbudget.gui.month;
029    
030    import java.awt.event.MouseEvent;
031    import java.awt.event.MouseMotionAdapter;
032    import java.util.ArrayList;
033    import java.util.List;
034    import javax.swing.JTable;
035    import javax.swing.table.JTableHeader;
036    import javax.swing.table.TableColumn;
037    import javax.swing.table.TableColumnModel;
038    
039    /**
040     *
041     * @author petrov
042     */
043    
044    /** A class that enables different tooltips for different columns in of a 
045     * JTable.
046     */
047    public class ToolTipManager extends MouseMotionAdapter 
048    {
049      // Current column whose tooltip is being displayed.
050      // This variable is used to minimize the calls to setToolTipText().
051      int currColInd = -1; 
052      
053      List<String>toolTips = new ArrayList<String>();
054      
055      public void setToolTip(int colInd, String toolTip){
056        if (colInd < toolTips.size())
057          toolTips.set(colInd, toolTip);
058        else
059          toolTips.add(colInd, toolTip);
060      }
061      
062      public void update(JTable table)
063      {
064        if (currColInd >= 0 && currColInd < toolTips.size())
065            table.getTableHeader().setToolTipText(toolTips.get(currColInd));
066      }
067            
068      public void mouseMoved(MouseEvent evt) 
069      {
070        JTableHeader header = (JTableHeader)evt.getSource();
071        JTable table = header.getTable();
072        TableColumnModel colModel = table.getColumnModel();
073        int colIndex = colModel.getColumnIndexAtX(evt.getX());
074        
075        if ((colIndex != currColInd) && 
076            (colIndex < toolTips.size()) && 
077            (colIndex >= 0)) 
078        {
079            header.setToolTipText(toolTips.get(colIndex));
080            currColInd = colIndex;
081        }
082      }
083    }