001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.util.SStringBuilder;
016:
017: /**
018: * Default implementation of a {@link SListCellRenderer}.
019: *
020: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
021: */
022: public class SDefaultListCellRenderer extends SLabel implements
023: SListCellRenderer {
024:
025: private SStringBuilder nameBuffer = new SStringBuilder();
026: /**
027: * Style class to use for the foreground for selected nodes.
028: */
029: protected String selectionStyle = null;
030:
031: /**
032: * Style class to use for the foreground for non-selected nodes.
033: */
034: protected String nonSelectionStyle = null;
035:
036: /**
037: * Create a SDefaultListCellRenderer with default properties.
038: */
039: public SDefaultListCellRenderer() {
040: }
041:
042: /**
043: * Sets the style the cell is drawn with when the cell is selected.
044: */
045: public void setSelectionStyle(String newStyle) {
046: selectionStyle = newStyle;
047: }
048:
049: /**
050: * Returns the style the cell is drawn with when the cell is selected.
051: */
052: public String getSelectionStyle() {
053: return selectionStyle;
054: }
055:
056: /**
057: * Sets the style the cell is drawn with when the cell isn't selected.
058: */
059: public void setNonSelectionStyle(String newStyle) {
060: nonSelectionStyle = newStyle;
061: }
062:
063: /**
064: * Returns the style the cell is drawn with when the cell isn't selected.
065: */
066: public String getNonSelectionStyle() {
067: return nonSelectionStyle;
068: }
069:
070: public SComponent getListCellRendererComponent(SComponent list,
071: Object value, boolean selected, int row) {
072: setNameRaw(name(list, row));
073: setText(null);
074: setIcon(null);
075:
076: SComponent rendererComponent = this ;
077: if (value == null)
078: setText("");
079: else if (value instanceof SIcon)
080: setIcon((SIcon) value);
081: else if (value instanceof SComponent)
082: rendererComponent = (SComponent) value;
083: else
084: setText(value.toString());
085:
086: if (selected && selectionStyle != null) {
087: rendererComponent.setStyle(selectionStyle);
088: } else {
089: rendererComponent.setStyle(nonSelectionStyle);
090: }
091:
092: return rendererComponent;
093: }
094:
095: protected String name(SComponent component, int row) {
096: nameBuffer.setLength(0);
097: nameBuffer.append(component.getName()).append(
098: SConstants.UID_DIVIDER).append(row);
099: return nameBuffer.toString();
100: }
101: }
|