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 018 package sears.gui; 019 020 import java.awt.Color; 021 import java.awt.Component; 022 import java.awt.Font; 023 import java.util.ArrayList; 024 025 import javax.swing.BorderFactory; 026 import javax.swing.Icon; 027 import javax.swing.ImageIcon; 028 import javax.swing.JLabel; 029 import javax.swing.JTable; 030 import javax.swing.UIManager; 031 import javax.swing.table.DefaultTableCellRenderer; 032 033 import sears.file.Subtitle; 034 import sears.file.SubtitleFile; 035 import sears.gui.resources.SearsResources; 036 import sears.tools.SearsResourceBundle; 037 038 /** 039 * Class SubtitleTableCellRenderer This renderer permits to display the anchor 040 * in the 4rth cell. It also permits to change the color of the row's cell if 041 * the subtitle is anchored. 042 */ 043 public class SubtitleTableCellRenderer extends DefaultTableCellRenderer{ 044 045 /** (<b>long</b>) serialVersionUID: The serialVersionUID */ 046 private static final long serialVersionUID = 6305494600328229080L; 047 048 /** (<b>ImageIcon</b>) anchorIcon: The anchorIcon */ 049 private static ImageIcon anchorIcon; 050 051 /** (<b>ImageIcon</b>) dotIcon: The anchorIcon */ 052 private static ImageIcon dotIcon; 053 054 /** (<b>Color</b>) oldBackgroundColor: The oldBackgroundColor */ 055 private static Color oldBackgroundColor; 056 057 /** (<b>Color</b>) oldSelectedBackgroundColor: The oldSelectedBackgroundColor */ 058 private static Color oldSelectedBackgroundColor; 059 060 /** (<b>Color</b>) ANCHORED_BACKGROUND_COLOR: The color to be used when ST is anchored */ 061 private static final Color ANCHORED_BACKGROUND_COLOR = new Color(190,240,190); 062 063 /** the default font used by component */ 064 public static final Font DEFAULT_FONT = UIManager.getFont("Label.font"); 065 066 /** the color of the background component when it is selected */ 067 public static final Color DEFAULT_SELECTION_BACKGROUND = UIManager.getColor("Table.selectionBackground"); 068 069 /** the color of the component background*/ 070 public static final Color DEFAULT_BACKGROUND = UIManager.getColor("Table.background"); 071 072 /** (<b>ArrayList<Subtitle></b>) subtitleList: The subtitleList. */ 073 private ArrayList<Subtitle> subtitleList; 074 075 // the cell component: 076 private JLabel cellHandle; 077 078 private String highlightedText; 079 080 /** 081 * Constructor SubtitleTableCellRenderer. <br> 082 * <b>Summary:</b><br> 083 * The constructor of the class SubtitleTableCellRenderer 084 * 085 * @param subtitleList 086 * The subtitleList. 087 */ 088 public SubtitleTableCellRenderer(ArrayList<Subtitle> subtitleList) { 089 this.subtitleList = subtitleList; 090 //save the background color 091 oldBackgroundColor = DEFAULT_BACKGROUND; 092 highlightedText = null; 093 } 094 095 /** 096 * a call to this method return the appropriate <code>String</code> instance 097 * for an <code>Object<code> object that could be a <code>String</code> or an <code>Integer</code> 098 * @param value the object to reveal 099 * @return the same object as a <code>String</code> object or null, if the object does not correspond 100 */ 101 private String objectToString(Object value) { 102 String str = null; 103 if( value instanceof Integer ) { 104 str = String.valueOf((Integer) value); 105 } else if( value instanceof String ) { 106 str = (String) value; 107 } 108 // else return a null string 109 return str; 110 } 111 112 /* 113 * (non-Javadoc) 114 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int) 115 */ 116 public Component getTableCellRendererComponent(JTable table, Object value, 117 boolean isSelected, boolean hasFocus, int row, int column) { 118 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 119 120 if( column == SubtitleTableModel.SUBTITLE_COLUMN ) { 121 SubtitleCellComponent cell = new SubtitleCellComponent(table.getRowHeight(row)); 122 cell.setHighlightedString(highlightedText); 123 cellHandle = cell; 124 } else { 125 cellHandle = new JLabel(); 126 } 127 // set the visibility of effects: 128 cellHandle.setOpaque(true); 129 // homogeneity for all the cells: 130 cellHandle.setFont(getFont()); 131 cellHandle.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0)); 132 133 // set the text: 134 String str = objectToString(value); 135 if(str != null) { 136 cellHandle.setText(str); 137 } 138 139 // display the selected row 140 if(isSelected) { 141 cellHandle.setBackground(DEFAULT_SELECTION_BACKGROUND); 142 } else { 143 cellHandle.setBackground(null); 144 } 145 146 if(oldBackgroundColor == null && isSelected == false){ 147 oldBackgroundColor = cellHandle.getBackground(); 148 } 149 if(oldSelectedBackgroundColor == null && isSelected == true){ 150 oldSelectedBackgroundColor = cellHandle.getBackground(); 151 } 152 // And now, add an anchor if subtitle is anchored. 153 Subtitle subtitle = subtitleList.get(row); 154 if (subtitle.isAnchored()) { 155 if (column == SubtitleTableModel.ANCHOR_COLUMN) { 156 cellHandle.setIcon(getAnchorIcon()); 157 cellHandle.setHorizontalAlignment(JLabel.CENTER); 158 } 159 cellHandle.setToolTipText(SearsResourceBundle.getResource("anchor_anchoredTo") 160 + " " + SubtitleFile.timeToString(subtitle.getAnchor())); 161 cellHandle.setBackground(ANCHORED_BACKGROUND_COLOR); 162 } else { 163 if (column == SubtitleTableModel.ANCHOR_COLUMN) { 164 cellHandle.setIcon(getDotIcon()); 165 cellHandle.setHorizontalAlignment(JLabel.CENTER); 166 } 167 cellHandle.setToolTipText(null); 168 if(isSelected){ 169 cellHandle.setBackground(oldSelectedBackgroundColor); 170 } else{ 171 cellHandle.setBackground(oldBackgroundColor); 172 } 173 } 174 // return the cell component: 175 return cellHandle; 176 } 177 178 /** 179 * Method getAnchorIcon. <br> 180 * <b>Summary:</b><br> 181 * Return the anchor icon. Use a cache to increase performance since the 182 * anchor icon is always the same. 183 * 184 * @return (<b>Icon</b>) The anchor icon. 185 */ 186 private Icon getAnchorIcon() { 187 if (anchorIcon == null) { 188 anchorIcon = SearsResources.getIcon("AnchorIcon"); 189 } 190 return anchorIcon; 191 } 192 193 /** 194 * Method getDotIcon. <br> 195 * <b>Summary:</b><br> 196 * Return the dot icon. Use a cache to increase performance since the 197 * dot icon is always the same. 198 * 199 * @return (<b>Icon</b>) The dot icon. 200 */ 201 private Icon getDotIcon() { 202 if (dotIcon == null) { 203 dotIcon = SearsResources.getIcon("DotIcon"); 204 } 205 return dotIcon; 206 } 207 208 /** 209 * Call this method to highlight all occurrences of <tt>str</tt> 210 * in subtitle text view 211 * @param str the string to highlight, if is null no view changes is done 212 */ 213 public void highlightString(String str) { 214 highlightedText = str; 215 216 } 217 }