001: /*
002: * ListLineRenderer.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Graphics;
025: import java.awt.FontMetrics;
026: import java.awt.Insets;
027: import java.awt.Component;
028:
029: import javax.swing.UIManager;
030: import javax.swing.border.Border;
031: import javax.swing.JLabel;
032: import javax.swing.border.EmptyBorder;
033: import javax.swing.JList;
034: import javax.swing.ListCellRenderer;
035:
036: import java.util.StringTokenizer;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: * The ListLineRenderer class provides a renderer for a JList where
047: * the contents may be text with new line characters.
048: * It removes the system new line characters (square symbols)
049: * from each statement for display.
050: *
051: * @author Takis Diakoumis
052: * @version $Revision: 1.4 $
053: * @date $Date: 2006/05/14 06:56:07 $
054: */
055: public class ListLineRenderer extends JLabel implements
056: ListCellRenderer {
057:
058: protected static Border noFocusBorder;
059: protected FontMetrics fMetrics = null;
060: protected Insets insets = new Insets(0, 0, 0, 0);
061:
062: protected int defaultLineSpace = 1;
063:
064: /**
065: * Constructor
066: */
067: public ListLineRenderer() {
068: super ();
069: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
070: setOpaque(true);
071: setBorder(noFocusBorder);
072: }
073:
074: public Component getListCellRendererComponent(JList list,
075: Object value, int index, boolean isSelected,
076: boolean cellHasFocus) {
077: setText(value.toString());
078: setBackground(isSelected ? list.getSelectionBackground() : list
079: .getBackground());
080: setForeground(isSelected ? list.getSelectionForeground() : list
081: .getForeground());
082:
083: setFont(list.getFont());
084: setBorder((cellHasFocus) ? UIManager
085: .getBorder("List.focusCellHighlightBorder")
086: : noFocusBorder);
087:
088: return this ;
089: }
090:
091: public void setDefaultLineSpace(int defaultLine) {
092: defaultLineSpace = defaultLine;
093: }
094:
095: public int getDefaultLineSpace() {
096: return defaultLineSpace;
097: }
098:
099: public int getTab(int index) {
100: return defaultLineSpace * index;
101: }
102:
103: public void paint(Graphics g) {
104: fMetrics = g.getFontMetrics();
105:
106: g.setColor(getBackground());
107: g.fillRect(0, 0, getWidth(), getHeight());
108: getBorder().paintBorder(this , g, 0, 0, getWidth(), getHeight());
109:
110: g.setColor(getForeground());
111: g.setFont(getFont());
112: insets = getInsets(insets);
113: int x = insets.left;
114: int y = insets.top + fMetrics.getAscent();
115:
116: StringTokenizer st = new StringTokenizer(getText(), "\n");
117: while (st.hasMoreTokens()) {
118: String sNext = st.nextToken();
119: g.drawString(sNext, x, y);
120: x += fMetrics.stringWidth(sNext);
121:
122: if (!st.hasMoreTokens())
123: break;
124:
125: int index = 0;
126: while (x >= getTab(index))
127: index++;
128:
129: x = getTab(index);
130: }
131: }
132:
133: }
|