1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

Removed dependency from Base in PreferencesData.

This commit is contained in:
Claudio Indellicati
2014-08-21 19:47:33 +02:00
committed by Cristian Maglie
parent fa0d37dad6
commit d6bd77ec2b
3 changed files with 85 additions and 51 deletions

View File

@ -36,6 +36,7 @@ import processing.app.debug.TargetBoard;
import processing.app.debug.TargetPackage; import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform; import processing.app.debug.TargetPlatform;
import processing.app.helpers.FileUtils; import processing.app.helpers.FileUtils;
import processing.app.helpers.GUINotifier;
import processing.app.helpers.OSUtils; import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMap; import processing.app.helpers.PreferencesMap;
import processing.app.helpers.filefilters.OnlyDirs; import processing.app.helpers.filefilters.OnlyDirs;
@ -104,6 +105,8 @@ public class Base {
static public void main(String args[]) throws Exception { static public void main(String args[]) throws Exception {
BaseNoGui.initLogger(); BaseNoGui.initLogger();
BaseNoGui.notifier = new GUINotifier();
initPlatform(); initPlatform();
@ -1850,33 +1853,7 @@ public class Base {
static public File getSettingsFolder() { static public File getSettingsFolder() {
if (BaseNoGui.getPortableFolder() != null) return BaseNoGui.getSettingsFolder();
return BaseNoGui.getPortableFolder();
File settingsFolder = null;
String preferencesPath = Preferences.get("settings.path");
if (preferencesPath != null) {
settingsFolder = absoluteFile(preferencesPath);
} else {
try {
settingsFolder = getPlatform().getSettingsFolder();
} catch (Exception e) {
showError(_("Problem getting data folder"),
_("Error getting the Arduino data folder."), e);
}
}
// create the folder if it doesn't exist already
if (!settingsFolder.exists()) {
if (!settingsFolder.mkdirs()) {
showError(_("Settings issues"),
_("Arduino cannot run because it could not\n" +
"create a folder to store your settings."), null);
}
}
return settingsFolder;
} }
@ -1888,7 +1865,7 @@ public class Base {
* @return filename wrapped as a File object inside the settings folder * @return filename wrapped as a File object inside the settings folder
*/ */
static public File getSettingsFile(String filename) { static public File getSettingsFile(String filename) {
return new File(getSettingsFolder(), filename); return BaseNoGui.getSettingsFile(filename);
} }
@ -2323,17 +2300,7 @@ public class Base {
* for errors that allow P5 to continue running. * for errors that allow P5 to continue running.
*/ */
static public void showError(String title, String message, Throwable e, int exit_code) { static public void showError(String title, String message, Throwable e, int exit_code) {
if (title == null) title = _("Error"); BaseNoGui.showError(title, message, e, exit_code);
if (commandLine) {
System.err.println(title + ": " + message);
} else {
JOptionPane.showMessageDialog(new Frame(), message, title,
JOptionPane.ERROR_MESSAGE);
}
if (e != null) e.printStackTrace();
System.exit(exit_code);
} }
@ -2535,7 +2502,7 @@ public class Base {
* Return an InputStream for a file inside the Processing lib folder. * Return an InputStream for a file inside the Processing lib folder.
*/ */
static public InputStream getLibStream(String filename) throws IOException { static public InputStream getLibStream(String filename) throws IOException {
return new FileInputStream(new File(getContentFile("lib"), filename)); return BaseNoGui.getLibStream(filename);
} }

View File

@ -3,7 +3,9 @@ package processing.app;
import static processing.app.I18n._; import static processing.app.I18n._;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -19,8 +21,10 @@ import processing.app.debug.TargetBoard;
import processing.app.debug.TargetPackage; import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform; import processing.app.debug.TargetPlatform;
import processing.app.debug.TargetPlatformException; import processing.app.debug.TargetPlatformException;
import processing.app.helpers.BasicNotifier;
import processing.app.helpers.OSUtils; import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMap; import processing.app.helpers.PreferencesMap;
import processing.app.helpers.UserNotifier;
import processing.app.helpers.filefilters.OnlyDirs; import processing.app.helpers.filefilters.OnlyDirs;
import processing.app.helpers.filefilters.OnlyFilesWithExtension; import processing.app.helpers.filefilters.OnlyFilesWithExtension;
import processing.app.legacy.PApplet; import processing.app.legacy.PApplet;
@ -45,6 +49,8 @@ public class BaseNoGui {
// maps library name to their library folder // maps library name to their library folder
static private LibraryList libraries; static private LibraryList libraries;
static UserNotifier notifier = new BasicNotifier();
static public Map<String, TargetPackage> packages; static public Map<String, TargetPackage> packages;
static Platform platform; static Platform platform;
@ -126,6 +132,13 @@ public class BaseNoGui {
return libraries; return libraries;
} }
/**
* Return an InputStream for a file inside the Processing lib folder.
*/
static public InputStream getLibStream(String filename) throws IOException {
return new FileInputStream(new File(getContentFile("lib"), filename));
}
static public Platform getPlatform() { static public Platform getPlatform() {
return platform; return platform;
} }
@ -134,6 +147,47 @@ public class BaseNoGui {
return portableFolder; return portableFolder;
} }
/**
* Convenience method to get a File object for the specified filename inside
* the settings folder.
* For now, only used by Preferences to get the preferences.txt file.
* @param filename A file inside the settings folder.
* @return filename wrapped as a File object inside the settings folder
*/
static public File getSettingsFile(String filename) {
return new File(getSettingsFolder(), filename);
}
static public File getSettingsFolder() {
if (BaseNoGui.getPortableFolder() != null)
return BaseNoGui.getPortableFolder();
File settingsFolder = null;
String preferencesPath = Preferences.get("settings.path");
if (preferencesPath != null) {
settingsFolder = absoluteFile(preferencesPath);
} else {
try {
settingsFolder = getPlatform().getSettingsFolder();
} catch (Exception e) {
showError(_("Problem getting data folder"),
_("Error getting the Arduino data folder."), e);
}
}
// create the folder if it doesn't exist already
if (!settingsFolder.exists()) {
if (!settingsFolder.mkdirs()) {
showError(_("Settings issues"),
_("Arduino cannot run because it could not\n" +
"create a folder to store your settings."), null);
}
}
return settingsFolder;
}
static public File getSketchbookFolder() { static public File getSketchbookFolder() {
if (portableFolder != null) if (portableFolder != null)
return new File(portableFolder, Preferences.get("sketchbook.path")); return new File(portableFolder, Preferences.get("sketchbook.path"));
@ -376,4 +430,17 @@ public class BaseNoGui {
return res; return res;
} }
static public void showError(String title, String message, Throwable e) {
notifier.showError(title, message, e, 1);
}
/**
* Show an error message that's actually fatal to the program.
* This is an error that can't be recovered. Use showWarning()
* for errors that allow P5 to continue running.
*/
static public void showError(String title, String message, Throwable e, int exit_code) {
notifier.showError(title, message, e, exit_code);
}
} }

View File

@ -32,21 +32,21 @@ public class PreferencesData {
if (file != null) if (file != null)
preferencesFile = file; preferencesFile = file;
else else
preferencesFile = Base.getSettingsFile(Preferences.PREFS_FILE); preferencesFile = BaseNoGui.getSettingsFile(PREFS_FILE);
// start by loading the defaults, in case something // start by loading the defaults, in case something
// important was deleted from the user prefs // important was deleted from the user prefs
try { try {
prefs.load(Base.getLibStream("preferences.txt")); prefs.load(BaseNoGui.getLibStream("preferences.txt"));
} catch (IOException e) { } catch (IOException e) {
Base.showError(null, _("Could not read default settings.\n" + BaseNoGui.showError(null, _("Could not read default settings.\n" +
"You'll need to reinstall Arduino."), e); "You'll need to reinstall Arduino."), e);
} }
// set some runtime constants (not saved on preferences file) // set some runtime constants (not saved on preferences file)
File hardwareFolder = Base.getHardwareFolder(); File hardwareFolder = BaseNoGui.getHardwareFolder();
prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath()); prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
prefs.put("runtime.ide.version", "" + Base.REVISION); prefs.put("runtime.ide.version", "" + BaseNoGui.REVISION);
// clone the hash table // clone the hash table
defaults = new PreferencesMap(prefs); defaults = new PreferencesMap(prefs);
@ -56,11 +56,11 @@ public class PreferencesData {
try { try {
prefs.load(preferencesFile); prefs.load(preferencesFile);
} catch (IOException ex) { } catch (IOException ex) {
Base.showError(_("Error reading preferences"), BaseNoGui.showError(_("Error reading preferences"),
I18n.format(_("Error reading the preferences file. " I18n.format(_("Error reading the preferences file. "
+ "Please delete (or move)\n" + "Please delete (or move)\n"
+ "{0} and restart Arduino."), + "{0} and restart Arduino."),
preferencesFile.getAbsolutePath()), ex); preferencesFile.getAbsolutePath()), ex);
} }
} }