KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > GUIUpgrader


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.GridBagLayout JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.Toolkit JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.io.File JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.BoxLayout JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JCheckBox JavaDoc;
40 import javax.swing.JComponent JavaDoc;
41 import javax.swing.JFileChooser JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JOptionPane JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JProgressBar JavaDoc;
46 import javax.swing.JScrollPane JavaDoc;
47 import javax.swing.JSeparator JavaDoc;
48 import javax.swing.JTextArea JavaDoc;
49 import javax.swing.JTextField JavaDoc;
50 import javax.swing.JTextPane JavaDoc;
51 import javax.swing.event.DocumentEvent JavaDoc;
52 import javax.swing.event.DocumentListener JavaDoc;
53 import javax.swing.text.Document JavaDoc;
54 import javax.swing.text.SimpleAttributeSet JavaDoc;
55 import javax.swing.text.StyleConstants JavaDoc;
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 JavaDoc implements DocumentListener JavaDoc, Upgrader {
63     
64     final static Log log = LogFactory.getLog(GUIUpgrader.class);
65
66     private List JavaDoc upgrades;
67     private JPanel JavaDoc mainPanel, upgradeSelectionPanel;
68     private JTextField JavaDoc target, source;
69     private JButton JavaDoc browseSource, browseTarget;
70     private JTextPane JavaDoc console;
71
72     public GUIUpgrader() {
73         super(new BorderLayout JavaDoc());
74         JPanel JavaDoc info = new JPanel JavaDoc(new BorderLayout JavaDoc(2, 2));
75         
76 // info.setBackground(Color.white);
77
// info.setForeground(Color.black);
78
// info.setOpaque(true);
79
info.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
80         JLabel JavaDoc l = new JLabel JavaDoc("<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 JavaDoc(GUIUpgrader.class.getResource("upgrader-48x48.png")));
85         info.add(l, BorderLayout.CENTER);
86         info.add(new JSeparator JavaDoc(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
87         mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
88         add(info, BorderLayout.NORTH);
89         add(mainPanel, BorderLayout.CENTER);
90
91         // Installations panel
92
JPanel JavaDoc installations = new JPanel JavaDoc(new GridBagLayout JavaDoc());
93         installations.setBorder(BorderFactory.createTitledBorder("SSL-Explorer Installations"));
94         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
95         gbc.anchor = GridBagConstraints.WEST;
96         gbc.fill = GridBagConstraints.HORIZONTAL;
97         gbc.insets = new Insets JavaDoc(2, 2, 2, 2);
98         gbc.weightx = 2.0;
99         UIUtil.jGridBagAdd(installations, new JLabel JavaDoc("Source"), gbc, GridBagConstraints.REMAINDER);
100         gbc.weightx = 1.0;
101         source = new JTextField JavaDoc();
102         source.getDocument().addDocumentListener(this);
103         UIUtil.jGridBagAdd(installations, source, gbc, GridBagConstraints.RELATIVE);
104         browseSource = new JButton JavaDoc("Browse");
105         browseSource.setMnemonic('b');
106         browseSource.addActionListener(new ActionListener JavaDoc() {
107
108             public void actionPerformed(ActionEvent JavaDoc e) {
109                 JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(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 JavaDoc("Target"), gbc, GridBagConstraints.REMAINDER);
121         gbc.weightx = 1.0;
122         target = new JTextField JavaDoc(System.getProperty("user.dir"));
123         target.getDocument().addDocumentListener(this);
124         UIUtil.jGridBagAdd(installations, target, gbc, GridBagConstraints.RELATIVE);
125         browseTarget = new JButton JavaDoc("Browse");
126         browseTarget.setMnemonic('r');
127         browseTarget.addActionListener(new ActionListener JavaDoc() {
128             public void actionPerformed(ActionEvent JavaDoc e) {
129                 JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(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         // Upgrade selection
142
upgradeSelectionPanel = new JPanel JavaDoc();
143         upgradeSelectionPanel.setBorder(BorderFactory.createTitledBorder("Upgrades"));
144         upgradeSelectionPanel.setLayout(new BoxLayout JavaDoc(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 JavaDoc i = upgrades.iterator(); i.hasNext();) {
161             AbstractDatabaseUpgrade upgrade = (AbstractDatabaseUpgrade) i.next();
162             JCheckBox JavaDoc box = new JCheckBox JavaDoc(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 JavaDoc e) {
172         checkInstallations();
173     }
174
175     public void insertUpdate(DocumentEvent JavaDoc e) {
176         checkInstallations();
177     }
178
179     public void removeUpdate(DocumentEvent JavaDoc e) {
180         checkInstallations();
181     }
182
183     void checkInstallations() {
184         File JavaDoc oldDir = new File JavaDoc(source.getText());
185         try {
186             if (!oldDir.exists() || !oldDir.isDirectory()) {
187                 throw new Exception JavaDoc(oldDir.getAbsolutePath() + " does not exists or is not a directory");
188             }
189             File JavaDoc newDir = new File JavaDoc(target.getText());
190             if (!newDir.exists() || !newDir.isDirectory()) {
191                 throw new Exception JavaDoc(newDir.getAbsolutePath() + " does not exists or is not a directory");
192             }
193             if (oldDir.getCanonicalFile().equals(newDir.getCanonicalFile())) {
194                 throw new Exception JavaDoc("Source and target installation directories are identical");
195             }
196             File JavaDoc oldDbDir = new File JavaDoc(oldDir, "db");
197             File JavaDoc newDbDir = new File JavaDoc(newDir, "db");
198             if (!new File JavaDoc(newDir, "db").exists()) {
199                 throw new Exception JavaDoc("Target installation does not appear to be an SSL-Explorer installation.");
200             }
201             if (!new File JavaDoc(oldDir, "db").exists()) {
202                 throw new Exception JavaDoc("Source installation does not appear to be does not appear to be an SSL-Explorer installation.");
203             }
204             if (!new File JavaDoc(newDir, "install").exists()) {
205                 throw new Exception JavaDoc("Target installation does not appear to be 0.2.5+");
206             }
207             if (!new File JavaDoc(new File JavaDoc(oldDir, "db"),"explorer_accounts.data").exists()) {
208                 throw new Exception JavaDoc("Source installation does not appear to be 0.1.15+");
209             }
210
211             upgrades = new ArrayList JavaDoc();
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 JavaDoc e) {
223             removeUpgradeSelectionComponent();
224             upgrades = null;
225             upgradeSelectionPanel.add(new JLabel JavaDoc("<html>" + e.getMessage() + "</html>"));
226             upgradeSelectionPanel.revalidate();
227         }
228     }
229
230     public void error(String JavaDoc message) {
231         appendString(message, Color.red.darker());
232         log.error(message);
233     }
234
235     public void error(String JavaDoc message, Throwable JavaDoc exception) {
236         appendString(message, Color.red.darker());
237         log.error(message, exception);
238     }
239
240     public void info(String JavaDoc message) {
241         appendString(message, Color.blue.darker());
242         log.info(message);
243     }
244
245     public void upgrade() throws Exception JavaDoc {
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             //
252
final List JavaDoc l = new ArrayList JavaDoc();
253             for(int i = 0 ; i < upgradeSelectionPanel.getComponentCount(); i++) {
254                 JCheckBox JavaDoc b = (JCheckBox JavaDoc)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             // Progress panel
266
JPanel JavaDoc progressPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
267             progressPanel.setBorder(BorderFactory.createTitledBorder("Progress"));
268             final JProgressBar JavaDoc b = new JProgressBar JavaDoc(0, l.size());
269             b.setStringPainted(true);
270             progressPanel.add(b, BorderLayout.CENTER);
271             add(progressPanel, BorderLayout.NORTH);
272             
273             // Console panel
274
JPanel JavaDoc consolePanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
275             consolePanel.setBorder(BorderFactory.createTitledBorder("Output"));
276             console = new JTextPane JavaDoc();
277             JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(console);
278             consolePanel.add(scrollPane, BorderLayout.CENTER);
279             add(consolePanel, BorderLayout.CENTER);
280             
281             //
282

283             validate();
284             repaint();
285             
286             //
287

288             Thread JavaDoc t = new Thread JavaDoc() {
289                 public void run() {
290                     try {
291                         for(Iterator JavaDoc 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 JavaDoc ie) {
299                             }
300                         }
301                         info("Complete");
302                         Toolkit.getDefaultToolkit().beep();
303                     }
304                     catch(Exception JavaDoc e) {
305                         error("Failed to upgrade.", e);
306                     }
307                 }
308             };
309             t.start();
310             
311
312         }
313
314     }
315
316     public void warn(String JavaDoc message) {
317         appendString(message, Color.orange.darker());
318         log.warn(message);
319     }
320
321     public void warn(String JavaDoc message, Throwable JavaDoc exception) {
322         appendString(message, Color.orange.darker());
323         log.warn(message, exception);
324
325     }
326     
327     void appendString(String JavaDoc message, Color JavaDoc c) {
328         Document JavaDoc doc = console.getDocument();
329         if(doc.getLength() != 0) {
330             message = "\n" + message;
331         }
332         SimpleAttributeSet JavaDoc attr = new SimpleAttributeSet JavaDoc();
333         StyleConstants.setForeground(attr, c);
334         try {
335             doc.insertString(doc.getLength(), message, attr);
336         }
337         catch(Exception JavaDoc e) {
338         }
339         console.setCaretPosition(doc.getLength());
340         console.scrollRectToVisible(console.getVisibleRect());
341     }
342 };
343
Popular Tags