1 | import java.util.ResourceBundle;
|
---|
2 | import java.awt.*;
|
---|
3 | import java.awt.event.*;
|
---|
4 | import javax.swing.JFrame;
|
---|
5 | import javax.swing.JPanel;
|
---|
6 | import javax.swing.JCheckBox;
|
---|
7 | import javax.swing.JLabel;
|
---|
8 | import javax.swing.JButton;
|
---|
9 | import javax.swing.BoxLayout;
|
---|
10 | import javax.swing.JTextArea;
|
---|
11 | import javax.swing.JScrollPane;
|
---|
12 | import javax.swing.border.EmptyBorder;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.Box;
|
---|
15 | import javax.swing.JDialog;
|
---|
16 | import java.io.File;
|
---|
17 | import java.io.BufferedReader;
|
---|
18 | import java.io.FileReader;
|
---|
19 | import java.io.File;
|
---|
20 | import java.util.regex.Pattern;
|
---|
21 | import java.util.regex.Matcher;
|
---|
22 |
|
---|
23 | public class Uninstaller {
|
---|
24 |
|
---|
25 | public static int SCREEN_WIDTH = 600;
|
---|
26 | public static int SCREEN_HEIGHT = 450;
|
---|
27 |
|
---|
28 | public static final ResourceBundle bundle = ResourceBundle.getBundle("Uninstaller");
|
---|
29 |
|
---|
30 | public static final File gs2InstallProps = new File("etc/installation.properties");
|
---|
31 | public static final File gs3InstallProps = new File("installation.properties");
|
---|
32 |
|
---|
33 | boolean keepCollections = true;
|
---|
34 | //boolean keepModifiedFiles = false;
|
---|
35 | boolean ignoreReadOnlys = false;
|
---|
36 |
|
---|
37 | JFrame frame;
|
---|
38 | JCheckBox keepCollectionsCheckbox;
|
---|
39 | //JCheckBox keepModifiedFilesCheckbox;
|
---|
40 | JPanel progressPanel;
|
---|
41 | JPanel introPanel;
|
---|
42 | JTextArea log;
|
---|
43 | JButton uninstallButton;
|
---|
44 | JPanel initialToolbar;
|
---|
45 | JPanel finishToolbar;
|
---|
46 |
|
---|
47 |
|
---|
48 | public static void main( String[] args ) {
|
---|
49 | (new Uninstaller()).go();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void go() {
|
---|
53 |
|
---|
54 | frame = new JFrame();
|
---|
55 | frame.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
|
---|
56 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
57 | frame.setLocation(screenSize.width/2 - frame.getWidth()/2, screenSize.height/2 - frame.getHeight()/2);
|
---|
58 |
|
---|
59 | //The panel to be displayed while the uninstall is happening
|
---|
60 | progressPanel = new JPanel(new BorderLayout());
|
---|
61 | log = new JTextArea();
|
---|
62 | JScrollPane logPane = new JScrollPane(log);
|
---|
63 | logPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
---|
64 | logPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
---|
65 | progressPanel.add( BorderLayout.NORTH, new JLabel("Progress") );
|
---|
66 | progressPanel.add( BorderLayout.CENTER, logPane );
|
---|
67 |
|
---|
68 | //The panel to introduce and ask for options
|
---|
69 | introPanel = new JPanel(new BorderLayout());
|
---|
70 | frame.getContentPane().add( BorderLayout.NORTH, introPanel );
|
---|
71 |
|
---|
72 | JPanel innerPanel = new JPanel();
|
---|
73 | innerPanel.setLayout( new BoxLayout(innerPanel,BoxLayout.Y_AXIS) );
|
---|
74 | innerPanel.setBorder( new EmptyBorder(10, 10, 5, 10) );
|
---|
75 | introPanel.add( BorderLayout.WEST, innerPanel );
|
---|
76 |
|
---|
77 | //initial toolbar
|
---|
78 | initialToolbar = new JPanel();
|
---|
79 | JButton cancelButton = new JButton(bundle.getString("cancel"));
|
---|
80 | cancelButton.addActionListener( new CancelListener() );
|
---|
81 |
|
---|
82 | uninstallButton = new JButton(bundle.getString("uninstall"));
|
---|
83 | uninstallButton.addActionListener( new StartUninstallListener() );
|
---|
84 |
|
---|
85 | initialToolbar.add(cancelButton);
|
---|
86 | initialToolbar.add(uninstallButton);
|
---|
87 | frame.getContentPane().add( BorderLayout.SOUTH, initialToolbar );
|
---|
88 |
|
---|
89 | //finish toolbar
|
---|
90 | finishToolbar = new JPanel();
|
---|
91 | JButton finishButton = new JButton(bundle.getString("finish"));
|
---|
92 | finishButton.addActionListener( new FinishListener() );
|
---|
93 | finishToolbar.add( finishButton );
|
---|
94 |
|
---|
95 | String pwd = (new File(".")).getAbsolutePath();
|
---|
96 | if ( pwd.endsWith("/.") ) {
|
---|
97 | pwd = pwd.substring( 0, pwd.length()-2 );
|
---|
98 | }
|
---|
99 |
|
---|
100 | JLabel l;
|
---|
101 |
|
---|
102 | l = new JLabel(bundle.getString("will.uninstall.from"));
|
---|
103 | innerPanel.add( l );
|
---|
104 | innerPanel.add( Box.createRigidArea(new Dimension(5,5)) );
|
---|
105 |
|
---|
106 | l = new JLabel(" " + pwd);
|
---|
107 | l.setFont( new Font( "Monospaced", Font.BOLD, 14 ) );
|
---|
108 | innerPanel.add( l );
|
---|
109 | innerPanel.add( Box.createRigidArea(new Dimension(5,20)) );
|
---|
110 |
|
---|
111 | l = new JLabel(bundle.getString("uninstall.options"));
|
---|
112 | innerPanel.add( l );
|
---|
113 |
|
---|
114 | keepCollectionsCheckbox = new JCheckBox(bundle.getString("keep.collections"));
|
---|
115 | keepCollectionsCheckbox.setSelected(true);
|
---|
116 | innerPanel.add( keepCollectionsCheckbox );
|
---|
117 |
|
---|
118 | //keepModifiedFilesCheckbox = new JCheckBox("Keep Modified Files");
|
---|
119 | //innerPanel.add( keepModifiedFilesCheckbox );
|
---|
120 |
|
---|
121 | frame.setTitle( bundle.getString("greenstone.uninstaller") );
|
---|
122 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
123 | frame.setVisible(true);
|
---|
124 |
|
---|
125 | boolean ready = precheck();
|
---|
126 | if ( !ready ) {
|
---|
127 | System.exit(0);
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | class CancelListener implements ActionListener {
|
---|
133 | public void actionPerformed( ActionEvent e ) {
|
---|
134 | System.exit(0);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | class FinishListener implements ActionListener {
|
---|
139 | public void actionPerformed( ActionEvent e ) {
|
---|
140 | System.exit(0);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | class StartUninstallListener implements ActionListener {
|
---|
145 | public void actionPerformed( ActionEvent e ) {
|
---|
146 |
|
---|
147 | //The dialog to ask "are you sure"
|
---|
148 | Object[] options = { bundle.getString("cancel"), bundle.getString("uninstall") };
|
---|
149 | int n = JOptionPane.showOptionDialog(
|
---|
150 | frame,
|
---|
151 | bundle.getString("are.you.sure"),
|
---|
152 | bundle.getString("confirmation"),
|
---|
153 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
154 | JOptionPane.QUESTION_MESSAGE,
|
---|
155 | null,
|
---|
156 | options,
|
---|
157 | options[0]
|
---|
158 | );
|
---|
159 | if ( n == 0 ) {
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | keepCollections = keepCollectionsCheckbox.isSelected();
|
---|
164 | //keepModifiedFiles = keepModifiedFilesCheckbox.isSelected();
|
---|
165 |
|
---|
166 | //confirm delete of collections
|
---|
167 | if ( !keepCollections ) {
|
---|
168 | options[0] = bundle.getString("cancel");
|
---|
169 | options[1] = bundle.getString("continue");
|
---|
170 | n = JOptionPane.showOptionDialog(
|
---|
171 | frame,
|
---|
172 | bundle.getString("are.you.sure.collections"),
|
---|
173 | bundle.getString("confirmation"),
|
---|
174 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
175 | JOptionPane.WARNING_MESSAGE,
|
---|
176 | null,
|
---|
177 | options,
|
---|
178 | options[0]
|
---|
179 | );
|
---|
180 | if ( n == 0 ) {
|
---|
181 | return;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | //swap to the progress panel
|
---|
186 | introPanel.setVisible(false);
|
---|
187 | frame.getContentPane().remove(introPanel);
|
---|
188 | frame.getContentPane().add( BorderLayout.CENTER, progressPanel);
|
---|
189 | frame.setVisible(true);
|
---|
190 |
|
---|
191 | //disable the uninstall button
|
---|
192 | uninstallButton.setEnabled( false );
|
---|
193 |
|
---|
194 |
|
---|
195 | //start the unstinall
|
---|
196 | doUninstall();
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Do some checks
|
---|
202 | */
|
---|
203 | public boolean precheck() {
|
---|
204 |
|
---|
205 | if ( !gs2InstallProps.exists() && !gs3InstallProps.exists() ) {
|
---|
206 | log.append( bundle.getString("error.couldnt.find.install.props") + "\n" );
|
---|
207 | JOptionPane.showMessageDialog(frame, bundle.getString("error.couldnt.find.install.props"), bundle.getString("error"), 0);
|
---|
208 | return false;
|
---|
209 | }
|
---|
210 | return true;
|
---|
211 |
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Does the uninstall
|
---|
216 | */
|
---|
217 | public void doUninstall() {
|
---|
218 |
|
---|
219 | File startMenuPath = null;
|
---|
220 |
|
---|
221 | //delete the start menu group if present
|
---|
222 |
|
---|
223 | if ( gs2InstallProps.exists() ) {
|
---|
224 | String smp = null;
|
---|
225 | try {
|
---|
226 | smp = getPropertyValue( "startmenu.path", gs2InstallProps );
|
---|
227 | } catch ( Exception e ) {}
|
---|
228 | if ( smp != null ) {
|
---|
229 | startMenuPath = new File( smp );
|
---|
230 | }
|
---|
231 | } else if ( gs3InstallProps.exists() ) {
|
---|
232 | String smp = null;
|
---|
233 | try {
|
---|
234 | smp = getPropertyValue( "startmenu.path", gs3InstallProps );
|
---|
235 | } catch ( Exception e ) {}
|
---|
236 | if ( smp != null ) {
|
---|
237 | startMenuPath = new File( smp );
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | if ( startMenuPath == null ) {
|
---|
242 | log.append( bundle.getString("info.no.startmenu") + "\n" );
|
---|
243 | } else {
|
---|
244 | log.append( "StartMenu Path: " + startMenuPath.getAbsolutePath() + "\n" );
|
---|
245 | try {
|
---|
246 | recursiveDelete( startMenuPath, null );
|
---|
247 | } catch ( CancelledException ce ) {
|
---|
248 | log.append( bundle.getString("cancelled") + "\n" );
|
---|
249 | changeToFinishToolbar();
|
---|
250 | JOptionPane.showMessageDialog(frame, bundle.getString("cancelled"), bundle.getString("complete"), 1);
|
---|
251 | return;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | //delete the files
|
---|
256 | try {
|
---|
257 | File[] exceptions = null;
|
---|
258 | if ( keepCollections ) {
|
---|
259 | exceptions = new File[2];
|
---|
260 | exceptions[0] = new File("web/sites/localsite/collect");
|
---|
261 | exceptions[1] = new File("collect");
|
---|
262 | }
|
---|
263 |
|
---|
264 | File cd = null;
|
---|
265 | try {
|
---|
266 | cd = new File( new File(".").getCanonicalPath() );
|
---|
267 | } catch ( Exception e ) {
|
---|
268 | JOptionPane.showMessageDialog(frame, bundle.getString("failed.to.figure.cd"), bundle.getString("error") , 0);
|
---|
269 | System.exit(0);
|
---|
270 | }
|
---|
271 | recursiveDelete( cd , exceptions );
|
---|
272 | } catch ( CancelledException ce ) {
|
---|
273 | log.append( bundle.getString("cancelled") + "\n" );
|
---|
274 | JOptionPane.showMessageDialog(frame, bundle.getString("cancelled"), bundle.getString("complete"), 1);
|
---|
275 | changeToFinishToolbar();
|
---|
276 | return;
|
---|
277 | }
|
---|
278 | changeToFinishToolbar();
|
---|
279 | JOptionPane.showMessageDialog(frame, bundle.getString("finished"), bundle.getString("complete"), 1);
|
---|
280 | }
|
---|
281 |
|
---|
282 | public String getPropertyValue( String propertyName, File file ) throws Exception {
|
---|
283 | String regex = "^" + propertyName.replaceAll("\\.","\\\\.") + "[:=]\\s*(.*)$";
|
---|
284 | Pattern wantedLine = Pattern.compile( regex );
|
---|
285 | BufferedReader in = null;
|
---|
286 | try {
|
---|
287 | in = new BufferedReader(new FileReader( file ));
|
---|
288 | } catch ( Exception e ) {
|
---|
289 | throw new Exception( "Error - couldn't open the properties file " + file );
|
---|
290 | }
|
---|
291 |
|
---|
292 | String line, value = null;
|
---|
293 |
|
---|
294 | try {
|
---|
295 | boolean found = false;
|
---|
296 | while ( (line = in.readLine()) != null && !found ) {
|
---|
297 |
|
---|
298 | Matcher matcher = wantedLine.matcher( line );
|
---|
299 | if ( matcher.matches() ) {
|
---|
300 | //found the property
|
---|
301 | //System.err.println( "found the property" );
|
---|
302 | value = matcher.group(1);
|
---|
303 | value = value.replaceAll( "#.*", "" ).trim();
|
---|
304 | return value;
|
---|
305 | }
|
---|
306 |
|
---|
307 | }
|
---|
308 |
|
---|
309 | //close the input stream
|
---|
310 | //System.err.println( "close the input stream" );
|
---|
311 | in.close();
|
---|
312 |
|
---|
313 | } catch ( Exception e ) {
|
---|
314 | throw new Exception( "Error - couldn't read from the properties file" );
|
---|
315 | }
|
---|
316 | return null;
|
---|
317 |
|
---|
318 | }
|
---|
319 |
|
---|
320 | public void recursiveDelete( File f, File[] exceptions ) throws CancelledException {
|
---|
321 |
|
---|
322 | //log.append( "Processing: " + f.getAbsolutePath() + "\n" );
|
---|
323 |
|
---|
324 | // Make sure the file or directory exists
|
---|
325 | if (!f.exists()) {
|
---|
326 | log.append( Strings.replaceAll( bundle.getString("warning.nonexistent"), "{file}", f.getAbsolutePath() ) + "\n" );
|
---|
327 | return;
|
---|
328 | }
|
---|
329 |
|
---|
330 | // if this is an exception, return
|
---|
331 | if ( exceptions != null ) {
|
---|
332 | for ( int i=0; i<exceptions.length; i++ ) {
|
---|
333 | //System.out.println( "Comparing '" + f.getAbsolutePath() + "' with '" + exceptions[i].getAbsolutePath() + "'" );
|
---|
334 | try {
|
---|
335 | if ( f.equals( exceptions[i] ) || f.getCanonicalPath().equals(exceptions[i].getCanonicalPath()) ) {
|
---|
336 | log.append( Strings.replaceAll( bundle.getString("info.skipping"), "{file}", f.getAbsolutePath() ) + "\n" );
|
---|
337 | return;
|
---|
338 | }
|
---|
339 | } catch ( Exception e ) {
|
---|
340 | System.err.println("ERROR: Failed to resolve a path");
|
---|
341 | return;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | //check existance
|
---|
347 | if ( !f.exists() ) {
|
---|
348 | log.append( Strings.replaceAll( bundle.getString("error.nonexistent"), "{file}", f.getAbsolutePath() ) + "\n" );
|
---|
349 | return;
|
---|
350 | }
|
---|
351 |
|
---|
352 | //if it is a directory, recurse
|
---|
353 | if (f.isDirectory()) {
|
---|
354 | File[] files = f.listFiles();
|
---|
355 | if ( files != null && files.length > 0) {
|
---|
356 | for ( int i=0; i<files.length; i++ ) {
|
---|
357 | try {
|
---|
358 | recursiveDelete( files[i], exceptions );
|
---|
359 | } catch ( CancelledException ce ) {
|
---|
360 | throw new CancelledException();
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | //if this is now an empty directory, or a file, delete it
|
---|
367 | boolean doDelete = true;
|
---|
368 | if ( f.isDirectory() ) {
|
---|
369 | File[] files = f.listFiles();
|
---|
370 | doDelete = ( files == null || files.length == 0 );
|
---|
371 | }
|
---|
372 |
|
---|
373 | if ( doDelete ) {
|
---|
374 |
|
---|
375 | log.append( Strings.replaceAll( bundle.getString("deleting"), "{file}", f.getAbsolutePath() ) + "\n" );
|
---|
376 | while ( !f.delete() ) {
|
---|
377 | log.append( Strings.replaceAll( bundle.getString("warning.couldnt.delete"), "{file}", f.getAbsolutePath() ) + "\n" );
|
---|
378 |
|
---|
379 | if ( ignoreReadOnlys ) {
|
---|
380 | return;
|
---|
381 | }
|
---|
382 |
|
---|
383 | Object[] options = { bundle.getString("cancel"), bundle.getString("retry"), bundle.getString("skip"), bundle.getString("skip.all") };
|
---|
384 | int n = JOptionPane.showOptionDialog(
|
---|
385 | frame,
|
---|
386 | Strings.replaceAll( bundle.getString("warning.readonly"), "{file}", f.getAbsolutePath() ),
|
---|
387 | bundle.getString("readonly"),
|
---|
388 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
389 | JOptionPane.QUESTION_MESSAGE,
|
---|
390 | null,
|
---|
391 | options,
|
---|
392 | options[2]
|
---|
393 | );
|
---|
394 |
|
---|
395 | if ( n == 0 ) {
|
---|
396 | throw new CancelledException();
|
---|
397 | } else if ( n == 2 ) {
|
---|
398 | return;
|
---|
399 | } else if ( n == 3 ) {
|
---|
400 | ignoreReadOnlys = true;
|
---|
401 | return;
|
---|
402 | }
|
---|
403 |
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | }
|
---|
408 |
|
---|
409 | class CancelledException extends Exception {}
|
---|
410 |
|
---|
411 | public void changeToFinishToolbar() {
|
---|
412 | initialToolbar.setVisible(false);
|
---|
413 | frame.getContentPane().remove(initialToolbar);
|
---|
414 | frame.getContentPane().add( BorderLayout.SOUTH, finishToolbar );
|
---|
415 | finishToolbar.setVisible(true);
|
---|
416 | }
|
---|
417 |
|
---|
418 | }
|
---|
419 |
|
---|
420 | class Strings {
|
---|
421 | static String replaceAll( String s, String nugget, String replacement ) {
|
---|
422 | StringBuffer string = new StringBuffer(s);
|
---|
423 | StringBuffer r = new StringBuffer();
|
---|
424 |
|
---|
425 | int io = 0;
|
---|
426 | while ( (io=string.toString().indexOf(nugget)) != -1 ) {
|
---|
427 | r.append( string.toString().substring( 0, io ) );
|
---|
428 | r.append( replacement );
|
---|
429 | string.delete( 0, io + nugget.length() );
|
---|
430 | }
|
---|
431 | r.append( string.toString() );
|
---|
432 | return r.toString();
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|