Fixed imports

This commit is contained in:
Jeroen Ketelaar
2014-02-14 03:39:54 +01:00
parent 521af49010
commit d11849bebb
+126 -133
View File
@@ -1,161 +1,154 @@
package org.parabot.core; package org.parabot.core;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import javax.swing.JFileChooser;
import org.parabot.environment.OperatingSystem; import org.parabot.environment.OperatingSystem;
import javax.swing.*;
import java.io.File;
import java.util.*;
/** /**
*
* Holds parabot's used directories * Holds parabot's used directories
* *
* @author Everel * @author Everel
* @author Matt * @author Matt
*
*/ */
public class Directories { public class Directories {
private static Map<String, File> cached = new HashMap<String, File>(); private static Map<String, File> cached = new HashMap<String, File>();
static { static {
switch (OperatingSystem.getOS()) { switch (OperatingSystem.getOS()) {
case WINDOWS: case WINDOWS:
cached.put("Root", new JFileChooser().getFileSystemView().getDefaultDirectory()); cached.put("Root", new JFileChooser().getFileSystemView().getDefaultDirectory());
break; break;
default: default:
cached.put("Root", new File(System.getProperty("user.home"))); cached.put("Root", new File(System.getProperty("user.home")));
} }
Core.verbose("Caching directories..."); Core.verbose("Caching directories...");
cached.put("Root", getDefaultDirectory()); cached.put("Root", getDefaultDirectory());
cached.put("Workspace", new File(cached.get("Root"), "/Parabot/")); cached.put("Workspace", new File(cached.get("Root"), "/Parabot/"));
cached.put("Sources", new File(cached.get("Root"), "/Parabot/scripts/sources/")); cached.put("Sources", new File(cached.get("Root"), "/Parabot/scripts/sources/"));
cached.put("Compiled", new File(cached.get("Root"), "/Parabot/scripts/compiled/")); cached.put("Compiled", new File(cached.get("Root"), "/Parabot/scripts/compiled/"));
cached.put("Resources", new File(cached.get("Root"), "/Parabot/scripts/resources/")); cached.put("Resources", new File(cached.get("Root"), "/Parabot/scripts/resources/"));
cached.put("Settings", new File(cached.get("Root"), "/Parabot/settings/")); cached.put("Settings", new File(cached.get("Root"), "/Parabot/settings/"));
cached.put("Servers", new File(cached.get("Root"), "/Parabot/servers/")); cached.put("Servers", new File(cached.get("Root"), "/Parabot/servers/"));
cached.put("Cache", new File(cached.get("Root"), "/Parabot/cache/")); cached.put("Cache", new File(cached.get("Root"), "/Parabot/cache/"));
Core.verbose("Directories cached."); Core.verbose("Directories cached.");
} }
/** /**
* Returns the root directory outside of the main Parabot folder. * Returns the root directory outside of the main Parabot folder.
* *
* @return * @return
*/ */
public static File getDefaultDirectory() { public static File getDefaultDirectory() {
return cached.get("Root"); return cached.get("Root");
} }
/** /**
* Returns the Parabot folder. * Returns the Parabot folder.
* *
* @return * @return
*/ */
public static File getWorkspace() { public static File getWorkspace() {
return cached.get("Workspace"); return cached.get("Workspace");
} }
/** /**
* Returns the script sources folder. * Returns the script sources folder.
* *
* @return * @return
*/ */
public static File getScriptSourcesPath() { public static File getScriptSourcesPath() {
return cached.get("Sources"); return cached.get("Sources");
} }
/** /**
* Returns the compiled scripts folder. * Returns the compiled scripts folder.
* *
* @return * @return
*/ */
public static File getScriptCompiledPath() { public static File getScriptCompiledPath() {
return cached.get("Compiled"); return cached.get("Compiled");
} }
/** /**
* Returns the scripts resources folder. * Returns the scripts resources folder.
* *
* @return * @return
*/ */
public static File getResourcesPath() { public static File getResourcesPath() {
return cached.get("Resources"); return cached.get("Resources");
} }
/** /**
* Returns the Parabot settings folder. * Returns the Parabot settings folder.
* *
* @return * @return
*/ */
public static File getSettingsPath() { public static File getSettingsPath() {
return cached.get("Settings"); return cached.get("Settings");
} }
/** /**
* Returns the Parabot servers folder. * Returns the Parabot servers folder.
* *
* @return * @return
*/ */
public static File getServerPath() { public static File getServerPath() {
return cached.get("Servers"); return cached.get("Servers");
} }
/** /**
* Returns the Parabot cache folder. * Returns the Parabot cache folder.
* *
* @return * @return
*/ */
public static File getCachePath() { public static File getCachePath() {
return cached.get("Cache"); return cached.get("Cache");
} }
/** /**
* Validates all directories and makes them if necessary * Validates all directories and makes them if necessary
*/ */
public static void validate() { public static void validate() {
final File defaultPath = getDefaultDirectory(); final File defaultPath = getDefaultDirectory();
if (defaultPath == null || !defaultPath.exists()) { if (defaultPath == null || !defaultPath.exists()) {
throw new RuntimeException("Default path not found"); throw new RuntimeException("Default path not found");
} }
final Queue<File> files = new LinkedList<File>(); final Queue<File> files = new LinkedList<File>();
files.add(getWorkspace()); files.add(getWorkspace());
files.add(getServerPath()); files.add(getServerPath());
files.add(getSettingsPath()); files.add(getSettingsPath());
files.add(getScriptSourcesPath()); files.add(getScriptSourcesPath());
files.add(getScriptCompiledPath()); files.add(getScriptCompiledPath());
files.add(getResourcesPath()); files.add(getResourcesPath());
files.add(getCachePath()); files.add(getCachePath());
while (files.size() > 0) { while (files.size() > 0) {
final File file = files.poll(); final File file = files.poll();
if (!file.exists()) { if (!file.exists()) {
Core.verbose("Generating directory: " + file.getAbsolutePath()); Core.verbose("Generating directory: " + file.getAbsolutePath());
file.mkdirs(); file.mkdirs();
if(!file.exists()) { if (!file.exists()) {
System.err.println("Failed to make directory: " + file.getAbsolutePath()); System.err.println("Failed to make directory: " + file.getAbsolutePath());
} }
} }
} }
} }
private static File temp = null; private static File temp = null;
public static File getTempDirectory() { public static File getTempDirectory() {
if (temp != null) { if (temp != null) {
return temp; return temp;
} }
int randomNum = new Random().nextInt(999999999); int randomNum = new Random().nextInt(999999999);
temp = new File(getResourcesPath(), randomNum + "/"); temp = new File(getResourcesPath(), randomNum + "/");
temp.mkdirs(); temp.mkdirs();
temp.deleteOnExit(); temp.deleteOnExit();
return temp; return temp;
} }
} }