implemented code functions: new, rename, add

This commit is contained in:
benfry
2004-06-22 16:15:59 +00:00
parent 6c7b333c2e
commit 3bbfd8bc57
4 changed files with 284 additions and 60 deletions

View File

@@ -291,6 +291,8 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ {
Reset file list (not needed?)
*/
//public JMenu rebuildMenu() {
//JMenuItem newItem;
public void rebuildMenu() {
if (menu != null) {
menu.removeAll();
@@ -313,18 +315,33 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ {
}
JMenuItem item;
item = new JMenuItem("New");
item = PdeEditor.newJMenuItem("New", 'T');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("TODO write code for New");
//System.out.println("TODO write code for New");
editor.sketch.newCode();
}
});
menu.add(item);
/*
if (newItem == null) {
newItem = PdeEditor.newJMenuItem("New", 'T');
newItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println("TODO write code for New");
editor.sketch.newCode();
}
});
}
System.out.println("adding new");
menu.add(newItem);
*/
item = new JMenuItem("Rename");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("TODO write code for Rename");
editor.sketch.renameCode();
}
});
menu.add(item);
@@ -332,7 +349,7 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ {
item = new JMenuItem("Delete");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("TODO write code for Delete");
editor.sketch.deleteCode();
}
});
menu.add(item);
@@ -340,8 +357,7 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ {
item = new JMenuItem("Hide");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// don't let the user hide if only 1 file open
System.out.println("TODO write code for Hide");
editor.sketch.hideCode();
}
});
menu.add(item);
@@ -349,7 +365,7 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ {
JMenu unhide = new JMenu("Unhide");
ActionListener unhideListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.sketch.unhide((String) (e.getActionCommand()));
editor.sketch.unhideCode((String) (e.getActionCommand()));
rebuildMenu();
}
};
@@ -375,7 +391,8 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ {
ActionListener jumpListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println("jump to " + e.getActionCommand());
System.out.println("jump to " + e);
//System.out.println("jump to " + e);
editor.sketch.setCurrent(e.getActionCommand());
}
};
for (int i = 0; i < sketch.codeCount; i++) {

View File

@@ -284,13 +284,14 @@ public class PdeEditorStatus extends JPanel implements ActionListener {
// KeyEvent.VK_SPACE);
int c = event.getKeyChar();
/*if (c == KeyEvent.VK_ENTER) { // accept the input
editor.handleSaveAs2(editField.getText());
unedit();
if (c == KeyEvent.VK_ENTER) { // accept the input
String answer = editField.getText();
editor.sketch.nameCode(answer);
unedit();
event.consume();
// easier to test the affirmative case than the negative
} else*/ if ((c == KeyEvent.VK_BACK_SPACE) ||
} else if ((c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE) ||
(c == KeyEvent.VK_RIGHT) ||
(c == KeyEvent.VK_LEFT) ||
@@ -417,13 +418,12 @@ public class PdeEditorStatus extends JPanel implements ActionListener {
else if (mode == EDIT) unedit();
editor.buttons.clear();
/*
} else if (e.getSource() == okButton) {
// answering to "save as..." question
String answer = editField.getText();
editor.handleSaveAs2(answer);
//editor.handleSaveAs2(answer);
editor.sketch.nameCode(answer);
unedit();
*/
}
}
}

View File

@@ -199,6 +199,27 @@ public class PdeSketch {
}
}
// sort the entries at the top
sortCode();
// set the main file to be the current tab
//current = code[0];
setCurrent(0);
}
protected void insertCode(PdeCode newCode) {
// add file to the code/codeCount list, resort the list
if (codeCount == code.length) {
PdeCode temp[] = new PdeCode[codeCount+1];
System.arraycopy(code, 0, temp, 0, codeCount);
code = temp;
}
code[codeCount++] = newCode;
}
protected void sortCode() {
// cheap-ass sort of the rest of the files
// it's a dumb, slow sort, but there shouldn't be more than ~5 files
for (int i = 1; i < codeCount; i++) {
@@ -214,10 +235,188 @@ public class PdeSketch {
code[i] = temp;
}
}
}
// set the main file to be the current tab
//current = code[0];
setCurrent(0);
boolean renamingCode;
/*
public void nameCode(String newName) {
if (renamingCode) {
renameCode2(newName);
} else {
newCode2(newName);
}
}
*/
public void newCode() {
//System.out.println("new code");
// ask for name of new file
// maybe just popup a text area?
renamingCode = false;
editor.status.edit("Name for new file:", "");
}
public void renameCode() {
// don't allow rename of the main code
if (current == code[0]) return;
// TODO maybe gray out the menu on setCurrent(0)
// ask for new name of file (internal to window)
// TODO maybe just popup a text area?
renamingCode = true;
editor.status.edit("New name for file:", current.name);
}
/**
* This is called upon return from entering a new file name.
* This code is almost identical for both the newCode and renameCode
* cases, so they're kept merged except for right in the middle
* where they diverge.
*/
public void nameCode(String newName) {
// if renaming to the same thing as before, just ignore
if (renamingCode && newName.equals(current.name)) {
// exit quietly for the 'rename' case.
// if it's a 'new' then an error will occur down below
return;
}
String newFilename = null;
int newFlavor = 0;
// add .pde to file if it has no extension
if (newName.endsWith(".pde")) {
newFilename = newName;
newName = newName.substring(0, newName.length() - 4);
newFlavor = PDE;
} else if (newName.endsWith(".java")) {
newFilename = newName;
newName = newName.substring(0, newName.length() - 5);
newFlavor = JAVA;
} else {
newFilename = newName + ".pde";
newFlavor = PDE;
}
// create the new file, new PdeCode object and load it
File newFile = new File(folder, newFilename);
if (newFile.exists()) { // yay! users will try anything
PdeBase.showMessage("Nope",
"A file named \"" + newFile + "\" already exists\n" +
"in \"" + folder.getAbsolutePath() + "\"");
return;
}
if (renamingCode) {
if (!current.file.renameTo(newFile)) {
PdeBase.showWarning("Error",
"Could not rename \"" + current.file.getName() +
"\" to \"" + newFile.getName() + "\"", null);
return;
}
current.file = newFile;
current.name = newName;
current.flavor = newFlavor;
} else { // creating a new file
try {
newFile.createNewFile(); // TODO returns a boolean
} catch (IOException e) {
PdeBase.showWarning("Error",
"Could not create the file \"" + newFile + "\"\n" +
"in \"" + folder.getAbsolutePath() + "\"", e);
return;
}
PdeCode newCode = new PdeCode(newName, newFile, newFlavor);
insertCode(newCode);
}
// sort the entries
sortCode();
// set the new guy as current
setCurrent(newName);
// update the tabs
editor.header.repaint();
}
//public void renameCode2(String newName) {
// if 'ok' hit, then rename the feller
// update the tabs
//}
public void deleteCode() {
// don't allow delete of the main code
// TODO maybe gray out the menu on setCurrent(0)
if (current == code[0]) {
PdeBase.showMessage("Can't do that",
"You cannot delete the main " +
".pde file from a sketch\n");
return;
}
// confirm deletion with user, yes/no
// delete the file
// remove it from the internal list of files
// resort internal list of files
// set current tab to the one to the left
// update the tabs
}
// move things around in the array (as opposed to full reload)
// may need to call setCurrent() if the tab was the last one
// or maybe just call setCurrent(0) for good measure
// don't allow the user to hide the 0 tab (the main file)
public void hideCode() {
// don't allow hide of the main code
// maybe gray out the menu on setCurrent(0)
}
public void unhideCode(String what) {
//System.out.println("unhide " + e);
File from = null;
for (int i = 0; i < hiddenCount; i++) {
if (hidden[i].name.equals(what)) {
from = hidden[i].file;
}
}
if (from == null) {
System.err.println("could find " + what + " to unhide.");
return;
}
String filename = from.getName();
if (!filename.endsWith(".x")) {
System.err.println("error while trying to unhide a file");
} else if (!from.exists()) {
System.err.println("file no longer exists");
} else {
File to = new File(from.getPath(),
filename.substring(0, filename.length() - 2));
if (!from.renameTo(to)) {
System.err.println("problem while unhiding");
}
}
}
@@ -405,6 +604,7 @@ public class PdeSketch {
File sourceFile = new File(directory, filename);
File destFile = null;
boolean addingCode = false;
// if the file appears to be code related, drop it
// into the code folder, instead of the data folder
@@ -420,6 +620,7 @@ public class PdeSketch {
} else if (filename.toLowerCase().endsWith(".pde") ||
filename.toLowerCase().endsWith(".java")) {
destFile = new File(this.folder, filename);
addingCode = true;
} else {
//File dataFolder = new File(this.folder, "data");
@@ -443,6 +644,26 @@ public class PdeSketch {
"Could not add '" + filename +
"' to the sketch.", e);
}
// make the tabs update after this guy is added
if (addingCode) {
String newName = destFile.getName();
int newFlavor = -1;
if (newName.toLowerCase().endsWith(".pde")) {
newName = newName.substring(0, newName.length() - 4);
newFlavor = PDE;
} else {
newName = newName.substring(0, newName.length() - 5);
newFlavor = JAVA;
}
// see also "nameCode" for identical situation
PdeCode newCode = new PdeCode(newName, destFile, newFlavor);
insertCode(newCode);
sortCode();
setCurrent(newName);
editor.header.repaint();
}
}
@@ -473,6 +694,19 @@ public class PdeSketch {
}
/**
* Internal helper function to set the current tab
* based on a name (used by codeNew and codeRename).
*/
protected void setCurrent(String findName) {
for (int i = 0; i < codeCount; i++) {
if (findName.equals(code[i].name)) {
setCurrent(i);
return;
}
}
}
/**
* Cleanup temporary files used during a build/run.
@@ -1260,47 +1494,6 @@ public class PdeSketch {
}
// move things around in the array (as opposed to full reload)
// may need to call setCurrent() if the tab was the last one
// or maybe just call setCurrent(0) for good measure
// don't allow the user to hide the 0 tab (the main file)
public void hide(int which) {
}
public void unhide(String what) {
//System.out.println("unhide " + e);
File from = null;
for (int i = 0; i < hiddenCount; i++) {
if (hidden[i].name.equals(what)) {
from = hidden[i].file;
}
}
if (from == null) {
System.err.println("could find " + what + " to unhide.");
return;
}
String filename = from.getName();
if (!filename.endsWith(".x")) {
System.err.println("error while trying to unhide a file");
} else if (!from.exists()) {
System.err.println("file no longer exists");
} else {
File to = new File(from.getPath(),
filename.substring(0, filename.length() - 2));
if (!from.renameTo(to)) {
System.err.println("problem while unhiding");
}
}
}
/**
* Returns path to the main .pde file for this sketch.
*/

