|
What this is
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
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.