alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.registry;

import org.netbeans.core.projects.SessionManager;
import org.netbeans.spi.registry.ResettableContext;
import org.netbeans.spi.registry.SpiUtils;
import org.netbeans.api.registry.ContextException;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.Repository;
import org.openide.ErrorManager;

import java.io.IOException;

public class ResettableContextImpl extends ContextImpl implements ResettableContext {

    private ContextBindings resettableContextBindings;
    
    private FileSystem defaults;
    private FileSystem customizations;
    
    public ResettableContextImpl () {
        this (Repository.getDefault().getDefaultFileSystem().getRoot());        
    }
    
    public ResettableContextImpl (FileObject folder) {
        this (folder, null, SessionManager.getDefault().getLayer(SessionManager.LAYER_INSTALL), SessionManager.getDefault().getLayer(SessionManager.LAYER_SESSION));
    }
    
    public ResettableContextImpl(FileObject folder, ContextImpl rootContext, FileSystem defaults, FileSystem customizations) {
        super(folder, rootContext);
        this.defaults = defaults;
        this.customizations = customizations;
    }
    
    public synchronized ContextBindings getContextBindings() {
        if (resettableContextBindings == null) {
            resettableContextBindings = ContextBindings.createResettableContextBindings(getFolder(), this, defaults, customizations);
        }
        return resettableContextBindings;
    }
    
    public ContextImpl getCtx(FileObject fo) {
        // this method must be overriden on root context and must
        // retrieve context if it already exists
        ContextImpl ctx = getRootContextImpl().getContextCache().retrieveContextFromCache(fo);
        if (ctx == null) {
            ctx = new ResettableContextImpl(fo, getRootContextImpl(), defaults, customizations);
        }
        return ctx;
    }

    /*ResettableContext.hasDefault */
    public boolean hasDefault(String bindingName) {
        if (bindingName != null) {
            return getContextBindings().hasDefault(bindingName);
        } else {
            FileSystem fs = defaults;
            FileObject defaults = fs.findResource(getFolder ().getPath());
            return (defaults != null);
        }
    }

    /*ResettableContext.isModified */    
    public boolean isModified(String bindingName) {
        if (!(this instanceof ResettableContext)) {
            // assert - should never happen!!
            // these methods can be called only from ResettableContextImpl
            return false;
        }
        if (bindingName != null) {
            if (getContextBindings().isModified(bindingName)) {
                return true;
            }
            return false;
        } else {
            FileSystem fs = customizations;
            FileObject custs = fs.findResource(getFolder ().getPath());
            return (custs != null);
        }
    }

    /*ResettableContext.revert */        
    public void revert(String bindingName) throws ContextException {
        if (!isModified(bindingName)) {
            return;
        }
        
        if (bindingName != null) {
            if (getContextBindings().isModified(bindingName)) {
                getContextBindings().revert(bindingName);
                return;
            }
        } else {
            FileSystem fs = customizations;
            FileObject custs = fs.findResource(getFolder ().getPath());
            if (custs != null) {
                deleteFolderContent(custs);
            }
        }
    }

    private void deleteFolderContent(FileObject fo) throws ContextException {
        try {
// #16761 - the following code does not properly clean attributes. 
// Use delete folder + create folder instead
//            FileObject fos[] = fo.getChildren();
//            for (int i=0; i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.