001/* 002 * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License as published by the Free Software Foundation; either 007 * version 2.1 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014*/ 015/** 016 * A very simple GUI for the BZip program. 017 * Uses swing to display dialog boxes and windows. 018 * Uses a thread to throw of the compress/decompress operation. 019 * Also does testing of a bzipped file. 020 * 021 * TODO : Improve the test result window. 022 * Show progress during compress/decompress/test 023 */ 024package com.aftexsw.util.bzip; 025 026import org.apache.excalibur.bzip2.*; 027 028import com.aftexsw.ui.*; 029import com.aftexsw.util.*; 030 031import java.io.File; 032import java.util.*; 033import javax.swing.filechooser.*; 034 035import java.awt.*; 036import java.awt.event.*; 037import javax.swing.*; 038 039class BZipGUI extends JFrame { 040 public BZipGUI() { 041 this.setTitle("BZip for Java"); 042 this.setBounds(new Rectangle(100, 100, 300, 260)); 043 Util.centerFrame(this); 044 this.getContentPane().setLayout(new BorderLayout()); 045 JPanel buttonPanel = new JPanel(); 046 buttonPanel.setLayout(new FlowLayout()); 047 JButton but1 = new JButton("BZip file..."); 048 but1.addActionListener(new ActionListener() { 049 public void actionPerformed(ActionEvent event) { 050 handleOpenToZip(); 051 } 052 } 053 ); 054 JButton but2 = new JButton("unBZip file..."); 055 but2.addActionListener(new ActionListener() { 056 public void actionPerformed(ActionEvent event) { 057 handleOpenToUnzip(); 058 } 059 } 060 ); 061 JButton but3 = new JButton("test BZipped file..."); 062 but3.addActionListener(new ActionListener() { 063 public void actionPerformed(ActionEvent event) { 064 handleTest(); 065 } 066 } 067 ); 068 JButton but4 = new JButton("About BZip2GUI..."); 069 but4.addActionListener(new ActionListener() { 070 public void actionPerformed(ActionEvent event) { 071 handleAbout(); 072 } 073 } 074 ); 075 buttonPanel.add(but1); 076 buttonPanel.add(but2); 077 buttonPanel.add(but3); 078 buttonPanel.add(but4); 079 080 getContentPane().add(buttonPanel, BorderLayout.CENTER); 081 addWindowListener(new WindowAdapter() { 082 public void windowClosing(WindowEvent e) { 083 System.exit(0); 084 } 085 } 086 ); 087 088 try { 089 Image logo = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/bzip.jpeg")); 090 if (logo != null) { 091 JLabel canvas = new JLabel(new ImageIcon(logo)); 092 JPanel imagePanel = new JPanel(); 093 imagePanel.setLayout(new FlowLayout()); 094 imagePanel.add(canvas); 095 getContentPane().add(imagePanel, BorderLayout.SOUTH); 096 } 097 } catch(Exception e) {} 098 } 099 100 class TestZipThread extends Thread { 101 File in; 102 // BZipProgress guiProg = new BZipProgress(); 103 104 TestZipThread(File i) { 105 in = i; 106 } 107 108 public void run() { 109 if(BZip.test(in)) { 110 final JDialog jd = new JDialog(new JFrame(), "Test", true); 111 // jd.getContentPane().setLayout(new BorderLayout()); 112 jd.getContentPane().add(new JLabel("The file appears to be OK."), BorderLayout.NORTH); 113 JButton but = new JButton("OK"); 114 jd.getContentPane().add(but, BorderLayout.SOUTH); 115 but.addActionListener(new ActionListener() { 116 public void actionPerformed(ActionEvent ae) { 117 jd.dispose(); 118 } 119 } 120 ); 121 jd.setVisible(true); 122 } else { 123 final JDialog jd = new JDialog(new JFrame(), "Test", true); 124 // jd.getContentPane().setLayout(new BorderLayout()); 125 jd.getContentPane().add(new JLabel("The file appears to be corrupted."), BorderLayout.NORTH); 126 JButton but = new JButton("OK"); 127 jd.getContentPane().add(but, BorderLayout.SOUTH); 128 but.addActionListener(new ActionListener() { 129 public void actionPerformed(ActionEvent ae) { 130 jd.dispose(); 131 } 132 } 133 ); 134 jd.setVisible(true); 135 } 136 // guiProg.dispose(); 137 } 138 } 139 140 private void handleOpenToZip() { 141 JFileChooser chooser = new JFileChooser(); 142 chooser.setDialogType(JFileChooser.OPEN_DIALOG); 143 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 144 chooser.setCurrentDirectory(new File("/")); 145 int retval = chooser.showDialog(this, null); 146 if(retval == JFileChooser.APPROVE_OPTION) { 147 File theFile = chooser.getSelectedFile(); 148 if(theFile != null) { 149 if(theFile.isDirectory()) {} 150 else { 151 String in = theFile.toString(); 152 String out = in + ".bz2"; 153 ZipThread zp = new ZipThread(theFile, new File(out), 9); 154 zp.start(); 155 } 156 } 157 } 158 } 159 160 private void handleOpenToUnzip() { 161 BZipFileFilter bzipFilter; 162 163 JFileChooser chooser = new JFileChooser(); 164 bzipFilter = new BZipFileFilter("bz2", "BZip2 Compressed Files"); 165 chooser.addChoosableFileFilter(bzipFilter); 166 chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); 167 168 chooser.setDialogType(JFileChooser.OPEN_DIALOG); 169 // chooser.setDialogType(JFileChooser.SAVE_DIALOG); 170 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 171 int retval = chooser.showDialog(this, null); 172 if(retval == JFileChooser.APPROVE_OPTION) { 173 File theFile = chooser.getSelectedFile(); 174 if(theFile != null) { 175 if(theFile.isDirectory()) {} 176 else { 177 String in = theFile.toString(); 178 String out; 179 out = in.substring(0, in.length() - 4); 180 UnZipThread zp = new UnZipThread(theFile, new File(out)); 181 zp.start(); 182 } 183 } 184 } 185 } 186 187 private void handleTest() { 188 BZipFileFilter bzipFilter; 189 190 JFileChooser chooser = new JFileChooser(); 191 bzipFilter = new BZipFileFilter("bz2", "BZip2 Compressed Files"); 192 chooser.addChoosableFileFilter(bzipFilter); 193 194 chooser.setDialogType(JFileChooser.OPEN_DIALOG); 195 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 196 int retval = chooser.showDialog(this, null); 197 if(retval == JFileChooser.APPROVE_OPTION) { 198 File theFile = chooser.getSelectedFile(); 199 if(theFile != null) { 200 if(theFile.isDirectory()) {} 201 else { 202 TestZipThread zp = new TestZipThread(theFile); 203 zp.start(); 204 } 205 } 206 } 207 } 208 209 static void handleAbout() { 210 Splash splash = new Splash("BZip2GUI for Java v1.0", "images/aftexsw.jpeg"); 211 splash.setBounds(200, 200, 276, 130); 212 Util.centerFrame(splash); 213 splash.setVisible(true); 214 PerishTimer timer = new PerishTimer(splash, 15000); 215 timer.start(); 216 } 217} 218 219class BZipFileFilter extends FileFilter { 220 private String extension = null; 221 private String description = null; 222 private boolean useExtensionsInDescription = true; 223 224 public BZipFileFilter(String extension) { 225 this(extension, null); 226 } 227 228 public BZipFileFilter(String ext, String desc) { 229 extension = ext; 230 description = desc; 231 } 232 233 public boolean accept(File f) { 234 if(f != null) { 235 if(f.isDirectory()) { 236 return true; 237 } 238 if(extension.equals(getExtension(f))) 239 return true; 240 } 241 return false; 242 } 243 244 String getExtension(File f) { 245 if(f != null) { 246 String filename = f.getName(); 247 int i = filename.lastIndexOf('.'); 248 if(i > 0 && i < filename.length() - 1) { 249 return filename.substring(i + 1).toLowerCase(); 250 } 251 } 252 return null; 253 } 254 255 public String getDescription() { 256 return description; 257 } 258}