001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing.renderer;
018:
019: import com.l2fprod.common.model.DefaultObjectRenderer;
020: import com.l2fprod.common.model.ObjectRenderer;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.SystemColor;
025:
026: import javax.swing.Icon;
027: import javax.swing.JList;
028: import javax.swing.JTable;
029: import javax.swing.ListCellRenderer;
030: import javax.swing.table.DefaultTableCellRenderer;
031:
032: /**
033: * DefaultCellRenderer.<br>
034: *
035: */
036: public class DefaultCellRenderer extends DefaultTableCellRenderer
037: implements ListCellRenderer {
038:
039: private ObjectRenderer objectRenderer = new DefaultObjectRenderer();
040:
041: private Color oddBackgroundColor = SystemColor.window;
042: private Color evenBackgroundColor = SystemColor.window;
043: private boolean showOddAndEvenRows = true;
044:
045: public void setOddBackgroundColor(Color c) {
046: oddBackgroundColor = c;
047: }
048:
049: public void setEvenBackgroundColor(Color c) {
050: evenBackgroundColor = c;
051: }
052:
053: public void setShowOddAndEvenRows(boolean b) {
054: showOddAndEvenRows = b;
055: }
056:
057: public Component getListCellRendererComponent(JList list,
058: Object value, int index, boolean isSelected,
059: boolean cellHasFocus) {
060:
061: setBorder(null);
062:
063: if (isSelected) {
064: setBackground(list.getSelectionBackground());
065: setForeground(list.getSelectionForeground());
066: } else {
067: setBackground(list.getBackground());
068: setForeground(list.getForeground());
069: }
070:
071: setValue(value);
072:
073: return this ;
074: }
075:
076: public Component getTableCellRendererComponent(JTable table,
077: Object value, boolean isSelected, boolean hasFocus,
078: int row, int column) {
079: super .getTableCellRendererComponent(table, value, isSelected,
080: hasFocus, row, column);
081:
082: if (showOddAndEvenRows && !isSelected) {
083: if (row % 2 == 0) {
084: setBackground(oddBackgroundColor);
085: } else {
086: setBackground(evenBackgroundColor);
087: }
088: }
089:
090: setValue(value);
091:
092: return this ;
093: }
094:
095: public void setValue(Object value) {
096: String text = convertToString(value);
097: Icon icon = convertToIcon(value);
098:
099: setText(text == null ? "" : text);
100: setIcon(icon);
101: setDisabledIcon(icon);
102: }
103:
104: protected String convertToString(Object value) {
105: return objectRenderer.getText(value);
106: }
107:
108: protected Icon convertToIcon(Object value) {
109: return null;
110: }
111:
112: }
|