KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > Utilities


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * Utilities.java
22  * Created on November 5, 2004, 5:08 PM
23  */

24  
25 package org.netbeans.modules.css;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.nio.channels.FileChannel JavaDoc;
32 import java.security.AccessController JavaDoc;
33 import java.security.PrivilegedAction JavaDoc;
34 import javax.swing.JFileChooser JavaDoc;
35
36 /**
37  * Some useful utilities
38  * @author Winston Prakash
39  * @version 1.0
40  */

41 public class Utilities {
42     
43     /**
44      * This does a special instantiation of JFileChooser
45      * to workaround floppy access bug 5037322.
46      * Using privileged code block.
47      */

48     public static JFileChooser JavaDoc getJFileChooser() {
49         return (JFileChooser JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc<Object JavaDoc>() {
50             public Object JavaDoc run() {
51                 return new JFileChooser JavaDoc() ;
52             }
53         });
54     }
55     
56     public static JFileChooser JavaDoc getJFileChooser(final String JavaDoc currentDirectoryPath) {
57         return (JFileChooser JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc<Object JavaDoc>() {
58             public Object JavaDoc run() {
59                 return new JFileChooser JavaDoc(currentDirectoryPath);
60             }
61         });
62     }
63     
64     public static JFileChooser JavaDoc getJFileChooser(final java.io.File JavaDoc currentDirectory) {
65         return (JFileChooser JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc<Object JavaDoc>() {
66             public Object JavaDoc run() {
67                 return new JFileChooser JavaDoc(currentDirectory) ;
68             }
69         });
70     }
71     
72     public static void copyFile(File JavaDoc sourceFile, File JavaDoc destFile) throws IOException JavaDoc {
73         if(!destFile.exists()) {
74             destFile.createNewFile();
75         }
76         
77         FileChannel JavaDoc source = null;
78         FileChannel JavaDoc destination = null;
79         try {
80             source = new FileInputStream JavaDoc(sourceFile).getChannel();
81             destination = new FileOutputStream JavaDoc(destFile).getChannel();
82             destination.transferFrom(source, 0, source.size());
83         } finally {
84             if(source != null) {
85                 source.close();
86             }
87             if(destination != null) {
88                 destination.close();
89             }
90         }
91     }
92 }
93
Popular Tags