001    //////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.gui;
018    
019    import java.awt.Color;
020    import java.awt.Component;
021    import java.util.ArrayList;
022    
023    import javax.swing.Icon;
024    import javax.swing.ImageIcon;
025    import javax.swing.JLabel;
026    import javax.swing.JTable;
027    import javax.swing.table.DefaultTableCellRenderer;
028    
029    import sears.file.Subtitle;
030    import sears.file.SubtitleFile;
031    import sears.gui.resources.SearsResources;
032    import sears.tools.SearsResourceBundle;
033    
034    /**
035     * Class SubtitleTableCellRenderer This renderer permits to display the anchor
036     * in the 4rth cell. It also permits to change the color of the row's cell if
037     * the subtitle is anchored.
038     */
039    public class SubtitleTableCellRenderer extends DefaultTableCellRenderer {
040    
041            /** (<b>long</b>) serialVersionUID: The serialVersionUID */
042            private static final long serialVersionUID = 6305494600328229080L;
043    
044            /** (<b>ImageIcon</b>) anchorIcon: The anchorIcon */
045            private static ImageIcon anchorIcon;
046            
047            /** (<b>ImageIcon</b>) dotIcon: The anchorIcon */
048            private static ImageIcon dotIcon;
049    
050            /** (<b>Color</b>) oldBackgroundColor: The oldBackgroundColor */
051            private static Color oldBackgroundColor;
052            
053            /** (<b>Color</b>) oldSelectedBackgroundColor: The oldSelectedBackgroundColor */
054            private static Color oldSelectedBackgroundColor;
055            
056            /** (<b>Color</b>) ANCHORED_BACKGROUND_COLOR: The color to be used when ST is anchored */
057            private static final Color ANCHORED_BACKGROUND_COLOR = new Color(190,240,190);
058            /** (<b>ArrayList<Subtitle></b>) subtitleList: The subtitleList. */
059            private ArrayList<Subtitle> subtitleList;
060    
061            /**
062             * Constructor SubtitleTableCellRenderer. <br>
063             * <b>Summary:</b><br>
064             * The constructor of the class SubtitleTableCellRenderer
065             * 
066             * @param subtitleList
067             *            The subtitleList.
068             */
069            public SubtitleTableCellRenderer(ArrayList<Subtitle> subtitleList) {
070                    this.subtitleList = subtitleList;
071                    //save the background color
072                    oldBackgroundColor = getBackground();
073            }
074    
075            /*
076             * (non-Javadoc)
077             * 
078             * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
079             *      java.lang.Object, boolean, boolean, int, int)
080             */
081            @Override
082            public Component getTableCellRendererComponent(JTable table, Object value,
083                            boolean isSelected, boolean hasFocus, int row, int column) {
084                    // reach the super method.
085                    Component result = super.getTableCellRendererComponent(table, value,
086                                    isSelected, hasFocus, row, column);
087                    if(oldBackgroundColor == null && isSelected == false){
088                            oldBackgroundColor = getBackground();
089                    }
090                    if(oldSelectedBackgroundColor == null && isSelected == true){
091                            oldSelectedBackgroundColor = getBackground();
092                    }
093                    // And now, add an anchor if subtitle is anchored.
094                    Subtitle subtitle = subtitleList.get(row);
095                    if (subtitle.isAnchored()) {
096                            if (column == SubtitleTableModel.ANCHOR_COLUMN) {
097                                    setIcon(getAnchorIcon());
098                                    setHorizontalAlignment(JLabel.CENTER);
099                            }
100                            setToolTipText(SearsResourceBundle.getResource("anchor_anchoredTo")
101                                            + " " + SubtitleFile.timeToString(subtitle.getAnchor()));
102                            setBackground(ANCHORED_BACKGROUND_COLOR);
103    
104                    } else {
105                            if (column == SubtitleTableModel.ANCHOR_COLUMN) {
106                                    setIcon(getDotIcon());
107                                    setHorizontalAlignment(JLabel.CENTER);
108                            }
109                            setToolTipText(null);
110                            if(isSelected){
111                                    setBackground(oldSelectedBackgroundColor);
112                            }else{
113                                    setBackground(oldBackgroundColor);
114                            }
115                    }
116    
117                    // return the result.
118                    return result;
119            }
120    
121            /**
122             * Method getAnchorIcon. <br>
123             * <b>Summary:</b><br>
124             * Return the anchor icon. Use a cache to increase performance since the
125             * anchor icon is always the same.
126             * 
127             * @return (<b>Icon</b>) The anchor icon.
128             */
129            private Icon getAnchorIcon() {
130                    if (anchorIcon == null) {
131                            anchorIcon = SearsResources.getIcon("AnchorIcon");
132                    }
133                    return anchorIcon;
134            }
135    
136    
137            /**
138             * Method getDotIcon. <br>
139             * <b>Summary:</b><br>
140             * Return the dot icon. Use a cache to increase performance since the
141             * dot icon is always the same.
142             * 
143             * @return (<b>Icon</b>) The dot icon.
144             */
145            private Icon getDotIcon() {
146                    if (dotIcon == null) {
147                            dotIcon = SearsResources.getIcon("DotIcon");
148                    }
149                    return dotIcon;
150            }       
151    }
152