KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > upgrade > AutoUpgrade


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.bluej.upgrade;
21 import java.io.*;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Array JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.swing.JOptionPane JavaDoc;
35 import javax.swing.text.SimpleAttributeSet JavaDoc;
36 import javax.swing.text.StyleConstants JavaDoc;
37
38 import org.netbeans.bluej.util.Util;
39 import org.openide.ErrorManager;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.filesystems.LocalFileSystem;
43 import org.openide.filesystems.MultiFileSystem;
44 import org.openide.filesystems.Repository;
45 import org.openide.filesystems.XMLFileSystem;
46 import org.openide.util.NbBundle;
47 import org.xml.sax.SAXException JavaDoc;
48
49 /** pending
50  *
51  * @author Jiri Rechtacek
52  */

53 public final class AutoUpgrade {
54
55     public static void main (String JavaDoc[] args) throws Exception JavaDoc {
56         String JavaDoc[] version = new String JavaDoc[1];
57         File JavaDoc sourceFolder = checkPrevious (version);
58         if (sourceFolder != null) {
59             if (!showUpgradeDialog (sourceFolder)) {
60                 throw new org.openide.util.UserCancelException ();
61             }
62             doUpgrade (sourceFolder, version[0]);
63         }
64     }
65     
66     // the order of VERSION_TO_CHECK here defines the precedence of imports
67
// the first one will be choosen for import
68
final static private List JavaDoc VERSION_TO_CHECK = Collections.EMPTY_LIST; //Arrays.asList (new String[] { ".netbeans/5.0" });
69

70     static private File JavaDoc checkPrevious (String JavaDoc[] version) {
71         boolean exists;
72         
73         String JavaDoc userHome = System.getProperty ("user.home"); // NOI18N
74
File JavaDoc sourceFolder = null;
75         
76         if (userHome != null) {
77             File JavaDoc userHomeFile = new File JavaDoc (userHome);
78             exists = userHomeFile.isDirectory ();
79
80             Iterator JavaDoc it = VERSION_TO_CHECK.iterator ();
81             String JavaDoc ver;
82             while (it.hasNext () && sourceFolder == null) {
83                 ver = (String JavaDoc) it.next ();
84                 sourceFolder = new File JavaDoc (userHomeFile.getAbsolutePath (), ver);
85                 
86                 if (sourceFolder.isDirectory ()) {
87                     version[0] = sourceFolder.getName();
88                     break;
89                 }
90                 sourceFolder = null;
91             }
92             return sourceFolder;
93         } else {
94             return null;
95         }
96     }
97     
98     private static boolean showUpgradeDialog (final File JavaDoc source) {
99         Util.setDefaultLookAndFeel();
100         JOptionPane JavaDoc p = new JOptionPane JavaDoc (
101             new AutoUpgradePanel (source.getAbsolutePath ()),
102             JOptionPane.QUESTION_MESSAGE,
103             JOptionPane.YES_NO_OPTION
104         );
105         javax.swing.JDialog JavaDoc d = p.createDialog (
106             null,
107             NbBundle.getMessage (AutoUpgrade.class, "MSG_Confirmation_Title") // NOI18N
108
);
109         d.setModal (true);
110         d.setVisible (true);
111
112         return new Integer JavaDoc (JOptionPane.YES_OPTION).equals (p.getValue ());
113     }
114     
115     static void doUpgrade (File JavaDoc source, String JavaDoc oldVersion)
116     throws java.io.IOException JavaDoc, java.beans.PropertyVetoException JavaDoc {
117         
118         File JavaDoc userdir = new File JavaDoc(System.getProperty ("netbeans.user", "")); // NOI18N
119

120         java.util.Set JavaDoc includeExclude;
121         try {
122             Reader r = new InputStreamReader (
123                     AutoUpgrade.class.getResourceAsStream ("copy" + oldVersion), // NOI18N
124
"utf-8"); // NOI18N
125
includeExclude = IncludeExclude.create (r);
126             r.close ();
127         } catch (IOException JavaDoc ex) {
128             IOException JavaDoc e = new IOException JavaDoc ("Cannot import from version: " + oldVersion);
129             e.initCause (ex);
130             throw e;
131         }
132
133         ErrorManager.getDefault ().log (
134             ErrorManager.USER, "Import: Old version: " // NOI18N
135
+ oldVersion + ". Importing from " + source + " to " + userdir // NOI18N
136
);
137         
138         File JavaDoc oldConfig = new File JavaDoc (source, "config"); // NOI18N
139
org.openide.filesystems.FileSystem old;
140         {
141             LocalFileSystem lfs = new LocalFileSystem ();
142             lfs.setRootDirectory (oldConfig);
143             
144             XMLFileSystem xmlfs = null;
145             try {
146                 URL JavaDoc url = AutoUpgrade.class.getResource("layer" + oldVersion + ".xml"); // NOI18N
147
xmlfs = new XMLFileSystem(url);
148             } catch (SAXException JavaDoc ex) {
149                 IOException JavaDoc e = new IOException JavaDoc ("Cannot import from version: " + oldVersion); // NOI18N
150
e.initCause (ex);
151                 throw e;
152             }
153             
154             old = createLayeredSystem(lfs, xmlfs);
155         }
156         org.openide.filesystems.FileSystem mine = Repository.getDefault ().
157             getDefaultFileSystem ();
158         
159         Copy.copyDeep (old.getRoot (), mine.getRoot (), includeExclude);
160         
161     }
162
163     static MultiFileSystem createLayeredSystem(final LocalFileSystem lfs, final XMLFileSystem xmlfs) {
164         MultiFileSystem old;
165         
166         old = new MultiFileSystem (
167             new org.openide.filesystems.FileSystem[] { lfs, xmlfs }
168         ) {
169             {
170                 setPropagateMasks(true);
171             }
172         };
173         return old;
174     }
175
176 }
177
178
179
180
Popular Tags