001/**
002 *  '$Author: barseghian $'
003 *  '$Date: 2010-12-23 00:33:45 +0000 (Thu, 23 Dec 2010) $'
004 *  '$Revision: 26597 $'
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.Component;
029import java.text.DateFormat;
030import java.text.Format;
031import java.util.Map;
032
033import javax.swing.JTable;
034import javax.swing.table.DefaultTableCellRenderer;
035
036import org.kepler.util.WorkflowRun;
037import org.kepler.workflowrunmanager.WRMDefaults;
038
039import util.TableSorter;
040
041/*
042 *      Use a formatter to format the cell Object
043 */
044public class FormatRenderer extends DefaultTableCellRenderer {
045        private Format formatter;
046        private TableSorter ts = null;
047        private WorkflowRunManagerTableModel wrmtm = null;
048        private int modelRowIndex;
049
050        /*
051         * Use the specified formatter to format the Object
052         */
053        public FormatRenderer(Format formatter) {
054                this.formatter = formatter;
055        }
056
057        public void setValue(Object value) {
058                // Format the Object before setting its value in the renderer
059
060                try {
061                        if (value != null)
062                                value = formatter.format(value);
063                } catch (IllegalArgumentException e) {
064                }
065
066                super.setValue(value);
067        }
068
069        public Component getTableCellRendererComponent(JTable table, Object value,
070                        boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
071                // 'value' is value contained in the cell located at
072                // (rowIndex, vColIndex)
073
074                setFont(WRMDefaults.WRM_FONT);
075
076                if (isSelected) {
077                        // cell (and perhaps other cells) are selected
078                        setBackground(table.getSelectionBackground());
079                        setForeground(table.getSelectionForeground());
080                } else {
081                        setBackground(table.getBackground());
082                        setForeground(table.getForeground());
083                }
084                setSpecialBackgroundIfNecessary(table, rowIndex, isSelected);
085
086                // if (hasFocus) {
087                // this cell is the anchor and the table has the focus
088                // this.setBackground(table.getSelectionBackground());
089                // }
090
091                // Set tool tip if desired
092                // setToolTipText((String)value);
093                setValue(value);
094
095                // Since the renderer is a component, return itself
096                return this;
097        }
098
099        /*
100         * Use the default date/time formatter for the default locale
101         */
102        public static FormatRenderer getDateTimeRenderer() {
103                return new FormatRenderer(DateFormat.getDateTimeInstance());
104        }
105
106        /*
107         * Use the default time formatter for the default locale
108         */
109        public static FormatRenderer getTimeRenderer() {
110                return new FormatRenderer(DateFormat.getTimeInstance());
111        }
112
113        private void setSpecialBackgroundIfNecessary(JTable table, int rowIndex,
114                        boolean isSelected) {
115                // paint cells in run rows with non-empty error msgs red.
116                // if not an error run, but an imported run, also paint special color
117                // TODO better way? i'm guessing this is very expensive:
118                ts = (TableSorter) table.getModel();
119                wrmtm = (WorkflowRunManagerTableModel) ts.getTableModel();
120                modelRowIndex = ts.modelIndex(rowIndex);
121                WorkflowRun wr = wrmtm.getWorkflowRunForRow(modelRowIndex);
122                Map<Integer,String> errs = wr.getErrorMessages();
123                String type = wr.getType();
124                
125                //reset foreground in case it's changed
126                setForeground(table.getForeground());
127                
128                if (!errs.isEmpty()) {
129                        if (isSelected) {
130                                setBackground(WRMDefaults.ERROR_ROW_SELECTED_COLOR);
131                        } else {
132                                setBackground(WRMDefaults.ERROR_ROW_COLOR);
133                        }
134                }
135                else if (type.equals(WorkflowRun.type.Imported.toString())) {
136                        if (isSelected) {
137                                setBackground(WRMDefaults.IMPORTED_ROW_SELECTED_COLOR);
138                        } else {
139                                setBackground(WRMDefaults.IMPORTED_ROW_COLOR);
140                        }
141                }
142                
143                if (type.equals(WorkflowRun.type.Preview.toString()) ||
144                                type.equals(WorkflowRun.type.Preview_Error.toString())) {
145                        if (!isSelected){
146                                        setForeground(WRMDefaults.PREVIEW_TEXT_COLOR);
147                        }
148                }
149                
150        }
151
152}