1
package latexDraw.installer;
3
import java.awt.BorderLayout;
5
import java.awt.Dimension;
6
import java.awt.Toolkit;
10
import javax.swing.border.CompoundBorder;
11
import javax.swing.border.EmptyBorder;
12
import javax.swing.border.TitledBorder;
14
import latexDraw.ui.listeners.InstallerListener;
15
import latexDraw.util.LaTeXDrawPath;
18
* This class defines the LaTeXDraw installer.<br>
20
* This file is part of LaTeXDraw<br>
21
* Copyright (c) 2005-2008 Arnaud BLOUIN<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>
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>
33
* @author Arnaud BLOUIN<br>
36
public final class InstallerUI extends JFrame implements Runnable
38
private static final long serialVersionUID = 1L;
40
/** The name of the os. */
43
/** The listener of the installer. */
44
protected transient InstallerListener listener;
46
/** The current position of the slide of the installer. */
47
protected int slidePosition;
49
/** The first slide. */
50
protected JPanel panel1;
52
/** The second slide. */
53
protected JPanel panel2;
55
/** The third slide. */
56
protected JPanel panel3;
58
/** The button "previous". */
59
protected JButton previousB;
61
/** The button "next". */
62
protected JButton nextB;
64
/** The field that defines the path where we have to install LaTeXDraw. */
65
protected JTextField pathInstall;
67
/** The progress bar of the installation. */
68
protected JProgressBar progressBar;
70
/** Used to stop the thread of the installation. */
71
protected boolean continueInstall;
73
/** The thread that installs LaTeXDraw. */
74
protected transient Thread installThread;
76
/** The label that can appear during the installation. */
77
protected JLabel labelProgress;
79
/** The path where the jar file is placed. */
80
protected String pathJar;
82
/** The maximum number of slides. */
83
public static final int MAX_PANEL = 3;
85
public static final String ACTION_NEXT = "next";//$NON-NLS-1$
87
public static final String ACTION_PREVIOUS = "previous";//$NON-NLS-1$
89
public static final String ACTION_CANCEL = "cancel";//$NON-NLS-1$
91
public static final String ACTION_TERMINATE = "terminate";//$NON-NLS-1$
93
public static final String LABEL_NEXT = "Next";
95
public static final String LABEL_TERMINATE = "Terminate";
97
public static final String ACTION_CHOOSE_FOLDER = "choose";//$NON-NLS-1$
99
/** The dimension of a slide. */
100
public static final Dimension PANEL_SIZE = new Dimension(650, 350);
102
public static final String NAME_TEMPLATES_DIR = "templates";//$NON-NLS-1$
104
public static final String NAME_CACHE_TEMPLATES_DIR = ".cache";//$NON-NLS-1$
106
public static final String DATA_DIR = "data";//$NON-NLS-1$
110
* The main function: it creates an installer and launch it.
111
* @param argc Useless.
114
public static void main(String argc[])
116
InstallerUI installer = new InstallerUI();
123
* The default constructor.
127
super("LaTeXDraw installer");
129
pathJar = LaTeXDrawPath.getPathJar();
130
os = System.getProperty("os.name");//$NON-NLS-1$
131
listener = new InstallerListener(this);
134
if(os.toLowerCase().equals("linux") && System.getProperty("java.version").toLowerCase().compareTo("1.6")>=0)//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
136
{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); }//$NON-NLS-1$
137
catch(Exception e) { /* Dommage ;) */ }
139
if(os.toLowerCase().contains("win")) //$NON-NLS-1$
141
{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }//$NON-NLS-1$
142
catch(Exception e) { /* Dommage ;) */ }
144
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("res/LaTeXDrawIcon.png")));//$NON-NLS-1$
145
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
148
getContentPane().setLayout(new BorderLayout());
149
getContentPane().add(createHeaderPanel(), BorderLayout.NORTH);
150
getContentPane().add(panel1, BorderLayout.CENTER);
151
getContentPane().add(createControlButtonsPanel(), BorderLayout.SOUTH);
153
previousB.setEnabled(false);
154
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
156
setLocation(dim.width/2-getWidth()/2, dim.height/2-getHeight()/2);
162
* Creates the first slide.
165
protected final void createStartPanel()
167
panel1 = new JPanel();
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.";
173
plus = "";//$NON-NLS-1$
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$
178
setPanelDimension(panel1);
184
* Creates the second slide.
187
protected final void createPanelChoosePath()
189
JPanel p1 = new JPanel();
190
JPanel p2 = new JPanel();
192
panel2 = new JPanel();
194
panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
195
p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
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);
207
p2.add(new JLabel("Choose the path where the directory LaTeXDraw will be installed:"));
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));
219
setPanelDimension(panel2);
221
if(os.equals("Linux"))//$NON-NLS-1$
222
pathInstall.setText("/opt");//$NON-NLS-1$
224
if(os.toLowerCase().contains("windows"))//$NON-NLS-1$
226
File dir = new File("C:\\Program Files\\");//$NON-NLS-1$
227
int cpt = 0, max = 10;
229
while(!dir.exists() && cpt<max)
230
dir = new File((char)('C'+cpt++)+":\\Program Files\\");//$NON-NLS-1$
232
pathInstall.setText(dir.exists() ? dir.getPath() : "C:\\");//$NON-NLS-1$
238
* Creates the third slide.
241
protected final void createPanelInstall()
243
JPanel p1 = new JPanel();
244
JPanel p2 = new JPanel();
245
panel3 = new JPanel();
247
panel3.setLayout(new BoxLayout(panel3, BoxLayout.PAGE_AXIS));
248
p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
250
progressBar = new JProgressBar(0, 100);
251
progressBar.setMinimumSize(new Dimension(300, 30));
252
progressBar.setPreferredSize(new Dimension(300, 30));
253
labelProgress = new JLabel();
255
p2.add(labelProgress);
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));
267
nextB.setEnabled(false);
268
previousB.setEnabled(false);
270
setPanelDimension(panel3);
276
* Set the default dimension of a slide to the given panel.
277
* @param p The given panel.
280
protected void setPanelDimension(JPanel p)
283
throw new IllegalArgumentException();
285
p.setPreferredSize(PANEL_SIZE);
286
p.setMinimumSize(PANEL_SIZE);
287
p.setMaximumSize(PANEL_SIZE);
293
* @return The panel containing the control buttons (next, previous, and so on).
296
protected final JPanel createControlButtonsPanel()
298
JPanel ctrlPanel = new JPanel();
300
JButton b = new JButton("Cancel");
301
b.setActionCommand(ACTION_CANCEL);
302
b.setName(ACTION_CANCEL);
303
b.addActionListener(listener);
307
previousB = new JButton("Previous");
308
previousB.setActionCommand(ACTION_PREVIOUS);
309
previousB.setName(ACTION_PREVIOUS);
310
previousB.addActionListener(listener);
312
ctrlPanel.add(previousB);
314
nextB = new JButton(LABEL_NEXT);
315
nextB.setActionCommand(ACTION_NEXT);
316
nextB.setName(ACTION_NEXT);
317
nextB.addActionListener(listener);
319
ctrlPanel.add(nextB);
321
ctrlPanel.setBorder(new CompoundBorder(new TitledBorder(null, null, TitledBorder.LEFT, TitledBorder.TOP), new EmptyBorder(0,0,0,0)));
329
* @return The top panel (the banner in fact).
332
protected final JPanel createHeaderPanel()
334
JPanel header = new JPanel();
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)));
346
* Launch the installer.
357
* Allows to go to the next slide.
360
public synchronized void incrementSlidePosition()
362
if(slidePosition<MAX_PANEL)
366
switch(slidePosition)
370
createPanelChoosePath();
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());
383
createPanelInstall();
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);
394
installThread = new Thread(this);
395
installThread.start();
405
* Allows to go to the previous slide.
408
public synchronized void decrementSlidePosition()
414
switch(slidePosition)
417
getContentPane().remove(panel2);
418
getContentPane().add(panel1, BorderLayout.CENTER);
419
previousB.setEnabled(false);
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());
442
* @param path the pathInstall to set.
445
public synchronized void setPathInstall(String path)
447
pathInstall.setText(path);
448
File f = new File(pathInstall.getText());
449
nextB.setEnabled(f.exists() && f.isDirectory() && f.canRead());
456
setContinueInstall(true);
457
progressBar.setValue(progressBar.getMinimum());
459
LaTeXDrawPath.checkInstallDirectories();
460
LaTeXDrawPath.checkDirectories();
461
progressBar.setValue(progressBar.getValue()+20);
462
boolean okInstall = false;
463
boolean okCopying = true;
465
if(!isContinueInstall())
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);
473
progressBar.setValue(progressBar.getValue()+20);
475
if(!isContinueInstall())
478
if(!isContinueInstall())
481
String path = pathInstall.getText() + File.separator + "latexdraw";//$NON-NLS-1$
483
File mainDir = new File(path);
486
okCopying = delete(mainDir);
488
okCopying = mainDir.mkdir();
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);
498
if(!isContinueInstall())
501
createProgramLauncher();
502
progressBar.setValue(progressBar.getValue()+20);
504
if(!isContinueInstall())
507
okInstall = okCopying;
509
}catch(Exception e) { okInstall = false; }
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$
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);
525
setContinueInstall(false);
531
* Creates the program launcher for the OS.
532
* @return true if the creation succeeded.
535
private final boolean createProgramLauncher()
539
if(os.toLowerCase().equals("linux"))//$NON-NLS-1$
541
File f = new File("/usr/bin/latexdraw");//$NON-NLS-1$
545
FileOutputStream fos = new FileOutputStream(f);
546
Writer osw = new OutputStreamWriter(fos);
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$
553
//JAVA6 Use f.setReadable() and f.setExecutable()
554
Runtime.getRuntime().exec("chmod 755 /usr/bin/latexdraw");//$NON-NLS-1$
559
}catch(Exception e) { return false; }
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.
574
private final boolean copyFile(String from, String here, boolean replace) throws FileNotFoundException, NullPointerException
576
File source = new File(from);
578
if(!source.exists() || !source.canRead())
579
throw new FileNotFoundException();
581
File dest = new File(here + File.separator + source.getName());
583
if(dest.exists() && replace)
587
if(source.isDirectory())
588
copyFiles(from, here, replace);
590
return copy(source, dest);
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.
604
private final static boolean copy(File source, File destination)
606
if(destination==null || source==null)
607
throw new IllegalArgumentException();
609
if(!source.canRead())
612
FileInputStream sourceFile;
613
FileOutputStream destinationFile;
617
destination.createNewFile();
618
sourceFile = new java.io.FileInputStream(source);
619
destinationFile = new java.io.FileOutputStream(destination);
620
byte buffer[]=new byte[512*1024];
623
while( (nbLecture = sourceFile.read(buffer)) != -1 )
624
destinationFile.write(buffer, 0, nbLecture);
627
destinationFile.close();
631
catch(Exception e) { return false; }
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.
644
private final boolean copyFiles(String from, String into, boolean replace) throws FileNotFoundException, NullPointerException
646
File src = new File(from);
647
File here = new File(into);
650
if(!src.exists() || !src.isDirectory() || !src.canRead())
651
throw new FileNotFoundException();
653
File[] files = src.listFiles();
657
for(File file : files)
659
dest = new File(into + File.separator + file.getName());
666
ok = copy(file, dest) && ok;
669
String path = file.getPath();
670
path = path.substring(path.lastIndexOf(File.separator));
671
ok = copyFiles(file.getPath(), into+path, replace) && ok;
681
* Add progression to the progress bar.
682
* @param add The value to add.
685
public synchronized void addProgressBar(int add)
687
progressBar.setValue(progressBar.getValue()+add);
692
* @return the continueInstall.
695
public synchronized boolean isContinueInstall()
697
return continueInstall;
703
* @param continueInstall the continueInstall to set.
706
public synchronized void setContinueInstall(boolean continueInstall)
708
this.continueInstall = continueInstall;
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.
719
private final static boolean delete(File f)
721
boolean status = true;
737
for(idxFile = 0; idxFile < lstFile.length && status; idxFile++)
739
fileCur = new File(f, lstFile[idxFile]);
741
if(fileCur.isDirectory())
742
status = delete(fileCur);
744
status = fileCur.delete();
749
catch (SecurityException se) { return false; }
756
* @return the labelProgress.
759
public final synchronized JLabel getLabelProgress()
761
return labelProgress;
766
* @return the pathInstall.
769
public final synchronized JTextField getPathInstall()