Coverage Report - org.homeunix.thecave.buddi.plugin.builtin.cellrenderer.DefaultTransactionCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultTransactionCellRenderer
13%
12/92
1%
1/68
9.5
 
 1  
 /*
 2  
  * Created on Sep 8, 2006 by wyatt
 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.Transaction;
 21  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 22  
 import org.homeunix.thecave.buddi.plugin.api.BuddiTransactionCellRendererPlugin;
 23  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 24  
 import org.homeunix.thecave.buddi.util.Formatter;
 25  
 import org.homeunix.thecave.buddi.util.InternalFormatter;
 26  
 
 27  
 import ca.digitalcave.moss.common.OperatingSystemUtil;
 28  
 
 29  
 
 30  
 /**
 31  
  * The cell renderer for TransactionsFrame.  On a Mac, we use Quaqua to
 32  
  * make this look pretty; on a non Mac, we have to do it ourselves.
 33  
  * 
 34  
  * @author wyatt
 35  
  *
 36  
  */
 37  
 public class DefaultTransactionCellRenderer extends BuddiTransactionCellRendererPlugin {
 38  
         public static final long serialVersionUID = 0;
 39  
         private Transaction transaction;
 40  
 
 41  2780
         private boolean showCleared = PrefsModel.getInstance().isShowCleared();
 42  2780
         private boolean showReconciled = PrefsModel.getInstance().isShowReconciled();
 43  2780
         protected boolean simple = false;
 44  2780
         protected boolean cheques = false;
 45  
 
 46  2780
         private int maxDescriptionLength = 50;//, maxToFromLength = 40;
 47  
 
 48  
         private static Font f;
 49  
         private static Font bold;
 50  
         private static Font italic;
 51  
 
 52  695
         private static final Color GREEN = new Color(0, 128, 0);
 53  
 
 54  
         /**
 55  
          * Creates a new TransactionCellRenderer object
 56  
          */
 57  2780
         public DefaultTransactionCellRenderer(){
 58  2780
                 if (OperatingSystemUtil.isMac()){
 59  0
                         this.putClientProperty("Quaqua.Component.visualMargin", new Insets(0,0,0,0));
 60  
                 }
 61  
                 else {
 62  2780
                         this.setOpaque(true);
 63  
                 }
 64  
 
 65  
                 //Make this big enough to have two lines of text.  Since the 
 66  
                 // FontMetrics object is not yet initialized, we have to guess
 67  
                 // the correct size, based on JLabel size.
 68  2780
                 this.setPreferredSize(new Dimension(200, (int) new JLabel("<html>A<br>B</html>").getPreferredSize().getHeight()));
 69  2780
         }
 70  
         
 71  
         @Override
 72  
         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 73  0
                 if (value instanceof Transaction){
 74  0
                         transaction = (Transaction) value;
 75  
                 }
 76  
                 else {
 77  0
                         transaction = null;
 78  
                 }
 79  
                 
 80  0
                 maxDescriptionLength = list.getWidth() - 230;
 81  
 
 82  0
                 return super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
 83  
         }
 84  
 
 85  
         @Override
 86  
         protected void paintComponent(Graphics g) {
 87  0
                 super.paintComponent(g);
 88  
 
 89  0
                 int height = this.getHeight();
 90  0
                 int width = this.getWidth();
 91  
 
 92  
                 // Antialias text
 93  0
                 ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 94  
                                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
 95  
 
 96  0
                 if (transaction != null){
 97  0
                         Color textColor = g.getColor();
 98  
                         FontMetrics fm;
 99  0
                         int topRowYPos = height / 2 - 5;
 100  0
                         int bottomRowYPos = height - 5;
 101  
 
 102  0
                         f = g.getFont();
 103  0
                         if (simple) {
 104  0
                                 bold = f;
 105  0
                                 italic = f;
 106  
                         }
 107  
                         else {
 108  0
                                 bold = new Font(f.getName(), Font.BOLD, f.getSize());
 109  0
                                 italic = new Font(f.getName(), Font.ITALIC, f.getSize());                                
 110  
                         }
 111  
                         
 112  
                         //Get the font metrics for column alignment
 113  0
                         fm = this.getGraphics().getFontMetrics();
 114  
                         
 115  
                         //Date
 116  0
                         g.drawString(TextFormatter.getDateFormat().format(transaction.getDate()), 10, topRowYPos);
 117  
                         
 118  
                         //Description
 119  0
                         g.setFont(bold);
 120  0
                         g.drawString(
 121  
                                         Formatter.getStringToLength(
 122  
                                         transaction.getDescription() + (cheques && transaction.getNumber() != null && transaction.getNumber().trim().length() > 0 ? "     #" + transaction.getNumber(): ""),
 123  
                                         maxDescriptionLength, fm), 
 124  
                                         150, 
 125  
                                         topRowYPos);
 126  0
                         g.setFont(f);
 127  
 
 128  
                         //Cleared and Reconciled
 129  0
                         if (getAccount() != null){
 130  0
                                 if (showCleared){
 131  0
                                         g.setColor(GREEN);
 132  0
                                         if (getAccount().equals(transaction.getTo()) && transaction.isClearedTo()
 133  
                                                         || getAccount().equals(transaction.getFrom()) && transaction.isClearedFrom())
 134  0
                                                 g.drawString(PrefsModel.getInstance().getTranslator().get(BuddiKeys.SHORT_CLEARED), 20, bottomRowYPos);
 135  0
                                         g.setColor(textColor);
 136  
                                 }
 137  0
                                 if (showReconciled){
 138  0
                                         g.setColor(GREEN);
 139  0
                                         if (getAccount().equals(transaction.getTo()) && transaction.isReconciledTo()
 140  
                                                         || getAccount().equals(transaction.getFrom()) && transaction.isReconciledFrom())
 141  0
                                                 g.drawString(PrefsModel.getInstance().getTranslator().get(BuddiKeys.SHORT_RECONCILED), 30, bottomRowYPos);
 142  0
                                         g.setColor(textColor);
 143  
                                 }
 144  
                         }
 145  
 
 146  
                         //Amount renderer when account is null
 147  0
                         int xPos = 0, minXPos = Integer.MAX_VALUE;
 148  0
                         if (getAccount() == null){
 149  0
                                 fm = this.getGraphics().getFontMetrics();
 150  0
                                 if (transaction != null){
 151  
 
 152  0
                                         long value = transaction.getAmount();
 153  0
                                         xPos = width - 20 - fm.stringWidth(TextFormatter.getFormattedCurrency(value, false, false));
 154  0
                                         if (xPos < minXPos) minXPos = xPos;
 155  0
                                         g.setColor(InternalFormatter.isRed(transaction) ? Color.RED : textColor);
 156  0
                                         g.drawString(TextFormatter.getFormattedCurrency(value, false, false), xPos, bottomRowYPos);
 157  0
                                         g.setColor(textColor);
 158  0
                                 }
 159  
                         }
 160  
                         else {
 161  
                                 //Left column - max 100 px
 162  0
                                 if (transaction.getFrom() != null
 163  
                                                 && transaction.getFrom().equals(getAccount())){
 164  0
                                         xPos = width - 220 - fm.stringWidth(TextFormatter.getFormattedCurrency(transaction.getAmount(), false, false)); 
 165  0
                                         if (xPos < minXPos) minXPos = xPos;
 166  0
                                         g.setColor(InternalFormatter.isRed(transaction, transaction.getTo().equals(getAccount())) ? Color.RED : textColor);
 167  0
                                         g.drawString(TextFormatter.getFormattedCurrency(transaction.getAmount(), false, false), xPos, bottomRowYPos);
 168  0
                                         g.setColor(textColor);
 169  
                                 }
 170  
                                 //Right Column - max 100 px
 171  0
                                 if (transaction.getTo() != null
 172  
                                                 && transaction.getTo().equals(getAccount())){
 173  0
                                         xPos = width - 120 - fm.stringWidth(TextFormatter.getFormattedCurrency(transaction.getAmount(), false, false));
 174  0
                                         if (xPos < minXPos) minXPos = xPos;
 175  0
                                         g.setColor(InternalFormatter.isRed(transaction, transaction.getTo().equals(getAccount())) ? Color.RED : textColor);
 176  0
                                         g.drawString(TextFormatter.getFormattedCurrency(transaction.getAmount(), false, false), xPos, bottomRowYPos);
 177  0
                                         g.setColor(textColor);
 178  
                                 }
 179  
 
 180  
                                 //Balance - max 150px
 181  0
                                 g.setFont(bold);
 182  0
                                 fm = this.getGraphics().getFontMetrics();
 183  0
                                 if (transaction != null){
 184  0
                                         final long balanceValue = transaction.getBalance(getAccount().getUid());
 185  
 
 186  0
                                         xPos = width - 20 - fm.stringWidth(TextFormatter.getFormattedCurrency(balanceValue, false, getAccount().getAccountType().isCredit()));
 187  0
                                         if (xPos < minXPos) minXPos = xPos;
 188  0
                                         g.setColor(InternalFormatter.isRed(getAccount(), balanceValue) ? Color.RED : textColor);
 189  0
                                         g.drawString(TextFormatter.getFormattedCurrency(balanceValue, false, getAccount().getAccountType().isCredit()), xPos, bottomRowYPos);
 190  0
                                         g.setColor(textColor);
 191  
                                 }
 192  
                         }
 193  0
                         g.setFont(f);
 194  
                         
 195  
                         //To / From sources
 196  0
                         g.setFont(italic);
 197  0
                         g.drawString(
 198  
                                         Formatter.getStringToLength(
 199  
                                                         transaction.getFrom().getFullName() + "       " + transaction.getTo().getFullName(),
 200  
                                                         minXPos - 70,
 201  
                                                         fm), 
 202  
                                                         50, 
 203  
                                                         bottomRowYPos);
 204  0
                         fm = this.getGraphics().getFontMetrics();
 205  0
                         int arrowOffset = 50 + fm.stringWidth(transaction.getFrom().getFullName() + " ");
 206  0
                         g.setFont(f);
 207  0
                         g.drawString(PrefsModel.getInstance().getTranslator().get(BuddiKeys.TO), arrowOffset, bottomRowYPos);
 208  
 
 209  0
                         if (transaction.isDeleted()){
 210  0
                                 g.setFont(new Font(f.getName(), Font.BOLD, (int) (f.getSize() * 2.5)));
 211  0
                                 g.setColor(new Color(0, 0, 0, 64));
 212  0
                                 g.drawString(TextFormatter.getTranslation(BuddiKeys.VOID_TRANSACTION), maxDescriptionLength / 2, (int) (f.getSize() * 2.5 + 2));
 213  
                         }
 214  
                 }
 215  0
         }
 216  
         
 217  
         @Override
 218  
         public String getName() {
 219  5596
                 return TextFormatter.getTranslation(BuddiKeys.DEFAULT_TRANSLATION_CELL_RENDERER);
 220  
         }
 221  
 }