KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > VCellRenderer


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.grid.ed;
15
16 import javax.swing.table.*;
17 import java.awt.*;
18 import java.text.*;
19 import javax.swing.*;
20 import java.util.*;
21
22 import org.compiere.util.*;
23 import org.compiere.model.*;
24 import org.compiere.apps.*;
25 import org.compiere.plaf.*;
26
27 /**
28  * Table Cell Renderer based on DisplayType
29  *
30  * @author Jorg Janke
31  * @version $Id: VCellRenderer.java,v 1.10 2003/10/27 15:22:02 jjanke Exp $
32  */

33 public final class VCellRenderer extends DefaultTableCellRenderer
34 {
35     /**
36      * Constructor for Grid
37      * @param mField field model
38      */

39     public VCellRenderer(MField mField)
40     {
41         this (mField.getDisplayType());
42         m_columnName = mField.getColumnName();
43         this.setName(m_columnName);
44         m_lookup = mField.getLookup();
45         m_password = mField.isEncryptedField();
46     } // VCellRenderer
47

48     /**
49      * Constructor for MiniGrid
50      * @param displayType Display Type
51      */

52     public VCellRenderer (int displayType)
53     {
54         super();
55         m_displayType = displayType;
56         // Number
57
if (DisplayType.isNumeric(m_displayType))
58         {
59             m_numberFormat = DisplayType.getNumberFormat(m_displayType);
60             setHorizontalAlignment(JLabel.RIGHT);
61         }
62         // Date
63
else if (DisplayType.isDate(m_displayType))
64             m_dateFormat = DisplayType.getDateFormat(m_displayType);
65         //
66
else if (m_displayType == DisplayType.YesNo)
67         {
68             m_check = new JCheckBox();
69             m_check.setMargin(new Insets(0,0,0,0));
70             m_check.setHorizontalAlignment(JLabel.CENTER);
71             m_check.setOpaque(true);
72         }
73     } // VCellRenderer
74

75     private int m_displayType;
76     private String JavaDoc m_columnName = null;
77     private Lookup m_lookup = null;
78     private boolean m_password = false;
79     //
80
private SimpleDateFormat m_dateFormat = null;
81     private DecimalFormat m_numberFormat = null;
82     private JCheckBox m_check = null;
83
84     /**
85      * Get TableCell RendererComponent
86      *
87      * @param table table
88      * @param value value
89      * @param isSelected selected
90      * @param hasFocus focus
91      * @param row row
92      * @param col col
93      * @return component
94      */

95     public Component getTableCellRendererComponent (JTable table, Object JavaDoc value,
96         boolean isSelected, boolean hasFocus, int row, int col)
97     {
98     // Log.trace(Log.l5_DData, "VCellRenderer.getTableCellRendererComponent - " + (value == null ? "null" : value.toString()),
99
// "Row=" + row + ", Col=" + col);
100

101         Component c = null;
102         if (m_displayType == DisplayType.YesNo)
103             c = m_check;
104         else
105         {
106             c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
107             c.setFont(CompierePLAF.getFont_Field());
108         }
109
110         // Background & Foreground
111
Color bg = CompierePLAF.getFieldBackground_Normal();
112         Color fg = CompierePLAF.getTextColor_Normal();
113         // Selected is white on blue in Windows
114
if (isSelected && !hasFocus)
115         {
116             bg = table.getSelectionBackground();
117             fg = table.getSelectionForeground();
118         }
119         // row not selected or field has focus
120
else
121         {
122             // Foregroundw
123
int cCode = 0;
124             // Grid
125
if (table instanceof org.compiere.grid.VTable)
126                 cCode = ((org.compiere.grid.VTable)table).getColorCode (row);
127             // MiniGrid
128
else if (table instanceof org.compiere.minigrid.MiniTable)
129                 cCode = ((org.compiere.minigrid.MiniTable)table).getColorCode (row);
130             //
131
if (cCode == 0)
132                 ;
133             else if (cCode < 0)
134                 fg = CompierePLAF.getTextColor_Issue();
135             else
136                 fg = CompierePLAF.getTextColor_OK();
137             // Inactive Background
138
if (!table.isCellEditable(row, col))
139                 bg = CompierePLAF.getFieldBackground_Inactive();
140         }
141         // Set Color
142
c.setBackground(bg);
143         c.setForeground(fg);
144         //
145
// Log.trace(Log.l6_Database, "r=" + row + " c=" + col, // + " - " + c.getClass().getName(),
146
// "sel=" + isSelected + ", focus=" + hasFocus + ", edit=" + table.isCellEditable(row, col));
147
// Log.trace(7, "BG=" + (bg.equals(Color.white) ? "white" : bg.toString()), "FG=" + (fg.equals(Color.black) ? "black" : fg.toString()));
148

149         // Format it
150
setValue(value);
151         return c;
152     } // getTableCellRendererComponent
153

154
155     /**
156      * Format Display Value
157      * @param value (key)value
158      */

159     protected void setValue (Object JavaDoc value)
160     {
161         String JavaDoc retValue = null;
162         try
163         {
164             // Checkbox
165
if (m_displayType == DisplayType.YesNo)
166             {
167                 m_check.setSelected("Y".equals(value));
168                 return;
169             }
170             else if (value == null)
171                 ;
172             // Number
173
else if (DisplayType.isNumeric(m_displayType))
174                 retValue = m_numberFormat.format(value);
175             // Date
176
else if (DisplayType.isDate(m_displayType))
177                 retValue = m_dateFormat.format(value);
178             // Row ID
179
else if (m_displayType == DisplayType.RowID)
180                 retValue = "";
181             // Lookup
182
else if (m_lookup != null && (DisplayType.isLookup(m_displayType)
183                     || m_displayType == DisplayType.Location
184                     || m_displayType == DisplayType.Account
185                     || m_displayType == DisplayType.Locator
186                     || m_displayType == DisplayType.PAttribute ))
187                 retValue = m_lookup.getDisplay(value);
188             // Button
189
else if (m_displayType == DisplayType.Button)
190             {
191                 if ("Record_ID".equals(m_columnName))
192                     retValue = "#" + value + "#";
193                 else
194                     retValue = null;
195             }
196             // Password (fixed string)
197
else if (m_password)
198                 retValue = "**********";
199             // other (String ...)
200
else
201             {
202                 super.setValue(value);
203                 return;
204             }
205         }
206         catch (Exception JavaDoc e)
207         {
208             Log.error("VCellRenderer - setValue (" + value + ") " + value.getClass().getName() , e);
209             retValue = value.toString();
210         }
211         super.setValue(retValue);
212     } // setValue
213

214     /**
215      * to String
216      * @return String representation
217      */

218     public String JavaDoc toString()
219     {
220         return "VCellRenderer - DisplayType=" + m_displayType + " - " + m_lookup;
221     } // toString
222

223     public void dispose()
224     {
225         if (m_lookup != null)
226             m_lookup.dispose();
227         m_lookup = null;
228     } // dispose
229

230 } // VCellRenderer
231
Popular Tags