~ubuntu-branches/debian/squeeze/latexdraw/squeeze

« back to all changes in this revision

Viewing changes to latexDraw/installer/InstallerUI.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-05-03 23:49:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090503234935-cls7n48x018g0vk2
Tags: upstream-2.0.2+1
ImportĀ upstreamĀ versionĀ 2.0.2+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package latexDraw.installer;
 
2
 
 
3
import java.awt.BorderLayout;
 
4
import java.awt.Color;
 
5
import java.awt.Dimension;
 
6
import java.awt.Toolkit;
 
7
import java.io.*;
 
8
 
 
9
import javax.swing.*;
 
10
import javax.swing.border.CompoundBorder;
 
11
import javax.swing.border.EmptyBorder;
 
12
import javax.swing.border.TitledBorder;
 
13
 
 
14
import latexDraw.ui.listeners.InstallerListener;
 
15
import latexDraw.util.LaTeXDrawPath;
 
16
 
 
17
/** 
 
18
 * This class defines the LaTeXDraw installer.<br>
 
19
 *<br>
 
20
 * This file is part of LaTeXDraw<br>
 
21
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
22
 *<br>
 
23
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
24
 *  it under the terms of the GNU General Public License as published by
 
25
 *  the Free Software Foundation; either version 2 of the License, or
 
26
 *  (at your option) any later version.<br>
 
27
 *<br>
 
28
 *  LaTeXDraw is distributed without any warranty; without even the 
 
29
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
30
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
31
 *<br>
 
32
 * 05/11/07<br>
 
33
 * @author Arnaud BLOUIN<br>
 
34
 * @version 0.1<br>
 
35
 */
 
36
public final class InstallerUI extends JFrame implements Runnable
 
37
{
 
38
        private static final long serialVersionUID = 1L;
 
39
 
 
40
        /** The name of the os. */
 
41
        protected String os;
 
42
 
 
43
        /** The listener of the installer. */
 
44
        protected transient InstallerListener listener;
 
45
        
 
46
        /** The current position of the slide of the installer. */
 
47
        protected int slidePosition;
 
48
        
 
49
        /** The first slide. */
 
50
        protected JPanel panel1;
 
51
        
 
52
        /** The second slide. */
 
53
        protected JPanel panel2;
 
54
        
 
55
        /** The third slide. */
 
56
        protected JPanel panel3;
 
57
        
 
58
        /** The button "previous". */
 
59
        protected JButton previousB;
 
60
        
 
61
        /** The button "next". */
 
62
        protected JButton nextB;
 
63
        
 
64
        /** The field that defines the path where we have to install LaTeXDraw. */
 
65
        protected JTextField pathInstall;
 
66
        
 
67
        /** The progress bar of the installation. */
 
68
        protected JProgressBar progressBar;
 
69
        
 
70
        /** Used to stop the thread of the installation. */
 
71
        protected boolean continueInstall;
 
72
        
 
73
        /** The thread that installs LaTeXDraw. */
 
74
        protected transient Thread installThread;
 
75
 
 
76
        /** The label that can appear during the installation. */
 
77
        protected JLabel labelProgress;
 
78
        
 
79
        /** The path where the jar file is placed. */
 
80
        protected String pathJar;
 
81
        
 
82
        /** The maximum number of slides. */
 
83
        public static final int MAX_PANEL = 3;
 
84
        
 
85
        public static final String ACTION_NEXT = "next";//$NON-NLS-1$
 
86
        
 
87
        public static final String ACTION_PREVIOUS = "previous";//$NON-NLS-1$
 
88
        
 
89
        public static final String ACTION_CANCEL = "cancel";//$NON-NLS-1$
 
90
        
 
91
        public static final String ACTION_TERMINATE = "terminate";//$NON-NLS-1$
 
92
        
 
93
        public static final String LABEL_NEXT = "Next";
 
94
        
 
95
        public static final String LABEL_TERMINATE = "Terminate";
 
96
        
 
97
        public static final String ACTION_CHOOSE_FOLDER = "choose";//$NON-NLS-1$
 
98
        
 
99
        /** The dimension of a slide. */
 
100
        public static final Dimension PANEL_SIZE = new Dimension(650, 350);
 
101
        
 
102
        public static final String NAME_TEMPLATES_DIR = "templates";//$NON-NLS-1$
 
103
        
 
104
        public static final String NAME_CACHE_TEMPLATES_DIR = ".cache";//$NON-NLS-1$
 
105
        
 
106
        public static final String DATA_DIR = "data";//$NON-NLS-1$
 
107
        
 
108
        
 
109
        /**
 
110
         * The main function: it creates an installer and launch it.
 
111
         * @param argc Useless.
 
112
         * @since 1.9.2
 
113
         */
 
114
        public static void main(String argc[])
 
115
        {
 
116
                InstallerUI installer  = new InstallerUI();
 
117
                
 
118
                installer.launch();
 
119
        }
 
120
        
 
121
        
 
122
        /**
 
123
         * The default constructor.
 
124
         */
 
125
        public InstallerUI()
 
126
        {
 
127
                super("LaTeXDraw installer");
 
128
                
 
129
                pathJar = LaTeXDrawPath.getPathJar();
 
130
                os = System.getProperty("os.name");//$NON-NLS-1$
 
131
                listener = new InstallerListener(this);
 
132
                slidePosition = 1;
 
133
                
 
134
                if(os.toLowerCase().equals("linux") && System.getProperty("java.version").toLowerCase().compareTo("1.6")>=0)//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
 
135
                        try
 
136
                        { UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); }//$NON-NLS-1$
 
137
                        catch(Exception e) { /* Dommage ;) */ }
 
138
                
 
139
                if(os.toLowerCase().contains("win")) //$NON-NLS-1$
 
140
                        try
 
141
                        { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }//$NON-NLS-1$
 
142
                        catch(Exception e) { /* Dommage ;) */ }
 
143
                        
 
144
                setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("res/LaTeXDrawIcon.png")));//$NON-NLS-1$
 
