|
| 1 | +package window_interface.File_Utilities; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.PrintStream; |
| 6 | +import java.security.NoSuchAlgorithmException; |
| 7 | +import java.util.Vector; |
| 8 | + |
| 9 | +import javax.swing.JFileChooser; |
| 10 | +import javax.swing.JFrame; |
| 11 | +import javax.swing.JPanel; |
| 12 | +import javax.swing.border.EmptyBorder; |
| 13 | +import javax.swing.DefaultListModel; |
| 14 | +import javax.swing.JButton; |
| 15 | +import javax.swing.JOptionPane; |
| 16 | +import javax.swing.ListSelectionModel; |
| 17 | +import javax.swing.SpringLayout; |
| 18 | +import javax.swing.JScrollPane; |
| 19 | +import javax.swing.JList; |
| 20 | +import javax.swing.SwingWorker; |
| 21 | +import javax.swing.JProgressBar; |
| 22 | +import javax.swing.JLabel; |
| 23 | + |
| 24 | +import scripts.File_Utilities.MD5Checksum; |
| 25 | +import util.FileSelection; |
| 26 | + |
| 27 | +import java.awt.Component; |
| 28 | +import java.awt.Container; |
| 29 | +import java.awt.Cursor; |
| 30 | +import java.awt.Font; |
| 31 | +import java.awt.Color; |
| 32 | +import java.awt.event.ActionEvent; |
| 33 | +import java.awt.event.ActionListener; |
| 34 | +import java.beans.PropertyChangeEvent; |
| 35 | +import java.beans.PropertyChangeListener; |
| 36 | + |
| 37 | +@SuppressWarnings("serial") |
| 38 | +public class MD5ChecksumWindow extends JFrame implements ActionListener, PropertyChangeListener { |
| 39 | + private JPanel contentPane; |
| 40 | + protected JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir"))); |
| 41 | + |
| 42 | + private File OUTPUT_PATH = null; |
| 43 | + private PrintStream OUT = null; |
| 44 | + final DefaultListModel<String> expList; |
| 45 | + Vector<File> Files = new Vector<File>(); |
| 46 | + |
| 47 | + private JButton btnLoad; |
| 48 | + private JButton btnRemove; |
| 49 | + private JButton btnConvert; |
| 50 | + |
| 51 | + private JProgressBar progressBar; |
| 52 | + public Task task; |
| 53 | + private JLabel lblCurrent; |
| 54 | + private JLabel lblDefaultToLocal; |
| 55 | + private JButton btnOutput; |
| 56 | + |
| 57 | + class Task extends SwingWorker<Void, Void> { |
| 58 | + @Override |
| 59 | + public Void doInBackground() throws IOException, NoSuchAlgorithmException { |
| 60 | + setProgress(0); |
| 61 | + |
| 62 | + if(OUTPUT_PATH == null) OUT = new PrintStream("md5checksum.txt"); |
| 63 | + else OUT = new PrintStream(OUTPUT_PATH + File.separator + "md5checksum.txt"); |
| 64 | + for(int x = 0; x < Files.size(); x++) { |
| 65 | + String md5hash = MD5Checksum.calculateMD5(Files.get(x).getAbsolutePath()); |
| 66 | + OUT.println("MD5 (" + Files.get(x).getName() + ") = " + md5hash); |
| 67 | + int percentComplete = (int)(((double)(x + 1) / Files.size()) * 100); |
| 68 | + setProgress(percentComplete); |
| 69 | + } |
| 70 | + setProgress(100); |
| 71 | + |
| 72 | + OUT.close(); |
| 73 | + JOptionPane.showMessageDialog(null, "Calculation Complete"); |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + public void done() { |
| 78 | + massXable(contentPane, true); |
| 79 | + setCursor(null); //turn off the wait cursor |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public MD5ChecksumWindow() { |
| 84 | + setTitle("MD5 Checksum"); |
| 85 | + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 86 | + |
| 87 | + setBounds(125, 125, 450, 300); |
| 88 | + contentPane = new JPanel(); |
| 89 | + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); |
| 90 | + setContentPane(contentPane); |
| 91 | + SpringLayout sl_contentPane = new SpringLayout(); |
| 92 | + contentPane.setLayout(sl_contentPane); |
| 93 | + |
| 94 | + JScrollPane scrollPane = new JScrollPane(); |
| 95 | + sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 5, SpringLayout.WEST, contentPane); |
| 96 | + sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, -5, SpringLayout.EAST, contentPane); |
| 97 | + contentPane.add(scrollPane); |
| 98 | + |
| 99 | + expList = new DefaultListModel<String>(); |
| 100 | + final JList<String> listExp = new JList<String>(expList); |
| 101 | + listExp.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
| 102 | + scrollPane.setViewportView(listExp); |
| 103 | + |
| 104 | + btnLoad = new JButton("Load Files"); |
| 105 | + sl_contentPane.putConstraint(SpringLayout.WEST, btnLoad, 5, SpringLayout.WEST, contentPane); |
| 106 | + sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 6, SpringLayout.SOUTH, btnLoad); |
| 107 | + btnLoad.addActionListener(new ActionListener() { |
| 108 | + public void actionPerformed(ActionEvent e) { |
| 109 | + File[] newFiles = FileSelection.getGenericFiles(fc); |
| 110 | + if(newFiles != null) { |
| 111 | + for(int x = 0; x < newFiles.length; x++) { |
| 112 | + Files.add(newFiles[x]); |
| 113 | + expList.addElement(newFiles[x].getName()); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + contentPane.add(btnLoad); |
| 119 | + |
| 120 | + btnRemove = new JButton("Remove Files"); |
| 121 | + sl_contentPane.putConstraint(SpringLayout.NORTH, btnLoad, 0, SpringLayout.NORTH, btnRemove); |
| 122 | + sl_contentPane.putConstraint(SpringLayout.NORTH, btnRemove, 0, SpringLayout.NORTH, contentPane); |
| 123 | + sl_contentPane.putConstraint(SpringLayout.EAST, btnRemove, -5, SpringLayout.EAST, contentPane); |
| 124 | + btnRemove.addActionListener(new ActionListener() { |
| 125 | + public void actionPerformed(ActionEvent arg0) { |
| 126 | + while(listExp.getSelectedIndex() > -1) { |
| 127 | + Files.remove(listExp.getSelectedIndex()); |
| 128 | + expList.remove(listExp.getSelectedIndex()); |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + contentPane.add(btnRemove); |
| 133 | + |
| 134 | + btnConvert = new JButton("Calculate"); |
| 135 | + sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -62, SpringLayout.NORTH, btnConvert); |
| 136 | + sl_contentPane.putConstraint(SpringLayout.WEST, btnConvert, 167, SpringLayout.WEST, contentPane); |
| 137 | + sl_contentPane.putConstraint(SpringLayout.SOUTH, btnConvert, 0, SpringLayout.SOUTH, contentPane); |
| 138 | + sl_contentPane.putConstraint(SpringLayout.EAST, btnConvert, -175, SpringLayout.EAST, contentPane); |
| 139 | + contentPane.add(btnConvert); |
| 140 | + |
| 141 | + progressBar = new JProgressBar(); |
| 142 | + sl_contentPane.putConstraint(SpringLayout.NORTH, progressBar, 3, SpringLayout.NORTH, btnConvert); |
| 143 | + sl_contentPane.putConstraint(SpringLayout.EAST, progressBar, -5, SpringLayout.EAST, contentPane); |
| 144 | + progressBar.setStringPainted(true); |
| 145 | + contentPane.add(progressBar); |
| 146 | + |
| 147 | + btnConvert.setActionCommand("start"); |
| 148 | + |
| 149 | + lblCurrent = new JLabel("Current Output:"); |
| 150 | + sl_contentPane.putConstraint(SpringLayout.NORTH, lblCurrent, 37, SpringLayout.SOUTH, scrollPane); |
| 151 | + sl_contentPane.putConstraint(SpringLayout.WEST, lblCurrent, 0, SpringLayout.WEST, scrollPane); |
| 152 | + lblCurrent.setFont(new Font("Lucida Grande", Font.BOLD, 13)); |
| 153 | + contentPane.add(lblCurrent); |
| 154 | + |
| 155 | + lblDefaultToLocal = new JLabel("Default to Local Directory"); |
| 156 | + sl_contentPane.putConstraint(SpringLayout.NORTH, lblDefaultToLocal, 1, SpringLayout.NORTH, lblCurrent); |
| 157 | + sl_contentPane.putConstraint(SpringLayout.WEST, lblDefaultToLocal, 6, SpringLayout.EAST, lblCurrent); |
| 158 | + lblDefaultToLocal.setBackground(Color.WHITE); |
| 159 | + contentPane.add(lblDefaultToLocal); |
| 160 | + |
| 161 | + btnOutput = new JButton("Output Directory"); |
| 162 | + btnOutput.addActionListener(new ActionListener() { |
| 163 | + public void actionPerformed(ActionEvent e) { |
| 164 | + OUTPUT_PATH = FileSelection.getOutputDir(fc); |
| 165 | + if(OUTPUT_PATH != null) { |
| 166 | + lblDefaultToLocal.setText(OUTPUT_PATH.getAbsolutePath()); |
| 167 | + } |
| 168 | + } |
| 169 | + }); |
| 170 | + sl_contentPane.putConstraint(SpringLayout.NORTH, btnOutput, 6, SpringLayout.SOUTH, scrollPane); |
| 171 | + sl_contentPane.putConstraint(SpringLayout.WEST, btnOutput, 150, SpringLayout.WEST, contentPane); |
| 172 | + sl_contentPane.putConstraint(SpringLayout.EAST, btnOutput, -150, SpringLayout.EAST, contentPane); |
| 173 | + contentPane.add(btnOutput); |
| 174 | + btnConvert.addActionListener(this); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void actionPerformed(ActionEvent arg0) { |
| 179 | + massXable(contentPane, false); |
| 180 | + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
| 181 | + |
| 182 | + task = new Task(); |
| 183 | + task.addPropertyChangeListener(this); |
| 184 | + task.execute(); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Invoked when task's progress property changes. |
| 189 | + */ |
| 190 | + public void propertyChange(PropertyChangeEvent evt) { |
| 191 | + if ("progress" == evt.getPropertyName()) { |
| 192 | + int progress = (Integer) evt.getNewValue(); |
| 193 | + progressBar.setValue(progress); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public void massXable(Container con, boolean status) { |
| 198 | + for(Component c : con.getComponents()) { |
| 199 | + c.setEnabled(status); |
| 200 | + if(c instanceof Container) { massXable((Container)c, status); } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | + |
0 commit comments