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    package sears.gui;
018    
019    import java.awt.BorderLayout;
020    import java.awt.GridBagConstraints;
021    import java.awt.GridBagLayout;
022    import java.io.File;
023    
024    import javax.swing.JButton;
025    import javax.swing.JLabel;
026    import javax.swing.JOptionPane;
027    import javax.swing.JPanel;
028    import javax.swing.JTextField;
029    
030    import sears.gui.resources.SearsResources;
031    import sears.tools.SearsResourceBundle;
032    import sears.tools.Trace;
033    
034    /**
035     * Class JDialogAppend.
036     * <br><b>Summary:</b><br>
037     * Permit to perform an append action.
038     */
039    public class JDialogAppend extends SearsJDialog {
040        private static final long serialVersionUID = -6078008025583338318L;
041    
042        private JPanel jContentPane = null;
043    
044        private JPanel jPanelCenter = null;
045    
046        private JLabel jLabelFile = null;
047    
048        private JLabel jLabelDelay = null;
049    
050        private JTextField jTextFieldDelay = null;
051    
052        private JTextField jTextFieldFileToAppend = null;
053    
054        private JButton jButtonBrowse = null;
055    
056    
057    
058        /**
059         * This is the default constructor
060         */
061        public JDialogAppend() {
062            super(SearsResourceBundle.getResource("append_title"));
063            setContentPane(getJContentPane());
064            configureSize();
065        }
066        
067        /**
068         * This method initializes jContentPane
069         * 
070         * @return javax.swing.JPanel
071         */
072        private JPanel getJContentPane() {
073            if (jContentPane == null) {
074                jContentPane = new JPanel();
075                jContentPane.setLayout(new BorderLayout());
076                jContentPane.setBorder(super.createEmptyBorder());
077                jContentPane.add(getJPanelCenter(), java.awt.BorderLayout.CENTER);
078                jContentPane.add(getJPanelButtons(), java.awt.BorderLayout.SOUTH);
079            }
080            return jContentPane;
081        }
082    
083        /**
084         * This method initializes jPanel   
085         *  
086         * @return javax.swing.JPanel       
087         */
088        private JPanel getJPanelCenter() {
089            if (jPanelCenter == null) {
090                jPanelCenter = new JPanel();
091                jPanelCenter.setLayout(new GridBagLayout());
092                GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
093                gridBagConstraints2.gridx = 2;
094                gridBagConstraints2.gridy = 0;
095                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
096                gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
097                gridBagConstraints1.gridy = 0;
098                gridBagConstraints1.weightx = 1.0;
099                gridBagConstraints1.gridx = 1;
100                GridBagConstraints gridBagConstraints = new GridBagConstraints();
101                gridBagConstraints.gridx = 0;
102                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
103                gridBagConstraints.gridy = 0;
104                GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
105                gridBagConstraints3.gridx = 0;
106                gridBagConstraints3.gridy = 1;
107                gridBagConstraints3.anchor = java.awt.GridBagConstraints.WEST;
108                GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
109                gridBagConstraints4.gridx = 1;
110                gridBagConstraints4.gridy = 1;
111                gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
112                jLabelFile = new JLabel();
113                jLabelFile.setText(SearsResourceBundle.getResource("append_label"));
114                jPanelCenter.add(jLabelFile, gridBagConstraints);
115                jPanelCenter.add(getJTextFieldFileToAppend(), gridBagConstraints1);
116                jPanelCenter.add(getJButtonBrowse(), gridBagConstraints2);
117                jLabelDelay = new JLabel(SearsResourceBundle.getResource("delay_label"));
118                jPanelCenter.add(jLabelDelay, gridBagConstraints3);
119                jPanelCenter.add(getJTextFieldDelay(), gridBagConstraints4);
120            }
121            return jPanelCenter;
122        }
123    
124        /**
125         * Method getJTextFieldDelay.
126         * <br><b>Summary:</b><br>
127         * return the JTextField that contains the delay.
128         */
129        private JTextField getJTextFieldDelay() {
130            if (jTextFieldDelay == null) {
131                jTextFieldDelay = new JTextField(4);
132                jTextFieldDelay.setToolTipText(SearsResourceBundle.getResource("delay_tip"));
133                jTextFieldDelay.setText("1");
134            }
135            return jTextFieldDelay;
136        }
137    
138    
139    
140    
141    
142        /**
143         * Method okAction.
144         * <br><b>Summary:</b><br>
145         * This method is called when user validate the dialog.
146         */
147        protected void okAction() {
148            //Just have to check parameters validity, if valids, dispose dialog,and set validation status to true.
149            String error = checkParameters();
150            if (error != null && !error.equals("")) {
151                //Show error message.
152                JOptionPane.showMessageDialog(this, error, SearsResourceBundle.getResource("error_appendConfigurationError"), JOptionPane.ERROR_MESSAGE);
153                Trace.trace("Error in Append parameters:" + error, Trace.WARNING_PRIORITY);
154            } else {
155                //else split configuration is valid, release dialog.
156                validationStatus = true;
157                dispose();
158            }
159        }
160    
161        /**
162         * Method checkParameters.
163         * <br><b>Summary:</b><br>
164         * This method checks the parameters.
165         * @return  <b>String</b>      "" if there is no error, or the error message.
166         */
167        private String checkParameters() {
168            //Check file to open
169            String errorMessage = "";
170            String fileName = getJTextFieldFileToAppend().getText();
171            if (fileName != null && !fileName.equals("")) {
172                File fileToAppend = new File(fileName);
173                if (!fileToAppend.exists()) {
174                    errorMessage += SearsResourceBundle.getResource("error_fileDoesNotExist1")
175                                    +" " + fileToAppend.getAbsolutePath() 
176                                    +" " +SearsResourceBundle.getResource("error_fileDoesNotExist2")+"\n";
177                }
178            } else {
179                errorMessage += SearsResourceBundle.getResource("error_fileIsNull")+"\n";
180            }
181            //check delay.
182            String delay = getJTextFieldDelay().getText();
183            if (delay != null && !delay.equals("")) {
184                try {
185                    Double.parseDouble(delay);
186                } catch (NumberFormatException e) {
187                    //Delay value is not a number.
188                    errorMessage += SearsResourceBundle.getResource("error_delayNotValid")+"\n";
189                }
190            } else {
191                //Delay value is null.
192                errorMessage += SearsResourceBundle.getResource("error_delayNull")+"\n";
193            }
194            return errorMessage;
195        }
196    
197    
198        /**
199         * This method initializes jTextField       
200         *  
201         * @return javax.swing.JTextField   
202         */
203        private JTextField getJTextFieldFileToAppend() {
204            if (jTextFieldFileToAppend == null) {
205                jTextFieldFileToAppend = new JTextField(14);
206            }
207            return jTextFieldFileToAppend;
208        }
209    
210        /**
211         * This method initializes jButton  
212         *  
213         * @return javax.swing.JButton      
214         */
215        private JButton getJButtonBrowse() {
216            if (jButtonBrowse == null) {
217                jButtonBrowse = new JButton(SearsResources.getIcon("BrowseIcon"));
218                jButtonBrowse.addActionListener(new java.awt.event.ActionListener() {
219                    public void actionPerformed(java.awt.event.ActionEvent e) {
220                        browseFileToAppend();
221                    }
222                });
223            }
224            return jButtonBrowse;
225        }
226    
227        /**
228         * Method getFileToAppend.
229         * <br><b>Summary:</b><br>
230         * Return the file that has been selected, or null if user canceled.
231         * @return File The <b>File</b> to append.
232         */
233        public File getFileToAppend() {
234            //the result of the method.
235            File result = null;
236            if (validationStatus) {
237                result = new File(getJTextFieldFileToAppend().getText());
238            }
239            //return the result
240            return result;
241        }
242    
243        /**
244         * Method browseFileToAppend.
245         * <br><b>Summary:</b><br>
246         * Use thid method to choose destination files.
247         */
248        protected void browseFileToAppend() {
249            File choosenFile = MainWindow.instance.showSRTBrowser();
250            if (choosenFile != null) {
251                getJTextFieldFileToAppend().setText(choosenFile.getAbsolutePath());
252            }
253        }
254    
255    
256    
257        /**
258         * Method getDelay.
259         * <br><b>Summary:</b><br>
260         * return the delay to apply before appending.
261         * @return  <b>double</b>  The delay to apply before appending.
262         */
263        public double getDelay() {
264            //The result
265            double result = 1;
266            //Get the entered delay.
267            String delay = getJTextFieldDelay().getText();
268            if (delay != null && !delay.equals("")) {
269                try {
270                    result = Double.parseDouble(delay);
271                } catch (NumberFormatException e) {
272                    Trace.trace("Delay is not a number.", Trace.ERROR_PRIORITY);
273                }
274            }
275            return result;
276        }
277        /* (non-Javadoc)
278         * @see sears.gui.SearsJDialog#getDialogName()
279         */
280        protected String getDialogName() {
281            return "append";
282        }
283    }