145
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
146
                createStartPanel();
 
147
                
 
148
                getContentPane().setLayout(new BorderLayout());
 
149
                getContentPane().add(createHeaderPanel(), BorderLayout.NORTH);
 
150
                getContentPane().add(panel1, BorderLayout.CENTER);
 
151
                getContentPane().add(createControlButtonsPanel(), BorderLayout.SOUTH);
 
152
                
 
153
                previousB.setEnabled(false);
 
154
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
 
155
                pack();
 
156
                setLocation(dim.width/2-getWidth()/2, dim.height/2-getHeight()/2);
 
157
                setVisible(false);
 
158
        }
 
159
        
 
160
        
 
161
        /**
 
162
         * Creates the first slide.
 
163
         * @since 1.9.2
 
164
         */
 
165
        protected final void createStartPanel()
 
166
        {
 
167
                panel1 = new JPanel();
 
168
                String plus;
 
169
                
 
170
                if(os.toLowerCase().contains("vista"))//$NON-NLS-1$
 
171
                        plus = "</li><br><li>Be careful with Vista! Files\".<br>You must have launched this installer via the script install_vista.vbs.";
 
172
                else
 
173
                        plus = "";//$NON-NLS-1$
 
174
                
 
175
                panel1.add(new JLabel("<html><br><br><br><ul><li>"+"This installer will, create the necessary directories, place the templates in your profile<br>and place the latexdraw files in the chosen directory."+//$NON-NLS-1$
 
176
                                "</li><br><li>"+"You must be administrator/root to install LaTeXDraw."+ plus+"</li></ul></html>"), BorderLayout.CENTER);//$NON-NLS-1$//$NON-NLS-3$
 
177
                
 
178
                setPanelDimension(panel1);
 
179
        }
 
