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

package org.netbeans.core.deprecated;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import org.openide.*;
import org.openide.cookies.*;
import org.openide.loaders.*;
import org.openide.filesystems.*;
import org.openide.filesystems.FileSystem; // override java.io.FileSystem
import org.openide.util.NbBundle;
import org.openide.util.io.*;
import org.openide.nodes.*;

import org.netbeans.core.projects.TrivialProjectManager;

/** Holder for current project. Also provides the default one.
*
* @author Jaroslav Tulach
*/
public class NbProjectOperation extends TrivialProjectManager {
    /** current project */
    private static ProjectCookie project;

    /** desktop node for current project */
    private static Node projectDesktop;

    /** name and extension for basic serialized project file */
    private static final String LAST_PROJECT_NAME = "project"; // NOI18N
    private static final String LAST_PROJECT_EXT = "last"; // NOI18N
    
    // ----------------------------------------------------------------------------------------
    // Static methods for manipulation with projects

    public void store() throws IOException {
        if (project != null) {
            project.projectSave ();
            saveObject (getFile (LAST_PROJECT_NAME, LAST_PROJECT_EXT, true), project, false);
        }
    }

    private static Object loadObject (FileObject fin, boolean safe) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new NbObjectInputStream (fin.getInputStream ());
        try {
            if (safe)
                return NbObjectInputStream.readSafely (ois);
            else
                return ois.readObject ();
        } finally {
            ois.close ();
        }
    }

    private static void saveObject (FileObject fout, Object o, boolean safe) throws IOException {
        FileLock lock = fout.lock ();
        try {
            ObjectOutputStream oos = new NbObjectOutputStream (fout.getOutputStream (lock));
            try {
                if (safe)
                    NbObjectOutputStream.writeSafely (oos, o);
                else
                    oos.writeObject (o);
            } finally {
                oos.close ();
            }
        } finally {
            lock.releaseLock ();
        }
    }
    
    /** Getter for the project desktop node.
    */
    public static Node getProjectDesktop () {
        if (projectDesktop != null) {
            return projectDesktop;
        }
        
        if ( !hasProjectDesktop() ) {
            return null;
        }

        // the node should be improved to have better look, actions and properties
        FileSystem fs = Repository.getDefault().getDefaultFileSystem ();
        FileObject fo = fs.findResource("Workplace");
        return DataFolder.findFolder(fo).getNodeDelegate ();
    }

    /** Test if the project has own desktop or not */
    public static boolean hasProjectDesktop () {
        FileSystem fs = Repository.getDefault().getDefaultFileSystem ();
        FileObject fo = fs.findResource("Workplace");
        return fo != null;
    }
    
    /** Getter for current project.
    * @return the project
    */
    public static ProjectCookie getProject () {
        return project;
    }

    /** Setter for changing the project.
    * @param p the new project to use
    * @exception IOException if the previous project cannot be closed or new opened
    */
    private static void setProject (final ProjectCookie p) throws IOException {
        if (project == p) {
            // no change in project
            return;
        }

        if (project != null) {
            project.projectSave();
            org.netbeans.core.projects.XMLSettingsHandler.saveOptions();
        }

        Node oldDesktop = projectDesktop;
        projectDesktop = p.projectDesktop ();
        
        try {
            // open the project
            p.projectOpen ();
            if (project != null) project.projectClose();
        }
        catch (IOException e) {
            projectDesktop = oldDesktop;
            throw e;
        }

        project = p;
        org.netbeans.core.projects.XMLSettingsHandler.openOptions();

        if (!(project instanceof NbProject)) {
            saveObject (getFile (LAST_PROJECT_NAME, LAST_PROJECT_EXT, true), project, false);
        }
    }

    /** Opens last used project or creates a default one. */
    public void load() throws IOException {
        ProjectCookie project = null;
        // the file, but do not create new one
        FileObject nameFile = getFile (LAST_PROJECT_NAME, LAST_PROJECT_EXT, false);
        if (nameFile != null) {
            try {
                project = (ProjectCookie) loadObject (nameFile, false);
            } catch (ClassNotFoundException cnfe) {
                IOException ioe = new IOException(cnfe.toString());
                ErrorManager.getDefault().annotate(ioe, cnfe);
                throw ioe;
            }
        }
        NbProjectOperation.project = setOpeningProject (project);
    }
    
    /** Try to set up new project. If it fails then NbProject is used as default.
     * @param project project to set up. If null then NbProject
     * is used as default.
     * @return set project.
     */
    public static ProjectCookie setOpeningProject(ProjectCookie project) {
        try {
            if (project == null) {
                project = createDefaultProject();
            }
            setProject (project);
            return project;
        } catch (Exception exc) {
            if (project instanceof NbProject) {
                ErrorManager.getDefault ().notify (exc);
            } else {
                ErrorManager.getDefault ()
                .log (NbBundle.getBundle(NbProjectOperation.class)
                .getString("EXC_CorruptedProject"));
                return setOpeningProject(null);
            }
        }
        return createDefaultProject();
    }

    /** Creates and returns newly created default project */
    private static ProjectCookie createDefaultProject () {
        // create empty new
        ProjectCookie project = new NbProject();
        return project;
    }

    /** Returns file object.
    * @param name name of the file
    * @param ext extensionf of the file
    * @param create true if the file should be created
    * @return the file object
    */
    private static FileObject getFile (String name, String ext, boolean create) throws java.io.IOException {
        org.openide.filesystems.FileSystem def =
            Repository.getDefault().getDefaultFileSystem ();

        FileObject root = def.getRoot ();
        FileObject fo = root.getFileObject (name, ext);
        if (fo == null && create) {
            fo = root.createData (name, ext);
        }
        return fo;
    }
    
}
... 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.