Java tutorial
/* * Tin Lok CHENG * Rename File Application can rename file name by 3 functions Add Text, Replace Text and Auto Generate * Copyright (C) 2009 T.L. CHENG * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ package phenoviewer; //import com.apple.mrj.MRJFileUtils; import java.awt.Color; import java.awt.Image; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.TransferHandler; import javax.swing.table.DefaultTableModel; public class ReNameJFrame extends javax.swing.JFrame { /** Creates new form ReNameJFrame */ public ReNameJFrame() { initComponents(); vector = new Vector<TableClass>(); // vector for jtable nvector = new Vector<TableClass>(); // vector for new table class rnTable.setTransferHandler(new FilePathReader(vector, this.rnTable)); rnScrollPane.setTransferHandler(new FilePathReader(vector, this.rnTable)); // img = Toolkit.getDefaultToolkit().getImage("./resources/RNIcon.png"); setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { bgPanel = new javax.swing.JPanel(); mainjPanel = new javax.swing.JPanel(); rnTabbedPane = new javax.swing.JTabbedPane(); AddTextPanel = new javax.swing.JPanel(); at_runButton = new javax.swing.JButton(); at_addLabel = new javax.swing.JLabel(); at_addTextField = new javax.swing.JTextField(); at_whereComboBox = new javax.swing.JComboBox(); at_exampleLabel = new javax.swing.JLabel(); at_editExampleLabel = new javax.swing.JLabel(); ReplacePanel = new javax.swing.JPanel(); rp_findLabel = new javax.swing.JLabel(); rp_replaceLabel = new javax.swing.JLabel(); rp_findTextField = new javax.swing.JTextField(); rp_replaceTextField = new javax.swing.JTextField(); rp_runButton = new javax.swing.JButton(); AutoGeneratePanel = new javax.swing.JPanel(); ag_addLabel = new javax.swing.JLabel(); ag_existingCheckBox = new javax.swing.JCheckBox(); ag_newCheckBox = new javax.swing.JCheckBox(); ag_newTextField = new javax.swing.JTextField(); ag_startLabel = new javax.swing.JLabel(); ag_startTextField = new javax.swing.JTextField(); ag_digitsLabel = new javax.swing.JLabel(); ag_digitsTextField = new javax.swing.JTextField(); ag_exampleLabel = new javax.swing.JLabel(); ag_editExampleLabel = new javax.swing.JLabel(); ag_runButton = new javax.swing.JButton(); ag_whereComboBox = new javax.swing.JComboBox(); toLabel = new javax.swing.JLabel(); toTextField = new javax.swing.JTextField(); browseButton = new javax.swing.JButton(); replaceCheckBox = new javax.swing.JCheckBox(); rnScrollPane = new javax.swing.JScrollPane(); rnTable = new javax.swing.JTable(); addButton = new javax.swing.JButton(); removeButton = new javax.swing.JButton(); aboutSeparator = new javax.swing.JSeparator(); // appIcon = new javax.swing.JLabel(); // appName = new javax.swing.JLabel(); // appVersion = new javax.swing.JLabel(); // copyright = new javax.swing.JLabel(); // homepage = new javax.swing.JLabel(); // softpedla = new javax.swing.JLabel(); // macSoftpedia = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); // setIconImage(img); bgPanel.setAutoscrolls(true); bgPanel.setMaximumSize(null); bgPanel.setMinimumSize(new java.awt.Dimension(467, 680)); bgPanel.setPreferredSize(new java.awt.Dimension(467, 680)); mainjPanel.setAutoscrolls(true); mainjPanel.setMaximumSize(null); mainjPanel.setMinimumSize(new java.awt.Dimension(467, 680)); mainjPanel.setOpaque(false); mainjPanel.setPreferredSize(new java.awt.Dimension(467, 680)); at_runButton.setText("Run"); at_runButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { at_runButtonActionPerformed(evt); } }); at_addLabel.setText("Add:"); at_addTextField.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { at_addTextFieldCaretUpdate(evt); } }); at_whereComboBox.setMaximumRowCount(2); at_whereComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Before", "After" })); at_whereComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { at_whereComboBoxActionPerformed(evt); } }); at_exampleLabel.setText("Example:"); at_editExampleLabel.setText("FileName.filetype"); org.jdesktop.layout.GroupLayout AddTextPanelLayout = new org.jdesktop.layout.GroupLayout(AddTextPanel); AddTextPanel.setLayout(AddTextPanelLayout); AddTextPanelLayout.setHorizontalGroup(AddTextPanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AddTextPanelLayout.createSequentialGroup().add(AddTextPanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AddTextPanelLayout.createSequentialGroup().add(48, 48, 48).add(at_addLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(at_addTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE) .add(34, 34, 34)) .add(org.jdesktop.layout.GroupLayout.TRAILING, AddTextPanelLayout.createSequentialGroup().addContainerGap().add(at_exampleLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(at_editExampleLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(at_runButton))) .addContainerGap()) .add(org.jdesktop.layout.GroupLayout.TRAILING, AddTextPanelLayout.createSequentialGroup().addContainerGap(246, Short.MAX_VALUE) .add(at_whereComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(78, 78, 78))); AddTextPanelLayout.setVerticalGroup(AddTextPanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AddTextPanelLayout.createSequentialGroup().add(55, 55, 55) .add(AddTextPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(at_addLabel).add(at_addTextField)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(at_whereComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(43, 43, 43) .add(AddTextPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(at_runButton).add(at_exampleLabel).add(at_editExampleLabel)) .addContainerGap())); rnTabbedPane.addTab("Add Text", AddTextPanel); rp_findLabel.setText("Find:"); rp_replaceLabel.setText("Replace:"); rp_runButton.setText("Run"); rp_runButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { rp_runButtonActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout ReplacePanelLayout = new org.jdesktop.layout.GroupLayout(ReplacePanel); ReplacePanel.setLayout(ReplacePanelLayout); ReplacePanelLayout.setHorizontalGroup(ReplacePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(ReplacePanelLayout.createSequentialGroup().add(ReplacePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(ReplacePanelLayout.createSequentialGroup().add(41, 41, 41).add(rp_findLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(rp_findTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)) .add(ReplacePanelLayout.createSequentialGroup().addContainerGap().add(rp_replaceLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(rp_replaceTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))) .add(58, 58, 58)) .add(org.jdesktop.layout.GroupLayout.TRAILING, ReplacePanelLayout.createSequentialGroup() .addContainerGap(311, Short.MAX_VALUE).add(rp_runButton).addContainerGap())); ReplacePanelLayout.setVerticalGroup(ReplacePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(ReplacePanelLayout.createSequentialGroup().add(20, 20, 20) .add(ReplacePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(rp_findLabel).add(rp_findTextField)) .add(40, 40, 40) .add(ReplacePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(rp_replaceLabel).add(rp_replaceTextField)) .add(55, 55, 55).add(rp_runButton).addContainerGap())); rnTabbedPane.addTab("Replace Text", ReplacePanel); ag_addLabel.setText("Add num to:"); ag_existingCheckBox.setSelected(true); ag_existingCheckBox.setText("existing file name"); ag_existingCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ag_existingCheckBoxActionPerformed(evt); } }); ag_newCheckBox.setText("new file name"); ag_newCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ag_newCheckBoxActionPerformed(evt); } }); ag_newTextField.setBackground(new java.awt.Color(200, 200, 200)); ag_newTextField.setEditable(false); ag_newTextField.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { ag_newTextFieldCaretUpdate(evt); } }); ag_startLabel.setText("Start num:"); ag_startTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT); ag_startTextField.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { ag_startTextFieldCaretUpdate(evt); } }); ag_digitsLabel.setText("Num digits:"); ag_digitsTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT); ag_digitsTextField.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { ag_digitsTextFieldCaretUpdate(evt); } }); ag_exampleLabel.setText("Example:"); ag_editExampleLabel.setText("FileName.filetype"); ag_runButton.setText("Run"); ag_runButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ag_runButtonActionPerformed(evt); } }); ag_whereComboBox.setMaximumRowCount(3); ag_whereComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Before", "After" })); ag_whereComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ag_whereComboBoxActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout AutoGeneratePanelLayout = new org.jdesktop.layout.GroupLayout( AutoGeneratePanel); AutoGeneratePanel.setLayout(AutoGeneratePanelLayout); AutoGeneratePanelLayout.setHorizontalGroup(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AutoGeneratePanelLayout.createSequentialGroup().addContainerGap().add(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AutoGeneratePanelLayout.createSequentialGroup().add(ag_addLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(ag_existingCheckBox) .add(AutoGeneratePanelLayout.createSequentialGroup().add(ag_newCheckBox) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(AutoGeneratePanelLayout .createParallelGroup( org.jdesktop.layout.GroupLayout.TRAILING) .add(ag_whereComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(ag_newTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)))) .addContainerGap()) .add(AutoGeneratePanelLayout.createSequentialGroup().add(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(AutoGeneratePanelLayout.createSequentialGroup().add(9, 9, 9).add(ag_startLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(ag_startTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)) .add(AutoGeneratePanelLayout.createSequentialGroup().add(ag_digitsLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(ag_digitsTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))) .add(218, 218, 218)) .add(AutoGeneratePanelLayout.createSequentialGroup().add(ag_exampleLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(ag_editExampleLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 117, Short.MAX_VALUE) .add(ag_runButton).addContainerGap())))); AutoGeneratePanelLayout.setVerticalGroup(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AutoGeneratePanelLayout.createSequentialGroup().add(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AutoGeneratePanelLayout.createSequentialGroup().add(6, 6, 6).add(ag_existingCheckBox) .add(4, 4, 4) .add(AutoGeneratePanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(AutoGeneratePanelLayout.createSequentialGroup().add(1, 1, 1).add( ag_newTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)) .add(ag_newCheckBox))) .add(AutoGeneratePanelLayout.createSequentialGroup().add(23, 23, 23).add(ag_addLabel))) .add(16, 16, 16) .add(AutoGeneratePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(ag_startLabel).add(ag_startTextField).add(ag_whereComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(14, 14, 14) .add(AutoGeneratePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(ag_digitsTextField).add(ag_digitsLabel)) .add(22, 22, 22) .add(AutoGeneratePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(ag_runButton).add(ag_exampleLabel).add(ag_editExampleLabel)) .addContainerGap())); rnTabbedPane.addTab("Auto Generate", AutoGeneratePanel); toLabel.setText("To:"); toTextField.setEnabled(false); browseButton.setText("Browse"); browseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseButtonActionPerformed(evt); } }); replaceCheckBox.setText("Replace exist File"); replaceCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { replaceCheckBoxActionPerformed(evt); } }); rnTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Name", "Path" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class }; boolean[] canEdit = new boolean[] { false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); rnTable.setDragEnabled(true); rnScrollPane.setViewportView(rnTable); rnTable.getColumnModel().getColumn(0).setPreferredWidth(10); addButton.setText("+"); addButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addButtonActionPerformed(evt); } }); removeButton.setText("-"); removeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { removeButtonActionPerformed(evt); } }); // appIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/renameapplication/resources/RNIcon.png"))); // NOI18N // appIcon.setOpaque(true); // appIcon.addMouseListener(new java.awt.event.MouseAdapter() { // public void mouseClicked(java.awt.event.MouseEvent evt) { // appIconMouseClicked(evt); // } // }); // // appName.setFont(new java.awt.Font("Apple Casual", 0, 18)); // appName.setText("ReName File Application"); // // appVersion.setFont(new java.awt.Font("Apple Casual", 0, 13)); // appVersion.setText("Version 2.0 (31/05/2009)"); // // copyright.setFont(new java.awt.Font("Apple Casual", 0, 12)); // copyright.setText("Copyright (C) 2009 T.L. CHENG"); // // homepage.setFont(new java.awt.Font("Apple Casual", 0, 10)); // homepage.setText("https://sourceforge.net/projects/renameapp/"); // homepage.addMouseListener(new java.awt.event.MouseAdapter() { // public void mouseClicked(java.awt.event.MouseEvent evt) { // homepageMouseClicked(evt); // } // }); // // softpedla.setIcon(new javax.swing.ImageIcon(getClass().getResource("/renameapplication/resources/softpedia_free_award_f.png"))); // NOI18N // softpedla.addMouseListener(new java.awt.event.MouseAdapter() { // public void mouseClicked(java.awt.event.MouseEvent evt) { // softpedlaMouseClicked(evt); // } // }); // // macSoftpedia.setFont(new java.awt.Font("Apple Casual", 0, 10)); // macSoftpedia.setText("http://mac.softpedia.com/get/Utilities/Rename-File-Application.shtml"); // macSoftpedia.addMouseListener(new java.awt.event.MouseAdapter() { // public void mouseClicked(java.awt.event.MouseEvent evt) { // macSoftpediaMouseClicked(evt); // } // }); org.jdesktop.layout.GroupLayout mainjPanelLayout = new org.jdesktop.layout.GroupLayout(mainjPanel); mainjPanel.setLayout(mainjPanelLayout); mainjPanelLayout.setHorizontalGroup(mainjPanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, aboutSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE) // .add(mainjPanelLayout.createSequentialGroup() // .add(24, 24, 24) // .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) // .add(mainjPanelLayout.createSequentialGroup() // .add(appIcon) // .add(21, 21, 21)) // .add(mainjPanelLayout.createSequentialGroup() // .add(softpedla) // .add(18, 18, 18))) // .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) // .add(homepage) // .add(appVersion) // .add(appName) // .add(macSoftpedia) // .add(copyright)) // .add(78, 78, 78)) .add(mainjPanelLayout.createSequentialGroup().addContainerGap().add(mainjPanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(org.jdesktop.layout.GroupLayout.LEADING, rnScrollPane, 0, 424, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, mainjPanelLayout.createSequentialGroup().add(21, 21, 21).add(replaceCheckBox)) .add(org.jdesktop.layout.GroupLayout.LEADING, mainjPanelLayout.createSequentialGroup().add(toLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(toTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE) .add(18, 18, 18).add(browseButton).add(14, 14, 14)) .add(org.jdesktop.layout.GroupLayout.LEADING, mainjPanelLayout.createSequentialGroup() .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(removeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(org.jdesktop.layout.GroupLayout.LEADING, rnTabbedPane)).add(85, 85, 85))); mainjPanelLayout.linkSize(new java.awt.Component[] { addButton, removeButton }, org.jdesktop.layout.GroupLayout.HORIZONTAL); mainjPanelLayout.setVerticalGroup(mainjPanelLayout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(mainjPanelLayout.createSequentialGroup().add(10, 10, 10) .add(rnTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE) .add(18, 18, 18) .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(mainjPanelLayout.createSequentialGroup().add(7, 7, 7).add(toTextField)) .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(toLabel).add(browseButton))) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED).add(replaceCheckBox) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(rnScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(addButton).add(removeButton)) .add(18, 18, 18).add(aboutSeparator) // .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) // .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) // .add(org.jdesktop.layout.GroupLayout.TRAILING, appVersion) // .add(appIcon, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) // .add(mainjPanelLayout.createSequentialGroup() // .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) // .add(appName))) // .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) // .add(mainjPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) // .add(mainjPanelLayout.createSequentialGroup() // .add(homepage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) // .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) // .add(macSoftpedia) // .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) // .add(copyright)) // .add(softpedla)) )); mainjPanelLayout.linkSize(new java.awt.Component[] { addButton, removeButton }, org.jdesktop.layout.GroupLayout.VERTICAL); org.jdesktop.layout.GroupLayout bgPanelLayout = new org.jdesktop.layout.GroupLayout(bgPanel); bgPanel.setLayout(bgPanelLayout); bgPanelLayout.setHorizontalGroup(bgPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(mainjPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); bgPanelLayout.setVerticalGroup(bgPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(mainjPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(bgPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(bgPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed // choosing location of file which will be send to BrowseChooser chooser = new BrowseChooser(); File file = chooser.getFolder(); if (file != null) { toTextField.setText(file.getPath()); } }//GEN-LAST:event_browseButtonActionPerformed private void replaceCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replaceCheckBoxActionPerformed // choose the check box which will replace the file in their exist location if (replaceCheckBox.isSelected()) { toTextField.setEditable(false); toTextField.setBackground(new Color(200, 200, 200)); } else { toTextField.setEditable(true); toTextField.setBackground(new Color(255, 255, 255)); } }//GEN-LAST:event_replaceCheckBoxActionPerformed private List<File> explore(File root, FileFilter filter) { List<File> files = new ArrayList<File>(); File[] leaves = root.listFiles(filter); for (File f : leaves) { if (f.isDirectory()) files.addAll(explore(f, filter)); else files.add(f); } return files; } private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed // add the files into the vector and to the table int totalFileNum; // total file of added boolean equal = false; // false = vector and the file we would like to add is same // MultiFileChooser chooser = new MultiFileChooser(); //function of multi file chooser // File[] files = chooser.getFiles(); BrowseChooser chooser = new BrowseChooser(); File root = chooser.getFolder(); List<File> list = explore(root, new java.io.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".jpg") || f.isDirectory(); } }); File[] files = list.toArray(new File[list.size()]); if (files != null) { totalFileNum = files.length; for (int i = 0; i < totalFileNum; i++) { for (int p = 0; p < vector.size(); p++) { if (vector.get(p).getFileName().equals(files[i].getName()) && vector.get(p).getFilePath().equals(files[i].getPath())) { equal = true; break; // the adding file is alreadlly in the vector } } if (equal == true) { equal = false; continue; } TableClass tc = new TableClass(files[i].getName(), files[i].getPath()); vector.add(tc); } Collections.sort(vector, new vectorComparator()); // sort the vector // update the table DefaultTableModel d = clearTable((DefaultTableModel) this.rnTable.getModel()); d = addVectorTable(d, vector); this.rnTable.setModel(d); } }//GEN-LAST:event_addButtonActionPerformed private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed // remove the file from table and vector if (rnTable.getSelectedRows().length > 0) { int rownums = rnTable.getSelectedRows().length; int selected[] = rnTable.getSelectedRows(); for (int i = rownums - 1; i >= 0; i--) { vector.removeElementAt(selected[i]); } Collections.sort(vector, new vectorComparator()); DefaultTableModel d = clearTable((DefaultTableModel) this.rnTable.getModel()); d = addVectorTable(d, vector); this.rnTable.setModel(d); } }//GEN-LAST:event_removeButtonActionPerformed private void rp_runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rp_runButtonActionPerformed // replace the file name from "Find" to "To" String find = rp_findTextField.getText(); // text in find field String replace = rp_replaceTextField.getText(); // text in replace field Vector<String> error = new Vector<String>(); // vector to save the error message // return if the find field is empty if (find.equals("")) { JOptionPane.showMessageDialog(null, "Enter the text you whould like to find", "(Find) input error", JOptionPane.ERROR_MESSAGE); return; } // return if the replace check box is empty and the text of to field is empty if (replaceCheckBox.isSelected() == false && toTextField.getText().equals("") == true) { JOptionPane.showMessageDialog(null, "Enter the location or select the Replace exist file", "(To/Replace) input error", JOptionPane.ERROR_MESSAGE); return; } // if replaceCheckBox.isSelected() == true, the replace check is selected // if replaceCheckBox.isSelected() == false, the replace check is not selected rp_addVectorProcessing(replaceCheckBox.isSelected(), find, replace); if (showConfirm() == true) renameProcessing(error); // choosed ok else return; // choosed no or cancel if (error.size() > 0) showError(error); // some error in rename processing else JOptionPane.showMessageDialog(null, "Done", "ReName", JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_rp_runButtonActionPerformed private void at_addTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_at_addTextFieldCaretUpdate // update the example text if (at_whereComboBox.getSelectedIndex() == 0) { at_editExampleLabel.setText(at_addTextField.getText() + "FileName.filetype"); } else { at_editExampleLabel.setText("FileName" + at_addTextField.getText() + ".filetype"); } }//GEN-LAST:event_at_addTextFieldCaretUpdate private void at_whereComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_at_whereComboBoxActionPerformed // update the example text if (at_whereComboBox.getSelectedIndex() == 0) { at_editExampleLabel.setText(at_addTextField.getText() + "FileName.filetype"); } else { at_editExampleLabel.setText("FileName" + at_addTextField.getText() + ".filetype"); } }//GEN-LAST:event_at_whereComboBoxActionPerformed private void at_runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_at_runButtonActionPerformed // add the text to file name String add = at_addTextField.getText(); // the add text String location = "before"; // the location of text will be add if (at_whereComboBox.getSelectedIndex() == 1) location = "after"; Vector<String> error = new Vector<String>(); // return if add text field is empty if (add.equals("")) { JOptionPane.showMessageDialog(null, "Enter the text you whould like to add", "(Add) input error", JOptionPane.ERROR_MESSAGE); return; } // return if the replace check box is empty and the text of to field is empty if (replaceCheckBox.isSelected() == false && toTextField.getText().equals("") == true) { JOptionPane.showMessageDialog(null, "Enter the location or select the Replace exist file", "(To/Replace) input error", JOptionPane.ERROR_MESSAGE); return; } // add text rename processing, it will add all new name and path to nvector at_addvectorProcessing(replaceCheckBox.isSelected(), location, add); if (showConfirm() == true) renameProcessing(error); // choosed ok else return; // choosed no or cancel if (error.size() > 0) showError(error); // some erroe happen in rename processing else JOptionPane.showMessageDialog(null, "Done", "ReName", JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_at_runButtonActionPerformed private void ag_existingCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ag_existingCheckBoxActionPerformed // checking the num caorrect and update the example ag_newCheckBox.setSelected(false); ag_existingCheckBox.setSelected(true); ag_newTextField.setEditable(false); ag_newTextField.setBackground(new Color(200, 200, 200)); // return if either of start and digits text field is empty if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("")) { return; } // return if it is not number try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } // return if it is not number try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); String gen = ""; if (digit > 0) { for (int i = 0; i < (digit - start.length()); i++) { gen += "0"; } gen += start; } if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + "FileName.filetype"); } else { ag_editExampleLabel.setText("FileName" + gen + ".filetype"); } }//GEN-LAST:event_ag_existingCheckBoxActionPerformed private void ag_newCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ag_newCheckBoxActionPerformed // checking the num correct and update the example by new name ag_existingCheckBox.setSelected(false); ag_newCheckBox.setSelected(true); ag_newTextField.setEditable(true); ag_newTextField.setBackground(new Color(255, 255, 255)); if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("")) { return; } try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); String gen = ""; if (digit > 0) { for (int i = 0; i < (digit - start.length()); i++) { gen += "0"; } gen += start; } if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + ag_newTextField.getText() + ".filetype"); } else { ag_editExampleLabel.setText(ag_newTextField.getText() + gen + ".filetype"); } }//GEN-LAST:event_ag_newCheckBoxActionPerformed private void ag_newTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_ag_newTextFieldCaretUpdate // checking the num correct and update the example by new name if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("") || ag_newCheckBox.isSelected() == false) { return; } try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); String gen = ""; if (digit > 0) { for (int i = 0; i < (digit - start.length()); i++) { gen += "0"; } gen += start; } if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + ag_newTextField.getText() + ".filetype"); } else { ag_editExampleLabel.setText(ag_newTextField.getText() + gen + ".filetype"); } }//GEN-LAST:event_ag_newTextFieldCaretUpdate private void ag_startTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_ag_startTextFieldCaretUpdate // checking the num correct and update the example base on which option is selected if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("")) { return; } try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); String gen = ""; if (digit > 0) { for (int i = 0; i < (digit - start.length()); i++) { gen += "0"; } gen += start; } if (ag_existingCheckBox.isSelected() == true) { if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + "FileName.filetype"); } else { ag_editExampleLabel.setText("FileName" + gen + ".filetype"); } } else { if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + ag_newTextField.getText() + ".filetype"); } else { ag_editExampleLabel.setText(ag_newTextField.getText() + gen + ".filetype"); } } }//GEN-LAST:event_ag_startTextFieldCaretUpdate private void ag_digitsTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_ag_digitsTextFieldCaretUpdate // checking the num correct and update the examplebase on which option is selected if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("")) { return; } try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); String gen = ""; if (digit > 0) { for (int i = 0; i < (digit - start.length()); i++) { gen += "0"; } gen += start; } if (ag_existingCheckBox.isSelected() == true) { if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + "FileName.filetype"); } else { ag_editExampleLabel.setText("FileName" + gen + ".filetype"); } } else { if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + ag_newTextField.getText() + ".filetype"); } else { ag_editExampleLabel.setText(ag_newTextField.getText() + gen + ".filetype"); } } }//GEN-LAST:event_ag_digitsTextFieldCaretUpdate private void ag_whereComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ag_whereComboBoxActionPerformed // checking the num correcy and update the example if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("")) { return; } try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); String gen = ""; if (digit > 0) { for (int i = 0; i < (digit - start.length()); i++) { gen += "0"; } gen += start; } if (ag_existingCheckBox.isSelected() == true) { if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + "FileName.filetype"); } else { ag_editExampleLabel.setText("FileName" + gen + ".filetype"); } } else { if (ag_whereComboBox.getSelectedIndex() == 0) { ag_editExampleLabel.setText(gen + ag_newTextField.getText() + ".filetype"); } else { ag_editExampleLabel.setText(ag_newTextField.getText() + gen + ".filetype"); } } }//GEN-LAST:event_ag_whereComboBoxActionPerformed private void ag_runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ag_runButtonActionPerformed // gen the num to the file name if (replaceCheckBox.isSelected() == false && toTextField.getText().equals("") == true) { JOptionPane.showMessageDialog(null, "Enter the location or select the Replace exist file", "(To/Replace) input error", JOptionPane.ERROR_MESSAGE); return; } if (ag_startTextField.getText().equals("") || ag_digitsTextField.getText().equals("")) { JOptionPane.showMessageDialog(null, "Enter the num to (Start num) and (Num digits).", "(Start num)/(Num digits) error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_startTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_startTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } try { Integer.valueOf(ag_digitsTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, ag_digitsTextField.getText() + " is not a number.", "(Start num/Digit) input error", JOptionPane.ERROR_MESSAGE); return; } String start = ag_startTextField.getText(); int digit = Integer.valueOf(ag_digitsTextField.getText()); Vector<String> error = new Vector<String>(); int genInteger = Integer.valueOf(start); ag_addvectorProcessing(replaceCheckBox.isSelected(), ag_existingCheckBox.isSelected(), ag_whereComboBox.getSelectedIndex(), genInteger, digit); if (showConfirm() == true) renameProcessing(error); // choosed ok else return; // choosed no or cancel if (error.size() > 0) showError(error); // some erroe happen in rename processing else JOptionPane.showMessageDialog(null, "Done", "ReName", JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_ag_runButtonActionPerformed // private void homepageMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_homepageMouseClicked // // mouse clicked event to open Explorer to the web // String url = "https://sourceforge.net/projects/renameapp/"; // displayURL(url); // }//GEN-LAST:event_homepageMouseClicked // // private void appIconMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_appIconMouseClicked // // mouse clicked event to open Explorer to the web // String url = "https://sourceforge.net/projects/renameapp/"; // displayURL(url); // }//GEN-LAST:event_appIconMouseClicked // // private void softpedlaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_softpedlaMouseClicked // // mouse clicked event to open Explorer to the web // String url = "http://mac.softpedia.com/progClean/Rename-File-Application-Clean-57324.html"; // displayURL(url); // }//GEN-LAST:event_softpedlaMouseClicked // // private void macSoftpediaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_macSoftpediaMouseClicked // // TODO add your handling code here: // String url = "http://mac.softpedia.com/get/Utilities/Rename-File-Application.shtml"; // displayURL(url); // }//GEN-LAST:event_macSoftpediaMouseClicked class TableClass { // used in vector private String fileName; private String filePath; public TableClass(String name, String path) { fileName = name; filePath = path; } public String getFileName() { return fileName; } public String getFilePath() { return filePath; } } class BrowseChooser extends JFileChooser { // function for choosing folder private JFileChooser chooser; public BrowseChooser() { } public File getFolder() { chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } return null; } } class MultiFileChooser extends JFileChooser { // function of multi choosing file private JFileChooser chooser; public MultiFileChooser() { } public File[] getFiles() { chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setMultiSelectionEnabled(true); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { File[] files = chooser.getSelectedFiles(); return files; } return null; } } class vectorComparator implements Comparator { // sorting vector public int compare(Object obj1, Object obj2) { TableClass o1 = (TableClass) obj1; TableClass o2 = (TableClass) obj2; if (o1.getFilePath().compareTo(o2.getFilePath()) > 0) { return 1; } if (o1.getFilePath().compareTo(o2.getFilePath()) < 0) { return -1; } return 0; } } final class FilePathReader extends TransferHandler { public FilePathReader(Vector<TableClass> vector, JTable rnTable) { } @Override // function use to check the file drog can use or not public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) { DataFlavor preferredOne = DataFlavor.javaFileListFlavor; //set DataFlavor to file list for (int i = 0; i < transferFlavors.length; ++i) { if (transferFlavors[i].equals(preferredOne)) return true; } return false; } @Override // function use to imaport the drog file public boolean importData(JComponent comp, Transferable t) { // check the drog file is a file or not if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor) == false) { return false; } // list the files in by getTransferData try { List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor); Object[] paths = files.toArray(); addDragDropVector(paths); // add those file into the vector } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } // function to add the file into the vector private void addDragDropVector(Object[] paths) { boolean equal = false; // check is the file added before for (int i = 0; i < paths.length; i++) { File file = new File(paths[i].toString()); if (file.isFile() && file.exists()) { // check is it a file for (int p = 0; p < vector.size(); p++) { if (vector.get(p).getFileName().equals(file.getName()) && vector.get(p).getFilePath().equals(file.getPath())) { equal = true; break; // the adding file is alreadlly in the vector } } if (equal == true) { equal = false; continue; } TableClass tc = new TableClass(file.getName(), file.getPath()); vector.add(tc); Collections.sort(vector, new vectorComparator()); // sort the vector // update the table DefaultTableModel d = clearTable((DefaultTableModel) rnTable.getModel()); d = addVectorTable(d, vector); rnTable.setModel(d); } } } } // // private void displayURL(String url){ // // function of show url on the default Explorer // Properties prop = System.getProperties(); // String os = prop.getProperty( "os.name" ); // if(os.startsWith("Mac")){ // Open Explorer in mac // try { // MRJFileUtils.openURL(url); // } catch (IOException ex) {} // } // else if(os.startsWith("Windows")){ // Open Explorer in Windows // try{ // Runtime.getRuntime().exec("cmd /c start " + url); // } catch (IOException ex) {} // } // else{ // Open Explorer in Lunix (non test) // try{ // String[] commandLine = { "netscape", url }; // Runtime.getRuntime().exec(commandLine); // } catch (IOException ex){} // } // } private DefaultTableModel addVectorTable(DefaultTableModel d, Vector<TableClass> v) { // function of adding vector to table int num = v.size(); String name; String path; for (int i = 0; i < num; i++) { name = v.elementAt(i).getFileName(); path = v.elementAt(i).getFilePath(); d.addRow(new String[] { name, path }); } return d; } private void rp_addVectorProcessing(Boolean selected, String find, String replace) { // add all the new file name in to nvector clearnewVector(); // clear the new name vector if (selected == true) { for (int i = 0; i < vector.size(); i++) { String newName = ""; // set the new name to empty String newNamePath = ""; // set the new name path to empty String oldName = ""; // set the old name to empty String oldNamePath = vector.elementAt(i).getFilePath(); File oldFile = new File(oldNamePath); // add the error message to new vector, if old file is not exist if (!oldFile.exists()) { //error.add(new String(oldFile.toString() + " file not exists")); nvector.add(new TableClass("Error", "File not exists")); continue; } oldName = oldFile.getName(); newName = oldName.replace(find, replace); // find --> replace newNamePath = oldNamePath.replace(oldName, newName); // oldName --> newName nvector.add(new TableClass(newName, newNamePath)); // add the new name and new name path yo new vector } } else { for (int i = 0; i < vector.size(); i++) { String newName = ""; // set the new name to empty String newNamePath = ""; // set the new name path to empty String oldName = ""; // set the old name to empty String oldNamePath = vector.elementAt(i).getFilePath(); File oldFile = new File(oldNamePath); if (!oldFile.exists()) { //error.add(new String(oldFile.toString() + " file not exists.")); nvector.add(new TableClass("Error", "File not exists")); continue; } oldName = oldFile.getName(); newName = oldName.replace(find, replace); newNamePath = new String(toTextField.getText() + "/" + newName); nvector.add(new TableClass(newName, newNamePath)); // add the new name and new name path yo new vector } } } private void at_addvectorProcessing(Boolean selected, String location, String add) { // add all the new file name in to nvector clearnewVector(); // clear the new name vector // the add text location is before if (location.equals(new String("before"))) { for (int i = 0; i < vector.size(); i++) { String newName = ""; // set the new name to empty String newNamePath = ""; // set the new name path to empty String oldName = ""; // set the old name to empty String oldNamePath = vector.elementAt(i).getFilePath(); File oldFile = new File(oldNamePath); // add the error message to new vector, if old file is not exist if (!oldFile.exists()) { //error.add(new String(oldFile.toString() + " file not exists")); nvector.add(new TableClass("Error", "File not exists")); continue; } oldName = oldFile.getName(); newName = add + oldName; if (selected == true) newNamePath = oldNamePath.replace(oldName, newName); else newNamePath = new String(toTextField.getText() + "/" + newName); nvector.add(new TableClass(newName, newNamePath)); // add the new name and new name path yo new vector } } // the add text location is after else { for (int i = 0; i < vector.size(); i++) { String newName = ""; // set the new name to empty String newNamePath = ""; // set the new name path to empty String oldName = ""; // set the old name to empty String oldNamePath = vector.elementAt(i).getFilePath(); File oldFile = new File(oldNamePath); // add the error message to new vector, if old file is not exist if (!oldFile.exists()) { //error.add(new String(oldFile.toString() + " file not exists")); nvector.add(new TableClass("Error", "File not exists")); continue; } oldName = oldFile.getName(); String[] splitOldFileName = oldName.split(new String("\\.")); if (splitOldFileName.length == 1) { newName = oldName + add; } else { for (int s = 0; s < splitOldFileName.length - 1; s++) { newName += splitOldFileName[s]; } newName += add + "." + splitOldFileName[splitOldFileName.length - 1]; } if (selected == true) newNamePath = oldNamePath.replace(oldName, newName); else newNamePath = new String(toTextField.getText() + "/" + newName); nvector.add(new TableClass(newName, newNamePath)); // add the new name and new name path yo new vector } } return; } private void ag_addvectorProcessing(boolean selected, boolean exist, int where, int getInt, int digit) { // add all the new file name in to nvector clearnewVector(); // clear the new name vector int genInteger = getInt; if (where == 0) { for (int i = 0; i < vector.size(); i++) { String newName = ""; // set the new name to empty String newNamePath = ""; // set the new name path to empty String oldName = ""; // set the old name to empty String oldNamePath = vector.elementAt(i).getFilePath(); File oldFile = new File(oldNamePath); // add the error message to new vector, if old file is not exist if (!oldFile.exists()) { //error.add(new String(oldFile.toString() + " file not exists")); nvector.add(new TableClass("Error", "File not exists")); continue; } oldName = oldFile.getName(); String gen = ""; if (digit > 0) { String genString = String.valueOf(genInteger); for (int p = 0; p < (digit - genString.length()); p++) gen += "0"; gen += genString; } if (exist == true) newName = gen + oldName; else { String[] splitOldFileName = oldName.split(new String("\\.")); newName = gen + ag_newTextField.getText() + "." + splitOldFileName[splitOldFileName.length - 1]; } genInteger++; if (selected == true) newNamePath = oldNamePath.replace(oldName, newName); else newNamePath = new String(toTextField.getText() + "/" + newName); nvector.add(new TableClass(newName, newNamePath)); // add the new name and new name path yo new vector } } else { for (int i = 0; i < vector.size(); i++) { String newName = ""; // set the new name to empty String newNamePath = ""; // set the new name path to empty String oldName = ""; // set the old name to empty String oldNamePath = vector.elementAt(i).getFilePath(); File oldFile = new File(oldNamePath); // add the error message to new vector, if old file is not exist if (!oldFile.exists()) { //error.add(new String(oldFile.toString() + " file not exists")); nvector.add(new TableClass("Error", "File not exists")); continue; } oldName = oldFile.getName(); String[] splitOldFileName = oldName.split(new String("\\.")); String gen = ""; if (digit > 0) { String genString = String.valueOf(genInteger); for (int p = 0; p < (digit - genString.length()); p++) gen += "0"; gen += genString; } if (exist == true) { if (splitOldFileName.length == 1) newName = oldName + gen; else { for (int s = 0; s < splitOldFileName.length - 1; s++) newName += splitOldFileName[s]; newName += gen + "." + splitOldFileName[splitOldFileName.length - 1]; } } else { if (splitOldFileName.length == 1) { newName += ag_newTextField.getText() + gen; } else { newName += ag_newTextField.getText() + gen + "." + splitOldFileName[splitOldFileName.length - 1]; } } genInteger++; if (selected == true) newNamePath = oldNamePath.replace(oldName, newName); else newNamePath = new String(toTextField.getText() + "/" + newName); nvector.add(new TableClass(newName, newNamePath)); // add the new name and new name path yo new vector } } } private static Pattern tokenPattern = Pattern.compile("\\{([^}]*)\\}"); private String doParameter(String field, Map<String, Object> params) { String str = ""; try { Object obj = params.get(field); str = obj.toString(); } catch (NullPointerException e) { throw new RuntimeException("Unknown token replacement.", e); } return str; } private String processParams(String template, Map<String, Object> params) { StringBuffer sb = new StringBuffer(); Matcher myMatcher = tokenPattern.matcher(template); while (myMatcher.find()) { String field = myMatcher.group(1); myMatcher.appendReplacement(sb, ""); sb.append(doParameter(field, params)); } myMatcher.appendTail(sb); return sb.toString(); } private Date readDate(File f) { String dat = null, tim = null; Date dateTime = null; try { BufferedReader br = new BufferedReader(new FileReader(f.getAbsolutePath())); try { String str = null; do { str = br.readLine(); } while ((str != null) && !str.toUpperCase().equals("SECTION FINGERPRINT")); do { str = br.readLine(); if (str != null) { if (str.substring(0, 3).toUpperCase().equals("DAT")) dat = str.substring(4, str.length()); if (str.substring(0, 3).toUpperCase().equals("TIM")) tim = str.substring(4, str.length()); } } while ((str != null) && !str.toUpperCase().equals("ENDSECTION FINGERPRINT")); } finally { br.close(); } } catch (IOException e) { System.out.println("File not found."); } if ((dat != null) && (tim != null)) { try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); dateTime = format.parse(dat + " " + tim); } catch (ParseException e) { throw new RuntimeException("Unknown file format.", e); } } return dateTime; } private String getText(int value, String str1, String str2) { return (value == 0 ? str1 : str2); } private String getString(int value, int size, String padStr) { return org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(value), size, padStr); } private String replaceTokens(Date date, int counter, String path) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); Map<String, Object> tokens = new HashMap<String, Object>(); tokens.put("ERA", getText(gc.get(Calendar.ERA), "BC", "AD")); tokens.put("YEAR", getString(gc.get(Calendar.YEAR), 4, "0")); tokens.put("MONTH", getString(gc.get(Calendar.MONTH) + 1, 2, "0")); tokens.put("WEEK_OF_YEAR", getString(gc.get(Calendar.WEEK_OF_YEAR), 2, "0")); tokens.put("WEEK_OF_MONTH", getString(gc.get(Calendar.WEEK_OF_MONTH), 0, "")); tokens.put("DATE", getString(gc.get(Calendar.DATE), 2, "0")); tokens.put("DAY_OF_MONTH", getString(gc.get(Calendar.DAY_OF_MONTH), 2, "0")); tokens.put("DAY_OF_YEAR", getString(gc.get(Calendar.DAY_OF_YEAR), 3, "0")); tokens.put("DAY_OF_WEEK", getString(gc.get(Calendar.DAY_OF_WEEK), 0, "")); tokens.put("DAY_OF_WEEK_IN_MONTH", getString(gc.get(Calendar.DAY_OF_WEEK_IN_MONTH), 0, "")); tokens.put("AM_PM", getText(gc.get(Calendar.AM_PM), "AM", "PM")); tokens.put("HOUR", getString(gc.get(Calendar.HOUR), 2, "0")); tokens.put("HOUR_OF_DAY", getString(gc.get(Calendar.HOUR_OF_DAY), 2, "0")); tokens.put("MINUTE", getString(gc.get(Calendar.MINUTE), 2, "0")); tokens.put("SECOND", getString(gc.get(Calendar.SECOND), 2, "0")); tokens.put("MILLISECOND", getString(gc.get(Calendar.MILLISECOND), 3, "0")); // tokens.put("ZONE_OFFSET", getString((gc.get(Calendar.ZONE_OFFSET)/(60*60*1000)), 0, "")); // tokens.put("DST_OFFSET", getString((gc.get(Calendar.DST_OFFSET)/(60*60*1000)), 0, "")); tokens.put("COUNTER", getString(counter, 0, "")); return processParams(path, tokens); } private void renameProcessing(Vector<String> error) { int counter = 1; // rename the old file to new file for (int i = 0; i < vector.size(); i++) { String opath = vector.get(i).getFilePath(); String nname = nvector.get(i).getFileName(); String npath = nvector.get(i).getFilePath(); if (nname.equals("Error") && npath.equals("File not exists")) { //System.out.println("error"); continue; } File oldFile = new File(opath); FileFunctions ff = new FileFunctions(); Date date = ff.readDate(oldFile); String tname = replaceTokens(date, counter, nname); String tpath = replaceTokens(date, counter, npath); File tmpFile = new File(tpath); if (!tmpFile.exists()) { counter = 1; tname = replaceTokens(date, counter, nname); tpath = replaceTokens(date, counter, npath); } else { counter++; tname = replaceTokens(date, counter, nname); tpath = replaceTokens(date, counter, npath); } nname = tname; npath = tpath; File newFile = new File(npath); boolean success = oldFile.renameTo(newFile); if (!success) { error.add(new String(oldFile.toString() + " file does not rename successfully.")); continue; } } } private boolean showConfirm() { // show the confirm message box Vector<String> columnNames = new Vector<String>(); columnNames.add(new String("Before")); columnNames.add(new String("After")); DefaultTableModel d = new DefaultTableModel(new Vector<String>(), columnNames); for (int i = 0; i < vector.size(); i++) { if (nvector.get(i).getFileName().equals("Error") == true && nvector.get(i).getFilePath().equals("File not exists") == true) { d.addRow(new String[] { vector.get(i).getFileName(), "Error: File not exist" }); } else { d.addRow(new String[] { vector.get(i).getFileName(), nvector.get(i).getFileName() }); } } JTable table = new JTable(d); JScrollPane scrollPane = new JScrollPane(table); int select = JOptionPane.showConfirmDialog(null, scrollPane); if (select == JOptionPane.YES_OPTION) { return true; } return false; } private void showError(Vector error) { // show the error massage JTextArea textArea = new JTextArea(10, 40); textArea.setEditable(false); String m = ""; for (int i = 0; i < error.size(); i++) { m += error.get(i) + "\n"; } textArea.setText(m); textArea.setForeground(Color.RED); JScrollPane scrollPane = new JScrollPane(textArea); JOptionPane.showMessageDialog(null, scrollPane, "Error Message", JOptionPane.ERROR_MESSAGE); return; } private DefaultTableModel clearTable(DefaultTableModel d) { // clear the tabel int numrows = d.getRowCount(); for (int i = numrows - 1; i >= 0; i--) { d.removeRow(i); } return d; } private void clearnewVector() { // clear the nvector if (nvector.size() > 1) { nvector.removeAllElements(); } return; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel AddTextPanel; private javax.swing.JPanel AutoGeneratePanel; private javax.swing.JPanel ReplacePanel; private javax.swing.JSeparator aboutSeparator; private javax.swing.JButton addButton; private javax.swing.JLabel ag_addLabel; private javax.swing.JLabel ag_digitsLabel; private javax.swing.JTextField ag_digitsTextField; private javax.swing.JLabel ag_editExampleLabel; private javax.swing.JLabel ag_exampleLabel; private javax.swing.JCheckBox ag_existingCheckBox; private javax.swing.JCheckBox ag_newCheckBox; private javax.swing.JTextField ag_newTextField; private javax.swing.JButton ag_runButton; private javax.swing.JLabel ag_startLabel; private javax.swing.JTextField ag_startTextField; private javax.swing.JComboBox ag_whereComboBox; private javax.swing.JLabel appIcon; private javax.swing.JLabel appName; private javax.swing.JLabel appVersion; private javax.swing.JLabel at_addLabel; private javax.swing.JTextField at_addTextField; private javax.swing.JLabel at_editExampleLabel; private javax.swing.JLabel at_exampleLabel; private javax.swing.JButton at_runButton; private javax.swing.JComboBox at_whereComboBox; private javax.swing.JPanel bgPanel; private javax.swing.JButton browseButton; private javax.swing.JLabel copyright; private javax.swing.JLabel homepage; private javax.swing.JLabel macSoftpedia; private javax.swing.JPanel mainjPanel; private javax.swing.JButton removeButton; private javax.swing.JCheckBox replaceCheckBox; private javax.swing.JScrollPane rnScrollPane; private javax.swing.JTabbedPane rnTabbedPane; private javax.swing.JTable rnTable; private javax.swing.JLabel rp_findLabel; private javax.swing.JTextField rp_findTextField; private javax.swing.JLabel rp_replaceLabel; private javax.swing.JTextField rp_replaceTextField; private javax.swing.JButton rp_runButton; private javax.swing.JLabel softpedla; private javax.swing.JLabel toLabel; private javax.swing.JTextField toTextField; // End of variables declaration//GEN-END:variables // Variables declaration private Vector<TableClass> vector; private Vector<TableClass> nvector; // private Image img; // End of variables declaration }