View File

@@ -125,15 +125,28 @@ X in which case, default to that (maybe show a message?)
X it's useful to have loose .pde files be runnable..
X i.e. when double-clicking on them.. downloaded off web..
X but need to deal with simply, not providing a new exception case
X begin writing 'new text file'
_ write 'new text file' (ctrl-t)
040623 tuesday morning
X write 'new text file' (ctrl-t)
X hitting return/enter on the status.edit() not always working
X it had been disabled, now re-enabled
X also update status after "add file" of a .pde or .java
_ implement delete
_ implement hide/unhide
_ see if multiple files are actually compiling
_ nope, they aren't properly.. need to figure this out
_ empty files = no class name found.. that's a problem
_ see if sonia project can compile
_ write export-to-application
_ write export-to-library
_ need to be able to produce the serial, video, etc libs
_ put bagel into its own package
_ don't force everything into a single .jar on export
_ ctrl-t doesn't always work
_ after using the popup menu once, it stops working
_ running present mode with a bug in the program hoses things
_ make sure the program compiles before starting present mode
@@ -158,6 +171,7 @@ _ what should the prefs file be named?
_ horizontal buttons? need final decision
_ remove underscores from the tab title?
_ need nice yes/no dialog boxes, also showError/Message/Warning
_ does "Open" go at the beginning or end of the sketch popup
_ NullPointerException when alt is pressed
_ might be something to do with the applet frame being an awt not swing