[zhyi~subversion:32] Added the functionality to save SRT file.

  • From: zhyi@kenai.com
  • To: commits@zhyi.kenai.com
  • Subject: [zhyi~subversion:32] Added the functionality to save SRT file.
  • Date: Fri, 13 Nov 2009 07:52:32 +0000

Project:    zhyi
Repository: subversion
Revision:   32
Author:     zhyi
Date:       2009-11-13 07:52:29 UTC
Link:       

Log Message:
------------
Added the functionality to save SRT file.


Revisions:
----------
32


Modified Paths:
---------------
SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle_zh_CN.properties
SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/common/SrtToolkit.java
SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.form
SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.java
SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle.properties


Diffs:
------
Index: SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle.properties
===================================================================
--- SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle.properties  
(revision 31)
+++ SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle.properties  
(revision 32)
@@ -68,4 +68,6 @@
 ContinuousEditToolTip=If selected, once a subtitle is updated, the next 
subtitle will automatically be loaded to editor.
 Loading=Loading...
 Saving=Saving...
-BadTime=<html><h4>Bad format</h4>The format of the begin or end time doesn't 
match "hh:MM:ss,mmm".</html>
+BadTimeTitle=Bad Time Format
+FileOverwritingWarning=File {0} already exists. Do you want to overwrite it?
+BadTimeContent=The format of the begin or end time doesn't match 
"hh:MM:ss,mmm".
Index: 
SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle_zh_CN.properties
===================================================================
--- SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle_zh_CN.properties  
  (revision 31)
+++ SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/Bundle_zh_CN.properties  
  (revision 32)
@@ -68,4 +68,6 @@
 
ContinuousEditToolTip=\u5982\u679C\u9009\u4E2D\uFF0C\u5219\u5728\u66F4\u65B0\u4E00\u6761\u5B57\u5E55\u540E\uFF0C\u81EA\u52A8\u5C06\u4E0B\u4E00\u6761\u5B57\u5E55\u8F7D\u5165\u5230\u7F16\u8F91\u533A\u3002
 Loading=\u6B63\u5728\u8F7D\u5165\u2026\u2026
 Saving=\u6B63\u5728\u4FDD\u5B58\u2026\u2026
-BadTime=<html><h4>\u683C\u5F0F\u9519\u8BEF</h4>\u5F00\u59CB\u6216\u7ED3\u675F\u65F6\u95F4\u7684\u683C\u5F0F\u4E0D\u7B26\u5408\u201Chh:MM:ss,mmm\u201D\u3002</html>
+BadTimeTitle=\u65F6\u95F4\u683C\u5F0F\u9519\u8BEF
+FileOverwritingWarning=\u6587\u4EF6 {0} 
\u5DF2\u5B58\u5728\u3002\u4F60\u60F3\u8986\u76D6\u5B83\u5417\uFF1F
+BadTimeContent=\u5F00\u59CB\u6216\u7ED3\u675F\u65F6\u95F4\u7684\u683C\u5F0F\u4E0D\u7B26\u5408\u201Chh:MM:ss,mmm\u201D\u3002
Index: SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/common/SrtToolkit.java
===================================================================
--- SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/common/SrtToolkit.java   
  (revision 31)
+++ SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/common/SrtToolkit.java   
  (revision 32)
@@ -58,7 +58,7 @@
     public static Vector<Vector<Object>> parseSrt(File srtFile)
             throws SubtitleFormatException, IOException {
         lineNumber = 0;
-        subtitleIndex = 0;
+        subtitleIndex = 1;
         BufferedReader in = new BufferedReader(new InputStreamReader(
                 new FileInputStream(srtFile), Context.getCharset()));
         Vector<Vector<Object>> subtitles = new Vector<Vector<Object>>();
@@ -75,16 +75,17 @@
      * Saves subtitles to an SRT file.
      * @param subtitles A vector of vector containing all subtitles which can
      * be got from the SRT table's model.
-     * @param srtFile The SRT file.
+     * @param srtFile The target SRT file.
      * @throws IOException If any I/O error occureed.
      */
     public static void saveSrt(Vector<Vector<Object>> subtitles, File 
srtFile)
             throws IOException {
         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                 new FileOutputStream(srtFile), Context.getCharset()));
