KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > Installer


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.modules.derby;
21
22 import java.io.IOException JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import org.netbeans.api.db.explorer.DatabaseException;
27 import org.netbeans.api.java.platform.JavaPlatform;
28 import org.netbeans.api.java.platform.JavaPlatformManager;
29 import org.netbeans.api.progress.ProgressHandle;
30 import org.netbeans.api.progress.ProgressHandleFactory;
31 import org.netbeans.modules.derby.api.DerbyDatabases;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.modules.ModuleInstall;
35 import org.openide.modules.SpecificationVersion;
36 import org.openide.util.NbBundle;
37 import org.openide.util.RequestProcessor;
38 import org.openide.windows.WindowManager;
39
40 /**
41  * @author Andrei Badea
42  */

43 public class Installer extends ModuleInstall {
44
45     public void restored() {
46         WindowManager.getDefault().invokeWhenUIReady(new RegisterJDKDerby());
47     }
48
49     private static final class RegisterJDKDerby implements Runnable JavaDoc {
50
51         public void run() {
52             if (SwingUtilities.isEventDispatchThread()) {
53                 RequestProcessor.getDefault().post(this);
54                 return;
55             }
56
57             JavaPlatform javaPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
58             SpecificationVersion version = javaPlatform.getSpecification().getVersion();
59             if (new SpecificationVersion("1.6").compareTo(version) > 0) { // NOI18N
60
return;
61             }
62
63             ProgressHandle handle = ProgressHandleFactory.createSystemHandle(NbBundle.getMessage(Installer.class, "MSG_RegisterJavaDB"));
64             handle.start();
65             try {
66                 if (registerJDKDerby(javaPlatform)) {
67                     registerSampleDatabase();
68                 }
69             } finally {
70                 handle.finish();
71             }
72         }
73
74         private boolean registerJDKDerby(JavaPlatform javaPlatform) {
75             if (DerbyOptions.getDefault().getLocation().length() > 0) {
76                 return false;
77             }
78             for (Object JavaDoc dir : javaPlatform.getInstallFolders()) {
79                 FileObject derbyDir = ((FileObject)dir).getFileObject("db"); // NOI18N
80
if (derbyDir != null) {
81                     DerbyOptions.getDefault().setLocation(FileUtil.toFile(derbyDir).getAbsolutePath());
82                     return true;
83                 }
84             }
85             return false;
86         }
87
88         private void registerSampleDatabase() {
89             try {
90                 DerbyDatabases.createSampleDatabase();
91             } catch (DatabaseException e) {
92                 Logger.getLogger(Installer.class.getName()).log(Level.WARNING, null, e);
93             } catch (IOException JavaDoc e) {
94                 Logger.getLogger(Installer.class.getName()).log(Level.WARNING, null, e);
95             }
96         }
97     }
98 }
99
Popular Tags