Coverage Report - org.homeunix.thecave.buddi.plugin.builtin.cellrenderer.ConciseTransactionCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
ConciseTransactionCellRenderer
14%
11/78
1%
1/56
8
 
 1  
 /*
 2  
  * Created on Nov 27, 2008 by igx89
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.builtin.cellrenderer;
 5  
 
 6  
 import java.awt.Color;
 7  
 import java.awt.Component;
 8  
 import java.awt.Dimension;
 9  
 import java.awt.Font;
 10  
 import java.awt.FontMetrics;
 11  
 import java.awt.Graphics;
 12  
 import java.awt.Graphics2D;
 13  
 import java.awt.Insets;
 14  
 import java.awt.RenderingHints;
 15  
 
 16  
 import javax.swing.JLabel;
 17  
 import javax.swing.JList;
 18  
 
 19  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 20  
 import org.homeunix.thecave.buddi.model.Account;
 21  
 import org.homeunix.thecave.buddi.model.Transaction;
 22  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 23  
 import org.homeunix.thecave.buddi.plugin.api.BuddiTransactionCellRendererPlugin;
 24  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 25  
 import org.homeunix.thecave.buddi.util.Formatter;
 26  
 import org.homeunix.thecave.buddi.util.InternalFormatter;
 27  
 
 28  
 import ca.digitalcave.moss.common.OperatingSystemUtil;
 29  
 
 30  
 
 31  
 /**
 32  
  * A modified version of wyatt's DefaultTransactionCellRenderer.
 33  
  * Tries to pack as much information into as little space as possible,
 34  
  * while still being very readable.
 35  
  * 
 36  
  * @author Matthew "IGx89" Lieder
 37  
  *
 38  
  */
 39  
 public class ConciseTransactionCellRenderer extends BuddiTransactionCellRendererPlugin {
 40  
         public static final long serialVersionUID = 0;
 41  
         private Transaction transaction;
 42  
 
 43  695
         private boolean showCleared = PrefsModel.getInstance().isShowCleared();
 44  695
         private boolean showReconciled = PrefsModel.getInstance().isShowReconciled();
 45  695
         protected boolean cheques = false;
 46  
 
 47  695
         private int maxDescriptionLength = 50;
 48  
 
 49  695
         private static final Color GREEN = new Color(0, 128, 0);
 50  
 
 51  695
         public ConciseTransactionCellRenderer(){
 52  695
                 if (OperatingSystemUtil.isMac()){
 53  0
                         this.putClientProperty("Quaqua.Component.visualMargin", new Insets(0,0,0,0));
 54  
                 }
 55  
                 else {
 56  695
                         this.setOpaque(true);
 57  
                 }
 58  
 
 59  
                 //Make this big enough to have two lines of text.  Since the 
 60  
                 // FontMetrics object is not yet initialized, we have to guess
 61  
                 // the correct size, based on JLabel size.
 62  695
                 this.setPreferredSize(new Dimension(200, (int) new JLabel("<html>A<br>B</html>").getPreferredSize().getHeight()));
 63  695
         }
 64  
 
 65  
         @Override
 66  
         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 67  0
                 if (value instanceof Transaction){
 68  0
                         transaction = (Transaction) value;
 69  
                 }
 70  
                 else {
 71  0
                         transaction = null;
 72  
                 }
 73  
 
 74  0
                 maxDescriptionLength = list.getWidth() - 230;
 75  
 
 76  0
                 return super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
 77  
         }
 78  
 
 79  
         @Override
 80  
         protected void paintComponent(Graphics g) {
 81  0
                 super.paintComponent(g);
 82  
 
 83  0
                 final int height = this.getHeight();
 84  0
                 final int width = this.getWidth();
 85  
 
 86  
                 // Antialias text
 87  0
                 ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 88  
                                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
 89  
 
 90  0
                 if (transaction != null){
 91  0
                         final Color textColor = g.getColor();
 92  0
                         final int topRowYPos = height / 2 - 5;
 93  0
                         final int bottomRowYPos = height - 5;
 94  
 
 95  0
                         final Font f = g.getFont();
 96  0
                         final Font bold = new Font(f.getName(), Font.BOLD, f.getSize());
 97  0
                         final Font italic = new Font(f.getName(), Font.ITALIC, f.getSize());                                
 98  
 
 99  0
                         final int amountXOffset = 180;
 100  
 
 101  
                         //Get the font metrics for column alignment
 102  0
                         final FontMetrics fm = this.getGraphics().getFontMetrics();
 103  
 
 104  
                         //Date
 105  0
                         g.drawString(TextFormatter.getDateFormat().format(transaction.getDate()), 10, topRowYPos);
 106  
 
 107  
                         //Description
 108  0
                         g.setFont(bold);
 109  0
                         g.drawString(
 110  
                                         Formatter.getStringToLength(
 111  
                                                         transaction.getDescription() + (cheques && transaction.getNumber() != null && transaction.getNumber().trim().length() > 0 ? "     #" + transaction.getNumber(): ""),
 112  
                                                         maxDescriptionLength, fm), 
 113  
                                                         150, 
 114  
                                                         topRowYPos);
 115  0
                         g.setFont(f);
 116  
 
 117  
                         //Memo
 118  0
                         g.drawString(
 119  
                                         Formatter.getStringToLength(
 120  
                                                         transaction.getMemo() != null ? transaction.getMemo() : "",
 121  
                                                         width - amountXOffset, fm
 122  
                                         ), 
 123  
                                         150, 
 124  
                                         bottomRowYPos);
 125  
 
 126  
                         //Cleared and Reconciled
 127  0
                         if (getAccount() != null){
 128  0
                                 if (showCleared){
 129  0
                                         g.setColor(GREEN);
 130  0
                                         if (getAccount().equals(transaction.getTo()) && transaction.isClearedTo()
 131  
                                                         || getAccount().equals(transaction.getFrom()) && transaction.isClearedFrom())
 132  0
                                                 g.drawString(PrefsModel.getInstance().getTranslator().get(BuddiKeys.SHORT_CLEARED), 20, bottomRowYPos);
 133  0
                                         g.setColor(textColor);
 134  
                                 }
 135  0
                                 if (showReconciled){
 136  0
                                         g.setColor(GREEN);
 137  0
                                         if (getAccount().equals(transaction.getTo()) && transaction.isReconciledTo()
 138  
                                                         || getAccount().equals(transaction.getFrom()) && transaction.isReconciledFrom())
 139  0
                                                 g.drawString(PrefsModel.getInstance().getTranslator().get(BuddiKeys.SHORT_RECONCILED), 30, bottomRowYPos);
 140  0
                                         g.setColor(textColor);
 141  
                                 }
 142  
                         }
 143  
 
 144  
                         //Amount renderer when account is null
 145  0
                         int xPos = 0, minXPos = Integer.MAX_VALUE;
 146  0
                         if (getAccount() == null){
 147  0
                                 long value = transaction.getAmount();
 148  0
                                 xPos = width - 20 - fm.stringWidth(TextFormatter.getFormattedCurrency(value, false, false));
 149  0
                                 if (xPos < minXPos) minXPos = xPos;
 150  0
                                 g.setColor(InternalFormatter.isRed(transaction) ? Color.RED : textColor);
 151  0
                                 g.drawString(TextFormatter.getFormattedCurrency(value, false, false), xPos, bottomRowYPos);
 152  0
                                 g.setColor(textColor);
 153  0
                         }
 154  
                         else {
 155  
                                 //Amount
 156  0
                                 xPos = width - amountXOffset;
 157  0
                                 if (xPos < minXPos) minXPos = xPos;
 158  0
                                 g.setColor(InternalFormatter.isRed(transaction, transaction.getTo().equals(getAccount())) ? Color.RED : textColor);
 159  0
                                 g.drawString(TextFormatter.getFormattedCurrency(transaction.getAmount(), false, false), xPos, bottomRowYPos);
 160  0
                                 g.setColor(textColor);
 161  
 
 162  
                                 //Balance - max 150px
 163  0
                                 g.setFont(bold);
 164  0
                                 final long balanceValue = transaction.getBalance(getAccount().getUid());
 165  
 
 166  0
                                 xPos = width - 20 - fm.stringWidth(TextFormatter.getFormattedCurrency(balanceValue, false, getAccount().getAccountType().isCredit()));
 167  0
                                 if (xPos < minXPos) minXPos = xPos;
 168  0
                                 g.setColor(InternalFormatter.isRed(getAccount(), balanceValue) ? Color.RED : textColor);
 169  0
                                 g.drawString(TextFormatter.getFormattedCurrency(balanceValue, false, getAccount().getAccountType().isCredit()), xPos, bottomRowYPos);
 170  0
                                 g.setColor(textColor);
 171  
                         }
 172  0
                         g.setFont(f);
 173  
 
 174  
                         //To / From sources
 175  0
                         g.setFont(italic);
 176  0
                         final String categoryName = transaction.getFrom() instanceof Account ? transaction.getTo().getFullName() : transaction.getFrom().getFullName();
 177  0
                         xPos = width - 20 - fm.stringWidth(categoryName);
 178  0
                         if (xPos < minXPos) minXPos = xPos;
 179  0
                         g.drawString(categoryName, xPos, topRowYPos);
 180  
 
 181  0
                         if (transaction.isDeleted()){
 182  0
                                 g.setFont(new Font(f.getName(), Font.BOLD, (int) (f.getSize() * 2.5)));
 183  0
                                 g.setColor(new Color(0, 0, 0, 64));
 184  0
                                 g.drawString(TextFormatter.getTranslation(BuddiKeys.VOID_TRANSACTION), maxDescriptionLength / 2, (int) (f.getSize() * 2.5 + 2));
 185  
                         }
 186  
                 }
 187  0
         }
 188  
 
 189  
         @Override
 190  
         public String getName() {
 191  2798
                 return TextFormatter.getTranslation(BuddiKeys.CONCISE_TRANSACTION_CELL_RENDERER);
 192  
         }
 193  
 }