package bluej.pkgmgr;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Path;
import java.util.Dictionary;
import java.util.Hashtable;
import bluej.Config;
import bluej.debugger.Debugger;
import bluej.extensions.SourceType;
import bluej.utility.BlueJFileReader;
import bluej.utility.DialogManager;
import bluej.utility.FileUtility;
import javafx.stage.Stage;
| Utilities for working with projects.
|
| @author Davin McCall
|
public class ProjectUtils
{
| Check the debugger state is suitable for execution: that is, it is not already
| executing anything or stuck at a breakpoint.
|
| <P>Returns true if the debugger is currently idle, or false if it is already
| executing, in which case an error dialog is also displayed.
|
| @param project the project to check execution state for.
| @param msgWindow the parent window for any error dialogs.
| @return true if the debugger is currently idle.
|
public static boolean checkDebuggerState(Project project, Stage msgWindow)
{
Debugger debugger = project.getDebugger();
if (debugger.getStatus() == Debugger.SUSPENDED)
{
DialogManager.showErrorFX(msgWindow, "stuck-at-breakpoint");
return false;
}
else if (debugger.getStatus() == Debugger.RUNNING)
{
DialogManager.showErrorFX(msgWindow, "already-executing");
return false;
}
return true;
}
| Given a project and a path, save a copy of the project to the new path. If there are any
| errors, display a dialog to the user (and return false).
|
| @param project The project to save
| @param newName The path to save the copy to
| @param window The parent window for any error dialogs
| @return true if the save was successful, or false otherwise
|
public static boolean saveProjectCopy(Project project, File newName, Stage window)
{
Path newPath = newName.toPath().toAbsolutePath();
Path existingPath = project.getProjectDir().toPath().toAbsolutePath();
if (newPath.startsWith(existingPath))
{
DialogManager.showErrorFX(window, "cannot-save-inside-self");
return false;
}
int result = FileUtility.copyDirectory(project.getProjectDir(),
newName);
switch (result)
{
case FileUtility.NO_ERROR:
break;
case FileUtility.DEST_EXISTS_NOT_DIR:
DialogManager.showErrorFX(window, "directory-exists-file");
return false;
case FileUtility.DEST_EXISTS_NON_EMPTY:
DialogManager.showErrorFX(window, "directory-exists-non-empty");
return false;
case FileUtility.SRC_NOT_DIRECTORY:
case FileUtility.COPY_ERROR:
DialogManager.showErrorFX(window, "cannot-save-project");
return false;
}
return true;
}
| Creates the skeleton for a new class
|
public static void createSkeleton(String className, String superClassName, File file, String templateFileName, String projCharsetName)
throws IOException
{
Dictionary<String, String> translations = new Hashtable<String, String>();
translations.put("CLASSNAME", className);
if (superClassName != null)
{
translations.put("EXTENDSANDSUPERCLASSNAME", "extends " + superClassName);
translations.put("EXTENDSSUPERCLASSNAME", "extends=\"" + superClassName + "\"");
}
else
{
translations.put("EXTENDSANDSUPERCLASSNAME", "");
translations.put("EXTENDSSUPERCLASSNAME", "");
}
String baseName = "greenfoot/templates/" + templateFileName;
File template = Config.getLanguageFile(baseName);
if (!template.canRead())
{
template = Config.getDefaultLanguageFile(baseName);
}
BlueJFileReader.translateFile(template, file, translations, StandardCharsets.UTF_8, selectCharset(projCharsetName));
}
| Creates the duplicate for a class
|
public static void duplicate(String originalClassName, String destinationClassName, File originalFile, File destination, SourceType type)
throws IOException
{
Dictionary<String, String> translations = new Hashtable<String, String>();
translations.put(originalClassName, destinationClassName);
BlueJFileReader.duplicateFile(originalFile, destination, translations);
}
private static Charset selectCharset(String projCharsetName)
{
Charset projCharset;
try
{
projCharset = Charset.forName(projCharsetName);
}
catch (UnsupportedCharsetException uce)
{
projCharset = StandardCharsets.UTF_8;
}
catch (IllegalCharsetNameException icne)
{
projCharset = StandardCharsets.UTF_8;
}
return projCharset;
}
}
top,
use,
map,
class ProjectUtils
. checkDebuggerState
. saveProjectCopy
. createSkeleton
. duplicate
. selectCharset
173 neLoCode
+ 17 LoComm