001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the 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, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * ExpObjectCellRenderer.java
028: *
029: * Created on September 21, 2006, 12:35 AM
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.expbuilder;
034:
035: import java.awt.Color;
036: import java.awt.Component;
037: import javax.swing.JList;
038: import javax.swing.JTextPane;
039: import javax.swing.ListCellRenderer;
040: import javax.swing.text.Style;
041: import javax.swing.text.StyleConstants;
042: import javax.swing.text.StyledDocument;
043: import javax.swing.text.DefaultStyledDocument;
044:
045: public class ExpObjectCellRenderer extends JTextPane implements
046: ListCellRenderer {
047: private Color selectionBackground;
048: private Color background;
049:
050: // Create a style object and then set the style attributes
051: Style typeStyle = null;
052: Style classTypeStyle = null;
053:
054: Style parameterStyle = null;
055: Style variableStyle = null;
056: Style fieldStyle = null;
057: Style whiteStyle = null;
058:
059: public ExpObjectCellRenderer(JList list) {
060: super ();
061: selectionBackground = list.getSelectionBackground();
062: background = list.getBackground();
063: StyledDocument doc = new DefaultStyledDocument();
064: this .setDocument(doc);
065:
066: typeStyle = doc.addStyle("typeStyle", null);
067: StyleConstants.setItalic(typeStyle, true);
068: StyleConstants.setForeground(typeStyle, Color.gray);
069:
070: classTypeStyle = doc.addStyle("classTypeStyle", null);
071: StyleConstants.setForeground(classTypeStyle, Color.gray);
072:
073: parameterStyle = doc.addStyle("parameterStyle", null);
074: StyleConstants
075: .setForeground(parameterStyle, Color.red.darker());
076:
077: variableStyle = doc.addStyle("variableStyle", null);
078: StyleConstants.setForeground(variableStyle, Color.blue);
079:
080: fieldStyle = doc.addStyle("fieldStyle", null);
081: StyleConstants.setForeground(fieldStyle, Color.green.darker()
082: .darker());
083:
084: whiteStyle = doc.addStyle("whiteStyle", null);
085: StyleConstants.setForeground(whiteStyle, Color.white);
086:
087: }
088:
089: public Component getListCellRendererComponent(JList list,
090: Object object, int index, boolean isSelected,
091: boolean cellHasFocus) {
092:
093: this .setText("");
094: StyledDocument doc = (StyledDocument) this .getDocument();
095: if (object instanceof ExpObject) {
096: ExpObject eo = (ExpObject) object;
097:
098: try {
099:
100: doc.insertString(doc.getLength(), eo.getName() + " ",
101: (isSelected) ? whiteStyle : null);
102:
103: Style s = parameterStyle;
104: String type = "Parameter";
105: if (eo.getType() == eo.TYPE_FIELD) {
106: s = fieldStyle;
107: type = "Field";
108: } else if (eo.getType() == eo.TYPE_VARIABLE) {
109: s = variableStyle;
110: type = "Variable";
111: }
112:
113: if (isSelected)
114: s = whiteStyle;
115:
116: doc.insertString(doc.getLength(), type + " ", s);
117:
118: String tp = eo.getClassType() + "";
119: if (tp.lastIndexOf(".") > 0)
120: tp = tp.substring(tp.lastIndexOf(".") + 1);
121:
122: doc.insertString(doc.getLength(), tp, classTypeStyle);
123: } catch (Exception ex) {
124: }
125: } else {
126: try {
127: doc.insertString(doc.getLength(), "" + object, null);
128: } catch (Exception ex) {
129: }
130: }
131: setBackground(isSelected ? selectionBackground : background);
132: return this;
133: }
134: }
|