180
        
 
181
        
 
182
        
 
183
        /**
 
184
         * Creates the second slide.
 
185
         * @since 1.9.2
 
186
         */
 
187
        protected final void createPanelChoosePath()
 
188
        {
 
189
                JPanel p1 = new JPanel();
 
190
                JPanel p2 = new JPanel();
 
191
                
 
192
                panel2 = new JPanel();
 
193
                
 
194
                panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
 
195
                p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
 
196
                
 
197
                pathInstall = new JTextField();
 
198
                pathInstall.setEditable(false);
 
199
                pathInstall.setPreferredSize(new Dimension(250, 35));
 
200
                pathInstall.setMinimumSize(new Dimension(250, 35));
 
201
                pathInstall.setMaximumSize(new Dimension(250, 35));
 
202
                JButton chooseB = new JButton("Choose folder");
 
203
                chooseB.setName(ACTION_CHOOSE_FOLDER);
 
204
                chooseB.setActionCommand(ACTION_CHOOSE_FOLDER);
 
205
                chooseB.addActionListener(listener);
 
206
                
 
207
                p2.add(new JLabel("Choose the path where the directory LaTeXDraw will be installed:"));
 
208
                p2.add(pathInstall);
 
209
                p2.add(chooseB);
 
210
                
 
211
                p2.setMaximumSize(new Dimension(450, 150));
 
212
                p2.setMinimumSize(new Dimension(450, 150));
 
213
                p2.setPreferredSize(new Dimension(450, 150));
 
214
                p1.setMaximumSize(new Dimension(450, 300));
 
215
                p1.setMinimumSize(new Dimension(450, 300));
 
216
                p1.setPreferredSize(new Dimension(450, 300));
 
217
                p1.add(p2);
 
218
                panel2.add(p1);
 
219
                setPanelDimension(panel2);
 
220
                
 
221
                if(os.equals("Linux"))//$NON-NLS-1$
 
222
                        pathInstall.setText("/opt");//$NON-NLS-1$
 
223
                else
 
224
                        if(os.toLowerCase().contains("windows"))//$NON-NLS-1$
 
225
                        {
 
226
                                File dir = new File("C:\\Program Files\\");//$NON-NLS-1$
 
227
                                int cpt = 0, max = 10;
 
228
                                
 
229
                                while(!dir.exists() && cpt<max)
 
230
                                        dir = new File((char)('C'+cpt++)+":\\Program Files\\");//$NON-NLS-1$
 
231
                                
 
232
                                pathInstall.setText(dir.exists() ? dir.getPath() : "C:\\");//$NON-NLS-1$
 
233
                        }
 
234
        }
 
235
        
 
236
        
 
237
        /**
 
238
         * Creates the third slide.
 
239
         * @since 1.9.2
 
240
         */
 
241
        protected final void createPanelInstall()
 
242
        {
 
243
                JPanel p1 = new JPanel();
 
244
                JPanel p2 = new JPanel();
 
245
                panel3 = new JPanel();
 
246
                
 
247
                panel3.setLayout(new BoxLayout(panel3, BoxLayout.PAGE_AXIS));
 
248
                p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
 
249
                
 
250
                progressBar = new JProgressBar(0, 100);
 
251
                progressBar.setMinimumSize(new Dimension(300, 30));
 
252
                progressBar.setPreferredSize(new Dimension(300, 30));
 
253
                labelProgress = new JLabel();
 
254
                p2.add(progressBar);
 
255
                p2.add(labelProgress);
 
256
                
 
257
                
 
258
                p2.setMaximumSize(new Dimension(350, 150));
 
259
                p2.setMinimumSize(new Dimension(350, 150));
 
260
                p2.setPreferredSize(new Dimension(350, 150));
 
261
                p1.setMaximumSize(new Dimension(350, 300));
 
262
                p1.setMinimumSize(new Dimension(350, 300));
 
263
                p1.setPreferredSize(new Dimension(350, 300));
 
264
                p1.add(p2);
 
265
                panel3.add(p1);
 
266
                
 
267
                nextB.setEnabled(false);
 
268
                previousB.setEnabled(false);
 
269
                
 
270
                setPanelDimension(panel3);
 
271
        }
 
