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