Skip to content

Commit 9f1b575

Browse files
committed
cleaning up Base to make accessors fair game
1 parent 77d3675 commit 9f1b575

File tree

11 files changed

+253
-162
lines changed

11 files changed

+253
-162
lines changed

app/src/processing/app/Base.java

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class Base {
5454
// so that the errors while building don't show up again.
5555
boolean builtOnce;
5656

57+
static File buildFolder;
58+
5759
// these are static because they're used by Sketch
5860
static private File examplesFolder;
5961
static private File librariesFolder;
@@ -210,7 +212,7 @@ public Base(String[] args) {
210212
* The complement to "storePreferences", this is called when the
211213
* application is first launched.
212214
*/
213-
public boolean restoreSketches() {
215+
protected boolean restoreSketches() {
214216
// figure out window placement
215217

216218
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
@@ -265,7 +267,7 @@ public boolean restoreSketches() {
265267
* Store list of sketches that are currently open.
266268
* Called when the application is quitting and documents are still open.
267269
*/
268-
public void storeSketches() {
270+
protected void storeSketches() {
269271
// Save the width and height of the screen
270272
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
271273
Preferences.setInteger("last.screen.width", screen.width);
@@ -328,7 +330,7 @@ public void storeSketch(Editor editor) {
328330
// changes to the focused and active Windows can be made. Developers must
329331
// never assume that this Window is the focused or active Window until this
330332
// Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event.
331-
public void handleActivated(Editor whichEditor) {
333+
protected void handleActivated(Editor whichEditor) {
332334
activeEditor = whichEditor;
333335

334336
// set the current window to be the console that's getting output
@@ -401,6 +403,9 @@ protected String createNewUntitled() throws IOException {
401403
}
402404

403405

406+
/**
407+
* Create a new untitled document in a new sketch window.
408+
*/
404409
public void handleNew() {
405410
try {
406411
String path = createNewUntitled();
@@ -415,6 +420,9 @@ public void handleNew() {
415420
}
416421

417422

423+
/**
424+
* Replace the sketch in the current window with a new untitled document.
425+
*/
418426
public void handleNewReplace() {
419427
if (!activeEditor.checkModified(false)) {
420428
return; // sketch was modified, and user canceled
@@ -440,6 +448,10 @@ protected void handleNewReplaceImpl() {
440448
}
441449

442450

451+
/**
452+
* Open a sketch, replacing the sketch in the current window.
453+
* @param path Location of the primary pde file for the sketch.
454+
*/
443455
public void handleOpenReplace(String path) {
444456
if (!activeEditor.checkModified(false)) {
445457
return; // sketch was modified, and user canceled
@@ -455,6 +467,9 @@ public void handleOpenReplace(String path) {
455467
}
456468

457469

470+
/**
471+
* Prompt for a sketch to open, and open it in a new window.
472+
*/
458473
public void handleOpenPrompt() {
459474
// get the frontmost window frame for placing file dialog
460475
FileDialog fd = new FileDialog(activeEditor,
@@ -487,7 +502,8 @@ public boolean accept(File dir, String name) {
487502

488503

489504
/**
490-
* @param path Path to the .pde file for the sketch in question
505+
* Open a sketch in a new window.
506+
* @param path Path to the pde file for the sketch in question
491507
* @return the Editor object, so that properties (like 'untitled')
492508
* can be set by the caller
493509
*/
@@ -496,7 +512,7 @@ public Editor handleOpen(String path) {
496512
}
497513

498514

499-
public Editor handleOpen(String path, int[] location) {
515+
protected Editor handleOpen(String path, int[] location) {
500516
File file = new File(path);
501517
if (!file.exists()) return null;
502518

@@ -552,6 +568,12 @@ public Editor handleOpen(String path, int[] location) {
552568
}
553569

554570

571+
/**
572+
* Close a sketch as specified by its editor window.
573+
* @param editor Editor object of the sketch to be closed.
574+
* @param quitting True if this is being called by File → Quit.
575+
* @return true if succeeded in closing, false if canceled.
576+
*/
555577
public boolean handleClose(Editor editor, boolean quitting) {
556578
// Check if modified
557579
if (!editor.checkModified(quitting)) { //false)) { // was false in 0126
@@ -616,6 +638,10 @@ public boolean handleClose(Editor editor, boolean quitting) {
616638
}
617639

618640

641+
/**
642+
* Handler for File → Quit.
643+
* @return false if canceled, true otherwise.
644+
*/
619645
public boolean handleQuit() {
620646
// If quit is canceled, this will be replaced anyway
621647
// by a later handleQuit() that is not canceled.
@@ -925,7 +951,7 @@ public void actionPerformed(ActionEvent e) {
925951
* Show the About box.
926952
*/
927953
public void handleAbout() {
928-
final Image image = Base.getImage("about.jpg", activeEditor);
954+
final Image image = Base.getLibImage("about.jpg", activeEditor);
929955
final Window window = new Window(activeEditor) {
930956
public void paint(Graphics g) {
931957
g.drawImage(image, 0, 0, null);
@@ -953,7 +979,7 @@ public void mousePressed(MouseEvent e) {
953979

954980

955981
/**
956-
* Show the preferences window.
982+
* Show the Preferences window.
957983
*/
958984
public void handlePrefs() {
959985
if (preferencesFrame == null) preferencesFrame = new Preferences();
@@ -965,8 +991,7 @@ public void handlePrefs() {
965991

966992

967993
/**
968-
* returns true if Processing is running on a Mac OS machine,
969-
* specifically a Mac OS X machine because it doesn't un on OS 9 anymore.
994+
* returns true if Processing is running on a Mac OS X machine.
970995
*/
971996
static public boolean isMacOS() {
972997
return PApplet.platform == PConstants.MACOSX;
@@ -1021,17 +1046,17 @@ static public File getSettingsFolder() {
10211046

10221047

10231048
/**
1049+
* Convenience method to get a File object for the specified filename inside
1050+
* the settings folder.
10241051
* For now, only used by Preferences to get the preferences.txt file.
1025-
* @param filename
1026-
* @return
1052+
* @param filename A file inside the settings folder.
1053+
* @return filename wrapped as a File object inside the settings folder
10271054
*/
10281055
static public File getSettingsFile(String filename) {
10291056
return new File(getSettingsFolder(), filename);
10301057
}
10311058

10321059

1033-
static File buildFolder;
1034-
10351060
static public File getBuildFolder() {
10361061
if (buildFolder == null) {
10371062
String buildPath = Preferences.get("build.path");
@@ -1169,7 +1194,7 @@ static public void openURL(String url) {
11691194
* Used to determine whether to disable the "Show Sketch Folder" option.
11701195
* @return true If a means of opening a folder is known to be available.
11711196
*/
1172-
static boolean openFolderAvailable() {
1197+
static protected boolean openFolderAvailable() {
11731198
return platform.openFolderAvailable();
11741199
}
11751200

@@ -1193,6 +1218,7 @@ static public void openFolder(File file) {
11931218

11941219

11951220
/**
1221+
* Prompt for a fodler and return it as a File object (or null).
11961222
* Implementation for choosing directories that handles both the
11971223
* Mac OS X hack to allow the native AWT file dialog, or uses
11981224
* the JFileChooser on other platforms. Mac AWT trick obtained from
@@ -1236,11 +1262,14 @@ static public File selectFolder(String prompt, File folder, Frame frame) {
12361262
// .................................................................
12371263

12381264

1265+
/**
1266+
* Give this Frame a Processing icon.
1267+
*/
12391268
static public void setIcon(Frame frame) {
12401269
// set the window icon
12411270
if (icon == null) {
12421271
try {
1243-
icon = Base.getImage("icon.gif", frame);
1272+
icon = Base.getLibImage("icon.gif", frame);
12441273
} catch (Exception e) { } // fail silently, no big whup
12451274
}
12461275
if (icon != null) {
@@ -1299,7 +1328,7 @@ static public void registerWindowCloseKeys(JRootPane root, //Window window,
12991328

13001329

13011330
static public void showReference(String referenceFile) {
1302-
openURL(Base.getContents("reference" + File.separator + referenceFile));
1331+
openURL(Base.getContentsPath("reference" + File.separator + referenceFile));
13031332
}
13041333

13051334

@@ -1370,24 +1399,28 @@ static public void showError(String title, String message,
13701399
// ...................................................................
13711400

13721401

1373-
// "contents" refers to the Mac OS X style way of handling Processing
1374-
// applications.
1375-
1376-
1377-
static public String getContents(String what) {
1402+
/**
1403+
* Retrieve a path to something in the Processing folder. Eventually this
1404+
* may refer to the Contents subfolder of Processing.app, if we bundle things
1405+
* up as a single .app file with no additional folders.
1406+
*/
1407+
static public String getContentsPath(String filename) {
13781408
String basePath = System.getProperty("user.dir");
13791409
/*
13801410
// do this later, when moving to .app package
13811411
if (PApplet.platform == PConstants.MACOSX) {
13821412
basePath = System.getProperty("processing.contents");
13831413
}
13841414
*/
1385-
return basePath + File.separator + what;
1415+
return basePath + File.separator + filename;
13861416
}
13871417

13881418

1389-
static public String getLibContents(String what) {
1390-
String libPath = getContents("lib/" + what);
1419+
/**
1420+
* Get a path for something in the Processing lib folder.
1421+
*/
1422+
static public String getLibContentsPath(String filename) {
1423+
String libPath = getContentsPath("lib/" + filename);
13911424
File libDir = new File(libPath);
13921425
if (libDir.exists()) {
13931426
return libPath;
@@ -1402,11 +1435,14 @@ static public String getLibContents(String what) {
14021435
}
14031436

14041437

1405-
static public Image getImage(String name, Component who) {
1438+
/**
1439+
* Return an Image object from inside the Processing lib folder.
1440+
*/
1441+
static public Image getLibImage(String name, Component who) {
14061442
Image image = null;
14071443
Toolkit tk = Toolkit.getDefaultToolkit();
14081444

1409-
image = tk.getImage(getLibContents(name));
1445+
image = tk.getImage(getLibContentsPath(name));
14101446
MediaTracker tracker = new MediaTracker(who);
14111447
tracker.addImage(image, 0);
14121448
try {
@@ -1416,8 +1452,11 @@ static public Image getImage(String name, Component who) {
14161452
}
14171453

14181454

1455+
/**
1456+
* Return an InputStream for a file inside the Processing lib folder.
1457+
*/
14191458
static public InputStream getStream(String filename) throws IOException {
1420-
return new FileInputStream(getLibContents(filename));
1459+
return new FileInputStream(getLibContentsPath(filename));
14211460
}
14221461

14231462

@@ -1443,9 +1482,12 @@ static public byte[] loadBytesRaw(File file) throws IOException {
14431482
}
14441483

14451484

1446-
static public void copyFile(File afile, File bfile) throws IOException {
1447-
InputStream from = new BufferedInputStream(new FileInputStream(afile));
1448-
OutputStream to = new BufferedOutputStream(new FileOutputStream(bfile));
1485+
static public void copyFile(File sourceFile,
1486+
File targetFile) throws IOException {
1487+
InputStream from =
1488+
new BufferedInputStream(new FileInputStream(sourceFile));
1489+
OutputStream to =
1490+
new BufferedOutputStream(new FileOutputStream(targetFile));
14491491
byte[] buffer = new byte[16 * 1024];
14501492
int bytesRead;
14511493
while ((bytesRead = from.read(buffer)) != -1) {
@@ -1457,17 +1499,17 @@ static public void copyFile(File afile, File bfile) throws IOException {
14571499
to.close(); // ??
14581500
to = null;
14591501

1460-
bfile.setLastModified(afile.lastModified()); // jdk13+ required
1461-
//} catch (IOException e) {
1462-
// e.printStackTrace();
1463-
//}
1502+
targetFile.setLastModified(sourceFile.lastModified());
14641503
}
14651504

14661505

14671506
/**
14681507
* Grab the contents of a file as a string.
14691508
*/
14701509
static public String loadFile(File file) throws IOException {
1510+
return PApplet.join(PApplet.loadStrings(file), "\n");
1511+
1512+
/*
14711513
// empty code file.. no worries, might be getting filled up later
14721514
if (file.length() == 0) return "";
14731515
@@ -1490,13 +1532,16 @@ static public String loadFile(File file) throws IOException {
14901532
}
14911533
reader.close();
14921534
return buffer.toString();
1535+
*/
14931536
}
14941537

14951538

14961539
/**
14971540
* Spew the contents of a String object out to a file.
14981541
*/
14991542
static public void saveFile(String str, File file) throws IOException {
1543+
PApplet.saveStrings(file, new String[] { str });
1544+
/*
15001545
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
15011546
InputStreamReader isr = new InputStreamReader(bis);
15021547
BufferedReader reader = new BufferedReader(isr);
@@ -1511,6 +1556,7 @@ static public void saveFile(String str, File file) throws IOException {
15111556
}
15121557
writer.flush();
15131558
writer.close();
1559+
*/
15141560
}
15151561

15161562

@@ -1601,8 +1647,8 @@ static public int calcFolderSize(File folder) {
16011647

16021648

16031649
/**
1604-
* Gets a list of all files within the specified folder,
1605-
* and returns a list of their relative paths.
1650+
* Recursively creates a list of all files within the specified folder,
1651+
* and returns a list of their relative paths.
16061652
* Ignores any files/folders prefixed with a dot.
16071653
*/
16081654
static public String[] listFiles(String path, boolean relative) {

0 commit comments

Comments
 (0)