KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > expbuilder > ExpObjectCellRenderer


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * ExpObjectCellRenderer.java
28  *
29  * Created on September 21, 2006, 12:35 AM
30  *
31  */

32
33 package it.businesslogic.ireport.gui.expbuilder;
34
35 import java.awt.Color JavaDoc;
36 import java.awt.Component JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.JTextPane JavaDoc;
39 import javax.swing.ListCellRenderer JavaDoc;
40 import javax.swing.text.Style JavaDoc;
41 import javax.swing.text.StyleConstants JavaDoc;
42 import javax.swing.text.StyledDocument JavaDoc;
43 import javax.swing.text.DefaultStyledDocument JavaDoc;
44
45 public class ExpObjectCellRenderer extends JTextPane JavaDoc implements ListCellRenderer JavaDoc {
46         private Color JavaDoc selectionBackground;
47         private Color JavaDoc background;
48         
49         // Create a style object and then set the style attributes
50
Style JavaDoc typeStyle = null;
51         Style JavaDoc classTypeStyle = null;
52         
53         Style JavaDoc parameterStyle = null;
54         Style JavaDoc variableStyle = null;
55         Style JavaDoc fieldStyle = null;
56         Style JavaDoc whiteStyle = null;
57
58         public ExpObjectCellRenderer(JList JavaDoc list) {
59             super();
60             selectionBackground = list.getSelectionBackground();
61             background = list.getBackground();
62             StyledDocument JavaDoc doc = new DefaultStyledDocument JavaDoc();
63             this.setDocument( doc );
64             
65             typeStyle = doc.addStyle("typeStyle", null);
66             StyleConstants.setItalic(typeStyle, true);
67             StyleConstants.setForeground(typeStyle, Color.gray);
68             
69             classTypeStyle = doc.addStyle("classTypeStyle", null);
70             StyleConstants.setForeground(classTypeStyle, Color.gray);
71             
72             parameterStyle = doc.addStyle("parameterStyle", null);
73             StyleConstants.setForeground(parameterStyle, Color.red.darker());
74             
75             variableStyle = doc.addStyle("variableStyle", null);
76             StyleConstants.setForeground(variableStyle, Color.blue);
77             
78             fieldStyle = doc.addStyle("fieldStyle", null);
79             StyleConstants.setForeground(fieldStyle, Color.green.darker().darker());
80             
81             whiteStyle = doc.addStyle("whiteStyle", null);
82             StyleConstants.setForeground(whiteStyle, Color.white);
83             
84         }
85         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc object,
86                 int index, boolean isSelected, boolean cellHasFocus) {
87             
88             this.setText("");
89             StyledDocument JavaDoc doc = (StyledDocument JavaDoc)this.getDocument();
90             if (object instanceof ExpObject)
91             {
92                 ExpObject eo = (ExpObject)object;
93                  
94                  try {
95                      
96                      doc.insertString(doc.getLength(), eo.getName() + " ", (isSelected) ? whiteStyle : null);
97                      
98                      Style JavaDoc s = parameterStyle;
99                      String JavaDoc type = "Parameter";
100                      if (eo.getType() == eo.TYPE_FIELD)
101                      {
102                          s = fieldStyle;
103                          type = "Field";
104                      }
105                      else if (eo.getType() == eo.TYPE_VARIABLE)
106                      {
107                          s = variableStyle;
108                          type = "Variable";
109                      }
110                      
111                      if (isSelected) s = whiteStyle;
112                      
113                      
114                      doc.insertString(doc.getLength(), type + " ", s);
115                      
116                      String JavaDoc tp = eo.getClassType() + "";
117                      if (tp.lastIndexOf(".") > 0) tp = tp.substring(tp.lastIndexOf(".")+1);
118                      
119                      doc.insertString(doc.getLength(), tp, classTypeStyle);
120                 } catch (Exception JavaDoc ex){}
121             }
122             else
123             {
124                  try {
125                 doc.insertString(doc.getLength(), "" + object, null);
126                  } catch (Exception JavaDoc ex){}
127             }
128             setBackground(isSelected ? selectionBackground : background);
129             return this;
130         }
131     }
132
133
Popular Tags