Skip to content

Commit

Permalink
Merge pull request WiringProject#2 from WiringProject/code-cleanups-1
Browse files Browse the repository at this point in the history
Code cleanups 1
  • Loading branch information
Damian Barragán committed Nov 18, 2011
2 parents 28fa308 + 09e9996 commit 06feb18
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 1,815 deletions.
379 changes: 8 additions & 371 deletions IDE/processing/app/Base.java

Large diffs are not rendered by default.

357 changes: 0 additions & 357 deletions IDE/processing/app/Editor.java

Large diffs are not rendered by default.

27 changes: 13 additions & 14 deletions IDE/processing/app/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ public class Preferences {

// data model

static Hashtable defaults;
static Hashtable table = new Hashtable();
static File preferencesFile;
private static Hashtable<String, String> defaults;
private static Hashtable<String, String> table =
new Hashtable<String, String>();
private static File preferencesFile;


static public void init(String commandLinePrefs) {
Expand All @@ -152,9 +153,9 @@ static public void init(String commandLinePrefs) {
// check for platform-specific properties in the defaults
String platformExt = "." + PConstants.platformNames[PApplet.platform];
int platformExtLength = platformExt.length();
Enumeration e = table.keys();
Enumeration<String> e = table.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String key = e.nextElement();
if (key.endsWith(platformExt)) {
// this is a key specific to a particular platform
String actualKey = key.substring(0, key.length() - platformExtLength);
Expand All @@ -163,8 +164,7 @@ static public void init(String commandLinePrefs) {
}
}

// clone the hash table
defaults = (Hashtable) table.clone();
defaults = new Hashtable<String, String>(table);

// other things that have to be set explicitly for the defaults
setColor("run.window.bgcolor", SystemColor.control);
Expand Down Expand Up @@ -674,7 +674,8 @@ static protected void load(InputStream input) throws IOException {
load(input, table);
}

static public void load(InputStream input, Map table) throws IOException {
static public void load(InputStream input,
Map<String,String> table) throws IOException {
String[] lines = PApplet.loadStrings(input); // Reads as UTF-8
for (String line : lines) {
if ((line.length() == 0) ||
Expand Down Expand Up @@ -704,15 +705,13 @@ static protected void save() {
// Fix for 0163 to properly use Unicode when writing preferences.txt
PrintWriter writer = PApplet.createWriter(preferencesFile);

Enumeration e = table.keys(); //properties.propertyNames();

// Sort keys alphabetically and store
// BH, 20110417
java.util.List<String> prefKeys = Collections.list(e);
java.util.List<String> prefKeys = Collections.list(table.keys());
Collections.sort(prefKeys);

for (String prefKey : prefKeys) {
writer.println(prefKey + "=" + ((String) table.get(prefKey)));
writer.println(prefKey + "=" + table.get(prefKey));
}

writer.flush();
Expand All @@ -734,7 +733,7 @@ static protected void save() {
//}

static public String get(String attribute /*, String defaultValue */) {
return (String) table.get(attribute);
return table.get(attribute);
/*
//String value = (properties != null) ?
//properties.getProperty(attribute) : applet.getParameter(attribute);
Expand All @@ -747,7 +746,7 @@ static public String get(String attribute /*, String defaultValue */) {


static public String getDefault(String attribute) {
return (String) defaults.get(attribute);
return defaults.get(attribute);
}


Expand Down
109 changes: 2 additions & 107 deletions IDE/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -1839,8 +1839,8 @@ protected void addManifest(ZipOutputStream zos) throws IOException {
* Read from a file with a bunch of attribute/value pairs
* that are separated by = and ignore comments with #.
*/
protected Hashtable readSettings(File inputFile) {
Hashtable outgoing = new Hashtable();
protected Hashtable<String, String> readSettings(File inputFile) {
Hashtable<String, String> outgoing = new Hashtable<String, String>();
if (!inputFile.exists()) return outgoing; // return empty hash

String lines[] = PApplet.loadStrings(inputFile);
Expand All @@ -1864,111 +1864,6 @@ protected Hashtable readSettings(File inputFile) {
}


/**
* Slurps up .class files from a colon (or semicolon on windows)
* separated list of paths and adds them to a ZipOutputStream.
*/
protected void packClassPathIntoZipFile(String path,
ZipOutputStream zos,
Hashtable zipFileContents)
throws IOException {
String[] pieces = PApplet.split(path, File.pathSeparatorChar);

for (int i = 0; i < pieces.length; i++) {
if (pieces[i].length() == 0) continue;

// is it a jar file or directory?
if (pieces[i].toLowerCase().endsWith(".jar") ||
pieces[i].toLowerCase().endsWith(".zip")) {
try {
ZipFile file = new ZipFile(pieces[i]);
Enumeration entries = file.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.isDirectory()) {
// actually 'continue's for all dir entries

} else {
String entryName = entry.getName();
// ignore contents of the META-INF folders
if (entryName.indexOf("META-INF") == 0) continue;

// don't allow duplicate entries
if (zipFileContents.get(entryName) != null) continue;
zipFileContents.put(entryName, new Object());

ZipEntry entree = new ZipEntry(entryName);

zos.putNextEntry(entree);
byte buffer[] = new byte[(int) entry.getSize()];
InputStream is = file.getInputStream(entry);

int offset = 0;
int remaining = buffer.length;
while (remaining > 0) {
int count = is.read(buffer, offset, remaining);
offset += count;
remaining -= count;
}

zos.write(buffer);
zos.flush();
zos.closeEntry();
}
}
} catch (IOException e) {
System.err.println("Error in file " + pieces[i]);
e.printStackTrace();
}
} else { // not a .jar or .zip, prolly a directory
File dir = new File(pieces[i]);
// but must be a dir, since it's one of several paths
// just need to check if it exists
if (dir.exists()) {
packClassPathIntoZipFileRecursive(dir, null, zos);
}
}
}
}


/**
* Continue the process of magical exporting. This function
* can be called recursively to walk through folders looking
* for more goodies that will be added to the ZipOutputStream.
*/
static protected void packClassPathIntoZipFileRecursive(File dir,
String sofar,
ZipOutputStream zos)
throws IOException {
String files[] = dir.list();
for (int i = 0; i < files.length; i++) {
// ignore . .. and .DS_Store
if (files[i].charAt(0) == '.') continue;

File sub = new File(dir, files[i]);
String nowfar = (sofar == null) ?
files[i] : (sofar + "/" + files[i]);

if (sub.isDirectory()) {
packClassPathIntoZipFileRecursive(sub, nowfar, zos);

} else {
// don't add .jar and .zip files, since they only work
// inside the root, and they're unpacked
if (!files[i].toLowerCase().endsWith(".jar") &&
!files[i].toLowerCase().endsWith(".zip") &&
files[i].charAt(0) != '.') {
ZipEntry entry = new ZipEntry(nowfar);
zos.putNextEntry(entry);
zos.write(Base.loadBytesRaw(sub));
zos.closeEntry();
}
}
}
}


/**
* Make sure the sketch hasn't been moved or deleted by some
* nefarious user. If they did, try to re-create it and save.
Expand Down
4 changes: 2 additions & 2 deletions IDE/processing/app/SketchCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ public boolean isModified() {
public void setPreprocName(String preprocName) {
this.preprocName = preprocName;
}
//
//


public String getPreprocName() {
return preprocName;
}
Expand Down
11 changes: 3 additions & 8 deletions IDE/processing/app/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static protected void init() {
setColor("run.window.bgcolor", SystemColor.control);

// clone the hash table
defaults = (HashMap<String, String>) table.clone();
defaults = new HashMap<String, String>(table);
}


Expand All @@ -90,12 +90,12 @@ static protected void load(InputStream input) throws IOException {


static public String get(String attribute) {
return (String) table.get(attribute);
return table.get(attribute);
}


static public String getDefault(String attribute) {
return (String) defaults.get(attribute);
return defaults.get(attribute);
}


Expand Down Expand Up @@ -148,17 +148,14 @@ static public Font getFont(String attr) {
boolean replace = false;
String value = get(attr);
if (value == null) {
//System.out.println("reset 1");
value = getDefault(attr);
replace = true;
}

String[] pieces = PApplet.split(value, ',');
if (pieces.length != 3) {
value = getDefault(attr);
//System.out.println("reset 2 for " + attr);
pieces = PApplet.split(value, ',');
//PApplet.println(pieces);
replace = true;
}

Expand All @@ -175,8 +172,6 @@ static public Font getFont(String attr) {

// replace bad font with the default
if (replace) {
//System.out.println(attr + " > " + value);
//setString(attr, font.getName() + ",plain," + font.getSize());
set(attr, value);
}

Expand Down
Loading

0 comments on commit 06feb18

Please sign in to comment.