Skip to content

Commit d6bd77e

Browse files
bitroncmaglie
authored andcommitted
Removed dependency from Base in PreferencesData.
1 parent fa0d37d commit d6bd77e

File tree

3 files changed

+85
-51
lines changed

3 files changed

+85
-51
lines changed

app/src/processing/app/Base.java

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import processing.app.debug.TargetPackage;
3737
import processing.app.debug.TargetPlatform;
3838
import processing.app.helpers.FileUtils;
39+
import processing.app.helpers.GUINotifier;
3940
import processing.app.helpers.OSUtils;
4041
import processing.app.helpers.PreferencesMap;
4142
import processing.app.helpers.filefilters.OnlyDirs;
@@ -104,6 +105,8 @@ public class Base {
104105

105106
static public void main(String args[]) throws Exception {
106107
BaseNoGui.initLogger();
108+
109+
BaseNoGui.notifier = new GUINotifier();
107110

108111
initPlatform();
109112

@@ -1850,33 +1853,7 @@ static public String getPlatformName() {
18501853

18511854

18521855
static public File getSettingsFolder() {
1853-
if (BaseNoGui.getPortableFolder() != null)
1854-
return BaseNoGui.getPortableFolder();
1855-
1856-
File settingsFolder = null;
1857-
1858-
String preferencesPath = Preferences.get("settings.path");
1859-
if (preferencesPath != null) {
1860-
settingsFolder = absoluteFile(preferencesPath);
1861-
1862-
} else {
1863-
try {
1864-
settingsFolder = getPlatform().getSettingsFolder();
1865-
} catch (Exception e) {
1866-
showError(_("Problem getting data folder"),
1867-
_("Error getting the Arduino data folder."), e);
1868-
}
1869-
}
1870-
1871-
// create the folder if it doesn't exist already
1872-
if (!settingsFolder.exists()) {
1873-
if (!settingsFolder.mkdirs()) {
1874-
showError(_("Settings issues"),
1875-
_("Arduino cannot run because it could not\n" +
1876-
"create a folder to store your settings."), null);
1877-
}
1878-
}
1879-
return settingsFolder;
1856+
return BaseNoGui.getSettingsFolder();
18801857
}
18811858

18821859

@@ -1888,7 +1865,7 @@ static public File getSettingsFolder() {
18881865
* @return filename wrapped as a File object inside the settings folder
18891866
*/
18901867
static public File getSettingsFile(String filename) {
1891-
return new File(getSettingsFolder(), filename);
1868+
return BaseNoGui.getSettingsFile(filename);
18921869
}
18931870

18941871

@@ -2323,17 +2300,7 @@ static public void showError(String title, String message, int exit_code) {
23232300
* for errors that allow P5 to continue running.
23242301
*/
23252302
static public void showError(String title, String message, Throwable e, int exit_code) {
2326-
if (title == null) title = _("Error");
2327-
2328-
if (commandLine) {
2329-
System.err.println(title + ": " + message);
2330-
2331-
} else {
2332-
JOptionPane.showMessageDialog(new Frame(), message, title,
2333-
JOptionPane.ERROR_MESSAGE);
2334-
}
2335-
if (e != null) e.printStackTrace();
2336-
System.exit(exit_code);
2303+
BaseNoGui.showError(title, message, e, exit_code);
23372304
}
23382305

23392306

@@ -2535,7 +2502,7 @@ static public Image getLibImage(String name, Component who) {
25352502
* Return an InputStream for a file inside the Processing lib folder.
25362503
*/
25372504
static public InputStream getLibStream(String filename) throws IOException {
2538-
return new FileInputStream(new File(getContentFile("lib"), filename));
2505+
return BaseNoGui.getLibStream(filename);
25392506
}
25402507

25412508

app/src/processing/app/BaseNoGui.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import static processing.app.I18n._;
44

55
import java.io.File;
6+
import java.io.FileInputStream;
67
import java.io.IOException;
8+
import java.io.InputStream;
79
import java.util.Arrays;
810
import java.util.Date;
911
import java.util.HashMap;
@@ -19,8 +21,10 @@
1921
import processing.app.debug.TargetPackage;
2022
import processing.app.debug.TargetPlatform;
2123
import processing.app.debug.TargetPlatformException;
24+
import processing.app.helpers.BasicNotifier;
2225
import processing.app.helpers.OSUtils;
2326
import processing.app.helpers.PreferencesMap;
27+
import processing.app.helpers.UserNotifier;
2428
import processing.app.helpers.filefilters.OnlyDirs;
2529
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
2630
import processing.app.legacy.PApplet;
@@ -45,6 +49,8 @@ public class BaseNoGui {
4549
// maps library name to their library folder
4650
static private LibraryList libraries;
4751

52+
static UserNotifier notifier = new BasicNotifier();
53+
4854
static public Map<String, TargetPackage> packages;
4955

5056
static Platform platform;
@@ -126,6 +132,13 @@ static public LibraryList getLibraries() {
126132
return libraries;
127133
}
128134

135+
/**
136+
* Return an InputStream for a file inside the Processing lib folder.
137+
*/
138+
static public InputStream getLibStream(String filename) throws IOException {
139+
return new FileInputStream(new File(getContentFile("lib"), filename));
140+
}
141+
129142
static public Platform getPlatform() {
130143
return platform;
131144
}
@@ -134,6 +147,47 @@ static public File getPortableFolder() {
134147
return portableFolder;
135148
}
136149

150+
/**
151+
* Convenience method to get a File object for the specified filename inside
152+
* the settings folder.
153+
* For now, only used by Preferences to get the preferences.txt file.
154+
* @param filename A file inside the settings folder.
155+
* @return filename wrapped as a File object inside the settings folder
156+
*/
157+
static public File getSettingsFile(String filename) {
158+
return new File(getSettingsFolder(), filename);
159+
}
160+
161+
static public File getSettingsFolder() {
162+
if (BaseNoGui.getPortableFolder() != null)
163+
return BaseNoGui.getPortableFolder();
164+
165+
File settingsFolder = null;
166+
167+
String preferencesPath = Preferences.get("settings.path");
168+
if (preferencesPath != null) {
169+
settingsFolder = absoluteFile(preferencesPath);
170+
171+
} else {
172+
try {
173+
settingsFolder = getPlatform().getSettingsFolder();
174+
} catch (Exception e) {
175+
showError(_("Problem getting data folder"),
176+
_("Error getting the Arduino data folder."), e);
177+
}
178+
}
179+
180+
// create the folder if it doesn't exist already
181+
if (!settingsFolder.exists()) {
182+
if (!settingsFolder.mkdirs()) {
183+
showError(_("Settings issues"),
184+
_("Arduino cannot run because it could not\n" +
185+
"create a folder to store your settings."), null);
186+
}
187+
}
188+
return settingsFolder;
189+
}
190+
137191
static public File getSketchbookFolder() {
138192
if (portableFolder != null)
139193
return new File(portableFolder, Preferences.get("sketchbook.path"));
@@ -376,4 +430,17 @@ static public LibraryList scanLibraries(File folder) throws IOException {
376430
return res;
377431
}
378432

433+
static public void showError(String title, String message, Throwable e) {
434+
notifier.showError(title, message, e, 1);
435+
}
436+
437+
/**
438+
* Show an error message that's actually fatal to the program.
439+
* This is an error that can't be recovered. Use showWarning()
440+
* for errors that allow P5 to continue running.
441+
*/
442+
static public void showError(String title, String message, Throwable e, int exit_code) {
443+
notifier.showError(title, message, e, exit_code);
444+
}
445+
379446
}

app/src/processing/app/PreferencesData.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ static public void init(File file) {
3232
if (file != null)
3333
preferencesFile = file;
3434
else
35-
preferencesFile = Base.getSettingsFile(Preferences.PREFS_FILE);
35+
preferencesFile = BaseNoGui.getSettingsFile(PREFS_FILE);
3636

3737
// start by loading the defaults, in case something
3838
// important was deleted from the user prefs
3939
try {
40-
prefs.load(Base.getLibStream("preferences.txt"));
40+
prefs.load(BaseNoGui.getLibStream("preferences.txt"));
4141
} catch (IOException e) {
42-
Base.showError(null, _("Could not read default settings.\n" +
43-
"You'll need to reinstall Arduino."), e);
42+
BaseNoGui.showError(null, _("Could not read default settings.\n" +
43+
"You'll need to reinstall Arduino."), e);
4444
}
4545

4646
// set some runtime constants (not saved on preferences file)
47-
File hardwareFolder = Base.getHardwareFolder();
47+
File hardwareFolder = BaseNoGui.getHardwareFolder();
4848
prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
49-
prefs.put("runtime.ide.version", "" + Base.REVISION);
49+
prefs.put("runtime.ide.version", "" + BaseNoGui.REVISION);
5050

5151
// clone the hash table
5252
defaults = new PreferencesMap(prefs);
@@ -56,11 +56,11 @@ static public void init(File file) {
5656
try {
5757
prefs.load(preferencesFile);
5858
} catch (IOException ex) {
59-
Base.showError(_("Error reading preferences"),
60-
I18n.format(_("Error reading the preferences file. "
61-
+ "Please delete (or move)\n"
62-
+ "{0} and restart Arduino."),
63-
preferencesFile.getAbsolutePath()), ex);
59+
BaseNoGui.showError(_("Error reading preferences"),
60+
I18n.format(_("Error reading the preferences file. "
61+
+ "Please delete (or move)\n"
62+
+ "{0} and restart Arduino."),
63+
preferencesFile.getAbsolutePath()), ex);
6464
}
6565
}
6666

0 commit comments

Comments
 (0)