import java.util.ResourceBundle; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.BoxLayout; import javax.swing.JTextArea; import javax.swing.JScrollPane; import javax.swing.border.EmptyBorder; import javax.swing.JOptionPane; import javax.swing.Box; import javax.swing.JDialog; import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.io.File; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Uninstaller { public static int SCREEN_WIDTH = 600; public static int SCREEN_HEIGHT = 450; public static final ResourceBundle bundle = ResourceBundle.getBundle("Uninstaller"); public static final File gs2InstallProps = new File("etc/installation.properties"); public static final File gs3InstallProps = new File("installation.properties"); boolean keepCollections = true; //boolean keepModifiedFiles = false; boolean ignoreReadOnlys = false; JFrame frame; JCheckBox keepCollectionsCheckbox; //JCheckBox keepModifiedFilesCheckbox; JPanel progressPanel; JPanel introPanel; JTextArea log; JButton uninstallButton; JPanel initialToolbar; JPanel finishToolbar; public static void main( String[] args ) { (new Uninstaller()).go(); } public void go() { frame = new JFrame(); frame.setSize( SCREEN_WIDTH, SCREEN_HEIGHT ); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width/2 - frame.getWidth()/2, screenSize.height/2 - frame.getHeight()/2); //The panel to be displayed while the uninstall is happening progressPanel = new JPanel(new BorderLayout()); log = new JTextArea(); JScrollPane logPane = new JScrollPane(log); logPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); logPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); progressPanel.add( BorderLayout.NORTH, new JLabel("Progress") ); progressPanel.add( BorderLayout.CENTER, logPane ); //The panel to introduce and ask for options introPanel = new JPanel(new BorderLayout()); frame.getContentPane().add( BorderLayout.NORTH, introPanel ); JPanel innerPanel = new JPanel(); innerPanel.setLayout( new BoxLayout(innerPanel,BoxLayout.Y_AXIS) ); innerPanel.setBorder( new EmptyBorder(10, 10, 5, 10) ); introPanel.add( BorderLayout.WEST, innerPanel ); //initial toolbar initialToolbar = new JPanel(); JButton cancelButton = new JButton(bundle.getString("cancel")); cancelButton.addActionListener( new CancelListener() ); uninstallButton = new JButton(bundle.getString("uninstall")); uninstallButton.addActionListener( new StartUninstallListener() ); initialToolbar.add(cancelButton); initialToolbar.add(uninstallButton); frame.getContentPane().add( BorderLayout.SOUTH, initialToolbar ); //finish toolbar finishToolbar = new JPanel(); JButton finishButton = new JButton(bundle.getString("finish")); finishButton.addActionListener( new FinishListener() ); finishToolbar.add( finishButton ); String pwd = (new File(".")).getAbsolutePath(); if ( pwd.endsWith("/.") ) { pwd = pwd.substring( 0, pwd.length()-2 ); } JLabel l; l = new JLabel(bundle.getString("will.uninstall.from")); innerPanel.add( l ); innerPanel.add( Box.createRigidArea(new Dimension(5,5)) ); l = new JLabel(" " + pwd); l.setFont( new Font( "Monospaced", Font.BOLD, 14 ) ); innerPanel.add( l ); innerPanel.add( Box.createRigidArea(new Dimension(5,20)) ); l = new JLabel(bundle.getString("uninstall.options")); innerPanel.add( l ); keepCollectionsCheckbox = new JCheckBox(bundle.getString("keep.collections")); keepCollectionsCheckbox.setSelected(true); innerPanel.add( keepCollectionsCheckbox ); //keepModifiedFilesCheckbox = new JCheckBox("Keep Modified Files"); //innerPanel.add( keepModifiedFilesCheckbox ); frame.setTitle( bundle.getString("greenstone.uninstaller") ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); boolean ready = precheck(); if ( !ready ) { System.exit(0); } } class CancelListener implements ActionListener { public void actionPerformed( ActionEvent e ) { System.exit(0); } } class FinishListener implements ActionListener { public void actionPerformed( ActionEvent e ) { System.exit(0); } } class StartUninstallListener implements ActionListener { public void actionPerformed( ActionEvent e ) { //The dialog to ask "are you sure" Object[] options = { bundle.getString("cancel"), bundle.getString("uninstall") }; int n = JOptionPane.showOptionDialog( frame, bundle.getString("are.you.sure"), bundle.getString("confirmation"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0] ); if ( n == 0 ) { return; } keepCollections = keepCollectionsCheckbox.isSelected(); //keepModifiedFiles = keepModifiedFilesCheckbox.isSelected(); //confirm delete of collections if ( !keepCollections ) { options[0] = bundle.getString("cancel"); options[1] = bundle.getString("continue"); n = JOptionPane.showOptionDialog( frame, bundle.getString("are.you.sure.collections"), bundle.getString("confirmation"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0] ); if ( n == 0 ) { return; } } //swap to the progress panel introPanel.setVisible(false); frame.getContentPane().remove(introPanel); frame.getContentPane().add( BorderLayout.CENTER, progressPanel); frame.setVisible(true); //disable the uninstall button uninstallButton.setEnabled( false ); //start the unstinall doUninstall(); } } /** * Do some checks */ public boolean precheck() { if ( !gs2InstallProps.exists() && !gs3InstallProps.exists() ) { log.append( bundle.getString("error.couldnt.find.install.props") + "\n" ); JOptionPane.showMessageDialog(frame, bundle.getString("error.couldnt.find.install.props"), bundle.getString("error"), 0); return false; } return true; } /** * Does the uninstall */ public void doUninstall() { File startMenuPath = null; //delete the start menu group if present if ( gs2InstallProps.exists() ) { String smp = null; try { smp = getPropertyValue( "startmenu.path", gs2InstallProps ); } catch ( Exception e ) {} if ( smp != null ) { startMenuPath = new File( smp ); } } else if ( gs3InstallProps.exists() ) { String smp = null; try { smp = getPropertyValue( "startmenu.path", gs3InstallProps ); } catch ( Exception e ) {} if ( smp != null ) { startMenuPath = new File( smp ); } } if ( startMenuPath == null ) { log.append( bundle.getString("info.no.startmenu") + "\n" ); } else { log.append( "StartMenu Path: " + startMenuPath.getAbsolutePath() + "\n" ); try { recursiveDelete( startMenuPath, null ); } catch ( CancelledException ce ) { log.append( bundle.getString("cancelled") + "\n" ); changeToFinishToolbar(); JOptionPane.showMessageDialog(frame, bundle.getString("cancelled"), bundle.getString("complete"), 1); return; } } //delete the files try { File[] exceptions = null; if ( keepCollections ) { exceptions = new File[2]; exceptions[0] = new File("web/sites/localsite/collect"); exceptions[1] = new File("collect"); } File cd = null; try { cd = new File( new File(".").getCanonicalPath() ); } catch ( Exception e ) { JOptionPane.showMessageDialog(frame, bundle.getString("failed.to.figure.cd"), bundle.getString("error") , 0); System.exit(0); } recursiveDelete( cd , exceptions ); } catch ( CancelledException ce ) { log.append( bundle.getString("cancelled") + "\n" ); JOptionPane.showMessageDialog(frame, bundle.getString("cancelled"), bundle.getString("complete"), 1); changeToFinishToolbar(); return; } //create the flag file to indicate the uninstaller wants jre and uninst.jar to be deleted try { (new File("uninst.flag")).createNewFile(); } catch (Exception e) { log.append( bundle.getString("couldnt-create-flagfile") + "\n" ); } changeToFinishToolbar(); JOptionPane.showMessageDialog(frame, bundle.getString("finished"), bundle.getString("complete"), 1); } public String getPropertyValue( String propertyName, File file ) throws Exception { String regex = "^" + propertyName.replaceAll("\\.","\\\\.") + "[:=]\\s*(.*)$"; Pattern wantedLine = Pattern.compile( regex ); BufferedReader in = null; try { in = new BufferedReader(new FileReader( file )); } catch ( Exception e ) { throw new Exception( "Error - couldn't open the properties file " + file ); } String line, value = null; try { boolean found = false; while ( (line = in.readLine()) != null && !found ) { Matcher matcher = wantedLine.matcher( line ); if ( matcher.matches() ) { //found the property //System.err.println( "found the property" ); value = matcher.group(1); value = value.replaceAll( "#.*", "" ).trim(); return value; } } //close the input stream //System.err.println( "close the input stream" ); in.close(); } catch ( Exception e ) { throw new Exception( "Error - couldn't read from the properties file" ); } return null; } public void recursiveDelete( File f, File[] exceptions ) throws CancelledException { //log.append( "Processing: " + f.getAbsolutePath() + "\n" ); // Make sure the file or directory exists if (!f.exists()) { log.append( Strings.replaceAll( bundle.getString("warning.nonexistent"), "{file}", f.getAbsolutePath() ) + "\n" ); return; } // if this is an exception, return if ( exceptions != null ) { for ( int i=0; i 0) { for ( int i=0; i