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.file;
019    
020    import java.util.HashMap;
021    
022    /**
023     * this class represents a ssa subtitle.
024     */
025    public class SsaSubtitle extends Subtitle {
026    
027            /** keys for 'events' section */
028            public static final String[] eventsDictionnaryKeys = {
029                    "Marked","Layer","Start","End","Style","Name",
030                    "MarginL","MarginR","MarginV","Effect","Karaoke",
031                    "Text","Comment","Picture","Sound","Movie",
032                    "Command"
033            };      
034    
035            private HashMap<String,String> dictionnary = null;
036    
037            /**
038             * Constructor
039             * @param subtitleNumber
040             */
041            public SsaSubtitle(int subtitleNumber) {
042                    super.setNumber(subtitleNumber);
043                    super.anchor(-1);
044                    constructAnEmptyDictionnary();          
045            }
046    
047            /**
048         * Constructor Subtitle.
049         * Copy another subtitle.
050         * @param aSubtitle  The <b>Subtitle</b> to copy.
051         */
052            public SsaSubtitle(Subtitle aSubtitle) {
053                    this(aSubtitle.getNumber());
054                    
055                    if(aSubtitle instanceof SsaSubtitle) {
056                            for(int i=0;i<eventsDictionnaryKeys.length;i++) {            
057                                    this.putEntrie(eventsDictionnaryKeys[i], 
058                                                    ((SsaSubtitle)aSubtitle).getEntrie(eventsDictionnaryKeys[i]));
059                            }
060                    } else {
061                            // tempory...
062                            System.out.println("fresh and copy needed");
063                    }
064            }
065    
066            /**
067             * this method constructs an empty dictionnary with the events keys
068             */
069            private void constructAnEmptyDictionnary() {
070                    dictionnary = new HashMap<String,String>(eventsDictionnaryKeys.length);
071                    for(int i=0;i<eventsDictionnaryKeys.length;i++) {
072                            dictionnary.put(eventsDictionnaryKeys[i], null);
073                    }
074            }
075    
076            /**
077             * this method allows to change a value of a key.
078             * No add-on is possible, the key must be already exist...
079             * @param key           the existing key
080             * @param value         the value who wants to associated to the key
081             * @return                      true if the key exists, false if not
082             */
083            public boolean putEntrie(String key, String value) {
084                    boolean result = false;
085                    // just in case:
086                    if(dictionnary != null) {               
087                            result = dictionnary.containsKey(key);
088                            if(result == true) {
089                                    dictionnary.put(key, value);
090                            }       
091                    }
092                    return result;
093            }
094    
095            /**
096             * this method returns the value associated to a key in a dictionnary.
097             * @param key           the existing key
098             * @return                      the associated value, if the value is null, null is returned.
099             *                                      <br>There's no way to know if the key does not exist in the dictionnary
100             */
101            public String getEntrie(String key) {
102                    String result = null;
103                    // just in case:
104                    if(dictionnary != null) {                       
105                            if(dictionnary.containsKey(key)) {
106                                    result = dictionnary.get(key);
107                            }       
108                    }
109                    return result;
110            }
111    
112            /*
113             * (non-Javadoc)
114             * @see sears.file.Subtitle#getEndDate()
115             */
116            public int getEndDate() {
117                    return Integer.parseInt(getEntrie("End"));
118            }
119    
120            /*
121             * (non-Javadoc)
122             * @see sears.file.Subtitle#setEndDate(int)
123             */
124            public void setEndDate(int endDate) {
125                    putEntrie("End", String.valueOf(endDate));
126            }
127    
128            /*
129             * (non-Javadoc)
130             * @see sears.file.Subtitle#getStartDate()
131             */
132            public int getStartDate() {
133                    return Integer.parseInt(getEntrie("Start"));
134            }
135    
136            /*
137             * (non-Javadoc)
138             * @see sears.file.Subtitle#setStartDate(int)
139             */
140            public void setStartDate(int startDate) {
141                    putEntrie("Start", String.valueOf(startDate));
142            }
143    
144            /*
145             * (non-Javadoc)
146             * @see sears.file.Subtitle#getSubtitle()
147             */
148            public String getSubtitle() {
149                    return getEntrie("Text");
150            }
151    
152            /*
153             * (non-Javadoc)
154             * @see sears.file.Subtitle#setSubtitle(java.lang.String)
155             */
156            public void setSubtitle(String subtitle) {
157                    putEntrie("Text", subtitle);
158            }
159    
160            /*
161             * (non-Javadoc)
162             * @see sears.file.Subtitle#delay(int)
163             */
164            public void delay(int delay) {
165                    setStartDate(getStartDate() + delay);
166                    setEndDate(getEndDate() + delay);
167            }
168    
169            /*
170             * (non-Javadoc)
171             * @see sears.file.Subtitle#accentRemove()
172             */
173            public void accentRemove() {
174                    // we store subtitle:
175                    super.setSubtitle(getSubtitle());
176                    // aplly the accent remove method:
177                    super.accentRemove();
178                    // and update dictionnary:
179                    setSubtitle(super.getSubtitle());
180            }
181    
182            /*
183             * (non-Javadoc)
184             * @see sears.file.Subtitle#htmlRemove()
185             */
186            public void htmlRemove() {
187                    // we store subtitle:
188                    super.setSubtitle(getSubtitle());
189                    // aplly the html remove method:
190                    super.htmlRemove();
191                    // and update dictionnary:
192                    setSubtitle(super.getSubtitle());
193            }
194    
195            /*
196             * (non-Javadoc)
197             * @see sears.file.Subtitle#compareTo(sears.file.Subtitle)
198             */
199            public int compareTo(Subtitle subtitle) {
200                    // we store start date:
201                    super.setStartDate(getStartDate());              
202                    return super.compareTo(subtitle);
203            }
204    }