272
        
 
273
        
 
274
        
 
275
        /**
 
276
         * Set the default dimension of a slide to the given panel.
 
277
         * @param p The given panel.
 
278
         * @since 1.9.2
 
279
         */
 
280
        protected void setPanelDimension(JPanel p)
 
281
        {
 
282
                if(p==null)
 
283
                        throw new IllegalArgumentException();
 
284
                
 
285
                p.setPreferredSize(PANEL_SIZE);
 
286
                p.setMinimumSize(PANEL_SIZE);
 
287
                p.setMaximumSize(PANEL_SIZE);
 
288
        }
 
289
        
 
290
        
 
291
        
 
292
        /**
 
293
         * @return The panel containing the control buttons (next, previous, and so on).
 
294
         * @since 1.9.2
 
295
         */
 
296
        protected final JPanel createControlButtonsPanel()
 
297
        {
 
298
                JPanel ctrlPanel = new JPanel();
 
299
                
 
300
                JButton b = new JButton("Cancel");
 
301
                b.setActionCommand(ACTION_CANCEL);
 
302
                b.setName(ACTION_CANCEL);
 
303
                b.addActionListener(listener);
 
304
                
 
305
                ctrlPanel.add(b);
 
306
                
 
307
                previousB = new JButton("Previous");
 
308
                previousB.setActionCommand(ACTION_PREVIOUS);
 
309
                previousB.setName(ACTION_PREVIOUS);
 
310
                previousB.addActionListener(listener);
 
311
                
 
312
                ctrlPanel.add(previousB);
 
313
                
 
314
                nextB = new JButton(LABEL_NEXT);
 
315
                nextB.setActionCommand(ACTION_NEXT);
 
316
                nextB.setName(ACTION_NEXT);
 
317
                nextB.addActionListener(listener);
 
318
                
 
319
                ctrlPanel.add(nextB);
 
320
                
 
321
                ctrlPanel.setBorder(new CompoundBorder(new TitledBorder(null, null, TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(0,0,0,0)));
 
322
                
 
323
                return ctrlPanel;
 
324
        }
 
325
        
 
326
        
 
327
        
 
328
        /**
 
329
         * @return The top panel (the banner in fact).
 
330
         * @since 1.9.2
 
331
         */
 
332
        protected final JPanel createHeaderPanel()
 
333
        {
 
334
                JPanel header = new JPanel();
 
335
                
 
336
                header.add(new JLabel("<html><div align=\"center\"><font size=\"+1\" color=\"white\">"+"Assistant of installation"+"</font></div></html>"));//$NON-NLS-1$//$NON-NLS-3$
 
337
                header.setBackground(new Color(90, 100, 130));
 
338
                header.setBorder(new CompoundBorder(new TitledBorder(null, null, TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(0,0,0,0)));
 
339
                
 
340
                return header;
 
341
        }
 
342
        
 
343
        
 
344
        
 
345
        /**
 
346
         * Launch the installer.
 
347
         * @since 1.9.2
 
348
         */
 
349
        public void launch()
 
350
        {
 
351
                setVisible(true);
 
352
        }
 
353
 
 
354
 
 
355
 
 
356
        /**
 
357
         * Allows to go to the next slide. 
 
358
         * @since 1.9.2
 
359
         */
 
360
        public synchronized void incrementSlidePosition()
 
361
        {
 
362
                if(slidePosition<MAX_PANEL)
 
363
                {
 
364
                        slidePosition++;
 
365
                        
 
366
                        switch(slidePosition)
 
367
                        {
 
368
                                case 2:
 
369
                                        if(panel2==null)
 
370
                                                createPanelChoosePath();
 
371
                                        
 
372
                                        File f = new File(pathInstall.getText());
 
373
                                        getContentPane().remove(panel1);
 
374
                                        getContentPane().add(panel2, BorderLayout.CENTER);
 
375
                                        previousB.setEnabled(true);
 
376
                                        nextB.setEnabled(f.exists() && f.isDirectory() && f.canRead());
 
377
                                        repaint();
 
378
                                        pack();
 
379
                                        break;
 
380
                                        
 
381
                                case 3:
 
382
                                        if(panel3==null)
 
383
                                                createPanelInstall();
 
384
                                        
 
385
                                        getContentPane().remove(panel2);
 
386
                                        getContentPane().add(panel3, BorderLayout.CENTER);
 
387
                                        labelProgress.setText("");//$NON-NLS-1$
 
388
                                        nextB.setText(LABEL_TERMINATE);
 
389
                                        nextB.setActionCommand(ACTION_TERMINATE);
 
390
                                        nextB.setName(ACTION_TERMINATE);
 
391
                                        repaint();
 
392
                                        pack();
 
393
                                        
 
394
                                        installThread = new Thread(this);
 
395
                                        installThread.start();
 
396
                                        
 
397
                                        break;
 
398
                        }
 
399
                }
 
400
        }
 
401
        
 
402
        
 
403
        
 
404
        /**
 
405
         * Allows to go to the previous slide.
 
406
         * @since 1.9.2
 
407
         */
 
408
        public synchronized void decrementSlidePosition()
 
409
        {
 
410
                if(slidePosition>1)
 
411
                {
 
412
                        slidePosition--;
 
413
                        
 
414
                        switch(slidePosition)
 
415
                        {
 
416
                                case 1:
 
417
                                        getContentPane().remove(panel2);
 
418
                                        getContentPane().add(panel1, BorderLayout.CENTER);
 
419
                                        previousB.setEnabled(false);
 
420
                                        repaint();
 
421
                                        pack();
 
422
                                        break;
 
423
                                        
 
424
                                case 2:
 
425
                                        File f = new File(pathInstall.getText());
 
426
                                        getContentPane().remove(panel3);
 
427
                                        getContentPane().add(panel2, BorderLayout.CENTER);
 
428
                                        nextB.setText(LABEL_NEXT);
 
429
                                        nextB.setActionCommand(ACTION_NEXT);
 
430
                                        nextB.setName(ACTION_NEXT);
 
431
                                        nextB.setEnabled(f.exists() && f.isDirectory() && f.canRead());
 
432
                                        repaint();
 
433
                                        pack();
 
434
                                        break;
 
435
                        }
 
436
                }
 
437
        }
 
438
 
 
439
 
 
440
 
 
441
        /**
 
442
         * @param path the pathInstall to set.
 
443
         * @since 0.1
 
444
         */
 
445
        public synchronized void setPathInstall(String path)
 
446
        {
 
447
                pathInstall.setText(path);
 
448
                File f = new File(pathInstall.getText());
 
449
                nextB.setEnabled(f.exists() && f.isDirectory() && f.canRead());
 
450
        }
 
451
 
 
452
 
 
453
        
 
454
        public void run()
 
455
        {
 
456
                setContinueInstall(true);
 
457
                progressBar.setValue(progressBar.getMinimum());
 
458
 
 
459
                LaTeXDrawPath.checkInstallDirectories();
 
460
                LaTeXDrawPath.checkDirectories();
 
461
                progressBar.setValue(progressBar.getValue()+20);
 
462
                boolean okInstall = false;
 
463
                boolean okCopying = true;
 
464
                
 
465
                if(!isContinueInstall())
 
466
                        return ;
 
467
 
 
468
                try
 
469
                {
 
470
                        okCopying = okCopying && copyFiles(pathJar+File.separator+DATA_DIR+File.separator+NAME_TEMPLATES_DIR, LaTeXDrawPath.PATH_TEMPLATES_SHARED, false);
 
471
                        okCopying = okCopying && copyFiles(pathJar+File.separator+DATA_DIR+File.separator+NAME_CACHE_TEMPLATES_DIR, LaTeXDrawPath.PATH_CACHE_SHARE_DIR, false);
 
472
                        
 
473
                        progressBar.setValue(progressBar.getValue()+20);
 
474
                        
 
475
                        if(!isContinueInstall())
 
476
                                return ;
 
477
                        
 
478
                        if(!isContinueInstall())
 
479
                                return ;
 
480
                        
 
481
                        String path = pathInstall.getText() + File.separator + "latexdraw";//$NON-NLS-1$
 
482
                        
 
483
                        File mainDir = new File(path);
 
484
                        
 
485
                        if(mainDir.exists())
 
486
                                okCopying = delete(mainDir);
 
487
                
 
488
                        okCopying = mainDir.mkdir();
 
489
                        
 
490
                        okCopying = okCopying && mainDir.exists();
 
491
                        okCopying = okCopying && copyFile(pathJar+File.separator+DATA_DIR+File.separator+"LaTeXDraw.jar", path, true);//$NON-NLS-1$
 
492
                        okCopying = okCopying && copyFile(pathJar+File.separator+"license.txt", path, true);//$NON-NLS-1$
 
493
                        okCopying = okCopying && copyFile(pathJar+File.separator+"release_note.txt", path, true);//$NON-NLS-1$
 
494
                        okCopying = okCopying && copyFiles(pathJar+File.separator+DATA_DIR+File.separator+"lib", path+File.separator+"lib", true);//$NON-NLS-1$//$NON-NLS-2$
 
495
                        okCopying = okCopying && copyFiles(pathJar+File.separator+DATA_DIR+File.separator+"help", path+File.separator+"help", true);//$NON-NLS-1$//$NON-NLS-2$
 
496
                        progressBar.setValue(progressBar.getValue()+30);
 
497
                        
 
498
                        if(!isContinueInstall())
 
499
                                return ;
 
500
                        
 
501
                        createProgramLauncher();
 
502
                        progressBar.setValue(progressBar.getValue()+20);
 
503
                        
 
504
                        if(!isContinueInstall())
 
505
                                return ;
 
506
                
 
507
                        okInstall = okCopying;
 
508
                        
 
509
                }catch(Exception e) { okInstall = false; }
 
510
                
 
511
                if(okInstall)
 
512
                {
 
513
                        progressBar.setValue(progressBar.getMaximum());
 
514
                        nextB.setEnabled(true);
 
515
                        previousB.setEnabled(true);
 
516
                        labelProgress.setText("<html><font color=\"green\">" + "Installation succeeded!" + "<br></font></html>");//$NON-NLS-1$//$NON-NLS-3$
 
517
                }
 
518
                else
 
519
                {
 
520
                        getLabelProgress().setText("<html><font color=\"red\">" + "Installation failed!" + "<br>" + //$NON-NLS-1$//$NON-NLS-3$
 
521
                                                                        "Launch the installer as administrator." + "</font></html>");//$NON-NLS-2$
 
522
                        previousB.setEnabled(true);
 
523
                }
 
524
                
 
525
                setContinueInstall(false);
 
526
        }
 
527
 
 
528
        
 
529
        
 
530
        /**
 
531
         * Creates the program launcher for the OS.
 
532
         * @return true if the creation succeeded.
 
533
         * @since 1.9.2
 
534
         */
 
535
        private final boolean createProgramLauncher()
 
536
        {
 
537
                try
 
538
                {
 
539
                        if(os.toLowerCase().equals("linux"))//$NON-NLS-1$
 
540
                        {
 
541
                                        File f = new File("/usr/bin/latexdraw");//$NON-NLS-1$
 
542
                                        
 
543
                                        if(!f.exists())
 
544
                                        {
 
545
                                                FileOutputStream fos = new FileOutputStream(f);
 
546
                                                Writer osw = new OutputStreamWriter(fos);
 
547
                                                
 
548
                                                osw.write("#! /bin/sh\n");//$NON-NLS-1$
 
549
                                                osw.write("java -jar "+getPathInstall().getText()+File.separator+"latexdraw"+File.separator+"LaTeXDraw.jar $@\n");//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
 
550
                                                osw.close();
 
551
                                                fos.close();
 
552
                                                
 
553
                                                //JAVA6 Use f.setReadable() and f.setExecutable()
 
554
                                                Runtime.getRuntime().exec("chmod 755 /usr/bin/latexdraw");//$NON-NLS-1$
 
555
                                        }
 
556
                        }
 
557
                
 
558
                        return true;
 
559
                }catch(Exception e) { return false; }
 
560
        }
 
561
 
 
562
 
 
563
 
 
564
        /**
 
565
         * Copy a file.
 
566
         * @param from The current path of the file to move.
 
567
         * @param here The directory where we have to move the file.
 
568
         * @param replace If set to true, the file will replace any file with the same name in "here".
 
569
         * @return True if the move succeeded
 
570
         * @throws FileNotFoundException If "from" is invalid.
 
571
         * @throws NullPointerException If you are not lucky.
 
572
         * @since 1.9.2
 
573
         */
 
574
        private final boolean copyFile(String from, String here, boolean replace) throws FileNotFoundException, NullPointerException
 
575
        {
 
576
                File source = new File(from);
 
577
                
 
578
                if(!source.exists() || !source.canRead())
 
579
                        throw new FileNotFoundException();
 
580
                
 
581
                File dest = new File(here + File.separator + source.getName());
 
582
                
 
583
                if(dest.exists() && replace)
 
584
                        delete(dest);
 
585
                        
 
586
                if(!dest.exists())
 
587
                        if(source.isDirectory())
 
588
                                copyFiles(from, here, replace);
 
589
                        else
 
590
                                return copy(source, dest);
 
591
                
 
592
                return true;
 
593
        }
 
594
        
 
595
        
 
596
        
 
597
        /**
 
598
         * Copy a file into another.
 
599
         * @param source The source file.
 
600
         * @param destination The output file.
 
601
         * @return True if no problem occurred.
 
602
         * @exception IllegalArgumentException If source or destination is null.
 
603
         */
 
604
        private final static boolean copy(File source, File destination)
 
605
        {
 
606
                if(destination==null || source==null)
 
607
                        throw new IllegalArgumentException();
 
608
                
 
609
                if(!source.canRead())
 
610
                        return false;
 
611
                        
 
612
        FileInputStream sourceFile;
 
613
        FileOutputStream destinationFile;
 
614
                
 
615
            try 
 
616
            {
 
617
                destination.createNewFile();
 
618
            sourceFile = new java.io.FileInputStream(source);
 
619
            destinationFile = new java.io.FileOutputStream(destination);
 
620
            byte buffer[]=new byte[512*1024];
 
621
            int nbLecture;
 
622
                        
 
623
            while( (nbLecture = sourceFile.read(buffer)) != -1 )
 
624
                destinationFile.write(buffer, 0, nbLecture);
 
625
                        
 
626
            sourceFile.close();
 
627
            destinationFile.close();
 
628
            
 
629
            return true;
 
630
        } 
 
631
            catch(Exception e) { return false; }
 
632
        } 
 
633
        
 
634
        
 
635
        /**
 
636
         * Move a couple of files but never replace an existing file.
 
637
         * @param from The directory where we have to find the files to move.
 
638
         * @param into The destination directory.
 
639
         * @param replace True : the file/directory will be replaced.
 
640
         * @throws FileNotFoundException If a problem occurred with a source file.
 
641
         * @throws NullPointerException If you are not lucky.
 
642
         * @since 1.9.2
 
643
         */
 
644
        private final boolean copyFiles(String from, String into, boolean replace) throws FileNotFoundException, NullPointerException
 
645
        {
 
646
                File src = new File(from);
 
647
                File here = new File(into);
 
648
                boolean ok = true;
 
649
                
 
650
                if(!src.exists() || !src.isDirectory() || !src.canRead())
 
651
                        throw new FileNotFoundException();
 
652
 
 
653
                File[] files = src.listFiles();
 
654
                File dest;
 
655
                here.mkdir();
 
656
                
 
657
                for(File file : files)
 
658
                {
 
659
                        dest = new File(into + File.separator + file.getName());
 
660
                        
 
661
                        if(replace)
 
662
                                delete(dest);
 
663
                        
 
664
                        if(!dest.exists())
 
665
                                if(file.isFile())
 
666
                                        ok = copy(file, dest) && ok;
 
667
                                else
 
668
                                {
 
669
                                        String path = file.getPath();
 
670
                                        path = path.substring(path.lastIndexOf(File.separator));
 
671
                                        ok = copyFiles(file.getPath(), into+path, replace) && ok;
 
672
                                }
 
673
                }
 
674
                
 
675
                return ok;
 
676
        }
 
677
        
 
678
        
 
679
        
 
680
        /**
 
681
         * Add progression to the progress bar.
 
682
         * @param add The value to add.
 
683
         * @since 1.9.2
 
684
         */
 
685
        public synchronized void addProgressBar(int add) 
 
686
        {
 
687
                progressBar.setValue(progressBar.getValue()+add);
 
688
        }
 
689
        
 
690
 
 
691
        /**
 
692
         * @return the continueInstall.
 
693
         * @since 0.1
 
694
         */
 
695
        public synchronized boolean isContinueInstall()
 
696
        {
 
697
                return continueInstall;
 
698
        }
 
699
 
 
700
 
 
701
 
 
702
        /**
 
703
         * @param continueInstall the continueInstall to set.
 
704
         * @since 0.1
 
705
         */
 
706
        public synchronized void setContinueInstall(boolean continueInstall)
 
707
        {
 
708
                this.continueInstall = continueInstall;
 
709
        }
 
710
        
 
711
        
 
712
        
 
713
        /**
 
714
         * remove a file or directory even if it is not empty.
 
715
         * @param f The file to remove
 
716
         * @return True if the file/directory was removed.
 
717
         * @since 1.9.2
 
718
         */
 
719
        private final static boolean delete(File f)
 
720
        {
 
721
          boolean status = true;
 
722
 
 
723
          try 
 
724
          {
 
725
                  File fileCur;
 
726
                  String[] lstFile;
 
727
                  int idxFile;
 
728
        
 
729
                  lstFile = f.list();
 
730
        
 
731
                  if(lstFile==null)
 
732
                  {
 
733
                          f.delete();
 
734
                          return true;
 
735
                  }
 
736
        
 
737
                  for(idxFile = 0; idxFile < lstFile.length && status; idxFile++) 
 
738
                  {
 
739
                          fileCur = new File(f, lstFile[idxFile]);
 
740
                       
 
741
                          if(fileCur.isDirectory()) 
 
742
                         status = delete(fileCur);
 
743
                          else 
 
744
                         status = fileCur.delete();
 
745
                  }
 
746
                  
 
747
                  f.delete();
 
748
          }
 
749
          catch (SecurityException se)  { return false; }
 
750
          
 
751
          return(status);
 
752
        }
 
753
 
 
754
 
 
755
        /**
 
756
         * @return the labelProgress.
 
757
         * @since 2.0.0
 
758
         */
 
759
        public final synchronized JLabel getLabelProgress()
 
760
        {
 
761
                return labelProgress;
 
762
        }
 
763
 
 
764
 
 
765
        /**
 
766
         * @return the pathInstall.
 
767
         * @since 2.0.0
 
768
         */
 
769
        public final synchronized JTextField getPathInstall()
 
770
        {
 
771
                return pathInstall;
 
772
        }
 
773
}