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.trans;
022    
023    import java.awt.BorderLayout;
024    import java.awt.Color;
025    import java.awt.Component;
026    import java.awt.Dimension;
027    import java.awt.event.ActionEvent;
028    import java.awt.event.ActionListener;
029    import java.awt.event.MouseEvent;
030    import java.io.IOException;
031    import java.text.ParseException;
032    import java.util.ArrayList;
033    import java.util.Date;
034    import java.util.EventObject;
035    import java.util.LinkedList;
036    import java.util.List;
037    import javax.swing.JButton;
038    import javax.swing.JOptionPane;
039    import javax.swing.JPanel;
040    import javax.swing.JPopupMenu;
041    import javax.swing.JTable;
042    import javax.swing.border.LineBorder;
043    import javax.swing.event.CellEditorListener;
044    import javax.swing.event.ChangeEvent;
045    import javax.swing.table.TableCellEditor;
046    import org.jbudget.Core.Transaction;
047    import org.jbudget.Core.User;
048    import org.jbudget.exceptions.EmptyTransactionEx;
049    import org.jbudget.exceptions.InvalidSrcDestEx;
050    import org.jbudget.gui.components.TriangleButton;
051    import org.jbudget.io.DataManager;
052    
053    
054    /** Cell editor component that is used with JTable in the Transaction and 
055     * Allocation panels.
056     * @author petrov
057     */
058    public class TransactionCellEditor extends JPanel
059                                       implements TableCellEditor
060    {
061        
062        //============================== Data ====================================//
063        
064        private final TransactionEditor editorComponent;
065        
066        private final JPanel menuPanel = new JPanel();
067        private final JButton menuButton = new TriangleButton(TriangleButton.SOUTH);
068        
069        /** Current row in the table that is being edited. */
070        private int currentRow = -1;
071        
072        /** Table with which this component is associated. */
073        private JTable tableObject = null;
074        
075        /** the list of listeners for editor events */
076        private final List<CellEditorListener> 
077                listeners = new LinkedList<CellEditorListener>();
078        
079        //=========================== Constructors ==============================//
080        
081        /**
082         * Creates a new instance of TransactionEditorPanel.
083         * Valid modes of the cell editor are defined in the TransactionEditor 
084         * class.
085         * @see TransactionEditor
086         */
087        public TransactionCellEditor(int mode)
088        {
089            editorComponent = new TransactionEditor(mode);
090            setBorder(new LineBorder(Color.RED));
091            arrangeComponents();
092            initializeActions();
093        }
094    
095        /**
096         * Creates a new instance of TransactionEditorPanel initialized from an
097         * existing transaction. Valid modes of the cell editor are defined in the
098         * TransactionEditor class.
099         * @see TransactionEditor
100         */
101        public TransactionCellEditor(int mode, Transaction transaction)
102                                                              throws IOException
103        {
104            this(mode);
105            editorComponent.setTransaction(transaction);
106        }
107    
108        /** Creates a new instance of TransactionEditorPanel initialized from
109         * a given Date. Valid modes of the cell editor are defined in the
110         * TransactionEditor class.
111         * @see TransactionEditor
112         */
113        public TransactionCellEditor(int mode, Date date)
114        {
115            this(mode);
116            editorComponent.setDate(date);
117        }
118    
119        private void arrangeComponents()
120        {
121            menuPanel.setLayout(new BorderLayout());
122            menuPanel.add(menuButton, BorderLayout.SOUTH);
123            
124            menuPanel.setPreferredSize(new Dimension(10, 0));
125            
126            setLayout(new BorderLayout());
127            add(editorComponent, BorderLayout.CENTER);
128            add(menuPanel, BorderLayout.EAST);
129        }
130        
131        private void initializeActions()
132        {
133            menuButton.addActionListener(new ActionListener()
134            {
135                public void actionPerformed(ActionEvent ev)
136                {
137                    JPopupMenu menu = editorComponent.getExtraActionsMenu();
138                    menu.show(menuButton, 0, 0);
139                }
140            });
141            
142            editorComponent.addListener(new TransactionEditor.TransferChangeListener()
143            {
144                public void numberTransfersChanged()
145                {
146                    updateRowHeight();
147                }
148            });
149        }
150        
151        private void updateRowHeight()
152        {
153            if (tableObject == null || getPreferredSize().height == 0)
154                return;
155            
156            if (tableObject.getRowHeight(currentRow) != getPreferredSize().height)
157            {
158                tableObject.setRowHeight(currentRow, getPreferredSize().height);
159            }
160        }
161    
162        //============================= Methods ==================================//
163        
164        public Component getTableCellEditorComponent(JTable table, Object value,
165                                                      boolean isSelected,
166                                                      int row, int column)
167        {
168            Transaction transaction = (Transaction)value;
169            try
170            {
171                editorComponent.setTransaction(transaction);
172            }
173            catch (IOException ex)
174            {
175                JOptionPane.showMessageDialog(null, 
176                        "IO error has occured. This might be caused by a hard drive \n" +
177                        "falure or by a loss of network connection.",
178                        "Error",
179                        JOptionPane.ERROR_MESSAGE);
180            }
181            
182            tableObject = table;
183            currentRow = row;
184            updateRowHeight();
185            
186            return this;
187        }
188    
189        public void addCellEditorListener(CellEditorListener l)
190        {
191            listeners.add(l);
192        }
193    
194        public void removeCellEditorListener(CellEditorListener l)
195        {
196            listeners.remove(l);
197        }
198    
199        public void cancelCellEditing()
200        {
201            ChangeEvent ev = new ChangeEvent(this);
202            List<CellEditorListener> shadow =
203                    new ArrayList<CellEditorListener>(listeners.size());
204            shadow.addAll(listeners);
205            for (CellEditorListener l : shadow)
206                l.editingCanceled(ev);
207        }
208    
209        public boolean stopCellEditing()
210        {
211            try
212            {
213                Transaction t = editorComponent.getTransaction();
214                if (t != null)
215                    signalEditingStopped();
216                return t != null;
217            }
218            catch (ParseException ex)
219            {
220                JOptionPane.showMessageDialog(null, 
221                        "Invalid amount string.",
222                        "Error",
223                        JOptionPane.ERROR_MESSAGE);
224            }
225            catch (InvalidSrcDestEx ex)
226            {
227                JOptionPane.showMessageDialog(null, 
228                        "Source and destination cannot be identical.",
229                        "Error",
230                        JOptionPane.ERROR_MESSAGE);
231            }
232            catch (EmptyTransactionEx ex)
233            {
234                JOptionPane.showMessageDialog(null, 
235                        "Empty transaction.",
236                        "Error",
237                        JOptionPane.ERROR_MESSAGE);
238            }
239            return false;
240        }
241    
242        public Object getCellEditorValue()
243        {
244            try
245            {
246                return editorComponent.getTransaction();
247            }
248            catch (Exception ex)
249            {
250                
251            }
252            
253            return null;
254        }
255    
256        public boolean isCellEditable(EventObject anEvent)
257        {
258            if (anEvent instanceof MouseEvent)
259            {
260                MouseEvent mEvent = (MouseEvent) anEvent;
261                JTable table = (JTable) mEvent.getSource();
262                int row = table.rowAtPoint(mEvent.getPoint());
263                if (row < 0)
264                    return false;
265                if (mEvent.getButton() == MouseEvent.BUTTON1 &&
266                        table.getSelectedRow() == row)
267                {
268                    Transaction tr = (Transaction) table.getModel().getValueAt(row, 0);
269                    User currentUser = DataManager.getInstance().getCurrentUser();
270                    return (!tr.isAutomatic()) && 
271                             (currentUser == User.SYSTEM ||
272                              tr.getUID() == currentUser.getUID());
273                }
274            }
275            return false;
276        }
277    
278        public boolean shouldSelectCell(EventObject anEvent)
279        {
280            return true;
281        }
282        
283        private void signalEditingStopped()
284        {
285            ChangeEvent ev = new ChangeEvent(this);
286            List<CellEditorListener> shadow =
287                    new ArrayList<CellEditorListener>(listeners.size());
288            shadow.addAll(listeners);
289            for (CellEditorListener l : shadow)
290                l.editingStopped(ev);
291        }
292    }