+        int index = 1;
         for (Vector<Object> subtitle : subtitles) {
             // Index.
-            out.write((String) subtitle.get(0));
+            out.write("" + index++);
             out.newLine();
             // Begin time.
             out.write((String) subtitle.get(1));
Index: SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.java
===================================================================
--- SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.java   
  (revision 31)
+++ SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.java   
  (revision 32)
@@ -34,6 +34,7 @@
 import java.awt.event.WindowListener;
 import java.io.File;
 import java.io.IOException;
+import java.text.MessageFormat;
 import java.util.ResourceBundle;
 import java.util.Vector;
 import javax.swing.BorderFactory;
@@ -84,8 +85,6 @@
         initComponents();
         settingsDialog = new SettingsDialog(this, true);
         srtTable.getSelectionModel().addListSelectionListener(this);
-//        
updateSubtitleButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
-//                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 
InputEvent.CTRL_DOWN_MASK), "clicked");
         enableEditingComponents(false);
     }
 
@@ -192,18 +191,18 @@
                     .addGroup(editSubtitlePanelLayout.createSequentialGroup()
                         .addComponent(beginTimeLabel)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(beginTimeTextField, 
GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
+                        .addComponent(beginTimeTextField, 
GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                         .addGap(18, 18, 18)
                         .addComponent(endTimeLabel)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(endTimeTextField, 
GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
+                        .addComponent(endTimeTextField, 
GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
                     .addGroup(editSubtitlePanelLayout.createSequentialGroup()
                         .addComponent(contentLabel)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(contentScrollPane, 
GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
+                        .addComponent(contentScrollPane, 
GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))
                     .addGroup(Alignment.TRAILING, 
editSubtitlePanelLayout.createSequentialGroup()
                         .addComponent(continuousEditCheckBox)
-                        .addPreferredGap(ComponentPlacement.RELATED, 223, 
Short.MAX_VALUE)
+                        .addPreferredGap(ComponentPlacement.RELATED, 167, 
Short.MAX_VALUE)
                         .addComponent(updateSubtitleButton)))
                 .addContainerGap())
         );
@@ -308,8 +307,10 @@
         );
 
         saveAsButton.setText(bundle.getString("SaveAs")); // NOI18N
+        saveAsButton.addActionListener(this);
 
         saveButton.setText(bundle.getString("Save")); // NOI18N
+        saveButton.addActionListener(this);
 
         aboutButton.setText(bundle.getString("AboutButton")); // NOI18N
         aboutButton.addActionListener(this);
@@ -337,26 +338,26 @@
             .addGroup(layout.createSequentialGroup()
                 .addContainerGap()
                 .addGroup(layout.createParallelGroup(Alignment.LEADING)
-                    .addComponent(separator1, GroupLayout.DEFAULT_SIZE, 644, 
Short.MAX_VALUE)
-                    .addComponent(srtScrollPane, GroupLayout.DEFAULT_SIZE, 
644, Short.MAX_VALUE)
+                    .addComponent(separator1, GroupLayout.DEFAULT_SIZE, 646, 
Short.MAX_VALUE)
+                    .addComponent(srtScrollPane, GroupLayout.DEFAULT_SIZE, 
646, Short.MAX_VALUE)
                     .addGroup(Alignment.TRAILING, 
layout.createSequentialGroup()
                         .addComponent(loadButton)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(pathTextField, 
GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
+                        .addComponent(pathTextField, 
GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
                         .addComponent(saveButton)
                         .addPreferredGap(ComponentPlacement.RELATED)
                         .addComponent(saveAsButton))
                     .addGroup(layout.createSequentialGroup()
-                        .addComponent(addBeforeButton, 
GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
+                        .addComponent(addBeforeButton, 
GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(addAfterButton, 
GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
+                        .addComponent(addAfterButton, 
GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(moveUpButton, 
GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
+                        .addComponent(moveUpButton, 
GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
                         .addComponent(moveDownButton, 
GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
-                        .addComponent(deleteButton, 
GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
+                        .addComponent(deleteButton, 
GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                     .addGroup(Alignment.TRAILING, 
layout.createSequentialGroup()
                         .addComponent(editSubtitlePanel, 
GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
@@ -365,7 +366,7 @@
                         .addComponent(settingsButton)
                         .addPreferredGap(ComponentPlacement.RELATED)
                         .addComponent(aboutButton)
-                        .addPreferredGap(ComponentPlacement.RELATED, 327, 
Short.MAX_VALUE)
+                        .addPreferredGap(ComponentPlacement.RELATED, 329, 
Short.MAX_VALUE)
                         .addComponent(progressBar, 
GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, 
GroupLayout.PREFERRED_SIZE)
                         .addPreferredGap(ComponentPlacement.RELATED)
                         .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
@@ -428,6 +429,12 @@
         else if (evt.getSource() == settingsButton) {
             SrtEditorFrame.this.settingsButtonActionPerformed(evt);
         }
+        else if (evt.getSource() == saveButton) {
+            SrtEditorFrame.this.saveButtonActionPerformed(evt);
+        }
+        else if (evt.getSource() == saveAsButton) {
+            SrtEditorFrame.this.saveAsButtonActionPerformed(evt);
+        }
     }
 
     public void keyPressed(java.awt.event.KeyEvent evt) {
@@ -515,7 +522,7 @@
         try {
             Context.saveSettings();
         } catch (Exception ex) {
-            showException(ex);
+            showError(bundle.getString("Error"), ex.getMessage());
         }
     }//GEN-LAST:event_formWindowClosing
 
@@ -541,6 +548,37 @@
         }
     }//GEN-LAST:event_contentTextAreaKeyReleased
 
+    private void saveButtonActionPerformed(ActionEvent evt) 
{//GEN-FIRST:event_saveButtonActionPerformed
+        saveSrtToFile(new File(pathTextField.getText()));
+    }//GEN-LAST:event_saveButtonActionPerformed
+
+    private void saveAsButtonActionPerformed(ActionEvent evt) 
{//GEN-FIRST:event_saveAsButtonActionPerformed
+        if (srtChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
+            File srtFile = srtChooser.getSelectedFile();
+            if (srtFile.exists()) {
+                int choice = JOptionPane.showConfirmDialog(
+                        this, MessageFormat.format(
+                                bundle.getString("FileOverwritingWarning"),
+                                srtFile.getName()),
+                        bundle.getString("Warning"),
+                        JOptionPane.YES_NO_OPTION);
+                if (choice == JOptionPane.NO_OPTION) {
+                    return;
+                }
+            }
+            saveSrtToFile(srtFile);
+        }
+    }//GEN-LAST:event_saveAsButtonActionPerformed
+
+    private void saveSrtToFile(File srtFile) {
+        DefaultTableModel modal = (DefaultTableModel) srtTable.getModel();
+        try {
+            SrtToolkit.saveSrt(modal.getDataVector(), srtFile);
+        } catch (Exception ex) {
+            showError(bundle.getString("Error"), ex.getMessage());
+        }
+    }
+
     private boolean isControlEnterDown(KeyEvent e) {
         return e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER;
     }
@@ -566,9 +604,9 @@
         cancelTaskButton.setEnabled(enabled);
     }
 
-    private void showException(Exception ex) {
-        JOptionPane.showMessageDialog(this, ex.getMessage(),
-                bundle.getString("Error"), JOptionPane.ERROR_MESSAGE);
+    private void showError(String title, String content) {
+        JOptionPane.showMessageDialog(
+                this, content, title, JOptionPane.ERROR_MESSAGE);
     }
 
     private void loadSrt() {
@@ -615,11 +653,16 @@
                     }
                 } catch (Exception ex) {
                     Throwable cause = ex.getCause();
-                    if (cause != null && (cause instanceof IOException
-                            || cause instanceof SubtitleFormatException)) {
-                        showException((Exception) cause);
+                    if (cause != null) {
+                        if (cause instanceof SubtitleFormatException) {
+                            showError(bundle.getString("BadTimeTitle"),
+                                    cause.getMessage());
+                        } else {
+                            showError(bundle.getString("Error"),
+                                    cause.getMessage());
+                        }
                     } else {
-                        showException(ex);
+                        showError(bundle.getString("Error"), 
ex.getMessage());
                     }
                     return;
                 } finally {
@@ -639,8 +682,8 @@
         System.out.println(beginTime + " --> " + endTime);
         if (!SrtToolkit.isLegalTime(beginTime)
                 || !SrtToolkit.isLegalTime(endTime)) {
-            showException(new SubtitleFormatException(
-                    bundle.getString("BadTime")));
+            showError(bundle.getString("BadTimeTitle"),
+                    bundle.getString("BadTimeContent"));
             return;
         }
 
@@ -654,11 +697,8 @@
         }
     }
 
-    public static void main(String args[]) {
-        try {
-            
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
-        } catch (Exception ex) {
-        }
+    public static void main(String args[]) throws Exception {
+        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 
         // Initialize the application's global settings.
         Context.init();
Index: SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.form
===================================================================
--- SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.form   
  (revision 31)
+++ SrtSubtitleEditor/src/com/zhyi/srtsubtitleeditor/ui/SrtEditorFrame.form   
  (revision 32)
@@ -44,27 +44,27 @@
           <Group type="102" alignment="0" attributes="0">
               <EmptySpace max="-2" attributes="0"/>
               <Group type="103" groupAlignment="0" attributes="0">
-                  <Component id="separator1" alignment="0" pref="644" 
max="32767" attributes="0"/>
-                  <Component id="srtScrollPane" alignment="0" pref="644" 
max="32767" attributes="0"/>
+                  <Component id="separator1" alignment="0" pref="646" 
max="32767" attributes="0"/>
+                  <Component id="srtScrollPane" alignment="0" pref="646" 
max="32767" attributes="0"/>
                   <Group type="102" alignment="1" attributes="0">
                       <Component id="loadButton" min="-2" max="-2" 
attributes="0"/>
                       <EmptySpace max="-2" attributes="0"/>
-                      <Component id="pathTextField" pref="387" max="32767" 
attributes="0"/>
+                      <Component id="pathTextField" pref="389" max="32767" 
attributes="0"/>
                       <EmptySpace max="-2" attributes="0"/>
                       <Component id="saveButton" linkSize="7" min="-2" 
max="-2" attributes="0"/>
                       <EmptySpace max="-2" attributes="0"/>
                       <Component id="saveAsButton" linkSize="7" min="-2" 
max="-2" attributes="0"/>
                   </Group>
                   <Group type="102" alignment="0" attributes="0">
-                      <Component id="addBeforeButton" pref="126" max="32767" 
attributes="0"/>
+                      <Component id="addBeforeButton" pref="125" max="32767" 
attributes="0"/>
                       <EmptySpace min="-2" max="-2" attributes="0"/>
-                      <Component id="addAfterButton" pref="124" max="32767" 
attributes="0"/>
+                      <Component id="addAfterButton" pref="123" max="32767" 
attributes="0"/>
                       <EmptySpace min="-2" max="-2" attributes="0"/>
-                      <Component id="moveUpButton" pref="123" max="32767" 
attributes="0"/>
+                      <Component id="moveUpButton" pref="124" max="32767" 
attributes="0"/>
                       <EmptySpace min="-2" max="-2" attributes="0"/>
                       <Component id="moveDownButton" pref="124" max="32767" 
attributes="0"/>
                       <EmptySpace min="-2" max="-2" attributes="0"/>
-                      <Component id="deleteButton" pref="123" max="32767" 
attributes="0"/>
+                      <Component id="deleteButton" pref="126" max="32767" 
attributes="0"/>
                   </Group>
                   <Group type="102" alignment="1" attributes="0">
                       <Component id="editSubtitlePanel" max="32767" 
attributes="0"/>
@@ -75,7 +75,7 @@
                       <Component id="settingsButton" linkSize="11" min="-2" 
max="-2" attributes="0"/>
                       <EmptySpace max="-2" attributes="0"/>
                       <Component id="aboutButton" linkSize="11" min="-2" 
max="-2" attributes="0"/>
-                      <EmptySpace pref="327" max="32767" attributes="0"/>
+                      <EmptySpace pref="329" max="32767" attributes="0"/>
                       <Component id="progressBar" min="-2" max="-2" 
attributes="0"/>
                       <EmptySpace max="-2" attributes="0"/>
                       <Component id="toolBar" min="-2" max="-2" 
attributes="0"/>
@@ -166,7 +166,7 @@
       <Properties>
         <Property name="border" type="javax.swing.border.Border" 
editor="org.netbeans.modules.form.editors2.BorderEditor">
           <Border 
info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
-            <TitledBorder title="Edit Subtitle (Press Ctrl-Enter to update 
the subtitle)">
+            <TitledBorder title="Edit Subtitle">
               <ResourceString PropertyName="titleX" 
bundle="com/zhyi/srtsubtitleeditor/Bundle.properties" key="Edit" 
replaceFormat="bundle.getString(&quot;{key}&quot;)"/>
             </TitledBorder>
           </Border>
@@ -182,20 +182,20 @@
                       <Group type="102" alignment="0" attributes="0">
                           <Component id="beginTimeLabel" min="-2" max="-2" 
attributes="0"/>
                           <EmptySpace max="-2" attributes="0"/>
-                          <Component id="beginTimeTextField" pref="154" 
max="32767" attributes="0"/>
+                          <Component id="beginTimeTextField" pref="155" 
max="32767" attributes="0"/>
                           <EmptySpace type="separate" min="-2" max="-2" 
attributes="0"/>
                           <Component id="endTimeLabel" min="-2" max="-2" 
attributes="0"/>
                           <EmptySpace min="-2" max="-2" attributes="0"/>
-                          <Component id="endTimeTextField" pref="154" 
max="32767" attributes="0"/>
+                          <Component id="endTimeTextField" pref="155" 
max="32767" attributes="0"/>
                       </Group>
                       <Group type="102" alignment="0" attributes="0">
                           <Component id="contentLabel" min="-2" max="-2" 
attributes="0"/>
                           <EmptySpace max="-2" attributes="0"/>
-                          <Component id="contentScrollPane" pref="384" 
max="32767" attributes="0"/>
+                          <Component id="contentScrollPane" pref="386" 
max="32767" attributes="0"/>
                       </Group>
                       <Group type="102" alignment="1" attributes="0">
                           <Component id="continuousEditCheckBox" min="-2" 
max="-2" attributes="0"/>
-                          <EmptySpace pref="223" max="32767" attributes="0"/>
+                          <EmptySpace pref="167" max="32767" attributes="0"/>
                           <Component id="updateSubtitleButton" min="-2" 
max="-2" attributes="0"/>
                       </Group>
                   </Group>
@@ -473,6 +473,9 @@
           <ResourceString 
bundle="com/zhyi/srtsubtitleeditor/Bundle.properties" key="SaveAs" 
replaceFormat="bundle.getString(&quot;{key}&quot;)"/>
         </Property>
       </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" 
listener="java.awt.event.ActionListener" 
parameters="java.awt.event.ActionEvent" 
handler="saveAsButtonActionPerformed"/>
+      </Events>
     </Component>
     <Component class="javax.swing.JButton" name="saveButton">
       <Properties>
@@ -480,6 +483,9 @@
           <ResourceString 
bundle="com/zhyi/srtsubtitleeditor/Bundle.properties" key="Save" 
replaceFormat="bundle.getString(&quot;{key}&quot;)"/>
         </Property>
       </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" 
listener="java.awt.event.ActionListener" 
parameters="java.awt.event.ActionEvent" handler="saveButtonActionPerformed"/>
+      </Events>
     </Component>
     <Component class="javax.swing.JButton" name="aboutButton">
       <Properties>





[zhyi~subversion:32] Added the functionality to save SRT file.

zhyi 11/13/2009
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close