Coverage Report - org.homeunix.thecave.buddi.view.swing.SourceListCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
SourceListCellRenderer
79%
27/34
72%
13/18
6
 
 1  
 /*
 2  
  * Created on Aug 7, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.view.swing;
 5  
 
 6  
 import java.awt.Component;
 7  
 import java.lang.reflect.Field;
 8  
 
 9  
 import javax.swing.JComponent;
 10  
 import javax.swing.JList;
 11  
 
 12  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 13  
 import org.homeunix.thecave.buddi.model.Source;
 14  
 import org.homeunix.thecave.buddi.model.swing.SourceComboBoxModel;
 15  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 16  
 
 17  
 public class SourceListCellRenderer extends MaxLengthListCellRenderer {
 18  
         public static final long serialVersionUID = 0;
 19  
         private final String nullLabel; 
 20  
         
 21  
         /**
 22  
          * Creates a new Source list renderer, using the given string
 23  
          * as the label when a source is not selected, with each item having
 24  
          * a maximum length conputed from the specified component.
 25  
          * @param nullLabel
 26  
          * @param component
 27  
          */
 28  
         public SourceListCellRenderer(String nullLabel, JComponent component) {
 29  2433
                 super(component);
 30  2433
                 this.nullLabel = nullLabel;
 31  2433
         }
 32  
         
 33  
                 
 34  
         @Override
 35  
         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 36  
                 //Find correct string
 37  32298
                 Object newValue = "";
 38  32298
                 if (value instanceof Source)
 39  13651
                         newValue = ((Source) value).getFullName();
 40  18647
                 else if (BuddiKeys.SPLITS.toString().equals(value))
 41  2717
                         newValue = TextFormatter.getTranslation(value.toString());
 42  15930
                 else if (index == -1)
 43  14308
                         newValue = nullLabel;
 44  
                 else
 45  1622
                         newValue = value;
 46  
                 
 47  
                 //Use the MaxLength renderer to cut off items which are too long
 48  32298
                 super.getListCellRendererComponent(list, newValue, index, isSelected, cellHasFocus);
 49  
                 
 50  
                 //Color items correctly
 51  32298
                 if (value instanceof Source){
 52  
                         //This will make the credit / expense sources red.  I don't really like this...
 53  
 //                        if ((value instanceof Account && ((Account) value).getAccountType().isCredit())
 54  
 //                                        || (value instanceof BudgetCategory && !((BudgetCategory) value).isIncome()))
 55  
 //                                this.setText("<font color='red'>" + this.getText() + "</font>");
 56  13651
                         Source source = (Source) value;
 57  13651
                         if (source.isDeleted()){
 58  0
                                 this.setText("<strike>" + this.getText() + "</strike>");
 59  
                         }
 60  
                         try {
 61  13651
                                 Field dataModelField = JList.class.getDeclaredField("dataModel");
 62  13651
                                 dataModelField.setAccessible(true);
 63  13651
                                 Object dataModel = dataModelField.get(list);
 64  13651
                                 if (dataModel instanceof SourceComboBoxModel){
 65  13651
                                         SourceComboBoxModel model = (SourceComboBoxModel) dataModel;
 66  13651
                                         if (model.getLevel(source) > 0){
 67  0
                                                 StringBuilder sb = new StringBuilder();
 68  0
                                                 for (int i = 0; i < model.getLevel(source); i++){
 69  0
                                                         sb.append("&nbsp;&nbsp;");
 70  
                                                 }
 71  0
                                                 this.setText(sb.toString() + this.getText());
 72  
                                         }
 73  
                                 }
 74  
                         }
 75  0
                         catch (Throwable t){
 76  0
                                 t.printStackTrace();
 77  
                                 //Ignore; this is only for prettiness, it is not critical.
 78  13651
                         }
 79  13651
                         this.setText("<html>" + this.getText() + "</html>");
 80  13651
                 }
 81  18647
                 else if (!BuddiKeys.SPLITS.toString().equals(value))
 82  15930
                         this.setText("<html><font color='gray'>" + this.getText() + "</font></html>");
 83  
                 
 84  32298
                 return this;
 85  
         }
 86  
 }