mirror of
https://github.com/processing/processing4.git
synced 2026-02-11 17:40:48 +01:00
sanitizing of sketch names to their legit equivalents, also on
PdeSketchbook.rebuildMenu()
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
/*
|
||||
PdePreferences - controls user preferences and environment settings
|
||||
Part of the Processing project - http://Proce55ing.net
|
||||
Part of the Processing project - http://Processing.org
|
||||
|
||||
Except where noted, code is written by Ben Fry and
|
||||
Copyright (c) 2001-03 Massachusetts Institute of Technology
|
||||
Except where noted, code is written by Ben Fry and is
|
||||
Copyright (c) 2001-04 Massachusetts Institute of Technology
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
||||
@@ -321,6 +321,7 @@ public class PdeSketch {
|
||||
|
||||
// user cancelled selection
|
||||
if (newName == null) return false;
|
||||
newName = PdeSketchbook.sanitizeName(newName);
|
||||
|
||||
// new sketch folder
|
||||
File newFolder = new File(newParentDir, newName);
|
||||
|
||||
@@ -45,6 +45,11 @@ public class PdeSketchbook {
|
||||
|
||||
JMenu menu;
|
||||
JMenu popup;
|
||||
|
||||
// set to true after the first time it's built.
|
||||
// so that the errors while building don't show up again.
|
||||
boolean builtOnce;
|
||||
|
||||
//File sketchbookFolder;
|
||||
//String sketchbookPath; // canonical path
|
||||
|
||||
@@ -133,6 +138,7 @@ public class PdeSketchbook {
|
||||
newbieName = fd.getFile();
|
||||
if (newbieName == null) return null;
|
||||
|
||||
newbieName = sanitizeName(newbieName);
|
||||
newbieDir = new File(newbieParentDir, newbieName);
|
||||
|
||||
} else {
|
||||
@@ -183,6 +189,53 @@ public class PdeSketchbook {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert to sanitized name and alert the user
|
||||
* if changes were made.
|
||||
*/
|
||||
static public String sanitizeName(String origName) {
|
||||
String newName = sanitizedName(origName);
|
||||
|
||||
if (!newName.equals(origName)) {
|
||||
PdeBase.showMessage("Naming issue",
|
||||
"The sketch name had to be modified.\n" +
|
||||
"You can only use basic letters and numbers\n" +
|
||||
"to name a sketch (ascii only and no spaces,\n" +
|
||||
"and it can't start with a number)");
|
||||
}
|
||||
return newName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Java classes are pretty limited about what you can use
|
||||
* for their naming. This helper function replaces everything
|
||||
* but A-Z, a-z, and 0-9 with underscores. Also disallows
|
||||
* starting the sketch name with a digit.
|
||||
*/
|
||||
static public String sanitizedName(String origName) {
|
||||
char c[] = origName.toCharArray();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
// can't lead with a digit, so start with an underscore
|
||||
if ((c[0] >= '0') && (c[0] <= '9')) {
|
||||
buffer.append('_');
|
||||
}
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (((c[i] >= '0') && (c[i] <= '9')) ||
|
||||
((c[i] >= 'a') && (c[i] <= 'z')) ||
|
||||
((c[i] >= 'A') && (c[i] <= 'Z'))) {
|
||||
buffer.append(c[i]);
|
||||
|
||||
} else {
|
||||
buffer.append('_');
|
||||
}
|
||||
}
|
||||
//return buffer.toString();
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
public String handleOpen() {
|
||||
// swing's file choosers are ass ugly, so we use the
|
||||
// native (awt peered) dialogs instead
|
||||
@@ -249,15 +302,19 @@ public class PdeSketchbook {
|
||||
popup.add(item);
|
||||
popup.addSeparator();
|
||||
|
||||
addSketches(menu, new File(PdePreferences.get("sketchbook.path")));
|
||||
// identical to below
|
||||
addSketches(popup, new File(PdePreferences.get("sketchbook.path")));
|
||||
|
||||
menu.addSeparator();
|
||||
popup.addSeparator();
|
||||
|
||||
addSketches(menu, examplesFolder);
|
||||
addSketches(popup, examplesFolder);
|
||||
|
||||
// disable error messages while loading
|
||||
builtOnce = true;
|
||||
|
||||
// identical to above
|
||||
addSketches(menu, new File(PdePreferences.get("sketchbook.path")));
|
||||
menu.addSeparator();
|
||||
addSketches(menu, examplesFolder);
|
||||
|
||||
} catch (IOException e) {
|
||||
PdeBase.showWarning("Problem while building sketchbook menu",
|
||||
"There was a problem with building the\n" +
|
||||
@@ -308,7 +365,20 @@ public class PdeSketchbook {
|
||||
|
||||
File subfolder = new File(folder, list[i]);
|
||||
File entry = new File(subfolder, list[i] + ".pde");
|
||||
// if a .pde file of the same prefix as the folder exists..
|
||||
if (entry.exists()) {
|
||||
String sanityCheck = sanitizedName(list[i]);
|
||||
if (!sanityCheck.equals(list[i])) {
|
||||
if (!builtOnce) {
|
||||
String mess =
|
||||
"The sketch \"" + list[i] + "\" cannot be used.\n" +
|
||||
"Sketch names must contain only basic letters and numbers.\n" +
|
||||
"(ascii only and no spaces, and it cannot start with a number)";
|
||||
PdeBase.showMessage("Ignoring bad sketch name", mess);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
JMenuItem item = new JMenuItem(list[i]);
|
||||
item.addActionListener(listener);
|
||||
item.setActionCommand(entry.getAbsolutePath());
|
||||
|
||||
11
todo.txt
11
todo.txt
@@ -106,10 +106,11 @@ X http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_Software;acti
|
||||
X maybe bring back the prompt option for "new sketch"
|
||||
X sometimes nice just to have it create an unnamed sketch quickly
|
||||
X shift-new will always prompt with filedialog
|
||||
X dashes shouldn't be allowed in filenames for sketches
|
||||
X actually, lost the naming stuff because now using FileDialog
|
||||
X this also needs to be checked when building the sketch menu
|
||||
|
||||
_ dashes shouldn't be allowed in filenames for sketches
|
||||
_ actually, lost the naming stuff because now using FileDialog
|
||||
_ this also needs to be checked when building the sketch menu
|
||||
_ write sketchbook.clean()
|
||||
|
||||
_ don't force everything into a single .jar on export
|
||||
|
||||
@@ -120,7 +121,6 @@ _ ( ) rename enclosing or ( ) add a subfolder
|
||||
_ it's useful to have loose .pde files be runnable..
|
||||
_ i.e. when double-clicking on them.. downloaded off web..
|
||||
_ but need to deal with simply, not providing a new exception case
|
||||
_ write sketchbook.clean()
|
||||
_ write 'new text file'
|
||||
_ implement hide/unhide
|
||||
|
||||
@@ -144,6 +144,9 @@ _ how to handle .pde vs .java
|
||||
_ how to determine what the 'main' file is
|
||||
_ sun won't want us to say things are .java when they aren't
|
||||
_ .pde files opened on their own
|
||||
_ what should the prefs file be named?
|
||||
_ horizontal buttons? need final decision
|
||||
_ remove underscores from the tab title?
|
||||
|
||||
_ NullPointerException when alt is pressed
|
||||
_ might be something to do with the applet frame being an awt not swing
|
||||
|
||||
Reference in New Issue
Block a user