From 5b1388f6ea4d7d257113b9ddccea5d54cf216aa2 Mon Sep 17 00:00:00 2001 From: benfry Date: Tue, 22 Jun 2004 02:55:34 +0000 Subject: [PATCH] sanitizing of sketch names to their legit equivalents, also on PdeSketchbook.rebuildMenu() --- processing/app/PdePreferences.java | 6 +-- processing/app/PdeSketch.java | 1 + processing/app/PdeSketchbook.java | 80 ++++++++++++++++++++++++++++-- processing/todo.txt | 11 ++-- 4 files changed, 86 insertions(+), 12 deletions(-) diff --git a/processing/app/PdePreferences.java b/processing/app/PdePreferences.java index 14273a47b..bef8c14d7 100644 --- a/processing/app/PdePreferences.java +++ b/processing/app/PdePreferences.java @@ -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 diff --git a/processing/app/PdeSketch.java b/processing/app/PdeSketch.java index 6857f3bd8..f0900d230 100644 --- a/processing/app/PdeSketch.java +++ b/processing/app/PdeSketch.java @@ -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); diff --git a/processing/app/PdeSketchbook.java b/processing/app/PdeSketchbook.java index 7d26236bb..e077cbcf3 100644 --- a/processing/app/PdeSketchbook.java +++ b/processing/app/PdeSketchbook.java @@ -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()); diff --git a/processing/todo.txt b/processing/todo.txt index 78fa53cd2..1316f0e64 100644 --- a/processing/todo.txt +++ b/processing/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