View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: SListCellRenderer.java,v 1.7 2002/09/05 15:41:49 ludovicc Exp $ 37 */ 38 package org.scopemvc.view.swing; 39 40 41 import java.awt.Component; 42 import javax.swing.DefaultListCellRenderer; 43 import javax.swing.Icon; 44 import javax.swing.JList; 45 import javax.swing.UIManager; 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import org.scopemvc.core.PropertyManager; 49 import org.scopemvc.core.Selector; 50 import org.scopemvc.util.convertor.StringConvertor; 51 import org.scopemvc.util.convertor.StringConvertors; 52 53 /*** 54 * <P> 55 * 56 * A javax.swing.DefaultListCellRenderer that tries to draw model objects as 57 * text with an icon, both from properties on the displayed model object 58 * identified by Selectors. If both Selectors don't work then same behaviour as 59 * DefaultListCellRenderer. If string convertor is not set then default one is 60 * used. </P> 61 * 62 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 63 * @created 05 September 2002 64 * @version $Revision: 1.7 $ $Date: 2002/09/05 15:41:49 $ 65 */ 66 public class SListCellRenderer extends DefaultListCellRenderer { 67 68 private final static Log LOG = LogFactory.getLog(SListCellRenderer.class); 69 70 private Selector textSelector; 71 private Selector iconSelector; 72 private StringConvertor convertor; 73 74 /*** 75 * Gets the text selector 76 * 77 * @return The textSelector value 78 */ 79 public final Selector getTextSelector() { 80 return textSelector; 81 } 82 83 84 /*** 85 * Gets the icon selector 86 * 87 * @return The iconSelector value 88 */ 89 public final Selector getIconSelector() { 90 return iconSelector; 91 } 92 93 94 /*** 95 * Gets the list cell renderer component 96 * 97 * @param list TODO: Describe the Parameter 98 * @param value TODO: Describe the Parameter 99 * @param index TODO: Describe the Parameter 100 * @param isSelected TODO: Describe the Parameter 101 * @param cellHasFocus TODO: Describe the Parameter 102 * @return The listCellRendererComponent value 103 */ 104 public Component getListCellRendererComponent( 105 JList list, 106 Object value, 107 int index, 108 boolean isSelected, 109 boolean cellHasFocus) { 110 // A lot of this is copied from DefaultListCellRenderer in JDK1.3 111 if (LOG.isDebugEnabled()) { 112 LOG.debug("getListCellRendererComponent: " + value); 113 } 114 115 if (list == null) { 116 setText(""); 117 setIcon(null); 118 return this; 119 } 120 121 setComponentOrientation(list.getComponentOrientation()); 122 123 if (isSelected) { 124 setBackground(list.getSelectionBackground()); 125 setForeground(list.getSelectionForeground()); 126 } else { 127 setBackground(list.getBackground()); 128 setForeground(list.getForeground()); 129 } 130 131 // value can be null, becouse we can be used also in combobox 132 // Try to get text and/or icon properties from the passed value 133 PropertyManager manager = null; 134 if (value != null) { 135 manager = PropertyManager.getInstance(value); 136 } 137 138 String text = null; 139 Icon icon = null; 140 141 // ***** Need to cache some of this for common case where List contains models of same Class 142 143 // text 144 if (manager != null && textSelector != null) { 145 try { 146 if (convertor == null) { 147 convertor = StringConvertors.forClass(manager.getPropertyClass(value, textSelector)); 148 } 149 Object textProperty = manager.get(value, textSelector); 150 if (convertor != null) { 151 text = convertor.valueAsString(textProperty); 152 } else { 153 text = (textProperty == null) ? "" : textProperty.toString(); 154 } 155 } catch (Exception e) { 156 // ignore and leave text == null 157 } 158 } 159 160 // icon 161 if (manager != null && iconSelector != null) { 162 try { 163 Object iconProperty = manager.get(value, iconSelector); 164 if (iconProperty instanceof Icon) { 165 icon = (Icon) iconProperty; 166 } 167 } catch (Exception e) { 168 // ignore and leave icon == null 169 } 170 } 171 172 if (text == null && icon == null) { 173 // Act like DefaultListCellRenderer 174 if (value instanceof Icon) { 175 setIcon((Icon) value); 176 setText(""); 177 } else { 178 setIcon(null); 179 setText((value == null) ? "" : value.toString()); 180 } 181 } else { 182 if (text == null) { 183 text = ""; 184 } 185 setText(text); 186 setIcon(icon); 187 } 188 189 setEnabled(list.isEnabled()); 190 setFont(list.getFont()); 191 setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder); 192 193 return this; 194 } 195 196 197 /*** 198 * Sets the text selector 199 * 200 * @param inSelector The new textSelector value 201 */ 202 public final void setTextSelector(Selector inSelector) { 203 textSelector = inSelector; 204 } 205 206 207 /*** 208 * Sets the text selector string 209 * 210 * @param inSelectorString The new textSelectorString value 211 */ 212 public final void setTextSelectorString(String inSelectorString) { 213 if (inSelectorString == null) { 214 setTextSelector((Selector) null); 215 } else { 216 setTextSelector(Selector.fromString(inSelectorString)); 217 } 218 } 219 220 /*** 221 * Sets the string convertor 222 * 223 * @param inConvertor The new stringConvertor value 224 * @throws IllegalArgumentException TODO: Describe the Exception 225 */ 226 public final void setStringConvertor(StringConvertor inConvertor) 227 throws IllegalArgumentException { 228 if (inConvertor == null) { 229 throw new IllegalArgumentException("StringConvertor argument cannot" 230 + " be null"); 231 } 232 convertor = inConvertor; 233 } 234 235 236 /*** 237 * Sets the icon selector 238 * 239 * @param inSelector The new iconSelector value 240 */ 241 public final void setIconSelector(Selector inSelector) { 242 iconSelector = inSelector; 243 } 244 245 246 /*** 247 * Sets the icon selector string 248 * 249 * @param inSelectorString The new iconSelectorString value 250 */ 251 public final void setIconSelectorString(String inSelectorString) { 252 if (inSelectorString == null) { 253 setIconSelector((Selector) null); 254 } else { 255 setIconSelector(Selector.fromString(inSelectorString)); 256 } 257 } 258 }

This page was automatically generated by Maven