File renamed from .pde to .ino during save.

This commit is contained in:
Cristian Maglie 2011-09-10 01:16:24 +02:00
parent 0b09a8edd9
commit 5d97d467c8
3 changed files with 77 additions and 77 deletions

View File

@ -2048,87 +2048,38 @@ public class Editor extends JFrame implements RunnerListener {
* modifications (if any) to the previous sketch need to be saved. * modifications (if any) to the previous sketch need to be saved.
*/ */
protected boolean handleOpenInternal(String path) { protected boolean handleOpenInternal(String path) {
// rename .pde files to .ino
File[] oldFiles = (new File(path)).getParentFile().listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.toLowerCase().endsWith(".pde"));
}
});
if (oldFiles != null && oldFiles.length > 0) {
if (!Preferences.getBoolean("editor.update_extension")) {
Object[] options = { "OK", "Cancel" };
String prompt =
"In Arduino 1.0, the file extension for sketches changed\n" +
"from \".pde\" to \".ino\". This version of the software only\n" +
"supports the new extension. Rename the files in this sketch\n" +
"(and future sketches) and continue?";
int result = JOptionPane.showOptionDialog(this,
prompt,
"New extension",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (result != JOptionPane.YES_OPTION) {
return false;
}
Preferences.setBoolean("editor.update_extension", true);
}
for (int i = 0; i < oldFiles.length; i++) {
String oldPath = oldFiles[i].getPath();
File newFile = new File(oldPath.substring(0, oldPath.length() - 4) + ".ino");
try {
Base.copyFile(oldFiles[i], newFile);
} catch (IOException e) {
Base.showWarning("Error", "Could not copy to a proper location.", e);
return false;
}
// remove the original file, so user doesn't get confused
oldFiles[i].delete();
// update with the new path
if (oldFiles[i].compareTo(new File(path)) == 0) {
path = newFile.getAbsolutePath();
}
}
}
// check to make sure that this .pde file is // check to make sure that this .pde file is
// in a folder of the same name // in a folder of the same name
File file = new File(path); File file = new File(path);
File parentFile = new File(file.getParent()); String fileName = file.getName();
String parentName = parentFile.getName(); File parent = file.getParentFile();
String pdeName = parentName + ".ino"; String parentName = parent.getName();
File altFile = new File(file.getParent(), pdeName); String pdeName = parentName + ".pde";
File altPdeFile = new File(parent, pdeName);
String inoName = parentName + ".ino";
File altInoFile = new File(parent, pdeName);
if (pdeName.equals(file.getName())) { if (pdeName.equals(fileName) || inoName.equals(fileName)) {
// no beef with this guy // no beef with this guy
} else if (altFile.exists()) { } else if (altPdeFile.exists()) {
// user selected a .java from the same sketch, // user selected a .java from the same sketch, but open the .pde instead
// but open the .pde instead path = altPdeFile.getAbsolutePath();
path = altFile.getAbsolutePath(); } else if (altInoFile.exists()) {
//System.out.println("found alt file in same folder"); path = altInoFile.getAbsolutePath();
} else if (!path.endsWith(".ino") && !path.endsWith(".pde")) {
} else if (!path.endsWith(".ino")) {
Base.showWarning("Bad file selected", Base.showWarning("Bad file selected",
"Processing can only open its own sketches\n" + "Processing can only open its own sketches\n" +
"and other files ending in .ino", null); "and other files ending in .ino or .pde", null);
return false; return false;
} else { } else {
String properParent = String properParent =
file.getName().substring(0, file.getName().length() - 4); fileName.substring(0, fileName.length() - 4);
Object[] options = { "OK", "Cancel" }; Object[] options = { "OK", "Cancel" };
String prompt = String prompt =
"The file \"" + file.getName() + "\" needs to be inside\n" + "The file \"" + fileName + "\" needs to be inside\n" +
"a sketch folder named \"" + properParent + "\".\n" + "a sketch folder named \"" + properParent + "\".\n" +
"Create this folder, move the file, and continue?"; "Create this folder, move the file, and continue?";
@ -2223,7 +2174,7 @@ public class Editor extends JFrame implements RunnerListener {
// need to get the name, user might also cancel here // need to get the name, user might also cancel here
} else if (immediately) { } else if (immediately) {
handleSave2(); return handleSave2();
} else { } else {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@ -2236,15 +2187,16 @@ public class Editor extends JFrame implements RunnerListener {
} }
protected void handleSave2() { protected boolean handleSave2() {
toolbar.activate(EditorToolbar.SAVE); toolbar.activate(EditorToolbar.SAVE);
statusNotice("Saving..."); statusNotice("Saving...");
boolean saved = false;
try { try {
if (sketch.save()) { saved = sketch.save();
if (saved)
statusNotice("Done Saving."); statusNotice("Done Saving.");
} else { else
statusEmpty(); statusEmpty();
}
// rebuild sketch menu in case a save-as was forced // rebuild sketch menu in case a save-as was forced
// Disabling this for 0125, instead rebuild the menu inside // Disabling this for 0125, instead rebuild the menu inside
// the Save As method of the Sketch object, since that's the // the Save As method of the Sketch object, since that's the
@ -2263,6 +2215,7 @@ public class Editor extends JFrame implements RunnerListener {
} }
//toolbar.clear(); //toolbar.clear();
toolbar.deactivate(EditorToolbar.SAVE); toolbar.deactivate(EditorToolbar.SAVE);
return saved;
} }

View File

@ -261,7 +261,6 @@ public class Sketch {
} }
} }
boolean renamingCode; boolean renamingCode;
/** /**
@ -709,14 +708,62 @@ public class Sketch {
if (!saveAs()) return false; if (!saveAs()) return false;
} }
// rename .pde files to .ino
File mainFile = new File(getMainFilePath());
File mainFolder = mainFile.getParentFile();
File[] pdeFiles = mainFolder.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".pde");
}
});
if (pdeFiles != null && pdeFiles.length > 0) {
Object[] options = { "YES", "NO, Cancel" };
String prompt = "From Arduino 1.0, the file extension for sketches changed\n"
+ "from \".pde\" to \".ino\". This version of the software only\n"
+ "supports the new extension.\n\n"
+ "By clicking YES, the following files will be renamed changing the\n"
+ "extension from \".pde\" to \".ino\":\n\n";
for (File f : pdeFiles)
prompt += f.getName() + "\n";
prompt += "\nContinue?";
int result = JOptionPane.showOptionDialog(editor,
prompt,
"New extension",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (result != JOptionPane.YES_OPTION)
return false;
for (File pdeFile : pdeFiles)
renameCodeToInoExtension(pdeFile);
}
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i].isModified()) code[i].save(); if (code[i].isModified())
code[i].save();
} }
calcModified(); calcModified();
return true; return true;
} }
protected boolean renameCodeToInoExtension(File pdeFile) {
for (SketchCode c : code) {
if (!c.getFile().equals(pdeFile))
continue;
String pdeName = pdeFile.getPath();
pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino";
return c.renameTo(new File(pdeName), "ino");
}
return false;
}
/** /**
* Handles 'Save As' for a sketch. * Handles 'Save As' for a sketch.
* <P> * <P>
@ -1261,7 +1308,7 @@ public class Sketch {
StringBuffer bigCode = new StringBuffer(); StringBuffer bigCode = new StringBuffer();
int bigCount = 0; int bigCount = 0;
for (SketchCode sc : code) { for (SketchCode sc : code) {
if (sc.isExtension("ino")) { if (sc.isExtension("ino") || sc.isExtension("pde")) {
sc.setPreprocOffset(bigCount); sc.setPreprocOffset(bigCount);
bigCode.append(sc.getProgram()); bigCode.append(sc.getProgram());
bigCode.append('\n'); bigCode.append('\n');
@ -1807,7 +1854,7 @@ public class Sketch {
* Returns a String[] array of proper extensions. * Returns a String[] array of proper extensions.
*/ */
public String[] getExtensions() { public String[] getExtensions() {
return new String[] { "ino", "c", "cpp", "h" }; return new String[] { "ino", "pde", "c", "cpp", "h" };
} }

View File

@ -113,8 +113,8 @@ public class SketchCode {
protected boolean renameTo(File what, String ext) { protected boolean renameTo(File what, String ext) {
boolean success = file.renameTo(what); boolean success = file.renameTo(what);
if (success) { if (success) {
this.file = what; // necessary? file = what;
this.extension = ext; extension = ext;
makePrettyName(); makePrettyName();
} }
return success; return success;