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<Object>, 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  
   @Override
 75  
   public final Component getListCellRendererComponent(final JList<?> list,
 76  
                                                       final Object value,
 77  
                                                       final int index,
 78  
                                                       final boolean isSelected,
 79  
                                                       final boolean cellHasFocus) {
 80  
 
 81  0
     final JLabel renderer = (JLabel)_listRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
 82  
     final String text;
 83  0
     if(value == null) {
 84  0
       text = "NULL";
 85  0
     } else if (value instanceof Icon) {
 86  0
       text = "";
 87  
     } else {
 88  0
       text = getStringValue(value);
 89  
     }
 90  0
     renderer.setText(text);
 91  
     
 92  0
     return renderer;
 93  
   }
 94  
 
 95  
   public final Component getTableCellRendererComponent(final JTable table,
 96  
                                                        final Object value,
 97  
                                                        final boolean isSelected,
 98  
                                                        final boolean hasFocus,
 99  
                                                        final int row,
 100  
                                                        final int column) {
 101  
 
 102  
 
 103  0
     final JLabel renderer = (JLabel)_tableRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 104  
     final String text;
 105  0
     if(value == null) {
 106  0
       text = "NULL";
 107  0
     } else if (value instanceof Icon) {
 108  0
       text = "";
 109  
     } else {
 110  0
       text = getStringValue(value);
 111  
     }
 112  0
     renderer.setText(text);
 113  
     
 114  0
     return renderer;
 115  
   }
 116  
 
 117  
   public Component getTreeCellRendererComponent(final JTree tree,
 118  
                                                 final Object value,
 119  
                                                 final boolean selected,
 120  
                                                 final boolean expanded,
 121  
                                                 final boolean leaf,
 122  
                                                 final int row,
 123  
                                                 final boolean hasFocus) {
 124  
 
 125  0
     final JLabel renderer = (JLabel)_treeRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
 126  
     final String text;
 127  0
     if(value == null) {
 128  0
       text = "NULL";
 129  0
     } else if (value instanceof Icon) {
 130  0
       text = "";
 131  
     } else {
 132  0
       text = getStringValue(value);
 133  
     }
 134  0
     renderer.setText(text);
 135  
     
 136  0
     return renderer;
 137  
   }
 138  
 
 139  
   /**
 140  
      Get the string representation of this object.
 141  
   **/
 142  
   public String getStringValue(final Object value) {
 143  0
     if(value instanceof Named) {
 144  0
       return ((Named)value).getName();
 145  
     } else {
 146  0
       return value.toString();
 147  
     }
 148  
 
 149  
   }
 150  
 
 151  
   //final private static Border _emptyBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
 152  
   
 153  
 }