001    /*****************************************************************************
002    
003        jBudget.
004        Copyright (C)  Dmitri Petrov
005    
006        This program is free software: you can redistribute it and/or modify
007        it under the terms of the GNU General Public License as published by
008        the Free Software Foundation, either version 3 of the License, or
009        (at your option) any later version.
010    
011        This program is distributed in the hope that it will be useful,
012        but WITHOUT ANY WARRANTY; without even the implied warranty of
013        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014        GNU General Public License for more details.
015    
016        You should have received a copy of the GNU General Public License
017        along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     
019    *****************************************************************************/
020    
021    package org.jbudget.gui;
022    
023    import java.awt.AWTEvent;
024    import java.awt.event.AWTEventListener;
025    import java.awt.event.MouseEvent;
026    import javax.swing.SwingUtilities;
027    import org.jbudget.gui.trans.AllocationListPanel;
028    import org.jbudget.gui.trans.TransactionListPanel;
029    
030    /** A special class that is used to listern to mouse events and stop 
031     * transaction/allocation editing in the transaction/allocation list panel
032     * if a mouse click occurrs outside of the list component. This is done to
033     * fix the strange behavior of JTable class -- the editing is not stopped when
034     * a cell looses focus.
035     * 
036     * @author petrov
037     */
038    public class EventListener implements AWTEventListener
039    {
040        public void eventDispatched(AWTEvent event) 
041        {
042         
043            if (!(event instanceof MouseEvent))
044                return;
045            
046            MouseEvent mouseEvent = (MouseEvent)event;
047            
048            int id = mouseEvent.getID();
049            
050            if (id == MouseEvent.MOUSE_CLICKED ||
051                id == MouseEvent.MOUSE_PRESSED || 
052                id == MouseEvent.MOUSE_RELEASED)
053            {
054                TransactionListPanel transactionsPanel = 
055                        MainWindow.getLastInstance().getTransactionsPanel();
056                
057                AllocationListPanel allocationsPanel = 
058                        MainWindow.getLastInstance().getAllocationsPanel();
059                
060                MouseEvent aEvent = 
061                        SwingUtilities.convertMouseEvent(mouseEvent.getComponent(),
062                                                         mouseEvent,
063                                                         allocationsPanel);
064                
065                if (!allocationsPanel.contains(aEvent.getX(), aEvent.getY()))
066                    allocationsPanel.stopCellEditing();
067                
068                MouseEvent tEvent = 
069                        SwingUtilities.convertMouseEvent(mouseEvent.getComponent(),
070                                                         mouseEvent,
071                                                         transactionsPanel);
072                
073                if (!transactionsPanel.contains(tEvent.getX(), tEvent.getY()))
074                    transactionsPanel.stopCellEditing();
075            }
076                
077        }
078    }