-
Notifications
You must be signed in to change notification settings - Fork 1
File Handling
Kango edited this page May 7, 2019
·
2 revisions
// http://forum.processing.org/topic/beginner-s-question-an-application-software-with-source-code-made-with-processing
// https://www.processing.org/reference/selectInput_.html
// https://docs.oracle.com/javase/7/docs/api/java/io/File.html
//
import java.io.File;
final String helpText =
"Commands: F2 save changes; F3 open; F9 open in Notepad; F10 New (deletes text); F12 Save as... new file; Backspace; Alt-F4 to quit.";
final color NORMAL_TEXT_COLOR = color (255); // white
final String EXTENSION = ".txt";
// current text
String input = "";
// file name as String
String currentFile = "";
// cursor sign blinks
boolean cursorBlinkFlag; // on/off
int timerCursorBlink; // timer
//---------------------------------------------------------------------------
// processing core functions
void setup() {
size(880, 800);
background(111); // gray
fill(NORMAL_TEXT_COLOR); // white
}
void draw() {
background(111); // gray
showHelpText();
text ("Current file : "
+ strTextOfCurrentFile(), 10, 50);
text (input
+ strBlink(), 10, 70);
}
//---------------------------------------------------------------------------
// Inputs
void keyPressed() {
// Is the key CODED?
if (key==CODED) {
// key is CODED --------------------------------------------
if ( keyCode == java.awt.event.KeyEvent.VK_F3 ) {
// open
load();
} else if ( keyCode == java.awt.event.KeyEvent.VK_F2 ) {
// save existing file
if ( !currentFile.equals("") ) {
// save existing file
saveExistingFileThatHasBeenChanged();
} else {
// no file to save to,
// save new File
save();
}
} else if ( keyCode == java.awt.event.KeyEvent.VK_F9 ) {
// open in notepad
openInNotepad();
} else if ( keyCode == java.awt.event.KeyEvent.VK_F10 ) {
// delete
input = "";
currentFile = "";
} else if ( keyCode == java.awt.event.KeyEvent.VK_F12 ) {
// save new File
save();
} else
{
// do nothing
}
//
} // if --------------------------------------------
else
{
// key is not CODED --------------------------------------------
if (key==BACKSPACE) {
// shorten
if (input.length()>0) {
input=input.substring(0, input.length()-1);
}
} else if (key==ESC) {
// kill Esc
key=0;
} else {
// add key (normal writing)
input+=key;
} // else
//
} // else // key is not CODED -----------------------------------------------
//
}
// ------------------------------------------------------
// Load
void load() {
String path1 = getDataPath();
File folderToStartFrom = new File(path1);
selectInput("Select a file to open", "fileSelectedOpen", folderToStartFrom);
}
void fileSelectedOpen(File selection) {
// the 'callback' function
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
currentFile=selection.getAbsolutePath();
input = join ( loadStrings( selection.getAbsolutePath() ), "\n" );
}//else
}
// ---------------------------------------------------------
// Save As...
void save() {
String path1 = getDataPath();
File folderToStartFrom = new File(path1); // ( dataPath("") + "//*" + EXTENSION );
selectOutput("Select a file to write to", "fileSelectedWrite", folderToStartFrom);
}
void fileSelectedWrite(File selection) {
// the 'callback' function
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
// Do we have a EXTENSION/".txt" at the end?
if (selection.getName().length() < 4 || selection.getName().indexOf(EXTENSION) != selection.getName().length()-4 ) {
// problem missing EXTENSION/".txt"
currentFile = selection.getAbsolutePath()+EXTENSION; // very rough approach...
} else {
currentFile = selection.getAbsolutePath();
}//else
String[] lines = new String[0];
lines = append ( lines, input );
// Writes the strings to a file, each on a separate line
saveStrings(currentFile, lines);
}
}
// ---------------------------------------------------------
// Save existing file
void saveExistingFileThatHasBeenChanged() {
// just using old name and save again
if (currentFile.equals(""))
return; // leave here
println("Save existing file ");
// Writes the strings to a file, each on a separate line (overwriting)
String[] lines = new String[0];
lines = append ( lines, input );
saveStrings( currentFile, lines );
}
// ---------------------------------------------------------
// open in notepad
void openInNotepad() {
// open in notepad
if (currentFile.equals("")) {
//return; // leave here (not necessary)
}
try
{
ProcessBuilder pb = new ProcessBuilder("notepad.exe", currentFile);
Process p = pb.start();
}
catch ( Exception /* IOException, URISyntaxException */ e )
{
e.printStackTrace();
}//catch
}
// ---------------------------------------------------------
// Tools
String getDataPath() {
// returns the data path. When folder "data" is not there, it returns the sketch path.
String path1=dataPath("") + "\\.";
File f1 = new File(path1);
if (!f1.exists()) {
// data path does not exist
println ("Make data path please. Using sketch folder instead. ");
path1=sketchPath("") + "*" + EXTENSION;
} else {
// data path does exist
path1=dataPath("") + "//*" + EXTENSION;
}
return path1;
}
String strTextOfCurrentFile() {
// shows either the path or "<Not a file>" when empty
if (currentFile.equals(""))
return "<Not a file>";
else
return currentFile;
}
String strBlink() {
// manage and show the Blinking Cursor |
// timer to toggle the flag cursorBlinkFlag
if (millis()-timerCursorBlink > 330) {
cursorBlinkFlag = !cursorBlinkFlag; // toggle
timerCursorBlink = millis(); // set timer
}//if
// return the line "|" or nothing
if (cursorBlinkFlag)
return"|";
else return"";
}//func
void showHelpText() {
// show help text
textAlign(RIGHT);
fill(255, 235, 49); // yellow
text(helpText, width, 30 );
// reset to normal
fill(NORMAL_TEXT_COLOR); // white
textAlign(LEFT);
}
//