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.BorderLayout;
021    import java.awt.GridBagConstraints;
022    import java.awt.GridBagLayout;
023    import java.awt.GridLayout;
024    import java.io.File;
025    import java.util.ArrayList;
026    
027    import javax.swing.BorderFactory;
028    import javax.swing.JButton;
029    import javax.swing.JComboBox;
030    import javax.swing.JLabel;
031    import javax.swing.JOptionPane;
032    import javax.swing.JPanel;
033    import javax.swing.JTextField;
034    import javax.swing.border.EmptyBorder;
035    
036    import sears.file.FileSystemAccess;
037    import sears.file.Subtitle;
038    import sears.gui.resources.SearsResources;
039    import sears.tools.SearsResourceBundle;
040    import sears.tools.Trace;
041    
042    /**
043     * Dialog to mix two subtitles.
044     * <br>Mix a subtitle with another means that it takes one of the part of the other:
045     * <br>subtitles or times instead of its part.
046     *
047     */
048    public class JDialogMixSubtitleFile extends SearsJDialog {
049    
050            private static final long serialVersionUID = 6258795853711630067L;
051    
052            // we get back all the text we need on the first time the class is used:
053            private static String titleText = SearsResourceBundle.getResource("mix_title");
054            private static String selectSubtitleText = SearsResourceBundle.getResource("mix_selectSubtitle");
055            private static String optionsText = SearsResourceBundle.getResource("mix_options");
056            private static String preserveText = SearsResourceBundle.getResource("mix_preserve");
057            private static String subtitlesElementsText = SearsResourceBundle.getResource("mix_subtilesElements");
058            private static String timesElementsText = SearsResourceBundle.getResource("mix_timesElement");
059    
060            // for checking the validity of the subtitle to mix with
061            private int numberOfSubtitles;
062    
063    
064            private JPanel jContentPane = null;
065    
066            private JTextField jTextFieldFileToMix = null;
067            // to know what part of subtitle user want to keep:
068            private JComboBox jComboBoxPartToMix = null;
069    
070            private JButton jButtonBrowse = null;
071    
072            private JPanel jPanelMain = null;
073            private JPanel jPanelChoosingFile;
074            private JPanel jPanelOptions;
075    
076            /**
077             * Constructs a new object
078             * @param numberOfSubtitles count of subtitles
079             */
080            public JDialogMixSubtitleFile(int numberOfSubtitles) {
081                    super(titleText);
082                    this.numberOfSubtitles = numberOfSubtitles;
083                    setContentPane(getJContentPane());
084                    configureSize();
085            }       
086    
087            private JPanel getJContentPane() {
088                    if (jContentPane  == null) {
089                            jContentPane = new JPanel();
090                            jContentPane.setLayout(new BorderLayout());
091                            jContentPane.setBorder(super.createEmptyBorder());
092                            jContentPane.add(getJPanelMain(), BorderLayout.CENTER);
093                            jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH);
094                    }
095                    return jContentPane;
096            }
097    
098            /**
099             * construct if needed and return JPanel instance for choosing a file to mix
100             * @return JPanel instance
101             */
102            private JPanel getJPanelChoosingFile() {
103                    if(jPanelChoosingFile == null) {
104                            jPanelChoosingFile = new JPanel();
105                            jPanelChoosingFile.setBorder(BorderFactory.createTitledBorder(selectSubtitleText));
106                            jPanelChoosingFile.setLayout(new GridBagLayout());
107    
108                            // adding components:
109                            GridBagConstraints c = new GridBagConstraints();
110                            c.fill = GridBagConstraints.HORIZONTAL;
111                            c.gridx = 0;
112                            c.gridy = 0;                    
113                            jPanelChoosingFile.add(getJTextFieldFileToMix());
114    
115                            c.gridx = 1;
116                            c.gridy = 0;
117                            jPanelChoosingFile.add(getJButtonBrowse());
118                    }
119    
120                    return jPanelChoosingFile;
121            }
122    
123            /**
124             * construct if needed and return JPanel instance for mix options
125             * @return JPanel instance
126             */
127            private JPanel getJPanelOptions() {
128                    if(jPanelOptions == null) {
129                            jPanelOptions = new JPanel();
130                            jPanelOptions.setBorder(BorderFactory.createTitledBorder(optionsText));
131                            // adding components:
132                            jPanelOptions.add(new JLabel(preserveText + ": "));
133                            jPanelOptions.add(getJComboBoxPartToMix());
134                    }
135    
136                    return jPanelOptions;
137            }
138    
139    
140    
141            private JPanel getJPanelMain() {
142                    if(jPanelMain  == null) {
143                            jPanelMain = new JPanel(new GridLayout(2,1));                   
144                            jPanelMain.setBorder(new EmptyBorder(2,2,2,2));
145                            // adding components:
146                            jPanelMain.add(getJPanelChoosingFile());
147                            jPanelMain.add(getJPanelOptions());
148                    }
149    
150                    return jPanelMain;
151            }
152    
153            /**
154             * Use this method to choose destination files.
155             */
156            protected void browseFileToMix() {
157                    File choosenFile = MainWindow.instance.showSRTBrowser();
158                    if (choosenFile != null) {
159                            getJTextFieldFileToMix().setText(choosenFile.getAbsolutePath());
160                    }
161            }
162    
163            /**
164             * 
165             * @return the JTextField instance for display the other subfile path
166             */
167            protected JTextField getJTextFieldFileToMix() {
168                    if(jTextFieldFileToMix == null) {
169                            jTextFieldFileToMix = new JTextField(20);
170                            jTextFieldFileToMix.setEditable(false);
171                            jTextFieldFileToMix.setDragEnabled(false);
172                    }
173                    return jTextFieldFileToMix;
174            }
175    
176            /**
177             * construct if needed and return the JComboBox instance
178             * <br>that allows user to choose what part of subtitleFile 
179             * <br>he wants to keep
180             */
181            private JComboBox getJComboBoxPartToMix() {
182                    if(jComboBoxPartToMix == null) {
183                            Object[] listOfChoice = {subtitlesElementsText,timesElementsText};              
184                            jComboBoxPartToMix = new JComboBox(listOfChoice);
185                    }       
186                    return jComboBoxPartToMix;
187            }
188    
189            /**
190             * This method initializes jButton      
191             *      
192             * @return javax.swing.JButton  
193             */
194            private JButton getJButtonBrowse() {
195                    if (jButtonBrowse  == null) {
196                            jButtonBrowse = new JButton(SearsResources.getIcon("BrowseIcon"));
197                            jButtonBrowse.addActionListener(new java.awt.event.ActionListener() {
198                                    public void actionPerformed(java.awt.event.ActionEvent e) {
199                                            browseFileToMix();
200                                    }
201                            });
202                    }
203                    return jButtonBrowse;
204            }
205    
206            /**
207             * This method is called when user validate the dialog.
208             */
209            protected void okAction() {
210                    //Just have to check parameters validity, if valids, dispose dialog,and set validation status to true.
211                    String error = checkParameters();
212                    if (error != null && !error.equals("")) {
213                            //Show error message.
214                            JOptionPane.showMessageDialog(this, error, " incorrect number of subtitles ", JOptionPane.ERROR_MESSAGE);
215                            Trace.trace("Error in mix condition " + error, Trace.WARNING_PRIORITY);
216                    } else {
217                            //else mix configuration is valid, release dialog.
218                            validationStatus = true;
219                            dispose();
220                    }
221            }
222    
223            /**
224             * this method check the needed conditions for a mix action.
225             * @return "" if there is no error, or the error message.
226             */
227            private String checkParameters() {
228                    // Check file to open
229                    String errorMessage = "";
230                    String filepath = getJTextFieldFileToMix().getText();
231                    // if filepath is set:
232                    if ( filepath != null && !filepath.equals("") ) {
233                            File fileToMix = new File(filepath);
234                            // we create SubtitleFile instance:
235                            ArrayList<Subtitle> subtitleList = new ArrayList<Subtitle>();                       
236                            FileSystemAccess.getInstance().openFile(fileToMix, subtitleList, null);                 
237                            // if fileToMix is not a subtitle file:
238                            if( subtitleList != null ) {
239                                    // check the number of subtitles:
240                                    if(subtitleList.size() != numberOfSubtitles) {
241                                            errorMessage += "number of subtitle does not correspond !";
242                                    }
243                            }       
244                    } else {
245                            errorMessage += SearsResourceBundle.getResource("error_fileIsNull") + "\n";
246                    }
247    
248                    return errorMessage;
249            }
250    
251            /**
252             * this methods returns the user's choice
253             * <br> false if not
254             * @return true if user wants to keep subtitles, false if not
255             */
256            protected boolean keepSubtitles() {
257                    // if subtitles item is selected return true, false if not
258                    return (jComboBoxPartToMix.getSelectedIndex() == 0);
259            }
260    
261            /*
262             * (non-Javadoc)
263             * @see sears.gui.SearsJDialog#getDialogName()
264             */
265            protected String getDialogName() {
266                    return "Mix";
267            }
268    }