1 19 20 package com.sslexplorer.upgrade; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.Insets ; 27 import java.awt.Toolkit ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.io.File ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import javax.swing.BorderFactory ; 36 import javax.swing.BoxLayout ; 37 import javax.swing.ImageIcon ; 38 import javax.swing.JButton ; 39 import javax.swing.JCheckBox ; 40 import javax.swing.JComponent ; 41 import javax.swing.JFileChooser ; 42 import javax.swing.JLabel ; 43 import javax.swing.JOptionPane ; 44 import javax.swing.JPanel ; 45 import javax.swing.JProgressBar ; 46 import javax.swing.JScrollPane ; 47 import javax.swing.JSeparator ; 48 import javax.swing.JTextArea ; 49 import javax.swing.JTextField ; 50 import javax.swing.JTextPane ; 51 import javax.swing.event.DocumentEvent ; 52 import javax.swing.event.DocumentListener ; 53 import javax.swing.text.Document ; 54 import javax.swing.text.SimpleAttributeSet ; 55 import javax.swing.text.StyleConstants ; 56 57 import org.apache.commons.logging.Log; 58 import org.apache.commons.logging.LogFactory; 59 60 import sun.util.logging.resources.logging; 61 62 public class GUIUpgrader extends JPanel implements DocumentListener , Upgrader { 63 64 final static Log log = LogFactory.getLog(GUIUpgrader.class); 65 66 private List upgrades; 67 private JPanel mainPanel, upgradeSelectionPanel; 68 private JTextField target, source; 69 private JButton browseSource, browseTarget; 70 private JTextPane console; 71 72 public GUIUpgrader() { 73 super(new BorderLayout ()); 74 JPanel info = new JPanel (new BorderLayout (2, 2)); 75 76 info.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); 80 JLabel l = new JLabel ("<html><p>This utility upgrades configuration from " 81 + "one version 0.1.16 SSL-Explorer installation to another " 82 + "0.2.5+ installation. You may choose which resources you " 83 + "wish to be copied. If resources with the same name already " + "exist they will be left as is."); 84 l.setIcon(new ImageIcon (GUIUpgrader.class.getResource("upgrader-48x48.png"))); 85 info.add(l, BorderLayout.CENTER); 86 info.add(new JSeparator (JSeparator.HORIZONTAL), BorderLayout.SOUTH); 87 mainPanel = new JPanel (new BorderLayout ()); 88 add(info, BorderLayout.NORTH); 89 add(mainPanel, BorderLayout.CENTER); 90 91 JPanel installations = new JPanel (new GridBagLayout ()); 93 installations.setBorder(BorderFactory.createTitledBorder("SSL-Explorer Installations")); 94 GridBagConstraints gbc = new GridBagConstraints (); 95 gbc.anchor = GridBagConstraints.WEST; 96 gbc.fill = GridBagConstraints.HORIZONTAL; 97 gbc.insets = new Insets (2, 2, 2, 2); 98 gbc.weightx = 2.0; 99 UIUtil.jGridBagAdd(installations, new JLabel ("Source"), gbc, GridBagConstraints.REMAINDER); 100 gbc.weightx = 1.0; 101 source = new JTextField (); 102 source.getDocument().addDocumentListener(this); 103 UIUtil.jGridBagAdd(installations, source, gbc, GridBagConstraints.RELATIVE); 104 browseSource = new JButton ("Browse"); 105 browseSource.setMnemonic('b'); 106 browseSource.addActionListener(new ActionListener () { 107 108 public void actionPerformed(ActionEvent e) { 109 JFileChooser chooser = new JFileChooser (source.getText()); 110 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 111 chooser.setDialogTitle("Select source SSL-Explorer installation directory (0.16.1)"); 112 if (chooser.showOpenDialog(GUIUpgrader.this) == JFileChooser.APPROVE_OPTION) { 113 source.setText(chooser.getSelectedFile().getAbsolutePath()); 114 } 115 } 116 }); 117 gbc.weightx = 0.0; 118 UIUtil.jGridBagAdd(installations, browseSource, gbc, GridBagConstraints.REMAINDER); 119 gbc.weightx = 2.0; 120 UIUtil.jGridBagAdd(installations, new JLabel ("Target"), gbc, GridBagConstraints.REMAINDER); 121 gbc.weightx = 1.0; 122 target = new JTextField (System.getProperty("user.dir")); 123 target.getDocument().addDocumentListener(this); 124 UIUtil.jGridBagAdd(installations, target, gbc, GridBagConstraints.RELATIVE); 125 browseTarget = new JButton ("Browse"); 126 browseTarget.setMnemonic('r'); 127 browseTarget.addActionListener(new ActionListener () { 128 public void actionPerformed(ActionEvent e) { 129 JFileChooser chooser = new JFileChooser (target.getText()); 130 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 131 chooser.setDialogTitle("Select target SSL-Explorer installation directory (0.2.5+)"); 132 if (chooser.showOpenDialog(GUIUpgrader.this) == JFileChooser.APPROVE_OPTION) { 133 target.setText(chooser.getSelectedFile().getAbsolutePath()); 134 } 135 } 136 }); 137 gbc.weightx = 0.0; 138 UIUtil.jGridBagAdd(installations, browseTarget, gbc, GridBagConstraints.REMAINDER); 139 mainPanel.add(installations, BorderLayout.NORTH); 140 141 upgradeSelectionPanel = new JPanel (); 143 upgradeSelectionPanel.setBorder(BorderFactory.createTitledBorder("Upgrades")); 144 upgradeSelectionPanel.setLayout(new BoxLayout (upgradeSelectionPanel, BoxLayout.Y_AXIS)); 145 mainPanel.add(upgradeSelectionPanel, BorderLayout.CENTER); 146 147 } 148 149 void removeUpgradeSelectionComponent() { 150 upgradeSelectionPanel.invalidate(); 151 upgradeSelectionPanel.removeAll(); 152 upgradeSelectionPanel.validate(); 153 upgradeSelectionPanel.repaint(); 154 155 } 156 157 void addUpgradeSelectionComponent() { 158 upgradeSelectionPanel.invalidate(); 159 upgradeSelectionPanel.removeAll(); 160 for (Iterator i = upgrades.iterator(); i.hasNext();) { 161 AbstractDatabaseUpgrade upgrade = (AbstractDatabaseUpgrade) i.next(); 162 JCheckBox box = new JCheckBox (upgrade.getName()); 163 box.setSelected(upgrade.isSelectedByDefault()); 164 box.setToolTipText(upgrade.getDescription()); 165 upgradeSelectionPanel.add(box); 166 box.putClientProperty("upgrade", upgrade); 167 } 168 upgradeSelectionPanel.validate(); 169 } 170 171 public void changedUpdate(DocumentEvent e) { 172 checkInstallations(); 173 } 174 175 public void insertUpdate(DocumentEvent e) { 176 checkInstallations(); 177 } 178 179 public void removeUpdate(DocumentEvent e) { 180 checkInstallations(); 181 } 182 183 void checkInstallations() { 184 File oldDir = new File (source.getText()); 185 try { 186 if (!oldDir.exists() || !oldDir.isDirectory()) { 187 throw new Exception (oldDir.getAbsolutePath() + " does not exists or is not a directory"); 188 } 189 File newDir = new File (target.getText()); 190 if (!newDir.exists() || !newDir.isDirectory()) { 191 throw new Exception (newDir.getAbsolutePath() + " does not exists or is not a directory"); 192 } 193 if (oldDir.getCanonicalFile().equals(newDir.getCanonicalFile())) { 194 throw new Exception ("Source and target installation directories are identical"); 195 } 196 File oldDbDir = new File (oldDir, "db"); 197 File newDbDir = new File (newDir, "db"); 198 if (!new File (newDir, "db").exists()) { 199 throw new Exception ("Target installation does not appear to be an SSL-Explorer installation."); 200 } 201 if (!new File (oldDir, "db").exists()) { 202 throw new Exception ("Source installation does not appear to be does not appear to be an SSL-Explorer installation."); 203 } 204 if (!new File (newDir, "install").exists()) { 205 throw new Exception ("Target installation does not appear to be 0.2.5+"); 206 } 207 if (!new File (new File (oldDir, "db"),"explorer_accounts.data").exists()) { 208 throw new Exception ("Source installation does not appear to be 0.1.15+"); 209 } 210 211 upgrades = new ArrayList (); 212 upgrades.add(new UserUpgrade(oldDbDir, newDbDir)); 213 upgrades.add(new AuthSchemeUpgrade(oldDbDir, newDbDir)); 214 upgrades.add(new TunnelsUpgrade(oldDbDir, newDbDir)); 215 upgrades.add(new NetworkPlacesUpgrade(oldDbDir, newDbDir)); 216 upgrades.add(new WebForwardsUpgrade(oldDbDir, newDbDir)); 217 upgrades.add(new IPRestrictionsUpgrade(oldDbDir, newDbDir)); 218 upgrades.add(new ApplicationShortcutsUpgrade(oldDbDir, newDbDir)); 219 upgrades.add(new ReplacementsUpgrade(oldDbDir, newDbDir)); 220 addUpgradeSelectionComponent(); 221 222 } catch (Exception e) { 223 removeUpgradeSelectionComponent(); 224 upgrades = null; 225 upgradeSelectionPanel.add(new JLabel ("<html>" + e.getMessage() + "</html>")); 226 upgradeSelectionPanel.revalidate(); 227 } 228 } 229 230 public void error(String message) { 231 appendString(message, Color.red.darker()); 232 log.error(message); 233 } 234 235 public void error(String message, Throwable exception) { 236 appendString(message, Color.red.darker()); 237 log.error(message, exception); 238 } 239 240 public void info(String message) { 241 appendString(message, Color.blue.darker()); 242 log.info(message); 243 } 244 245 public void upgrade() throws Exception { 246 if (JOptionPane 247 .showConfirmDialog( 248 this, 249 "All selected resources will be now upgrade from the source installation to the target. Are you sure you wish to continue?", 250 "Run Upgrade", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) { 251 final List l = new ArrayList (); 253 for(int i = 0 ; i < upgradeSelectionPanel.getComponentCount(); i++) { 254 JCheckBox b = (JCheckBox )upgradeSelectionPanel.getComponent(i); 255 if(b.isSelected()) { 256 l.add(b.getClientProperty("upgrade")); 257 } 258 } 259 260 261 removeUpgradeSelectionComponent(); 262 invalidate(); 263 removeAll(); 264 265 JPanel progressPanel = new JPanel (new BorderLayout ()); 267 progressPanel.setBorder(BorderFactory.createTitledBorder("Progress")); 268 final JProgressBar b = new JProgressBar (0, l.size()); 269 b.setStringPainted(true); 270 progressPanel.add(b, BorderLayout.CENTER); 271 add(progressPanel, BorderLayout.NORTH); 272 273 JPanel consolePanel = new JPanel (new BorderLayout ()); 275 consolePanel.setBorder(BorderFactory.createTitledBorder("Output")); 276 console = new JTextPane (); 277 JScrollPane scrollPane = new JScrollPane (console); 278 consolePanel.add(scrollPane, BorderLayout.CENTER); 279 add(consolePanel, BorderLayout.CENTER); 280 281 283 validate(); 284 repaint(); 285 286 288 Thread t = new Thread () { 289 public void run() { 290 try { 291 for(Iterator i = l.iterator(); i.hasNext(); ) { 292 AbstractDatabaseUpgrade upgrade = (AbstractDatabaseUpgrade)i.next(); 293 b.setValue(b.getValue() + 1); 294 upgrade.upgrade(GUIUpgrader.this); 295 try { 296 Thread.sleep(750); 297 } 298 catch(InterruptedException ie) { 299 } 300 } 301 info("Complete"); 302 Toolkit.getDefaultToolkit().beep(); 303 } 304 catch(Exception e) { 305 error("Failed to upgrade.", e); 306 } 307 } 308 }; 309 t.start(); 310 311 312 } 313 314 } 315 316 public void warn(String message) { 317 appendString(message, Color.orange.darker()); 318 log.warn(message); 319 } 320 321 public void warn(String message, Throwable exception) { 322 appendString(message, Color.orange.darker()); 323 log.warn(message, exception); 324 325 } 326 327 void appendString(String message, Color c) { 328 Document doc = console.getDocument(); 329 if(doc.getLength() != 0) { 330 message = "\n" + message; 331 } 332 SimpleAttributeSet attr = new SimpleAttributeSet (); 333 StyleConstants.setForeground(attr, c); 334 try { 335 doc.insertString(doc.getLength(), message, attr); 336 } 337 catch(Exception e) { 338 } 339 console.setCaretPosition(doc.getLength()); 340 console.scrollRectToVisible(console.getVisibleRect()); 341 } 342 }; 343 | Popular Tags |