KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > MultiPropertyFolder


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.editor.options;
21
22 import org.openide.filesystems.FileStateInvalidException;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import org.openide.filesystems.FileLock;
27 import org.openide.filesystems.FileObject;
28 import org.openide.xml.XMLUtil;
29 import java.util.List JavaDoc;
30 import org.openide.filesystems.FileSystem;
31 import org.w3c.dom.Document JavaDoc;
32 import org.openide.loaders.DataFolder;
33 import org.openide.loaders.DataObject;
34
35 /** Folder for multi properties.
36  * Standard representation of this type is Popup folder, where we have to gather
37  * properties from more than one file.
38  *
39  * @author Martin Roskanin
40  * @since 08/2001
41  */

42 public class MultiPropertyFolder {
43     protected BaseOptions base;
44     protected DataFolder folder;
45     /** Creates new MultiPropertyFolder */
46     public MultiPropertyFolder(DataFolder fld, BaseOptions option){
47         this.base = option;
48         this.folder = fld;
49     }
50     
51     /** Gets folder properties */
52     List JavaDoc getProperties(){
53         List JavaDoc newSettings = new ArrayList JavaDoc();
54         DataObject dob[] = folder.getChildren();
55         
56         for (int i=0; i<dob.length; i++){
57             newSettings.add(dob[i]);
58         }
59         
60         return newSettings;
61     }
62     
63     /** Set changed properties to XML files */
64     void setProperties(List JavaDoc newProps){
65         //[PENDING]
66
}
67     
68     public String JavaDoc getName(){
69         return folder.getName();
70     }
71     
72     /** Gets DataFolder that represents this MultiPropertFolder */
73     public DataFolder getDataFolder(){
74         return folder;
75     }
76     
77     /** Deletes file from multiPropertyFolder */
78     protected void deleteFile(final String JavaDoc fileToDelete, final String JavaDoc ext){
79         try{
80             folder.getPrimaryFile().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
81                 public void run() throws IOException JavaDoc {
82                     FileObject delFO = folder.getPrimaryFile().getFileObject(fileToDelete, ext);
83                     if( delFO != null){
84                         FileLock lock = delFO.lock();
85                         try {
86                             delFO.delete(lock);
87                         } finally {
88                             lock.releaseLock();
89                         }
90                         
91                     }
92                 }
93             });
94         }catch(FileStateInvalidException fsie){
95             fsie.printStackTrace();
96         }catch(IOException JavaDoc ioe){
97             ioe.printStackTrace();
98         }
99     }
100
101     /** Creates the empty XML files with names provided in fileName and given root tag, public and system ID */
102     protected void createEmptyXMLFiles(final List JavaDoc fileName, String JavaDoc tagRoot, String JavaDoc publicID, String JavaDoc systemID){
103         
104         final Document JavaDoc doc = XMLUtil.createDocument(tagRoot, null, publicID, systemID);
105         
106         try{
107             folder.getPrimaryFile().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
108                 public void run() throws IOException JavaDoc {
109                     for (int i=0; i<fileName.size(); i++){
110                         
111                         if( folder.getPrimaryFile().getFileObject((String JavaDoc)fileName.get(i), "xml") != null) continue; //NOI18N
112

113                         // file doesn't exist, create it.
114
FileObject fo = folder.getPrimaryFile().createData((String JavaDoc)fileName.get(i), "xml"); // NOI18N
115
FileLock lock = fo.lock();
116                         try {
117                             OutputStream JavaDoc os = fo.getOutputStream(lock);
118                             try {
119                                 XMLUtil.write(doc, fo.getOutputStream(lock), "UTF-8"); // NOI18N
120
} finally {
121                                 os.close();
122                             }
123                         } finally {
124                             lock.releaseLock();
125                         }
126                     }
127                 }
128             });
129         }catch(FileStateInvalidException fsie){
130             fsie.printStackTrace();
131         }catch(IOException JavaDoc ioe){
132             ioe.printStackTrace();
133         }
134     }
135     
136     
137 }
138
Popular Tags