Coverage Report - net.mtu.eggplant.util.gui.SmartRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
SmartRenderer
0%
0/32
0%
0/14
2.6
 
 1  
 /*
 2  
  * Copyright (c) 2000
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.util.gui;
 29  
 
 30  
 import java.awt.Component;
 31  
 
 32  
 import javax.swing.DefaultListCellRenderer;
 33  
 import javax.swing.Icon;
 34  
 import javax.swing.JLabel;
 35  
 import javax.swing.JList;
 36  
 import javax.swing.JTable;
 37  
 import javax.swing.JTree;
 38  
 import javax.swing.ListCellRenderer;
 39  
 import javax.swing.table.DefaultTableCellRenderer;
 40  
 import javax.swing.table.TableCellRenderer;
 41  
 import javax.swing.tree.DefaultTreeCellRenderer;
 42  
 import javax.swing.tree.TreeCellRenderer;
 43  
 
 44  
 import net.mtu.eggplant.util.Named;
 45  
 
 46  
 /**
 47  
  * <p>Class that knows how to render lots of things.  If you want to create a
 48  
  * renderer, just subclass this SmartRenderer and override the
 49  
  * getStringValue() method.  For any object that is not a known type toString
 50  
  * is used.  Null values are always displayed as "NULL".  This class counts on
 51  
  * the fact that the default renderers for list, trable and tree all return a
 52  
  * subclass of JLabel.  If that ever changes, this class breaks.<p>
 53  
  *
 54  
  * <p>Known types</p>
 55  
  * <ul>
 56  
  *   <li>Named</li>
 57  
  *   <li>Icon</li>
 58  
  * </ul>
 59  
  * 
 60  
  * @version $Revision$
 61  
  */
 62  
 public class SmartRenderer implements ListCellRenderer, TableCellRenderer, TreeCellRenderer {
 63  
 
 64  0
   public SmartRenderer() {
 65  0
     _treeRenderer = new DefaultTreeCellRenderer();
 66  0
     _listRenderer = new DefaultListCellRenderer();
 67  0
     _tableRenderer = new DefaultTableCellRenderer();
 68  0
   }
 69  
 
 70  
   private DefaultTreeCellRenderer _treeRenderer;
 71  
   private DefaultListCellRenderer _listRenderer;
 72  
   private DefaultTableCellRenderer _tableRenderer;
 73  
   
 74  
   public final Component getListCellRendererComponent(final JList list,
 75  
                                                       final Object value,
 76  
                                                       final int index,
 77  
                                                       final boolean isSelected,
 78  
                                                       final boolean cellHasFocus) {
 79  
 
 80  0
     final JLabel renderer = (JLabel)_listRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
 81  
     final String text;
 82  0
     if(value == null) {
 83  0
       text = "NULL";
 84  0
     } else if (value instanceof Icon) {
 85  0
       text = "";
 86  
     } else {
 87  0
       text = getStringValue(value);
 88  
     }
 89  0
     renderer.setText(text);
 90  
     
 91  0
     return renderer;
 92  
   }
 93  
 
 94  
   public final Component getTableCellRendererComponent(final JTable table,
 95  
                                                        final Object value,
 96  
                                                        final boolean isSelected,
 97  
                                                        final boolean hasFocus,
 98  
                                                        final int row,
 99  
                                                        final int column) {
 100  
 
 101  
 
 102  0
     final JLabel renderer = (JLabel)_tableRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 103  
     final String text;
 104  0
     if(value == null) {
 105  0
       text = "NULL";
 106  0
     } else if (value instanceof Icon) {
 107  0
       text = "";
 108  
     } else {
 109  0
       text = getStringValue(value);
 110  
     }
 111  0
     renderer.setText(text);
 112  
     
 113  0
     return renderer;
 114  
   }
 115  
 
 116  
   public Component getTreeCellRendererComponent(final JTree tree,
 117  
                                                 final Object value,
 118  
                                                 final boolean selected,
 119  
                                                 final boolean expanded,
 120  
                                                 final boolean leaf,
 121  
                                                 final int row,
 122  
                                                 final boolean hasFocus) {
 123  
 
 124  0
     final JLabel renderer = (JLabel)_treeRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
 125  
     final String text;
 126  0
     if(value == null) {
 127  0
       text = "NULL";
 128  0
     } else if (value instanceof Icon) {
 129  0
       text = "";
 130  
     } else {
 131  0
       text = getStringValue(value);
 132  
     }
 133  0
     renderer.setText(text);
 134  
     
 135  0
     return renderer;
 136  
   }
 137  
 
 138  
   /**
 139  
      Get the string representation of this object.
 140  
   **/
 141  
   public String getStringValue(final Object value) {
 142  0
     if(value instanceof Named) {
 143  0
       return ((Named)value).getName();
 144  
     } else {
 145  0
       return value.toString();
 146  
     }
 147  
 
 148  
   }
 149  
 
 150  
   //final private static Border _emptyBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
 151  
   
 152  
 }