KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > ResettableContextImpl


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.core.registry;
21
22 import org.netbeans.core.startup.layers.SessionManager;
23 import org.netbeans.spi.registry.ResettableContext;
24 import org.netbeans.spi.registry.SpiUtils;
25 import org.netbeans.api.registry.ContextException;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileSystem;
28 import org.openide.filesystems.Repository;
29 import org.openide.ErrorManager;
30
31 import java.io.IOException JavaDoc;
32
33 public class ResettableContextImpl extends ContextImpl implements ResettableContext {
34
35     private ContextBindings resettableContextBindings;
36     
37     private FileSystem defaults;
38     private FileSystem customizations;
39     
40     public ResettableContextImpl () {
41         this (Repository.getDefault().getDefaultFileSystem().getRoot());
42     }
43     
44     public ResettableContextImpl (FileObject folder) {
45         this (folder, null, SessionManager.getDefault().getLayer(SessionManager.LAYER_INSTALL), SessionManager.getDefault().getLayer(SessionManager.LAYER_SESSION));
46     }
47     
48     public ResettableContextImpl(FileObject folder, ContextImpl rootContext, FileSystem defaults, FileSystem customizations) {
49         super(folder, rootContext);
50         this.defaults = defaults;
51         this.customizations = customizations;
52     }
53     
54     public synchronized ContextBindings getContextBindings() {
55         if (resettableContextBindings == null) {
56             resettableContextBindings = ContextBindings.createResettableContextBindings(getFolder(), this, defaults, customizations);
57         }
58         return resettableContextBindings;
59     }
60     
61     public ContextImpl getCtx(FileObject fo) {
62         // this method must be overriden on root context and must
63
// retrieve context if it already exists
64
ContextImpl ctx = getRootContextImpl().getContextCache().retrieveContextFromCache(fo);
65         if (ctx == null) {
66             ctx = new ResettableContextImpl(fo, getRootContextImpl(), defaults, customizations);
67         }
68         return ctx;
69     }
70
71     /*ResettableContext.hasDefault */
72     public boolean hasDefault(String JavaDoc bindingName) {
73         if (bindingName != null) {
74             return getContextBindings().hasDefault(bindingName);
75         } else {
76             FileSystem fs = defaults;
77             FileObject defaults = fs.findResource(getFolder ().getPath());
78             return (defaults != null);
79         }
80     }
81
82     /*ResettableContext.isModified */
83     public boolean isModified(String JavaDoc bindingName) {
84         if (!(this instanceof ResettableContext)) {
85             // assert - should never happen!!
86
// these methods can be called only from ResettableContextImpl
87
return false;
88         }
89         if (bindingName != null) {
90             if (getContextBindings().isModified(bindingName)) {
91                 return true;
92             }
93             return false;
94         } else {
95             FileSystem fs = customizations;
96             FileObject custs = fs.findResource(getFolder ().getPath());
97             return (custs != null);
98         }
99     }
100
101     /*ResettableContext.revert */
102     public void revert(String JavaDoc bindingName) throws ContextException {
103         if (!isModified(bindingName)) {
104             return;
105         }
106         
107         if (bindingName != null) {
108             if (getContextBindings().isModified(bindingName)) {
109                 getContextBindings().revert(bindingName);
110                 return;
111             }
112         } else {
113             FileSystem fs = customizations;
114             FileObject custs = fs.findResource(getFolder ().getPath());
115             if (custs != null) {
116                 deleteFolderContent(custs);
117             }
118         }
119     }
120
121     private void deleteFolderContent(FileObject fo) throws ContextException {
122         try {
123 // #16761 - the following code does not properly clean attributes.
124
// Use delete folder + create folder instead
125
// FileObject fos[] = fo.getChildren();
126
// for (int i=0; i<fos.length; i++) {
127
// fos[i].delete();
128
// }
129
// Enumeration en = fo.getAttributes();
130
// while (en.hasMoreElements()) {
131
// String attr = (String)en.nextElement();
132
// fo.setAttribute(attr, null);
133
// Object oo = fo.getAttribute(attr);
134
// }
135
FileObject f = fo.getParent();
136             String JavaDoc name = fo.getNameExt();
137             fo.delete();
138             f.createFolder(name);
139         } catch (IOException JavaDoc ex) {
140             ContextException ce = SpiUtils.createContextException(this, "Error on underlaying filesystem occured during the revert.");
141             ErrorManager.getDefault().annotate(ce, ex);
142             throw ce;
143         }
144     }
145
146 }
147
Popular Tags