r3682 - in trunk: lima-business/src/main/java/org/chorem/lima/business/utils lima-swing/src/main/java/org/chorem/lima/ui/celleditor lima-swing/src/main/java/org/chorem/lima/ui/common lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionsearch lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced
Author: Bavencoff Date: 2013-06-17 15:08:18 +0200 (Mon, 17 Jun 2013) New Revision: 3682 Url: http://chorem.org/projects/lima/repository/revisions/3682 Log: user experience Added: trunk/lima-business/src/main/java/org/chorem/lima/business/utils/FinancialTransactionComparator.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellRender.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteHorizontalAction.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteVerticalAction.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/TableModelWithGroup.java Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AccountTableCellRenderer.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellEditor.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryTableCellRenderer.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionView.jaxx trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionsearch/FinancialTransactionSearchTableModel.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTable.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTableModel.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedView.jaxx trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedViewHandler.java Added: trunk/lima-business/src/main/java/org/chorem/lima/business/utils/FinancialTransactionComparator.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/utils/FinancialTransactionComparator.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/utils/FinancialTransactionComparator.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -0,0 +1,20 @@ +package org.chorem.lima.business.utils; + +import org.chorem.lima.entity.FinancialTransaction; + +import java.util.Comparator; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public class FinancialTransactionComparator implements Comparator<FinancialTransaction> { + + @Override + public int compare(FinancialTransaction o1, FinancialTransaction o2) { + int result = o1.getTransactionDate().compareTo(o2.getTransactionDate()); + if (result == 0) { + result = o1.getTopiaCreateDate().compareTo(o2.getTopiaCreateDate()); + } + return result; + } +} Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AccountTableCellRenderer.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AccountTableCellRenderer.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AccountTableCellRenderer.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -5,7 +5,7 @@ /** * @author Sylvain Bavencoff <bavencoff@codelutin.com> */ -public class AccountTableCellRenderer extends EntryTableCellRenderer { +public class AccountTableCellRenderer extends EntryTableCellRenderer { @Override public void setValue(Object value) { Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellEditor.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellEditor.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellEditor.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -135,7 +135,7 @@ } // Remet à faux pour la premiere lettre tapée au clavier keyPressed = false; - return !(evt instanceof MouseEvent) || ((MouseEvent) evt).getClickCount() == 2; + return !(evt instanceof MouseEvent) || ((MouseEvent) evt).getClickCount() == 1; } } Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellRender.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellRender.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryBookTableCellRender.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -0,0 +1,20 @@ +package org.chorem.lima.ui.celleditor; + +import org.chorem.lima.entity.EntryBook; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public class EntryBookTableCellRender extends EntryTableCellRenderer { + + @Override + public void setValue(Object value) { + Object result = value; + if (value != null && value instanceof EntryBook) { + EntryBook entryBook= (EntryBook) value; + result = entryBook.getCode() + " - " + entryBook.getLabel(); + } + super.setValue(result); + } + +} Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryTableCellRenderer.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryTableCellRenderer.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/EntryTableCellRenderer.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -1,5 +1,6 @@ package org.chorem.lima.ui.celleditor; +import org.chorem.lima.ui.common.TableModelWithGroup; import org.chorem.lima.ui.financialtransaction.FinancialTransactionTable; import org.chorem.lima.ui.financialtransaction.FinancialTransactionTableModel; import sun.swing.DefaultLookup; @@ -36,9 +37,10 @@ } else { Color alternateColor = DefaultLookup.getColor(myCell, myCell.getUI(), "Table.alternateRowColor"); if (alternateColor != null) { - if (table instanceof FinancialTransactionTable) { - FinancialTransactionTableModel model = (FinancialTransactionTableModel) ((FinancialTransactionTable)table).getModel(); - if (model.getIndexTransaction(row) % 2 != 0) { + + if (table.getModel() instanceof TableModelWithGroup) { + TableModelWithGroup model = (TableModelWithGroup) table.getModel(); + if (model.indexGroupAt(row) % 2 != 0) { background = alternateColor; } } else if (row % 2 == 0) { Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteHorizontalAction.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteHorizontalAction.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteHorizontalAction.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -0,0 +1,72 @@ +package org.chorem.lima.ui.common; + +import javax.swing.*; +import javax.swing.table.TableCellEditor; +import java.awt.event.ActionEvent; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public class CellRouteHorizontalAction extends AbstractAction{ + + JTable table; + + protected int step; + protected int route1; + protected int route2; + protected int nbCell1; + protected int nbCell2; + + public CellRouteHorizontalAction(JTable table, boolean nextCell) { + this.table = table; + step = nextCell ? +1 : -1; + } + + protected void init() { + route1 = table.getSelectedColumn(); + route2 = table.getSelectedRow(); + nbCell1 = table.getColumnCount(); + nbCell2 = table.getRowCount(); + } + + protected int getRow() { + return route2; + } + + protected int getColumn() { + return route1; + } + + protected boolean isEditable() { + return table.getModel().isCellEditable(getRow(), getColumn()); + } + + @Override + public void actionPerformed(ActionEvent e) { + init(); + if (route1 >= 0 && route2 >= 0) { + do { + route1 = route1 + step; + if (route1 < 0 || route1 >= nbCell1) { + route1 = route1 % nbCell1; + if (route1 < 0) { + route1 = route1 + nbCell1; + } + route2 = route2 + step; + if (route2 < 0 || route2 >= nbCell2) { + route2 = route2 % nbCell2; + if (route2 < 0) { + route2 = route2 + nbCell2; + } + } + } + } while (!isEditable()); + if (table.isEditing()) { + TableCellEditor editor = table.getCellEditor(); + editor.stopCellEditing(); + } + table.changeSelection(getRow(), getColumn(), false, false); + table.editCellAt(getRow(), getColumn()); + } + } +} Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteVerticalAction.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteVerticalAction.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/CellRouteVerticalAction.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -0,0 +1,29 @@ +package org.chorem.lima.ui.common; + +import javax.swing.*; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public class CellRouteVerticalAction extends CellRouteHorizontalAction { + + public CellRouteVerticalAction(JTable table, boolean nextCell) { + super(table, nextCell); + } + + protected void init() { + route1 = table.getSelectedRow(); + route2 = table.getSelectedColumn(); + nbCell1 = table.getRowCount(); + nbCell2 = table.getColumnCount(); + } + + protected int getRow() { + return route1; + } + + protected int getColumn() { + return route2; + } + +} Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/TableModelWithGroup.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/TableModelWithGroup.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/TableModelWithGroup.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -0,0 +1,9 @@ +package org.chorem.lima.ui.common; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public interface TableModelWithGroup { + + public int indexGroupAt(int row); +} Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -37,6 +37,8 @@ import org.chorem.lima.ui.celleditor.DateTableCellRenderer; import org.chorem.lima.ui.celleditor.EntryTableCellRenderer; import org.chorem.lima.ui.celleditor.StringTableCellEditor; +import org.chorem.lima.ui.common.CellRouteHorizontalAction; +import org.chorem.lima.ui.common.CellRouteVerticalAction; import javax.swing.*; import javax.swing.table.TableCellEditor; @@ -110,15 +112,15 @@ // action on Shift + Tab binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK)); - actionMap.put(binding, new CellRouteHorizontalFirstAction(false)); + actionMap.put(binding, new CellRouteHorizontalAction(this, false)); // action on Enter binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); - actionMap.put(binding, new CellRouteVerticalFirstAction(true)); + actionMap.put(binding, new CellRouteVerticalAction(this, true)); // action on Shift + Enter binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK)); - actionMap.put(binding, new CellRouteVerticalFirstAction(false)); + actionMap.put(binding, new CellRouteVerticalAction(this, false)); // add action on Ctrl + Shift + N binding = "new-transaction"; @@ -146,9 +148,7 @@ actionMap.put(binding, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - if (getSelectedRow() >= 0) { - handler.copyTransaction(getSelectedRow()); - } + handler.copyTransaction(); } }); @@ -158,9 +158,7 @@ actionMap.put(binding, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - if (getSelectedRow() >= 0) { - handler.pasteTransaction(getSelectedRow()); - } + handler.pasteTransaction(); } }); @@ -190,9 +188,7 @@ actionMap.put(binding, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - if (getSelectedRow() >= 0) { - handler.copyEntry(getSelectedRow()); - } + handler.copyEntry(); } }); @@ -202,9 +198,7 @@ actionMap.put(binding, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - if (getSelectedRow() >= 0) { - handler.pasteEntry(getSelectedRow()); - } + handler.pasteEntry(); } }); @@ -214,9 +208,7 @@ actionMap.put(binding, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - if (getSelectedRow() >= 0) { - handler.balanceTransaction(getSelectedRow()); - } + handler.balanceTransaction(); } }); @@ -236,6 +228,8 @@ inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK)); inputMap = inputMap.getParent(); } + + addMouseListener(new MyMouseAdapter()); } public FinancialTransactionViewHandler getHandler() { @@ -303,92 +297,6 @@ } } - private class CellRouteHorizontalFirstAction extends AbstractAction { - - FinancialTransactionTable table = FinancialTransactionTable.this; - FinancialTransactionTableModel tableModel = table.handler.view.getFinancialTransactionTableModel(); - - protected int step; - protected int route1; - protected int route2; - protected int nbCell1; - protected int nbCell2; - - public CellRouteHorizontalFirstAction(boolean nextCell) { - step = nextCell ? +1 : -1; - } - - protected void init() { - route1 = getSelectedColumn(); - route2 = getSelectedRow(); - nbCell1 = getColumnCount(); - nbCell2 = getRowCount(); - } - - protected int getRow() { - return route2; - } - - protected int getColumn() { - return route1; - } - - protected boolean isEditable() { - return tableModel.isCellEditable(getRow(), getColumn()); - } - - @Override - public void actionPerformed(ActionEvent e) { - init(); - if (route1 >= 0 && route2 >= 0) { - do { - route1 = route1 + step; - if (route1 < 0 || route1 >= nbCell1) { - route1 = route1 % nbCell1; - if (route1 < 0) { - route1 = route1 + nbCell1; - } - route2 = route2 + step; - if (route2 < 0 || route2 >= nbCell2) { - route2 = route2 % nbCell2; - if (route2 < 0) { - route2 = route2 + nbCell2; - } - } - } - } while (!isEditable()); - if (isEditing()) { - TableCellEditor editor = getCellEditor(); - editor.stopCellEditing(); - } - changeSelection(getRow(), getColumn(), false, false); - editCellAt(getRow(), getColumn()); - } - } - } - - private class CellRouteVerticalFirstAction extends CellRouteHorizontalFirstAction { - - public CellRouteVerticalFirstAction(boolean nextCell) { - super(nextCell); - } - - protected void init() { - route1 = getSelectedRow(); - route2 = getSelectedColumn(); - nbCell1 = getRowCount(); - nbCell2 = getColumnCount(); - } - - protected int getRow() { - return route1; - } - - protected int getColumn() { - return route2; - } - } - private class MyMouseAdapter extends MouseAdapter { @Override Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -28,17 +28,21 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.lima.business.api.FinancialTransactionService; +import org.chorem.lima.business.utils.EntryComparator; +import org.chorem.lima.business.utils.FinancialTransactionComparator; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.Entry; +import org.chorem.lima.entity.EntryImpl; import org.chorem.lima.entity.FinancialTransaction; +import org.chorem.lima.entity.FinancialTransactionImpl; import org.chorem.lima.service.LimaServiceFactory; +import org.chorem.lima.ui.common.TableModelWithGroup; import javax.swing.table.AbstractTableModel; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.Date; import java.util.List; @@ -51,12 +55,12 @@ * @author chatellier * @version $Revision$ */ -public class FinancialTransactionTableModel extends AbstractTableModel { +public class FinancialTransactionTableModel extends AbstractTableModel implements TableModelWithGroup { /** serialVersionUID. */ private static final long serialVersionUID = -7495388454688562991L; - private static final Log log = LogFactory.getLog(FinancialTransactionTableModel.class); + protected static final Log log = LogFactory.getLog(FinancialTransactionTableModel.class); /** Service (just to update setValueAt(). */ protected FinancialTransactionService financialTransactionService; @@ -68,32 +72,32 @@ public FinancialTransactionTableModel() { financialTransactionService = LimaServiceFactory.getService(FinancialTransactionService.class); + entries = new ArrayList<Entry>(); + transactions = new ArrayList<FinancialTransaction>(); } - public void setEntries(List<Entry> entries) { - this.entries = entries; - transactions = new ArrayList<FinancialTransaction>(); - for (Entry entry :entries) { - FinancialTransaction transaction = entry.getFinancialTransaction(); - if (!transactions.contains(transaction)) { - transactions.add(transaction); - } - } - fireTableDataChanged(); + public void setTransactions(List<FinancialTransaction> transactions) { + this.transactions.clear(); + this.transactions.addAll(transactions); + sortEntries(); } public void sortEntries() { - Comparator<FinancialTransaction> comparator = new Comparator<FinancialTransaction>() { - @Override - public int compare(FinancialTransaction o1, FinancialTransaction o2) { - return o1.getTransactionDate().compareTo(o2.getTransactionDate()); - } - }; - - Collections.sort(transactions, comparator); + Collections.sort(transactions, new FinancialTransactionComparator()); entries.clear(); for (FinancialTransaction transaction : transactions) { - entries.addAll(transaction.getEntry()); + if (transaction.sizeEntry() > 0) { + List<Entry> entries = new ArrayList<Entry>(transaction.getEntry()); + Collections.sort(entries, new EntryComparator()); + this.entries.addAll(entries); + } else { + Entry entry = new EntryImpl(); + entry.setFinancialTransaction(transaction); + entry = financialTransactionService.createEntry(entry); + transaction.addEntry(entry); + financialTransactionService.updateFinancialTransaction(transaction); + this.entries.add(entry); + } } fireTableDataChanged(); } @@ -149,10 +153,10 @@ result = _("lima.ui.financialtransaction.description"); break; case 4: - result = _("lima.ui.financialtransaction.credit"); + result = _("lima.ui.financialtransaction.debit"); break; case 5: - result = _("lima.ui.financialtransaction.debit"); + result = _("lima.ui.financialtransaction.credit"); break; } return result; @@ -190,10 +194,10 @@ result = entry.getDescription(); break; case 4: - result = entry.getDebit() ? BigDecimal.ZERO : entry.getAmount(); + result = entry.getDebit() ? entry.getAmount() : BigDecimal.ZERO; break; case 5: - result = entry.getDebit() ? entry.getAmount() : BigDecimal.ZERO; + result = entry.getDebit() ? BigDecimal.ZERO : entry.getAmount(); break; } return result; @@ -218,6 +222,7 @@ FinancialTransaction transaction = entry.getFinancialTransaction(); boolean updateEntry = false; boolean updateTransaction = false; + boolean updateBalance = false; switch (column) { case 0: //update @@ -233,26 +238,9 @@ if (updateEntry) { entry.setVoucher(voucher); } - /*int firstEntryRow = indexOf(transaction); - if (row == firstEntryRow) { - - // let's update the voucher of all entries of - // the transaction - for (Entry entryNext : transaction.getEntry()) { - entryNext.setVoucher(voucher); - } - - updateEntries = true; - } else { - - // not on first row, just update this entry voucher - entry.setVoucher(voucher); - } */ break; case 2: Account account = (Account) value; - - updateEntry = ((entry.getAccount()) == null != (account == null)) || (entry.getAccount() != null && account != null @@ -260,26 +248,6 @@ if (updateEntry) { entry.setAccount(account); } - - /*entry.setAccount(account); - if (account != null) { - - // let's copy the account description - description = account.getLabel(); - firstEntryRow = indexOf(transaction); - if (row == firstEntryRow) { - // let's update the description of all entries of - // the transaction - - for (Entry entryNext : transaction.getEntry()) { - entryNext.setDescription(description); - } - updateEntries = true; - } else { - // not on first row, just update this entry description - entry.setDescription(description); - } - } */ break; case 3: String description = ((String) value).trim(); @@ -288,46 +256,38 @@ if (updateEntry) { entry.setDescription(description); } - - /*firstEntryRow = indexOf(transaction); - if (row == firstEntryRow) { - - // let's update the description of all entries of - // the transaction - - for (Entry entryNext : transaction.getEntry()) { - entryNext.setDescription(description); - } - updateEntries = true; - } else { - - // not on first row, just update this entry description - entry.setDescription(description); - } */ break; case 4: + BigDecimal debit = (BigDecimal) value; + updateEntry = (debit.signum() > 0 && (!entry.isDebit() || entry.getAmount().compareTo(debit) != 0)); + if (updateEntry) { + entry.setAmount(debit); + entry.setDebit(true); + updateBalance = true; + } + break; + case 5: BigDecimal credit= (BigDecimal) value; updateEntry = (credit.signum() > 0 && (entry.isDebit() || entry.getAmount().compareTo(credit) != 0)); if (updateEntry) { entry.setAmount(credit); entry.setDebit(false); + updateBalance = true; } break; - case 5: - BigDecimal debit = (BigDecimal) value; - updateEntry = (debit.signum() > 0 && (!entry.isDebit() || entry.getAmount().compareTo(debit) != 0)); - if (updateEntry) { - entry.setAmount(debit); - entry.setDebit(true); - } - break; } // some modification must update all other // first row modification update following rows if (updateEntry) { financialTransactionService.updateEntry(entry); - fireTableCellUpdated(row, column); + if (updateBalance) { + int firstRow = indexOf(transaction); + int lastRow = firstRow + transaction.sizeEntry() - 1; + fireTableRowsUpdated(firstRow, lastRow); + } else { + fireTableCellUpdated(row, column); + } if (log.isDebugEnabled()) { log.debug("Update Entry"); } @@ -370,59 +330,96 @@ return index; } - public int getIndexTransaction(int row) { - FinancialTransaction transaction = getTransactionAt(row); - int index = transactions.indexOf(transaction); - return index; - } - /** * Insert new entry. * * @param entry entry to insert - * @param newRow position */ - public void addEntry(Entry entry, int newRow) { + public void addEntry(Entry entry) { FinancialTransaction transaction = entry.getFinancialTransaction(); if (transactions.contains(transaction)) { - entries.add(newRow, entry); - fireTableRowsInserted(newRow, newRow); + Entry newEntry = new EntryImpl(); + newEntry.setFinancialTransaction(transaction); + newEntry.setVoucher(entry.getVoucher()); + newEntry.setAccount(entry.getAccount()); + newEntry.setDescription(entry.getDescription()); + newEntry.setAmount(entry.getAmount()); + newEntry.setDebit(entry.getDebit()); + newEntry = financialTransactionService.createEntry(newEntry); + + transaction.addEntry(newEntry); + financialTransactionService.updateFinancialTransaction(transaction); + int row = indexOf(transaction) + transaction.sizeEntry() - 1; + entries.add(row, newEntry); + fireTableRowsInserted(row, row); } } /** - * Delete entry. + * Delete selected row in table (could be transaction or entry). + * <p/> + * Called by model. * - * @param entry entry to delete + * @param row */ - public void deleteEntry(Entry entry) { - int row = entries.indexOf(entry); - entries.remove(entry); + public void removeTransaction(int row) { + FinancialTransaction transaction = getTransactionAt(row); + int firstRow = indexOf(transaction); + int lastRow = firstRow + transaction.sizeEntry() - 1; + financialTransactionService.removeFinancialTransaction(transaction); + entries.removeAll(transaction.getEntry()); + transactions.remove(transaction); + fireTableRowsDeleted(firstRow, lastRow); + } + + public void removeEntry(int row) { + Entry entry = getEntryAt(row); + FinancialTransaction transaction = entry.getFinancialTransaction(); + if (transaction.sizeEntry() > 1) { + financialTransactionService.removeEntry(entry); + transaction.removeEntry(entry); + entries.remove(entry); + } else { + financialTransactionService.removeFinancialTransaction(transaction); + entries.removeAll(transaction.getEntry()); + transactions.remove(transaction); + } fireTableRowsDeleted(row, row); } public void addTransaction(FinancialTransaction transaction) { + FinancialTransaction newTransaction = new FinancialTransactionImpl(); + newTransaction.setEntryBook(transaction.getEntryBook()); + newTransaction.setTransactionDate(transaction.getTransactionDate()); + newTransaction = financialTransactionService.createFinancialTransaction(newTransaction); + + if (transaction.getEntry() == null || transaction.getEntry().isEmpty()) { + Entry newEntry = new EntryImpl(); + newEntry.setFinancialTransaction(newTransaction); + newEntry = financialTransactionService.createEntry(newEntry); + newTransaction.addEntry(newEntry); + } else { + for (Entry entry : transaction.getEntry()) { + Entry newEntry = new EntryImpl(); + newEntry.setFinancialTransaction(newTransaction); + newEntry.setVoucher(entry.getVoucher()); + newEntry.setAccount(entry.getAccount()); + newEntry.setDescription(entry.getDescription()); + newEntry.setAmount(entry.getAmount()); + newEntry.setDebit(entry.getDebit()); + newEntry = financialTransactionService.createEntry(newEntry); + newTransaction.addEntry(newEntry); + } + financialTransactionService.updateFinancialTransaction(newTransaction); + } + int indexFirstEntry = entries.size(); - if (!transactions.contains(transaction)) { - transactions.add(transaction); - entries.addAll(transaction.getEntry()); - } + transactions.add(newTransaction); + entries.addAll(newTransaction.getEntry()); int indexLastEntry = entries.size() - 1; fireTableRowsInserted(indexFirstEntry, indexLastEntry - 1); } - public void deleteTransaction(FinancialTransaction transaction) { - if (transactions.contains(transaction)) { - int indexFirst = indexOf(transaction); - int indexLast = indexFirst + transaction.sizeEntry() - 1; - entries.removeAll(transaction.getEntry()); - transactions.remove(transaction); - if (indexFirst <= indexLast) { - fireTableRowsDeleted(indexFirst, indexFirst); - } - } - } - public BigDecimal getBalanceTransactionInRow(int row) { FinancialTransaction transaction = getTransactionAt(row); BigDecimal debit = transaction.getAmountDebit(); @@ -440,4 +437,15 @@ } + @Override + public int indexGroupAt(int row) { + FinancialTransaction transaction = getTransactionAt(row); + int index = transactions.indexOf(transaction); + return index; + + } + + public void updateEntry(Entry entry) { + financialTransactionService.updateEntry(entry); + } } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionView.jaxx =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionView.jaxx 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionView.jaxx 2013-06-17 13:08:18 UTC (rev 3682) @@ -69,10 +69,10 @@ onActionPerformed="handler.deleteSelectedTransaction()" /> <JButton toolTipText="{ _("lima.entries.copy.transaction") + " (Ctrl+Shift+C)"}" actionIcon='copy' enabled="{isSelectedRow()}" - onActionPerformed="handler.copyTransaction(financialTransactionTable.getSelectedRow())" /> + onActionPerformed="handler.copyTransaction()" /> <JButton toolTipText="{ _("lima.entries.paste.transaction") + " (Ctrl+Shift+V)"}" actionIcon='paste' enabled="{isTransactionInClipBoard()}" - onActionPerformed="handler.pasteTransaction(financialTransactionTable.getSelectedRow())" /> + onActionPerformed="handler.pasteTransaction()" /> <JToolBar.Separator/> @@ -84,17 +84,17 @@ onActionPerformed="handler.deleteSelectedEntry()" /> <JButton toolTipText="{ _("lima.entries.copy.entry") + " (Ctrl+Alt+C)"}" actionIcon='copy' enabled="{isSelectedRow()}" - onActionPerformed="handler.copyEntry(financialTransactionTable.getSelectedRow())" /> + onActionPerformed="handler.copyEntry()" /> <JButton toolTipText="{ _("lima.entries.paste.entry") + " (Ctrl+Alt+V)"}" actionIcon='paste' enabled="{isEntryInClipBoard() && isSelectedRow()}" - onActionPerformed="handler.pasteEntry(financialTransactionTable.getSelectedRow())" /> + onActionPerformed="handler.pasteEntry()" /> <JButton toolTipText="{ _("lima.entries.assign.entries") + " (Ctrl+Alt+A)"}" actionIcon='assign-all-entries-in-transaction' enabled="{isAssignableInAllEntries() && isSelectedRow()}" onActionPerformed="handler.assignAllEntries()" /> <JToolBar.Separator/> <JButton toolTipText="{ _("lima.entries.balance") + " (Ctrl+B)"}" actionIcon='balance' enabled="{!isBalance()}" - onActionPerformed="handler.balanceTransaction(financialTransactionTable.getSelectedRow())" /> + onActionPerformed="handler.balanceTransaction()" /> <JToolBar.Separator/> Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -34,7 +34,6 @@ import org.chorem.lima.business.api.FinancialTransactionService; import org.chorem.lima.business.api.FiscalPeriodService; import org.chorem.lima.business.api.ImportService; -import org.chorem.lima.business.utils.EntryComparator; import org.chorem.lima.entity.Entry; import org.chorem.lima.entity.EntryBook; import org.chorem.lima.entity.EntryImpl; @@ -56,8 +55,6 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.List; @@ -98,6 +95,7 @@ LimaServiceFactory.addServiceListener(FiscalPeriodService.class, this); LimaServiceFactory.addServiceListener(ImportService.class, this); } + /** * Init all combo box in view. */ @@ -196,15 +194,7 @@ transactions = financialTransactionService.getAllFinancialTransactions(financialPeriod, entryBook); } - // move it to unique collection for table rendering - for (FinancialTransaction transaction : transactions) { - //transAndEntries.add(transaction); - List<Entry> entries = (List<Entry>) transaction.getEntry(); - Collections.sort(entries, new EntryComparator()); - transAndEntries.addAll(entries); - } - - view.getFinancialTransactionTableModel().setEntries(transAndEntries); + view.getFinancialTransactionTableModel().setTransactions(transactions); } public void selectionChanged() { @@ -222,115 +212,104 @@ } } - public void balanceTransaction(int rowSelected) { + public void balanceTransaction() { FinancialTransactionTableModel tableModel = view.getFinancialTransactionTableModel(); FinancialTransactionTable table = view.getFinancialTransactionTable(); - if (table.isEditing()) { - TableCellEditor editor = table.getCellEditor(); - editor.stopCellEditing(); - } + int rowSelected = table.getSelectedRow(); + if (rowSelected != -1) { + if (table.isEditing()) { + TableCellEditor editor = table.getCellEditor(); + editor.stopCellEditing(); + } - Entry entry = tableModel.getEntryAt(rowSelected); - FinancialTransaction transaction = entry.getFinancialTransaction(); - BigDecimal credit = transaction.getAmountCredit(); - BigDecimal debit = transaction.getAmountDebit(); - if (entry.isDebit()) { - debit = debit.subtract(entry.getAmount()); - } else { - credit = credit.subtract(entry.getAmount()); - } + Entry entry = tableModel.getEntryAt(rowSelected); + FinancialTransaction transaction = entry.getFinancialTransaction(); + BigDecimal credit = transaction.getAmountCredit(); + BigDecimal debit = transaction.getAmountDebit(); + if (entry.isDebit()) { + debit = debit.subtract(entry.getAmount()); + } else { + credit = credit.subtract(entry.getAmount()); + } - BigDecimal balance = credit.subtract(debit); - entry.setAmount(balance.abs()); - entry.setDebit(balance.signum() > 0); - financialTransactionService.updateEntry(entry); - tableModel.fireTableRowsUpdated(rowSelected, rowSelected); - view.setBalance(true); + BigDecimal balance = credit.subtract(debit); + entry.setAmount(balance.abs()); + entry.setDebit(balance.signum() > 0); + tableModel.updateEntry(entry); + int firstRow = tableModel.indexOf(transaction); + int lastRow = firstRow + transaction.sizeEntry() - 1; + tableModel.fireTableRowsUpdated(firstRow, lastRow); + view.setBalance(true); + } } /** * Copy selected Transaction - * - * @param indexSelectedRow selected row to copy a transaction + * */ - public void copyTransaction(int indexSelectedRow){ - clipBoard = view.getFinancialTransactionTableModel().getTransactionAt(indexSelectedRow); - view.setTransactionInClipBoard(true); - view.setEntryInClipBoard(false); + public void copyTransaction(){ + FinancialTransactionTable table = view.getFinancialTransactionTable(); + int indexSelectedRow = table.getSelectedRow(); + if (indexSelectedRow != -1) { + clipBoard = view.getFinancialTransactionTableModel().getTransactionAt(indexSelectedRow); + view.setTransactionInClipBoard(true); + view.setEntryInClipBoard(false); + } } /** * Copy selected Entry * - * @param indexSelectedRow selected row to copy a entry */ - public void copyEntry(int indexSelectedRow){ + public void copyEntry(){ + FinancialTransactionTable table = view.getFinancialTransactionTable(); + int indexSelectedRow = table.getSelectedRow(); + if (indexSelectedRow != -1) { clipBoard = view.getFinancialTransactionTableModel().getEntryAt(indexSelectedRow); view.setTransactionInClipBoard(false); view.setEntryInClipBoard(true); + } } /** * Paste Transaction. - * - * @param indexSelectedRow selected row to paste + * */ - public void pasteTransaction(int indexSelectedRow){ + public void pasteTransaction(){ FinancialTransactionTable table = view.getFinancialTransactionTable(); //select the new line ListSelectionModel selectionModel = table.getSelectionModel(); - if (clipBoard != null && clipBoard instanceof FinancialTransaction) { + int indexSelectedRow = table.getSelectedRow(); + if (indexSelectedRow != -1 && clipBoard != null && clipBoard instanceof FinancialTransaction) { FinancialTransactionTableModel tableModel = view.getFinancialTransactionTableModel(); - FinancialTransaction transactionTemp = (FinancialTransaction) clipBoard; + FinancialTransaction transaction = (FinancialTransaction) clipBoard; - //need to know number of entries of copied transaction - //to select the new transaction - Collection<Entry> entriesTmp = transactionTemp.getEntry(); - int nbentries = entriesTmp.size()+1; - - //new transaction with only entrybook and date - FinancialTransaction transaction = new FinancialTransactionImpl(); - EntryBook entryBook = (EntryBook)view.getEntryBookComboBox().getSelectedItem(); - transaction.setEntryBook(entryBook); - transaction.setTransactionDate(transactionTemp.getTransactionDate()); - transaction = financialTransactionService.createFinancialTransaction(transaction); - - for (Entry entryTmp : entriesTmp) { - Entry entry = new EntryImpl(); - entry.setFinancialTransaction(transaction); - entry.setVoucher(entryTmp.getVoucher()); - entry.setAccount(entryTmp.getAccount()); - entry.setDescription(entryTmp.getDescription()); - entry.setAmount(entryTmp.getAmount()); - entry.setDebit(entryTmp.getDebit()); - entry = financialTransactionService.createEntry(entry); - transaction.addEntry(entry); - } - tableModel.addTransaction(transaction); + if (table.isEditing()) { TableCellEditor editor = table.getCellEditor(); editor.stopCellEditing(); } int index = tableModel.indexOf(transaction); selectionModel.setSelectionInterval(index, index); + table.editCellAt(index, 1); } } /** * Paste row. * - * @param indexSelectedRow selected row to paste */ - public void pasteEntry(int indexSelectedRow){ + public void pasteEntry(){ + FinancialTransactionTable table = view.getFinancialTransactionTable(); FinancialTransactionTableModel tableModel = view.getFinancialTransactionTableModel(); //select the new line ListSelectionModel selectionModel = table.getSelectionModel(); - - if (clipBoard != null && clipBoard instanceof Entry) { + int indexSelectedRow = table.getSelectedRow(); + if (indexSelectedRow != -1 && clipBoard != null && clipBoard instanceof Entry) { Entry entryTmp = (Entry) clipBoard; FinancialTransaction transaction = tableModel.getTransactionAt(indexSelectedRow); Entry entry = new EntryImpl(); @@ -341,53 +320,22 @@ entry.setAmount(entryTmp.getAmount()); entry.setDebit(entryTmp.getDebit()); - entry = financialTransactionService.createEntry(entry); - - transaction.addEntry(entry); - - if (table.isEditing()) { TableCellEditor editor = table.getCellEditor(); editor.stopCellEditing(); } - int index = tableModel.indexOf(transaction) + transaction.sizeEntry() - 1; - tableModel.addEntry(entry, index); + tableModel.addEntry(entry); + + int index = tableModel.indexOf(transaction) + transaction.sizeEntry() - 1; selectionModel.setSelectionInterval(index, index); //focus on second column table.changeSelection(index, 1, false, false); - table.requestFocusInWindow(); + table.editCellAt(index, 1); } } - /** - * @param entry - * @param row - * @return int: indexOf new Entry - */ - protected int addEntry(Entry entry, int row) { - FinancialTransactionTableModel tableModel = view.getFinancialTransactionTableModel(); - FinancialTransactionTable table = view.getFinancialTransactionTable(); - FinancialTransaction currentTransaction = tableModel.getTransactionAt(row); - int financialTransactionRow = 0; - - //create it - entry.setFinancialTransaction(currentTransaction); - Entry newEntry = financialTransactionService.createEntry(entry); - currentTransaction.addEntry(entry); - - // on met a jour l'ui - if (table.isEditing()) { - TableCellEditor editor = table.getCellEditor(); - editor.stopCellEditing(); - } - int newRow = row + 1; - tableModel.addEntry(newEntry, newRow); - table.addRowSelectionInterval(newRow, newRow); - return newRow; - } - - /** + /** * Add new transaction. */ public void addFinancialTransaction() { @@ -425,15 +373,8 @@ // set date to the financial transaction financialTransaction.setTransactionDate(transactionDate); // create it - FinancialTransaction fTransaction = financialTransactionService.createFinancialTransaction(financialTransaction); + tableModel.addTransaction(financialTransaction); - // create first entry of this transaction - Entry entry = new EntryImpl(); - entry.setFinancialTransaction(fTransaction); - entry = financialTransactionService.createEntry(entry); - fTransaction.addEntry(entry); - // add it in ui - tableModel.addTransaction(fTransaction); int addIndex = tableModel.size() - 1; //select the new line @@ -444,6 +385,7 @@ ListSelectionModel selectionModel = table.getSelectionModel(); selectionModel.setSelectionInterval(addIndex, addIndex); table.changeSelection(addIndex, 1, false, false); + table.editCellAt(addIndex, 1); } /** @@ -458,14 +400,17 @@ // real selected row or selected transaction row ? int indexSelectedRow = table.getSelectedRow(); if (indexSelectedRow != -1) { - + TableCellEditor cellEditor = table.getCellEditor(); + if (cellEditor != null) { + cellEditor.cancelCellEditing(); + } //First case, where line selected is an entry : take its transaction //to select the last entry of the transaction Entry entryAt = tableModel.getEntryAt(indexSelectedRow); - Entry lastEntry = financialTransactionService.getLastEntry(entryAt.getFinancialTransaction()); + FinancialTransaction transaction = entryAt.getFinancialTransaction(); + int rowLasEntry = tableModel.indexOf(transaction) + transaction.sizeEntry() - 1; + Entry lastEntry = tableModel.getEntryAt(rowLasEntry); - FinancialTransaction transaction = tableModel.getTransactionAt(indexSelectedRow); - if (log.isDebugEnabled()) { log.debug("Selected row : " + indexSelectedRow); } @@ -514,20 +459,18 @@ entry.setDebit(credit.compareTo(debit) > 0); entry.setAmount(credit.subtract(debit).abs()); - // create it in service entry.setFinancialTransaction(transaction); - Entry newEntry = financialTransactionService.createEntry(entry); - transaction.addEntry(newEntry); - // add it in ui - int addIndex = tableModel.indexOf(lastEntry) + 1; - tableModel.addEntry(newEntry, addIndex); + // create it in service + tableModel.addEntry(entry); //select the new line + int row = rowLasEntry + 1; ListSelectionModel selectionModel = table.getSelectionModel(); selectionModel.setSelectionInterval( - addIndex, addIndex); - table.changeSelection(addIndex, 1, false, false); + row, row); + table.changeSelection(row, 1, false, false); + table.editCellAt(row, 1); } else { if (log.isWarnEnabled()) { @@ -543,7 +486,6 @@ int indexSelectedRow = table.getSelectedRow(); if (indexSelectedRow >= 0) { - FinancialTransaction transaction = tableModel.getTransactionAt(indexSelectedRow); TableCellEditor cellEditor = table.getCellEditor(); if (cellEditor != null) { cellEditor.cancelCellEditing(); @@ -554,11 +496,8 @@ if (response == JOptionPane.YES_OPTION) { - financialTransactionService.removeFinancialTransaction(transaction); + tableModel.removeTransaction(indexSelectedRow); - //must delete the entries of the deleted transaction - tableModel.deleteTransaction(transaction); - if (tableModel.size() > 0) { if (indexSelectedRow >= tableModel.size()) { indexSelectedRow = tableModel.size() - 1; @@ -566,6 +505,7 @@ selectionModel.setSelectionInterval( indexSelectedRow, indexSelectedRow); table.changeSelection(indexSelectedRow, 1, false, false); + table.editCellAt(indexSelectedRow, 1); } } @@ -590,16 +530,9 @@ _("lima.ui.financialtransaction.titleremoveentry"), JOptionPane.YES_NO_OPTION); if (response == JOptionPane.YES_OPTION) { - FinancialTransaction transaction = entry.getFinancialTransaction(); - if (transaction.sizeEntry() > 1) { - financialTransactionService.removeEntry(entry); - transaction.removeEntry(entry); - tableModel.deleteEntry(entry); - } else { - financialTransactionService.removeFinancialTransaction(transaction); - tableModel.deleteTransaction(transaction); - } + tableModel.removeEntry(indexSelectedRow); + if (tableModel.size() > 0) { if (indexSelectedRow >= tableModel.size()) { indexSelectedRow = tableModel.size() - 1; @@ -607,6 +540,7 @@ selectionModel.setSelectionInterval( indexSelectedRow, indexSelectedRow); table.changeSelection(indexSelectedRow, 1, false, false); + table.editCellAt(indexSelectedRow, 1); } } } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionsearch/FinancialTransactionSearchTableModel.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionsearch/FinancialTransactionSearchTableModel.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionsearch/FinancialTransactionSearchTableModel.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -115,7 +115,7 @@ } /** - * Le model est une combinaison de Transaction/Entries. + * Le model est une combinaison de Transaction/entries. * * @return */ Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTable.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTable.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTable.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -28,23 +28,26 @@ import org.chorem.lima.entity.Account; import org.chorem.lima.entity.EntryBook; import org.chorem.lima.ui.celleditor.AccountTableCellEditor; +import org.chorem.lima.ui.celleditor.AccountTableCellRenderer; import org.chorem.lima.ui.celleditor.BigDecimalTableCellEditor; import org.chorem.lima.ui.celleditor.BigDecimalTableCellRenderer; import org.chorem.lima.ui.celleditor.DateTableCellEditor; -import org.chorem.lima.ui.celleditor.EmptyCellRenderer; +import org.chorem.lima.ui.celleditor.DateTableCellRenderer; import org.chorem.lima.ui.celleditor.EntryBookTableCellEditor; -import org.jdesktop.swingx.JXTable; +import org.chorem.lima.ui.celleditor.EntryBookTableCellRender; +import org.chorem.lima.ui.celleditor.EntryTableCellRenderer; +import org.chorem.lima.ui.celleditor.StringTableCellEditor; +import org.chorem.lima.ui.common.CellRouteHorizontalAction; +import org.chorem.lima.ui.common.CellRouteVerticalAction; import org.jdesktop.swingx.decorator.ColorHighlighter; -import org.jdesktop.swingx.decorator.ComponentAdapter; -import org.jdesktop.swingx.decorator.HighlightPredicate; import org.jdesktop.swingx.decorator.Highlighter; -import java.awt.Color; -import java.awt.Component; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; +import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; import java.math.BigDecimal; import java.util.Date; @@ -54,190 +57,134 @@ * @author ore * @author Rémi Chapelet */ -public class FinancialTransactionUnbalancedTable extends JXTable - implements KeyListener, MouseListener { +public class FinancialTransactionUnbalancedTable extends JTable { /** serialVersionUID. */ private static final long serialVersionUID = 3133690382049594727L; - protected FinancialTransactionUnbalancedViewHandler handler; + protected final FinancialTransactionUnbalancedViewHandler handler; private Highlighter colorTransaction; private ColorHighlighter colorBalance; - public FinancialTransactionUnbalancedTable(FinancialTransactionUnbalancedViewHandler handler) { + public FinancialTransactionUnbalancedTable(final FinancialTransactionUnbalancedViewHandler handler) { this.handler = handler; - addKeyListener(this); - addMouseListener(this); - //Get new date editor setDefaultEditor(Date.class, new DateTableCellEditor()); + //Get new entry book editor setDefaultEditor(EntryBook.class, new EntryBookTableCellEditor()); + + //Get new String editor + StringTableCellEditor stringTableCellEditor = new StringTableCellEditor(); + setDefaultEditor(String.class, stringTableCellEditor); + //Get new account editor setDefaultEditor(Account.class, new AccountTableCellEditor()); + //Get new amount editor setDefaultEditor(BigDecimal.class, new BigDecimalTableCellEditor()); + //Get new BigDecimal renderer setDefaultRenderer(BigDecimal.class, new BigDecimalTableCellRenderer()); + + //get new Date renderer for empty cells + setDefaultRenderer(Date.class, new DateTableCellRenderer()); + //get new String renderer for empty cells - setDefaultRenderer(String.class, new EmptyCellRenderer()); + setDefaultRenderer(String.class, new EntryTableCellRenderer()); + //get new Account renderer for empty cells - setDefaultRenderer(Account.class, new EmptyCellRenderer()); + setDefaultRenderer(Account.class, new AccountTableCellRenderer()); + //get new EntryBook renderer for empty cells - setDefaultRenderer(EntryBook.class, new EmptyCellRenderer()); + setDefaultRenderer(EntryBook.class, new EntryBookTableCellRender()); - //highlight financial financial transactions - addColorTransaction(); - // highlight unbalanced financial transactions - addColorNonBalancedTransaction(); - } + setShowHorizontalLines(true); + setShowVerticalLines(true); + setGridColor(new Color(210,210,210)); - /** - * Cette méthode permet de colorer toutes les transactions dans le tableau - * afin de bien distinguer les transactions et entrées comptables. - * On récupère la première cellule, on vérifie que c'est une date - */ - protected void addColorTransaction() { - if (colorTransaction != null) { - removeHighlighter(colorTransaction); - } - HighlightPredicate predicate = new HighlightPredicate() { - @Override - public boolean isHighlighted(Component renderer, - ComponentAdapter adapter) { - return adapter.getValueAt(adapter.row, 0) instanceof Date; - } - }; - colorTransaction = - new ColorHighlighter(predicate, new Color(222, 222, 222), null); - addHighlighter(colorTransaction); - } + InputMap inputMap= getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + ActionMap actionMap = getActionMap(); + // action on Tab + Object binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); + actionMap.put(binding, new CellRouteHorizontalAction(this, true)); - /** - * Permet de surligner une transaction dans le tableau lorsque - * cette dernière n'est pas équilibrée. - * On récupère la dernière cellule de la ligne - * et on vérifie si la valeur est différente de 0 - */ - protected void addColorNonBalancedTransaction() { - if (colorBalance != null) { - removeHighlighter(colorBalance); - } - HighlightPredicate predicate = new HighlightPredicate() { - @Override - public boolean isHighlighted(Component renderer, - ComponentAdapter adapter) { - boolean isHighlighted = false; - Object value = adapter.getValueAt(adapter.row, 8); - if (value instanceof BigDecimal) { - BigDecimal currentBalance = (BigDecimal) value; - // can compare two BigDecimals with different scales - // e.g: 3.1 == 3.10 - if (currentBalance.compareTo(BigDecimal.ZERO) != 0) { - isHighlighted = true; - } - } - return isHighlighted; - } - }; - colorTransaction = - new ColorHighlighter(predicate, new Color(255, 198, 209), null); - addHighlighter(colorTransaction); - } + // action on Shift + Tab + binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK)); + actionMap.put(binding, new CellRouteHorizontalAction(this, false)); - /** - * for each action combination key are think - * for extend keyboard and laptop keyboard - */ - @Override - public void keyPressed(KeyEvent e) { + // action on Enter + binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); + actionMap.put(binding, new CellRouteVerticalAction(this, true)); - //TODO combinaison de touches dans la config + // action on Shift + Enter + binding = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK)); + actionMap.put(binding, new CellRouteVerticalAction(this, false)); - // delete selected row with the key : delete or ctrl clear - // ou de l'entree - if (e.getKeyCode() == KeyEvent.VK_DELETE - || e.getKeyCode() == KeyEvent.VK_CLEAR - && e.getModifiers() == KeyEvent.CTRL_MASK) { - handler.deleteSelectedRow(); - } + // add action on Ctrl + Shift + Delete + binding = "delete-transaction"; + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK + KeyEvent.SHIFT_DOWN_MASK), binding); + actionMap.put(binding, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + handler.deleteSelectedTransaction(); + } + }); - // add entry with the key combination : insert or ctrl + enter - if (e.getKeyCode() == KeyEvent.VK_INSERT - || e.getKeyCode() == KeyEvent.VK_ENTER - && e.getModifiers() == KeyEvent.CTRL_MASK) { - handler.addEmptyEntry(); - } + // add action on Ctrl + N + binding = "new-entry"; + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK), binding); + actionMap.put(binding, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + handler.addEntry(); + } + }); - // clear row selection with the key: escape - if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { - if (!isEditing()) { - clearSelection(); + // add action on Ctrl + Delete + binding = "delete-entry"; + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK), binding); + actionMap.put(binding, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + handler.deleteSelectedEntry(); } - } + }); - /** - * Touche tab - * Ajoute une entrée comptable si tab est sur - * la dernière cellule. - */ - - //TODO 2010-08-19 verifier si la transaction est équilibré - // cf. FinancialTransactionTable.java comme modèle - if (e.getKeyChar() == KeyEvent.VK_TAB) { - // Vérifie si la cellule sélectionnée est la dernière - - int x = getSelectedColumn(); - int y = getSelectedRow(); - int max_x = getColumnCount() - 1; - int max_y = getRowCount() - 1; - if (x == max_x && y == max_y) { - handler.addEmptyEntry(); - // positionne la sélection sur la nouvelle ligne créée - setColumnSelectionInterval(0, 0); - setRowSelectionInterval(max_y + 1, max_y + 1); + // add action on Ctrl + B + binding = "balance"; + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.CTRL_DOWN_MASK), binding); + actionMap.put(binding, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + handler.balanceTransaction(); } + }); + + // remove input Ctrl + Del + inputMap= stringTableCellEditor.getComponent().getInputMap(JComponent.WHEN_FOCUSED); + while (inputMap != null) { + inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK)); + inputMap = inputMap.getParent(); } - } + addMouseListener(new MyMouseAdapter()); - /* - * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent) - */ - @Override - public void keyTyped(KeyEvent e) { - } - @Override - public void keyReleased(KeyEvent e) { - } + private class MyMouseAdapter extends MouseAdapter { - @Override - public void mouseClicked(MouseEvent e) { - } - - @Override - public void mousePressed(MouseEvent e) { - if (rowAtPoint(e.getPoint()) == -1) { - clearSelection(); + @Override + public void mousePressed(MouseEvent e) { + if (rowAtPoint(e.getPoint()) == -1) { + clearSelection(); + } } } - @Override - public void mouseReleased(MouseEvent e) { - } - - @Override - public void mouseEntered(MouseEvent e) { - } - - @Override - public void mouseExited(MouseEvent e) { - } } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTableModel.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTableModel.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedTableModel.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -25,30 +25,22 @@ package org.chorem.lima.ui.financialtransactionunbalanced; -import static org.nuiton.i18n.I18n._; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -import javax.swing.table.AbstractTableModel; - import org.chorem.lima.LimaContext; -import org.chorem.lima.business.ServiceListener; -import org.chorem.lima.business.api.FinancialTransactionService; -import org.chorem.lima.business.utils.EntryComparator; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.Entry; import org.chorem.lima.entity.EntryBook; -import org.chorem.lima.entity.EntryImpl; import org.chorem.lima.entity.FinancialTransaction; import org.chorem.lima.entity.FiscalPeriod; -import org.chorem.lima.service.LimaServiceFactory; +import org.chorem.lima.ui.financialtransaction.FinancialTransactionTableModel; import org.nuiton.util.decorator.Decorator; import org.nuiton.util.decorator.DecoratorProvider; +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +import static org.nuiton.i18n.I18n._; + /** * Basic transaction table model. * <p/> @@ -57,30 +49,22 @@ * @author ore * @author chatellier */ -public class FinancialTransactionUnbalancedTableModel extends AbstractTableModel implements ServiceListener { +public class FinancialTransactionUnbalancedTableModel extends FinancialTransactionTableModel { /** serialVersionUID. */ private static final long serialVersionUID = 3914954536809622358L; - /** Transaction service. */ - protected final FinancialTransactionService financialTransactionService; - /** selected financial period */ protected FiscalPeriod selectedFiscalPeriod; - /** data cache */ - protected List<Object> cacheDataList; - /** * To decorate account objects. */ protected final Decorator<Account> accountDecorator; public FinancialTransactionUnbalancedTableModel() { - financialTransactionService = - LimaServiceFactory.getService( - FinancialTransactionService.class); - LimaServiceFactory.addServiceListener(financialTransactionService, this); + super(); + //LimaServiceFactory.addServiceListener(financialTransactionService, this); DecoratorProvider decoratorProvider = LimaContext.get().getDecoratorProvider(); @@ -88,34 +72,24 @@ } /** - * Le model est une combinaison de Transaction/Entries. + * Le model est une combinaison de Transaction/entries. * * @return */ - protected List<Object> getDataList() { - List<Object> results = new ArrayList<Object>(); + protected void refresh() { + transactions.clear(); + if (selectedFiscalPeriod != null) { - List<FinancialTransaction> financialtransactions = + List<FinancialTransaction> financialTransactions = financialTransactionService.getAllInexactFinancialTransactions(selectedFiscalPeriod); - for (FinancialTransaction financialtransaction : financialtransactions) { - results.add(financialtransaction); - List<Entry> entries = (List<Entry>) financialtransaction.getEntry(); - Collections.sort(entries, new EntryComparator()); - results.addAll(entries); - } + transactions.addAll(financialTransactions); + sortEntries(); } - - return results; } - public void refresh() { - cacheDataList = getDataList(); - fireTableDataChanged(); - } - @Override public int getColumnCount() { - return 9; + return 8; } @Override @@ -146,9 +120,6 @@ result = BigDecimal.class; break; case 7: - result = BigDecimal.class; - break; - case 8: result = String.class; break; } @@ -183,9 +154,6 @@ result = _("lima.table.credit"); break; case 7: - result = _("lima.table.balance"); - break; - case 8: result = _("lima.table.letter"); break; } @@ -194,105 +162,47 @@ } @Override - public int getRowCount() { - int result = 0; - - // just prevent too much result - if (cacheDataList != null) { - result = cacheDataList.size(); - } - - return result; - } - - @Override public Object getValueAt(int row, int column) { + Entry entry = entries.get(row); + FinancialTransaction transaction = entry.getFinancialTransaction(); Object result = null; - // just prevent too much result - if (cacheDataList != null) { - result = cacheDataList.get(row); - if (result instanceof FinancialTransaction) { - // Values for a transaction "header" - FinancialTransaction currentRow = (FinancialTransaction) result; - BigDecimal amountDebit = currentRow.getAmountDebit(); - BigDecimal amountCredit = currentRow.getAmountCredit(); - - switch (column) { - case 0: - result = currentRow.getTransactionDate(); - break; - case 1: - if (currentRow.getEntryBook() != null) { - result = currentRow.getEntryBook().getCode(); - } else { - result = null; - } - break; - case 2: - result = null; //entrybook - break; - case 3: - result = null; // account - break; - case 4: - result = null; // description - break; - case 5: - result = amountDebit; - break; - case 6: - result = amountCredit; - break; - case 7: - result = amountDebit.subtract(amountCredit); - break; - case 8: - result = null; - break; + switch (column) { + case 0: + if (row == 0 || entries.get(row - 1).getFinancialTransaction() != transaction) { + result = transaction.getTransactionDate(); } - } else if (result instanceof Entry) { - // Values for transaction content - Entry currentEntry = (Entry) result; - switch (column) { - case 0: - result = null; // date, set in header - break; - case 1: // entry book, set in header - result = null; - break; - case 2: - result = currentEntry.getVoucher(); - break; - case 3: // account - if (currentEntry.getAccount() == null) { - result = null; - } else { - Account acc = currentEntry.getAccount(); - result = accountDecorator.toString(acc); - } - break; - case 4: - result = currentEntry.getDescription(); - break; - case 5: - result = currentEntry.getDebit() ? currentEntry.getAmount() : BigDecimal.ZERO; - break; - case 6: - result = currentEntry.getDebit() ? BigDecimal.ZERO : currentEntry.getAmount(); - break; - case 7: - result = null; - break; - case 8: - result = currentEntry.getLettering(); - break; + break; + case 1: + if ((row == 0 || entries.get(row - 1).getFinancialTransaction() != transaction) && transaction.getEntryBook() != null) { + result = transaction.getEntryBook(); } - - } + break; + case 2: + result = entry.getVoucher(); + break; + case 3: // account + if (entry.getAccount() == null) { + result = null; + } else { + Account acc = entry.getAccount(); + result = accountDecorator.toString(acc); + } + break; + case 4: + result = entry.getDescription(); + break; + case 5: + result = entry.getDebit() ? entry.getAmount() : BigDecimal.ZERO; + break; + case 6: + result = entry.getDebit() ? BigDecimal.ZERO : entry.getAmount(); + break; + case 7: + result = entry.getLettering(); + break; } - return result; } @@ -306,150 +216,128 @@ */ @Override public boolean isCellEditable(int rowIndex, int columnIndex) { - boolean editableCell = false; - Object currentRow = cacheDataList.get(rowIndex); - // cells editable for the entry row, all cells exclude the date - if (currentRow instanceof Entry && !(columnIndex == 0 || columnIndex == 1)) { - editableCell = true; - } - // cells editable for the financialtransaction row, no cells exclude the date - if (currentRow instanceof FinancialTransaction && (columnIndex == 0 || columnIndex == 1)) { - editableCell = true; - } - return editableCell; + return columnIndex > 1 + || rowIndex == 0 + || entries.get(rowIndex).getFinancialTransaction() != entries.get(rowIndex - 1).getFinancialTransaction(); } - /** - * @param value - * @param description - * @param row - * @return int: indexOf new Entry - */ - public int addEmptyEntry(Object value, String description, int row) { - FinancialTransaction currentTransaction = null; - Object currentRow = cacheDataList.get(row); - Entry entry = new EntryImpl(); - entry.setAmount(BigDecimal.ZERO); - if (description != null) { - entry.setDescription(description); - } - //check if current row is a transaction or an entry - if (currentRow instanceof FinancialTransaction) { - currentTransaction = (FinancialTransaction) currentRow; - } else if (currentRow instanceof Entry) { - Entry currentEntry = (Entry) currentRow; - //get back the parent transaction of the entry - currentTransaction = currentEntry.getFinancialTransaction(); - } - //create it - entry.setFinancialTransaction(currentTransaction); - Entry newEntry = financialTransactionService.createEntry(entry); - //on recharge la liste - int newrow = cacheDataList.indexOf(newEntry); - fireTableRowsInserted(row, row); - return newrow; - } - /** to modifiy financialtransaction or entry */ @Override public void setValueAt(Object value, int row, int column) { - int financialTransactionRow = 0; - // just prevent too much result - if (selectedFiscalPeriod != null) { - Object currentRow = cacheDataList.get(row); - if (currentRow instanceof FinancialTransaction) { - FinancialTransaction currentFinancialTransaction = - (FinancialTransaction) currentRow; - switch (column) { - case 0: - //update - currentFinancialTransaction.setTransactionDate((Date) value); - break; - case 1: - currentFinancialTransaction.setEntryBook((EntryBook) value); - break; + + Entry entry = entries.get(row); + FinancialTransaction transaction = entry.getFinancialTransaction(); + boolean updateEntry = false; + boolean updateTransaction = false; + boolean updateBalance = false; + switch (column) { + case 0: + //update + updateTransaction = (transaction.getTransactionDate().compareTo((Date) value) != 0); + if (updateTransaction) { + transaction.setTransactionDate((Date) value); } - // notify service for modification - financialTransactionService. - updateFinancialTransaction(currentFinancialTransaction); + break; + case 1: + EntryBook entryBook = (EntryBook) value; + updateTransaction = ((transaction.getEntryBook()) == null != (entryBook == null)) + || (transaction.getEntryBook() != null + && entryBook != null + && !transaction.getEntryBook().getCode().equals(entryBook.getCode())); + if (updateTransaction) { + transaction.setEntryBook(entryBook); + } + break; + case 2: + String voucher = ((String) value).trim(); - //update the financial transaction in entire -// financialTransactionRow = -// cacheDataList.indexOf(currentRow); - } else if (currentRow instanceof Entry) { - Entry currentEntry = (Entry) currentRow; - switch (column) { - case 2: - currentEntry.setVoucher((String) value); - break; - case 3: - currentEntry.setAccount((Account) value); - break; - case 4: - currentEntry.setDescription((String) value); - break; - case 5: - currentEntry.setAmount((BigDecimal) value); - currentEntry.setDebit(true); - break; - case 6: - currentEntry.setAmount((BigDecimal) value); - currentEntry.setDebit(false); - break; - case 8: - currentEntry.setLettering((String) value); - break; + updateEntry = (entry.getVoucher() == null || entry.getVoucher().compareTo(voucher) != 0); + if (updateEntry) { + entry.setVoucher(voucher); } + break; + case 3: + Account account = (Account) value; + updateEntry = ((entry.getAccount()) == null != (account == null)) + || (entry.getAccount() != null + && account != null + && !entry.getAccount().getAccountNumber().equals(account.getAccountNumber())); + if (updateEntry) { + entry.setAccount(account); + } + break; + case 4: + String description = ((String) value).trim(); - financialTransactionService.updateEntry(currentEntry); + updateEntry = (entry.getDescription() == null || entry.getDescription().compareTo(description) != 0); + if (updateEntry) { + entry.setDescription(description); + } + break; + case 5: + BigDecimal debit = (BigDecimal) value; + updateEntry = (debit.signum() > 0 && (!entry.isDebit() || entry.getAmount().compareTo(debit) != 0)); + if (updateEntry) { + entry.setAmount(debit); + entry.setDebit(true); + updateBalance = true; + } + break; + case 6: + BigDecimal credit= (BigDecimal) value; + updateEntry = (credit.signum() > 0 && (entry.isDebit() || entry.getAmount().compareTo(credit) != 0)); + if (updateEntry) { + entry.setAmount(credit); + entry.setDebit(false); + updateBalance = true; + } + break; + case 8: + String lettering = ((String) value).trim(); - //update the financial transaction in entire -// financialTransactionRow = -// cacheDataList.indexOf(((Entry) currentRow). -// getFinancialTransaction()); + updateEntry = (entry.getLettering().compareTo(lettering) != 0); + if (updateEntry) { + entry.setLettering(lettering); + } + break; + } + + // some modification must update all other + // first row modification update following rows + if (updateEntry) { + financialTransactionService.updateEntry(entry); + if (updateBalance) { + int firstRow = indexOf(transaction); + int lastRow = firstRow + transaction.sizeEntry() - 1; + fireTableRowsUpdated(firstRow, lastRow); + } else { + fireTableCellUpdated(row, column); } - //on recharge la liste - cacheDataList = getDataList(); - fireTableDataChanged(); + if (log.isDebugEnabled()) { + log.debug("Update Entry"); + } + } else if (updateTransaction) { + financialTransactionService.updateFinancialTransaction(transaction); + fireTableCellUpdated(row, column); + if (log.isDebugEnabled()) { + log.debug("Update transaction"); + } + } else { + if (log.isDebugEnabled()) { + log.debug("No Update"); + } } } - public Object getElementAt(int row) { - Object currentRow = cacheDataList.get(row); - return currentRow; - } - - /** - * Delete selected row in table (could be transaction or entry). - * <p/> - * Called by model. - * - * @param object - * @param row - */ - public void removeObject(Object object, int row) { - Object currentRow = cacheDataList.get(row); - if (currentRow instanceof FinancialTransaction) { - FinancialTransaction currentTransaction = - (FinancialTransaction) currentRow; - financialTransactionService.removeFinancialTransaction(currentTransaction); - } else if (currentRow instanceof Entry) { - Entry currentEntry = (Entry) currentRow; - financialTransactionService.removeEntry(currentEntry); - } - //on recharge la liste - refresh(); - } - - @Override + /*@Override public void notifyMethod(String serviceName, String methodeName) { if (serviceName.contains("FinancialTransaction") || methodeName.contains("importEntries") || methodeName.contains("importAll")) { refresh(); } - } + }*/ } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedView.jaxx =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedView.jaxx 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedView.jaxx 2013-06-17 13:08:18 UTC (rev 3682) @@ -26,61 +26,76 @@ <Table> <import> javax.swing.ListSelectionModel + javax.swing.DefaultListSelectionModel org.chorem.lima.entity.FiscalPeriod org.chorem.lima.ui.combobox.FiscalPeriodComboBoxModel </import> <FinancialTransactionUnbalancedViewHandler id="handler" javaBean="new FinancialTransactionUnbalancedViewHandler(this)"/> + + <ListSelectionModel + id='selectionModel' + initializer="new DefaultListSelectionModel()" + selectionMode='{ListSelectionModel.SINGLE_SELECTION}' + onValueChanged="handler.selectionChanged()"/> + <Boolean id="selectedRow" javaBean="false"/> + <Boolean id="balance" javaBean="true"/> <org.chorem.lima.ui.combobox.FiscalPeriodComboBoxModel id="modelFiscalPeriod"/> <script> <![CDATA[ void $afterCompleteSetup() { - getHandler().refresh(); + getHandler().init(); } ]]> </script> + <row> + <cell fill="horizontal"> + <JToolBar floatable="false"> + <JButton toolTipText="{ _("lima.entries.remove.transaction") + " (Ctrl+Shift+Del)"}" + actionIcon='delete-financial-transaction' + enabled="{isSelectedRow()}" + onActionPerformed="getHandler().deleteSelectedTransaction()" /> + <JButton toolTipText ="{ _("lima.entries.addEntry") + " (Ctrl+N)"}" actionIcon='add-entry' + enabled="{isSelectedRow()}" + onActionPerformed="getHandler().addEntry()" /> + <JButton toolTipText="{ _("lima.entries.remove.entry") + " (Ctrl+Del)"}" actionIcon='delete-entry' + enabled="{isSelectedRow()}" + onActionPerformed="getHandler().deleteSelectedEntry()" /> + <JToolBar.Separator/> + <JButton toolTipText="{ _("lima.entries.balance") + " (Ctrl+B)"}" actionIcon='balance' + enabled="{!isBalance()}" + onActionPerformed="handler.balanceTransaction()" /> + <JToolBar.Separator/> - <row weightx="1" weighty="0" anchor="center"> - <cell anchor="west"> - <JButton text="lima.ui.common.refresh" onActionPerformed="getHandler().refresh()"/> - </cell> - <cell anchor="east"> - <JLabel id="fiscalPeriodLabel" text="lima.ui.financialtransaction.fiscalyear"/> - </cell> - <cell anchor="west"> - <JComboBox id="fiscalPeriodComboBox" - model="{modelFiscalPeriod}" - renderer="{new org.chorem.lima.ui.common.FiscalPeriodListRenderer()}" - onActionPerformed="getFinancialTransactionUnbalancedTableModel().setFiscalPeriod( (FiscalPeriod) fiscalPeriodComboBox.getSelectedItem()); + <JLabel id="fiscalPeriodLabel" + actionIcon='choose-fiscal-year' + labelFor='{fiscalPeriodComboBox}'/> + <JComboBox id="fiscalPeriodComboBox" model="{modelFiscalPeriod}" + toolTipText="lima.ui.financialtransaction.fiscalyear" + renderer="{new org.chorem.lima.ui.common.FiscalPeriodListRenderer()}" + onActionPerformed="getFinancialTransactionUnbalancedTableModel().setFiscalPeriod( (FiscalPeriod) fiscalPeriodComboBox.getSelectedItem()); getHandler().refresh()" - editable="false"/> + editable="false"/> + <JToolBar.Separator/> + <JButton toolTipText="{ _("lima.ui.common.refresh") + " (F5)"}" actionIcon='refresh' + onActionPerformed="getHandler().refresh()"/> + + </JToolBar> </cell> - <cell> - <JButton text="lima.entries.addEntry" - onActionPerformed="getHandler().addEmptyEntry()" - enabled="{isSelectedRow()}"/> - </cell> - <cell> - <JButton text="lima.common.remove" - onActionPerformed="getHandler().deleteSelectedRow()" - enabled="{isSelectedRow()}"/> - </cell> </row> + <row> - <cell fill="both" weightx="1" weighty="1" rows="1" columns="9"> + <cell fill="both" weightx="1" weighty="1" rows="1" > <JScrollPane> <FinancialTransactionUnbalancedTableModel id="financialTransactionUnbalancedTableModel"/> <FinancialTransactionUnbalancedTable - id="financialTransactionUnbalancedTable" sortable="false" + id="financialTransactionUnbalancedTable" rowHeight="22" constructorParams="getHandler()" - model="{getFinancialTransactionUnbalancedTableModel()}" - selectionMode="{ListSelectionModel.SINGLE_SELECTION}"/> - <ListSelectionModel - javaBean="getFinancialTransactionUnbalancedTable().getSelectionModel()" - onValueChanged="setSelectedRow(financialTransactionUnbalancedTable.getSelectedRow() != -1)"/> + selectionModel='{selectionModel}' + model="{getFinancialTransactionUnbalancedTableModel()}"/> </JScrollPane> </cell> </row> Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedViewHandler.java 2013-06-14 09:26:25 UTC (rev 3681) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransactionunbalanced/FinancialTransactionUnbalancedViewHandler.java 2013-06-17 13:08:18 UTC (rev 3682) @@ -25,16 +25,19 @@ package org.chorem.lima.ui.financialtransactionunbalanced; -import static org.nuiton.i18n.I18n._; - -import javax.swing.JOptionPane; -import javax.swing.ListSelectionModel; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.lima.entity.Entry; +import org.chorem.lima.entity.EntryImpl; import org.chorem.lima.entity.FinancialTransaction; import org.chorem.lima.ui.combobox.FiscalPeriodComboBoxModel; +import javax.swing.*; +import javax.swing.table.TableCellEditor; +import java.math.BigDecimal; + +import static org.nuiton.i18n.I18n._; + /** * Handler associated with financial transaction view. * @@ -56,26 +59,47 @@ this.view = view; } + + /** + * Init all combo box in view. + */ + public void init() { + + if (view.getFiscalPeriodComboBox().getModel().getSize() > 0) { + view.getFiscalPeriodComboBox().setSelectedIndex(0); + } + refresh(); + } + //implement new transaction button //add a new entry to the selected transaction - public void addEmptyEntry() { + public void addEntry() { FinancialTransactionUnbalancedTable table = view.getFinancialTransactionUnbalancedTable(); FinancialTransactionUnbalancedTableModel tableModel = view.getFinancialTransactionUnbalancedTableModel(); int indexSelectedRow = table.getSelectedRow(); if (indexSelectedRow != -1) { - Object selectedValue = tableModel.getElementAt(indexSelectedRow); + TableCellEditor cellEditor = table.getCellEditor(); + if (cellEditor != null) { + cellEditor.cancelCellEditing(); + } + + Entry entry = tableModel.getEntryAt(indexSelectedRow); //copy + paste the description - String description = (String) tableModel.getValueAt(indexSelectedRow, 4); - int index = tableModel.addEmptyEntry(selectedValue, description, indexSelectedRow); + Entry newEntry = new EntryImpl(); + newEntry.setFinancialTransaction(entry.getFinancialTransaction()); + newEntry.setVoucher(entry.getVoucher()); + newEntry.setDescription(entry.getDescription()); + + tableModel.addEntry(newEntry); //select the new line - ListSelectionModel selectionModel = - table.getSelectionModel(); - selectionModel.setSelectionInterval(indexSelectedRow + 1, indexSelectedRow + 1); - //focus on second column - table.changeSelection(index, 1, false, false); - table.requestFocusInWindow(); + int row = tableModel.indexOf(newEntry.getFinancialTransaction()) + newEntry.getFinancialTransaction().sizeEntry() - 1; + ListSelectionModel selectionModel = table.getSelectionModel(); + selectionModel.setSelectionInterval( + row, row); + table.changeSelection(row, 1, false, false); + table.editCellAt(row, 1); } else { if (log.isWarnEnabled()) { log.warn("Call addentry selected transaction without selection"); @@ -88,7 +112,7 @@ * <p/> * Called by tableModel. */ - public void deleteSelectedRow() { + public void deleteSelectedEntry() { FinancialTransactionUnbalancedTable table = view.getFinancialTransactionUnbalancedTable(); FinancialTransactionUnbalancedTableModel tableModel = view.getFinancialTransactionUnbalancedTableModel(); @@ -96,23 +120,76 @@ int indexSelectedRow = table.getSelectedRow(); if (indexSelectedRow != -1) { - Object selectedValue = tableModel.getElementAt(indexSelectedRow); - String message; - if (selectedValue instanceof FinancialTransaction) { - message = _("lima.charts.financialtransaction.question.removetransaction"); - } else { - message = _("lima.charts.financialtransaction.question.removeentry"); + TableCellEditor cellEditor = table.getCellEditor(); + if (cellEditor != null) { + cellEditor.cancelCellEditing(); } + String message = _("lima.charts.financialtransaction.question.removeentry"); + int response = JOptionPane.showConfirmDialog(view, message, _("lima.common.confirmation"), JOptionPane.YES_NO_OPTION); if (response == JOptionPane.YES_OPTION) { - tableModel.removeObject(selectedValue, indexSelectedRow); + + tableModel.removeEntry(indexSelectedRow); + + if (tableModel.size() > 0) { + if (indexSelectedRow >= tableModel.size()) { + indexSelectedRow = tableModel.size() - 1; + } + ListSelectionModel selectionModel = + table.getSelectionModel(); + selectionModel.setSelectionInterval( + indexSelectedRow, indexSelectedRow); + table.changeSelection(indexSelectedRow, 1, false, false); + table.editCellAt(indexSelectedRow, 1); + } + } + } else { + if (log.isWarnEnabled()) { + log.warn("Call delete selected row without selection"); + } + } + } + + /** + * Delete selected row in table (could be transaction or entry). + * <p/> + * Called by tableModel. + */ + public void deleteSelectedTransaction() { + + FinancialTransactionUnbalancedTable table = view.getFinancialTransactionUnbalancedTable(); + FinancialTransactionUnbalancedTableModel tableModel = view.getFinancialTransactionUnbalancedTableModel(); + + int indexSelectedRow = table.getSelectedRow(); + if (indexSelectedRow != -1) { + + TableCellEditor cellEditor = table.getCellEditor(); + if (cellEditor != null) { + cellEditor.cancelCellEditing(); + } + + String message = _("lima.charts.financialtransaction.question.removetransaction"); + + int response = JOptionPane.showConfirmDialog(view, message, + _("lima.common.confirmation"), JOptionPane.YES_NO_OPTION); + + if (response == JOptionPane.YES_OPTION) { + tableModel.removeTransaction(indexSelectedRow); //select the upper line - ListSelectionModel selectionModel = - table.getSelectionModel(); - selectionModel.setSelectionInterval(indexSelectedRow - 1, indexSelectedRow - 1); + if (tableModel.size() > 0) { + if (indexSelectedRow >= tableModel.size()) { + indexSelectedRow = tableModel.size() - 1; + } + ListSelectionModel selectionModel = + table.getSelectionModel(); + selectionModel.setSelectionInterval( + indexSelectedRow, indexSelectedRow); + table.changeSelection(indexSelectedRow, 1, false, false); + table.editCellAt(indexSelectedRow, 1); + } } } else { if (log.isWarnEnabled()) { @@ -121,9 +198,72 @@ } } + public void selectionChanged() { + FinancialTransactionUnbalancedTable table = view.getFinancialTransactionUnbalancedTable(); + FinancialTransactionUnbalancedTableModel tableModel = view.getFinancialTransactionUnbalancedTableModel(); + + int selectedRow = table.getSelectedRow(); + view.setSelectedRow(selectedRow >= 0 && selectedRow < tableModel.size()); + if (selectedRow >= 0 && selectedRow < tableModel.size()) { + FinancialTransaction transaction = tableModel.getTransactionAt(selectedRow); + BigDecimal credit = transaction.getAmountCredit(); + BigDecimal debit = transaction.getAmountDebit(); + view.setBalance(credit.compareTo(debit) == 0); + } else { + view.setBalance(true); + } + } + + public void balanceTransaction() { + FinancialTransactionUnbalancedTable table = view.getFinancialTransactionUnbalancedTable(); + FinancialTransactionUnbalancedTableModel tableModel = view.getFinancialTransactionUnbalancedTableModel(); + + int rowSelected = table.getSelectedRow(); + if (rowSelected != -1) { + + if (table.isEditing()) { + TableCellEditor editor = table.getCellEditor(); + editor.stopCellEditing(); + } + + Entry entry = tableModel.getEntryAt(rowSelected); + FinancialTransaction transaction = entry.getFinancialTransaction(); + BigDecimal credit = transaction.getAmountCredit(); + BigDecimal debit = transaction.getAmountDebit(); + if (entry.isDebit()) { + debit = debit.subtract(entry.getAmount()); + } else { + credit = credit.subtract(entry.getAmount()); + } + + BigDecimal balance = credit.subtract(debit); + entry.setAmount(balance.abs()); + entry.setDebit(balance.signum() > 0); + tableModel.updateEntry(entry); + int firstRow = tableModel.indexOf(transaction); + int lastRow = firstRow + transaction.sizeEntry() - 1; + tableModel.fireTableRowsUpdated(firstRow, lastRow); + view.setBalance(true); + + ListSelectionModel selectionModel = table.getSelectionModel(); + selectionModel.setSelectionInterval(rowSelected, rowSelected); + table.changeSelection(rowSelected, 1, false, false); + table.editCellAt(rowSelected, 1); + + } + } + public void refresh() { FinancialTransactionUnbalancedTableModel tableModel = view.getFinancialTransactionUnbalancedTableModel(); + FinancialTransactionUnbalancedTable table = view.getFinancialTransactionUnbalancedTable(); + + if (table.isEditing()) { + TableCellEditor editor = table.getCellEditor(); + editor.stopCellEditing(); + } + tableModel.refresh(); + table.clearSelection(); FiscalPeriodComboBoxModel comboBoxModel = view.getModelFiscalPeriod(); comboBoxModel.refresh();
participants (1)
-
Bavencoff@users.chorem.org