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