001/**
002 *  '$Author: barseghian $'
003 *  '$Date: 2011-01-22 02:59:40 +0000 (Sat, 22 Jan 2011) $'
004 *  '$Revision: 26750 $'
005 *
006 *  For Details:
007 *  http://www.kepler-project.org
008 *
009 *  Copyright (c) 2009-2010 The Regents of the
010 *  University of California. All rights reserved. Permission is hereby granted,
011 *  without written agreement and without license or royalty fees, to use, copy,
012 *  modify, and distribute this software and its documentation for any purpose,
013 *  provided that the above copyright notice and the following two paragraphs
014 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
015 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
016 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
017 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
018 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
019 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
020 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
021 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 *  ENHANCEMENTS, OR MODIFICATIONS.
024 */
025
026package org.kepler.workflowrunmanager.gui;
027
028import java.awt.Color;
029import java.awt.Component;
030
031import javax.swing.JTable;
032import javax.swing.SwingConstants;
033import javax.swing.UIManager;
034import javax.swing.table.DefaultTableCellRenderer;
035import javax.swing.table.JTableHeader;
036
037import org.kepler.workflowrunmanager.WRMDefaults;
038
039/**
040 * ColumnGroup, GroupableTableColumnModel, GroupableTableHeader,
041 * GroupableTableHeaderUI and GroupableTableCellRenderer taken from a post by
042 * Steve Webb (16/09/04). He extended code originally posted by Nobuo Tamemasa.
043 * Many thanks to both authors. I've made some changes, including making the
044 * headers editable.
045 * 
046 */
047
048public class GroupableTableCellRenderer extends DefaultTableCellRenderer {
049
050        private WRMDefaults wrmdefaults = WRMDefaults.getInstance();
051
052        public Component getTableCellRendererComponent(JTable table, Object value,
053                        boolean selected, boolean focused, int row, int column) {
054                JTableHeader header = table.getTableHeader();
055
056                setFont(WRMDefaults.WRM_FONT);
057
058                // Workaround for cursor of GroupableTableCellRender and parent and
059                // grandparent sometimes getting stuck with Wait Cursor. Not sure why this is necessary.
060                // Possibly relevant: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4132824
061                if (getParent() != null && getParent().getCursor() != null){
062                        WorkflowRunManagerPanel wrmp = 
063                                (WorkflowRunManagerPanel)getParent().getParent().getParent().getParent().getParent();
064                        //System.out.println("GroupableTableCellRenderer "+getCursor().getName()+" "+
065                        //              this.getParent().getClass() + ":"+this.getParent().getCursor().getName()+" "+
066                        //              this.getParent().getParent().getClass() + ":"+this.getParent().getParent().getCursor().getName()+" "+
067                        //              this.getParent().getParent().getParent().getClass() + ":"+this.getParent().getParent().getParent().getCursor().getName()+" "+
068                        //              this.getParent().getParent().getParent().getParent().getClass() + ":"+this.getParent().getParent().getParent().getParent().getCursor().getName()+" "+
069                        //              this.getParent().getParent().getParent().getParent().getParent().getClass() + ":"+this.getParent().getParent().getParent().getParent().getParent().getCursor().getName()
070                        //);
071                        if (!wrmp.getCursor().getName().equals(getCursor().getName())){
072                                //System.out.println("** GroupableTableCellRenderer cursor out of synch with wrmpanel cursor, synching...");
073                                setCursor(wrmp.getCursor());
074                                this.getParent().setCursor(wrmp.getCursor());
075                                this.getParent().getParent().setCursor(wrmp.getCursor());
076                        }
077                }
078
079                String wrmdefaultsRealColumnName = table.getColumnModel().getColumn(
080                                column).getHeaderValue().toString();
081
082                if (header != null) {
083                        setForeground(Color.BLACK);
084                        // System.out.println("derik debug. GroupableTableCellRenderer. 
085                        //              getTableCellRendererComponent. column: "+column+" value.toString:"
086                        //              + value.toString());
087            if (value == null) {
088                value = wrmdefaults.getDefault(wrmdefaultsRealColumnName);
089            } else if (value.toString().equals("")) {
090                                if (column != -1) {
091                                        value = wrmdefaults.getDefault(wrmdefaultsRealColumnName);
092                                } else {
093                                        value = wrmdefaults.getDefault("default");
094                                }
095                                setForeground(Color.LIGHT_GRAY);
096                        } else if (wrmdefaults.containsName(value.toString())) {
097                                // gray default values
098                                setForeground(Color.LIGHT_GRAY);
099                        }
100            
101                        setBackground(Color.WHITE);
102                } else {
103                        System.out.println("ERROR. header was null");
104                }
105
106                setHorizontalAlignment(SwingConstants.CENTER);
107                setText(value.toString());
108                setBorder(UIManager.getBorder("TableHeader.cellBorder"));
109                return this;
110        }
111}