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