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 // $Id: SubtitleTableModel.java,v 1.9 2007/08/16 09:07:21 floriaen Exp $ 018 //////////////////////////////////////////////// 019 package sears.gui; 020 021 import java.util.ArrayList; 022 023 import javax.swing.event.UndoableEditEvent; 024 import javax.swing.table.DefaultTableModel; 025 026 import sears.file.SrtFile; 027 import sears.file.Subtitle; 028 import sears.file.SubtitleFile; 029 import sears.gui.undo.SearsSimpleUndoEdit; 030 import sears.tools.SearsResourceBundle; 031 import sears.tools.Trace; 032 033 /** 034 * Defines table data and ensure synchronisation with array of subtitles 035 */ 036 public class SubtitleTableModel extends DefaultTableModel { 037 private static final long serialVersionUID = 3262302656286772741L; 038 039 /**The subtitle list.*/ 040 private ArrayList<Subtitle> subtitleList; 041 042 /** (<b>int</b>) ANCHOR_COLUMN: The ANCHOR_COLUMN index. */ 043 public static final int ANCHOR_COLUMN = 0; 044 /** (<b>int</b>) ID_COLUMN: The ID_COLUMN index. */ 045 public static final int ID_COLUMN = 1; 046 /** (<b>int</b>) START_DATE_COLUMN: The START_DATE_COLUMN index. */ 047 public static final int START_DATE_COLUMN = 2; 048 /** (<b>int</b>) END_DATE_COLUMN: The END_DATE_COLUMN index. */ 049 public static final int END_DATE_COLUMN = 3; 050 /** (<b>int</b>) SUBTITLE_COLUMN: The SUBTITLE_COLUMN index. */ 051 public static final int SUBTITLE_COLUMN = 4; 052 053 054 /** (<b>int</b>) COLUMN_NUMBER: The COLUMN_NUMBER */ 055 public static final int COLUMN_NUMBER = 5; 056 057 /** 058 * Constructor SubtitleTableModel. 059 * <br><b>Summary:</b><br> 060 * Constructor of the class. 061 * @param _subtitleList The subtitleList to display 062 */ 063 public SubtitleTableModel(ArrayList<Subtitle> _subtitleList) { 064 subtitleList = _subtitleList; 065 } 066 067 /* (non-Javadoc) 068 * @see javax.swing.table.TableModel#getColumnCount() 069 */ 070 public int getColumnCount() { 071 return COLUMN_NUMBER; 072 } 073 074 /* (non-Javadoc) 075 * @see javax.swing.table.TableModel#getColumnName(int) 076 */ 077 public String getColumnName(int column) { 078 //The result of the method. 079 String result = null; 080 switch (column) { 081 case ANCHOR_COLUMN: 082 //Will use an anchor icon instead of text. 083 result = ""; 084 break; 085 case ID_COLUMN: 086 result = SearsResourceBundle.getResource("table_id"); 087 break; 088 case START_DATE_COLUMN: 089 result = SearsResourceBundle.getResource("table_startDate"); 090 break; 091 case END_DATE_COLUMN: 092 result = SearsResourceBundle.getResource("table_endDate"); 093 break; 094 case SUBTITLE_COLUMN: 095 result = SearsResourceBundle.getResource("table_subtitle"); 096 break; 097 default: 098 break; 099 } 100 //return the result 101 return result; 102 } 103 104 /* (non-Javadoc) 105 * @see javax.swing.table.TableModel#getRowCount() 106 */ 107 public int getRowCount() { 108 int result = 0; 109 if (subtitleList != null) { 110 result = subtitleList.size(); 111 } 112 return result; 113 } 114 115 /* (non-Javadoc) 116 * @see javax.swing.table.TableModel#getValueAt(int, int) 117 */ 118 public Object getValueAt(int row, int column) { 119 //The result of the method 120 Object result = null; 121 //first, get the subtitle a this row. 122 Subtitle subtitle = (Subtitle) subtitleList.get(row); 123 //switch on column number to know what to return. 124 switch (column) { 125 case ID_COLUMN: { 126 //We are searching for the ID 127 result = new Integer(subtitle.getNumber()); 128 break; 129 } 130 case START_DATE_COLUMN: { 131 //getting start Date 132 result = SubtitleFile.timeToString(subtitle.getStartDate()); 133 break; 134 } 135 case END_DATE_COLUMN: { 136 //getting end Date 137 result = SubtitleFile.timeToString(subtitle.getEndDate()); 138 break; 139 } 140 case SUBTITLE_COLUMN: { 141 //getting subtitle 142 result = subtitle.getSubtitle(); 143 break; 144 } 145 } 146 return result; 147 } 148 149 /* (non-Javadoc) 150 * @see javax.swing.table.TableModel#isCellEditable(int, int) 151 */ 152 public boolean isCellEditable(int row, int column) { 153 return column != ID_COLUMN && column != ANCHOR_COLUMN; 154 } 155 156 /* (non-Javadoc) 157 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int) 158 */ 159 public void setValueAt(Object aValue, int row, int column) { 160 //first, get the subtitle a this row. 161 Subtitle subtitle = (Subtitle) subtitleList.get(row); 162 //keep an copy, to know if it has changed. 163 Subtitle oldSubtitle = new Subtitle(subtitle); 164 165 try { 166 //switch on column number to know what to return. 167 switch (column) { 168 case ID_COLUMN: { 169 //We are setting the ID 170 subtitle.setNumber(((Integer) aValue).intValue()); 171 break; 172 } 173 case START_DATE_COLUMN: { 174 //setting start Date 175 subtitle.setStartDate(SrtFile.stringToTime((String) aValue)); 176 break; 177 } 178 case END_DATE_COLUMN: { 179 //setting end Date 180 subtitle.setEndDate(SrtFile.stringToTime((String) aValue)); 181 break; 182 } 183 case SUBTITLE_COLUMN: { 184 //setting subtitle 185 subtitle.setSubtitle((String) aValue); 186 break; 187 } 188 } 189 //If subtitle has changed. 190 if(!subtitle.equals(oldSubtitle)){ 191 //indicate main window that file has changed. 192 MainWindow.instance.fileChanged(); 193 //Add this edit to undo manager 194 Trace.trace("Undoable edit happened"); 195 //Add it to manager 196 MainWindow.instance.getUndoManager().undoableEditHappened(new UndoableEditEvent(this, new SearsSimpleUndoEdit(subtitleList, row, oldSubtitle, subtitle))); 197 } 198 } catch (NumberFormatException e) { 199 //entered value was bad do nothing. 200 } 201 } 202 }