| 6 | 1 import java.util.*; | 
|  | 2 import java.awt.*; | 
|  | 3 import java.awt.event.ActionEvent; | 
|  | 4 import java.awt.event.ActionListener; | 
|  | 5 import java.io.*; | 
|  | 6 import javax.swing.*; | 
|  | 7 import javax.swing.filechooser.*; | 
|  | 8 import javax.swing.border.*; | 
|  | 9 import javax.swing.SwingUtilities; | 
|  | 10 import java.net.*; | 
|  | 11 | 
|  | 12 public class SmartInstaller extends JPanel implements ActionListener { | 
|  | 13   int       BUFFER = 1024; | 
|  | 14 | 
|  | 15   JFrame    mainFrame; | 
|  | 16   JTextArea logArea; | 
|  | 17 | 
|  | 18   // configuration chooser buttons | 
|  | 19   String       configurations[] = {"32 bits", "64 bits"}; | 
|  | 20   JRadioButton configurationButtons[]; | 
|  | 21 | 
|  | 22   // program chooser buttons | 
|  | 23   String    programChoosers[] = {"R", "R Color Brewer Package", "R HMisc Package", "MySQL", "MySQL account", "Python 2.6", "Python DB", "S-MART"}; | 
|  | 24   JCheckBox programChooserButtons[]; | 
|  | 25 | 
|  | 26   JButton   goButton; | 
|  | 27 | 
|  | 28   // install directory | 
|  | 29   JButton    installDirectoryChooserButton; | 
|  | 30   JTextField installDirectoryChooserTextField; | 
|  | 31 | 
|  | 32 | 
|  | 33   public SmartInstaller() { | 
|  | 34     super(); | 
|  | 35 | 
|  | 36     Box box = Box.createVerticalBox(); | 
|  | 37 | 
|  | 38     // Header | 
|  | 39     JPanel       headerPanel = new JPanel(false); | 
|  | 40     JTextArea    headerArea  = new JTextArea("This is the S-MART installation tool.\r\nIt will download and install the needed softwares, as well as S-MART itself.\r\nYou can unselect the software that you already have installed.\r\nDuring the installation, accept all the default parameters.\r\nPlease remember the root password if you install MySQL!"); | 
|  | 41     TitledBorder headerBorder = BorderFactory.createTitledBorder("Wellcome to the S-MART installer!"); | 
|  | 42     headerArea.setEditable(false); | 
|  | 43     headerArea.setBackground(headerPanel.getBackground()); | 
|  | 44     headerPanel.add(headerArea); | 
|  | 45     headerPanel.setBorder(headerBorder); | 
|  | 46 | 
|  | 47 | 
|  | 48     // Configuration | 
|  | 49     JPanel configurationPanel = new JPanel(false); | 
|  | 50     configurationPanel.setLayout(new GridLayout(1, 0)); | 
|  | 51     configurationButtons = new JRadioButton[configurations.length]; | 
|  | 52     ButtonGroup configurationGroup = new ButtonGroup(); | 
|  | 53     for (int i = 0; i < configurations.length; i++) { | 
|  | 54       JRadioButton button = new JRadioButton(configurations[i]); | 
|  | 55       configurationPanel.add(button); | 
|  | 56       configurationButtons[i] = button; | 
|  | 57       configurationGroup.add(button); | 
|  | 58     } | 
|  | 59     configurationButtons[0].setSelected(true); | 
|  | 60     TitledBorder configurationBorder = BorderFactory.createTitledBorder("Configuration"); | 
|  | 61     configurationPanel.setBorder(configurationBorder); | 
|  | 62 | 
|  | 63 | 
|  | 64     // Program chooser panel | 
|  | 65     JPanel programPanel = new JPanel(false); | 
|  | 66     programPanel.setLayout(new GridLayout(0, 1)); | 
|  | 67 | 
|  | 68     JLabel programLabel = new JLabel("Choose which programs to install:"); | 
|  | 69     programPanel.add(programLabel); | 
|  | 70     programChooserButtons = new JCheckBox[programChoosers.length]; | 
|  | 71     for (int i = 0; i < programChoosers.length; i++) { | 
|  | 72       JCheckBox button = new JCheckBox(programChoosers[i]); | 
|  | 73       button.setSelected(true); | 
|  | 74       programPanel.add(button); | 
|  | 75       programChooserButtons[i] = button; | 
|  | 76     } | 
|  | 77     TitledBorder programBorder = BorderFactory.createTitledBorder("Programs"); | 
|  | 78     programPanel.setBorder(programBorder); | 
|  | 79 | 
|  | 80     // Install directory chooser | 
|  | 81     JPanel installDirectoryChooserPanel = new JPanel(false); | 
|  | 82     installDirectoryChooserPanel.setLayout(new GridLayout(1, 0)); | 
|  | 83     JLabel installDirectoryChooserLabel = new JLabel("Choose a directory to install S-MART: "); | 
|  | 84     installDirectoryChooserTextField = new JTextField(); | 
|  | 85     installDirectoryChooserButton = new JButton("Open..."); | 
|  | 86     installDirectoryChooserButton.addActionListener(this); | 
|  | 87 | 
|  | 88     installDirectoryChooserPanel.add(installDirectoryChooserLabel); | 
|  | 89     installDirectoryChooserPanel.add(installDirectoryChooserTextField); | 
|  | 90     installDirectoryChooserPanel.add(installDirectoryChooserButton); | 
|  | 91     TitledBorder installDirectoryChooserBorder = BorderFactory.createTitledBorder("Installation directory"); | 
|  | 92     installDirectoryChooserPanel.setBorder(installDirectoryChooserBorder); | 
|  | 93 | 
|  | 94     // GO! | 
|  | 95     JPanel goPanel = new JPanel(false); | 
|  | 96     goButton = new JButton("GO!"); | 
|  | 97     goButton.addActionListener(this); | 
|  | 98     goButton.setSelected(true); | 
|  | 99     goPanel.add(goButton); | 
|  | 100     TitledBorder goBorder = BorderFactory.createTitledBorder("Start install"); | 
|  | 101     goPanel.setBorder(goBorder); | 
|  | 102 | 
|  | 103     // Log | 
|  | 104     logArea = new JTextArea(10, 120); | 
|  | 105     logArea.setFont(new Font("Monospaced", logArea.getFont().getStyle(), logArea.getFont().getSize())); | 
|  | 106     JScrollPane logScroll  = new JScrollPane(logArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | 
|  | 107     TitledBorder logBorder = BorderFactory.createTitledBorder("Log"); | 
|  | 108     logScroll.setBorder(logBorder); | 
|  | 109 | 
|  | 110     GridLayout horizontalLayout = new GridLayout(1, 0); | 
|  | 111 | 
|  | 112     box.add(headerPanel); | 
|  | 113     box.add(configurationPanel); | 
|  | 114     box.add(programPanel); | 
|  | 115     box.add(installDirectoryChooserPanel); | 
|  | 116     box.add(goPanel); | 
|  | 117     box.add(logScroll); | 
|  | 118 | 
|  | 119     add(box); | 
|  | 120   } | 
|  | 121 | 
|  | 122 | 
|  | 123   public void actionPerformed(ActionEvent e) { | 
|  | 124 | 
|  | 125     // Install directories chooser | 
|  | 126     if (e.getSource() == goButton) { | 
|  | 127       boolean[] selectedPrograms = new boolean[programChoosers.length]; | 
|  | 128       for (int i = 0; i < programChoosers.length; i++) { | 
|  | 129         selectedPrograms[i] = programChooserButtons[i].isSelected(); | 
|  | 130       } | 
|  | 131       SmartInstallerTask task = new SmartInstallerTask(logArea, selectedPrograms, installDirectoryChooserTextField.getText(), (configurationButtons[0].isSelected())? 0: 1); | 
|  | 132       task.execute(); | 
|  | 133     } | 
|  | 134     // Install directories chooser | 
|  | 135     else if (e.getSource() == installDirectoryChooserButton) { | 
|  | 136       JFileChooser chooser = new JFileChooser(); | 
|  | 137       chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | 
|  | 138       if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | 
|  | 139         installDirectoryChooserTextField.setText(chooser.getSelectedFile().getPath()); | 
|  | 140       } | 
|  | 141     } | 
|  | 142   } | 
|  | 143 | 
|  | 144   private static void createAndShowGUI() { | 
|  | 145     // Create and set up the window. | 
|  | 146     JFrame mainFrame = new JFrame("S-Mart Installer"); | 
|  | 147     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 
|  | 148 | 
|  | 149     //Create and set up the content pane. | 
|  | 150     JComponent newContentPane = new SmartInstaller(); | 
|  | 151     newContentPane.setOpaque(true); | 
|  | 152     mainFrame.setContentPane(newContentPane); | 
|  | 153 | 
|  | 154     // Display the window. | 
|  | 155     mainFrame.pack(); | 
|  | 156     mainFrame.setVisible(true); | 
|  | 157   } | 
|  | 158 | 
|  | 159 | 
|  | 160   public static void main(String[] args) { | 
|  | 161     javax.swing.SwingUtilities.invokeLater(new Runnable() { | 
|  | 162       public void run() { | 
|  | 163         createAndShowGUI(); | 
|  | 164       } | 
|  | 165     }); | 
|  | 166   } | 
|  | 167 } |