cleaning up accessors inside Sketch

This commit is contained in:
benfry
2008-08-18 20:55:25 +00:00
parent 2633678c42
commit b379fe5fca
5 changed files with 81 additions and 89 deletions

View File

@@ -36,8 +36,7 @@ import java.awt.print.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.event.*;
@@ -1778,7 +1777,7 @@ public class Editor extends JFrame {
// untitled document, then editor.untitled will be set by Base.
untitled = false;
sketch.setCurrent(codeIndex);
sketch.setCurrentCode(codeIndex);
textarea.select(selStart, selStop);
textarea.setScrollPosition(scrollPos);
}
@@ -2120,7 +2119,7 @@ public class Editor extends JFrame {
printerJob.setPrintable(textarea.getPainter());
}
// set the name of the job to the code name
printerJob.setJobName(sketch.current.getPrettyName());
printerJob.setJobName(sketch.getCurrentCode().getPrettyName());
if (printerJob.printDialog()) {
try {
@@ -2164,7 +2163,7 @@ public class Editor extends JFrame {
if (e instanceof RunnerException) {
RunnerException re = (RunnerException) e;
if (re.hasCodeIndex()) {
sketch.setCurrent(re.getCodeIndex());
sketch.setCurrentCode(re.getCodeIndex());
}
if (re.hasCodeLine()) {
int line = re.getCodeLine();

View File

@@ -108,9 +108,9 @@ public class EditorHeader extends JComponent {
} else {
Sketch sketch = editor.getSketch();
for (int i = 0; i < sketch.codeCount; i++) {
for (int i = 0; i < sketch.getCodeCount(); i++) {
if ((x > tabLeft[i]) && (x < tabRight[i])) {
sketch.setCurrent(i);
sketch.setCurrentCode(i);
repaint();
}
}
@@ -166,30 +166,25 @@ public class EditorHeader extends JComponent {
g.setColor(backgroundColor);
g.fillRect(0, 0, imageW, imageH);
if ((tabLeft == null) ||
(tabLeft.length < sketch.codeCount)) {
tabLeft = new int[sketch.codeCount];
tabRight = new int[sketch.codeCount];
int codeCount = sketch.getCodeCount();
if ((tabLeft == null) || (tabLeft.length < codeCount)) {
tabLeft = new int[codeCount];
tabRight = new int[codeCount];
}
// disable hide on the first tab
hideItem.setEnabled(sketch.current != sketch.code[0]);
hideItem.setEnabled(sketch.getCurrentCode() != sketch.getCode(0));
//int x = 0; //Preferences.GUI_SMALL;
//int x = (Base.platform == Base.MACOSX) ? 0 : 1;
int x = 6; // offset from left edge of the component
for (int i = 0; i < sketch.codeCount; i++) {
SketchCode code = sketch.code[i];
for (int i = 0; i < sketch.getCodeCount(); i++) {
SketchCode code = sketch.getCode(i);
// String codeName = (code.flavor == Sketch.PDE) ?
// code.name : code.file.getName();
String codeName = sketch.hideExtension(code.getExtension()) ?
code.getPrettyName() : code.getFileName();
// if modified, add the li'l glyph next to the name
String text = " " + codeName + (code.modified ? " \u00A7" : " ");
//int textWidth = metrics.stringWidth(text);
Graphics2D g2 = (Graphics2D) g;
int textWidth = (int)
font.getStringBounds(text, g2.getFontRenderContext()).getWidth();
@@ -197,7 +192,7 @@ public class EditorHeader extends JComponent {
int pieceCount = 2 + (textWidth / PIECE_WIDTH);
int pieceWidth = pieceCount * PIECE_WIDTH;
int state = (code == sketch.current) ? SELECTED : UNSELECTED;
int state = (code == sketch.getCurrentCode()) ? SELECTED : UNSELECTED;
g.drawImage(pieces[state][LEFT], x, 0, null);
x += PIECE_WIDTH;
@@ -400,11 +395,11 @@ public class EditorHeader extends JComponent {
ActionListener jumpListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.getSketch().setCurrent(e.getActionCommand());
editor.getSketch().setCurrentCode(e.getActionCommand());
}
};
for (int i = 0; i < sketch.codeCount; i++) {
item = new JMenuItem(sketch.code[i].getPrettyName());
for (SketchCode code : sketch.getCode()) {
item = new JMenuItem(code.getPrettyName());
item.addActionListener(jumpListener);
menu.add(item);
}

View File

@@ -44,10 +44,8 @@ public class Sketch {
private Editor editor;
/**
* Main pde file for this sketch.
*/
private File mainFile;
/** main pde file for this sketch. */
private File primaryFile;
/**
* Name of sketch, which is the name of main file
@@ -55,49 +53,37 @@ public class Sketch {
*/
private String name;
/**
* Name of 'main' file, used by load(), such as sketch_04040.pde
*/
//private String mainFilename;
/**
* true if any of the files have been modified.
*/
/** true if any of the files have been modified. */
private boolean modified;
/**
* Path to the main PDE file for this sketch.
*/
// private String path;
/** folder that contains this sketch */
private File folder;
/** data folder location for this sketch (may not exist yet) */
private File dataFolder;
/** code folder location for this sketch (may not exist yet) */
private File codeFolder;
//static public final int PDE = 0;
static public final int JAVA = 1;
public SketchCode current;
int currentIndex;
int codeCount;
SketchCode code[];
private SketchCode current;
private int currentIndex;
private int codeCount;
private SketchCode[] code;
int hiddenCount;
SketchCode hidden[];
//Hashtable zipFileContents;
// all these set each time build() is called
String mainClassName;
String classPath;
private String appletClassName;
private String classPath;
/**
* This is *not* the "Processing" libraries path, this is the Java libraries
* path, as in java.library.path=BlahBlah, which identifies search paths for
* DLLs or JNILIBs.
*/
String libraryPath;
// boolean externalRuntime;
Vector importedLibraries; // vec of File objects
private String libraryPath;
ArrayList<File> importedLibraries;
/**
* path is location of the main .pde file, because this is also
@@ -107,11 +93,11 @@ public class Sketch {
this.editor = editor;
// this.path = path;
mainFile = new File(path);
primaryFile = new File(path);
// get the name of the sketch by chopping .pde or .java
// off of the main file name
String mainFilename = mainFile.getName();
String mainFilename = primaryFile.getName();
int suffixLength = getDefaultExtension().length() + 1;
name = mainFilename.substring(0, mainFilename.length() - suffixLength);
@@ -250,7 +236,7 @@ public class Sketch {
// start at 1, if it's at zero, don't bother
for (int i = 1; i < codeCount; i++) {
//if (code[i].file.getName().equals(mainFilename)) {
if (code[i].getFile().equals(mainFile)) {
if (code[i].getFile().equals(primaryFile)) {
SketchCode temp = code[0];
code[0] = code[i];
code[i] = temp;
@@ -262,7 +248,7 @@ public class Sketch {
sortCode();
// set the main file to be the current tab
setCurrent(0);
setCurrentCode(0);
}
@@ -564,7 +550,7 @@ public class Sketch {
sortCode();
// set the new guy as current
setCurrent(newName);
setCurrentCode(newName);
// update the tabs
//editor.header.repaint();
@@ -635,7 +621,7 @@ public class Sketch {
removeCode(current);
// just set current tab to the main tab
setCurrent(0);
setCurrentCode(0);
// update the tabs
editor.header.repaint();
@@ -706,7 +692,7 @@ public class Sketch {
removeCode(current);
// update the tabs
setCurrent(0);
setCurrentCode(0);
editor.header.repaint();
}
@@ -753,7 +739,7 @@ public class Sketch {
// unhideCode.file = unhideFile;
insertCode(unhideCode);
sortCode();
setCurrent(unhideCode.getFileName());
setCurrentCode(unhideCode.getFileName());
editor.header.repaint();
}
@@ -764,7 +750,7 @@ public class Sketch {
public void handlePrevCode() {
int prev = currentIndex - 1;
if (prev < 0) prev = codeCount-1;
setCurrent(prev);
setCurrentCode(prev);
}
@@ -772,7 +758,7 @@ public class Sketch {
* Move to the next tab.
*/
public void handleNextCode() {
setCurrent((currentIndex + 1) % codeCount);
setCurrentCode((currentIndex + 1) % codeCount);
}
@@ -1160,7 +1146,7 @@ public class Sketch {
insertCode(newCode);
sortCode();
}
setCurrent(filename);
setCurrentCode(filename);
editor.header.repaint();
if (editor.untitled) { // TODO probably not necessary? problematic?
// Mark the new code as modified so that the sketch is saved
@@ -1199,7 +1185,7 @@ public class Sketch {
// if the current code is a .java file, insert into current
//if (current.flavor == PDE) {
if (hasDefaultExtension(current)) {
setCurrent(0);
setCurrentCode(0);
}
// could also scan the text in the file to see if each import
// statement is already in there, but if the user has the import
@@ -1226,7 +1212,7 @@ public class Sketch {
* <LI> change the text that's visible in the text area
* </OL>
*/
public void setCurrent(int which) {
public void setCurrentCode(int which) {
// if current is null, then this is the first setCurrent(0)
if ((currentIndex == which) && (current != null)) {
return;
@@ -1270,17 +1256,17 @@ public class Sketch {
* Internal helper function to set the current tab based on a name.
* @param findName the file name (not pretty name) to be shown
*/
protected void setCurrent(String findName) {
protected void setCurrentCode(String findName) {
for (int i = 0; i < codeCount; i++) {
if (findName.equals(code[i].getFileName()) ||
findName.equals(code[i].getPrettyName())) {
setCurrent(i);
setCurrentCode(i);
return;
}
}
}
/**
* Cleanup temporary files used during a build/run.
*/
@@ -1360,7 +1346,7 @@ public class Sketch {
// "_" + String.valueOf((int) (Math.random() * 10000)));
// handle preprocessing the main file's code
mainClassName =
appletClassName =
build(tempBuildFolder.getAbsolutePath(), suggestedClassName);
// externalPaths is magically set by build()
@@ -1378,7 +1364,7 @@ public class Sketch {
// }
// }
// }
return (mainClassName != null);
return (appletClassName != null);
}
@@ -1623,7 +1609,7 @@ public class Sketch {
// grab the imports from the code just preproc'd
importedLibraries = new Vector();
importedLibraries = new ArrayList<File>();
String imports[] = preprocessor.extraImports;
for (int i = 0; i < imports.length; i++) {
// remove things up to the last dot
@@ -1884,11 +1870,12 @@ public class Sketch {
// if a file called 'export.txt' is in there, it contains
// a list of the files that should be exported.
// otherwise, all files are exported.
Enumeration en = importedLibraries.elements();
while (en.hasMoreElements()) {
for (File libraryFolder : importedLibraries) {
// Enumeration en = importedLibraries.elements();
// while (en.hasMoreElements()) {
// in the list is a File object that points the
// library sketch's "library" folder
File libraryFolder = (File)en.nextElement();
// File libraryFolder = (File)en.nextElement();
File exportSettings = new File(libraryFolder, "export.txt");
Hashtable exportTable = readSettings(exportSettings);
String appletList = (String) exportTable.get("applet");
@@ -2358,9 +2345,10 @@ public class Sketch {
// if a file called 'export.txt' is in there, it contains
// a list of the files that should be exported.
// otherwise, all files are exported.
Enumeration en = importedLibraries.elements();
while (en.hasMoreElements()) {
File libraryFolder = (File)en.nextElement();
for (File libraryFolder : importedLibraries) {
// Enumeration en = importedLibraries.elements();
// while (en.hasMoreElements()) {
// File libraryFolder = (File)en.nextElement();
// in the list is a File object that points the
// library sketch's "library" folder
@@ -2862,8 +2850,8 @@ public class Sketch {
/**
* Returns a file object for the primary .pde of this sketch.
*/
public File getMainFile() {
return mainFile;
public File getPrimaryFile() {
return primaryFile;
}
@@ -2871,7 +2859,7 @@ public class Sketch {
* Returns path to the main .pde file for this sketch.
*/
public String getMainFilePath() {
return mainFile.getAbsolutePath();
return primaryFile.getAbsolutePath();
//return code[0].file.getAbsolutePath();
}
@@ -2934,6 +2922,11 @@ public class Sketch {
}
public SketchCode[] getCode() {
return code;
}
public int getCodeCount() {
return codeCount;
}
@@ -2942,7 +2935,12 @@ public class Sketch {
public SketchCode getCode(int index) {
return code[index];
}
public SketchCode getCurrentCode() {
return current;
}
public void setUntitled(boolean u) {
editor.untitled = u;
@@ -2954,8 +2952,8 @@ public class Sketch {
}
public String getMainClassName() {
return mainClassName;
public String getAppletClassName() {
return appletClassName;
}

View File

@@ -156,7 +156,7 @@ public class Runner implements MessageConsumer {
}
if (Base.isMacOS()) {
params.add("-Xdock:name=" + sketch.getMainClassName());
params.add("-Xdock:name=" + sketch.getAppletClassName());
// params.add("-Dcom.apple.mrj.application.apple.menu.about.name=" +
// sketch.getMainClassName());
}
@@ -232,7 +232,7 @@ public class Runner implements MessageConsumer {
Preferences.get("run.present.bgcolor"));
}
params.add(sketch.getMainClassName());
params.add(sketch.getAppletClassName());
//String command[] = (String[]) params.toArray();
String outgoing[] = new String[params.size()];

View File

@@ -64,7 +64,7 @@ public class FixEncoding {
code.modified = true; // yes, because we want them to save this
}
// Update the currently visible program with its code
editor.setText(sketch.current.program);
editor.setText(sketch.getCurrentCode().program);
} catch (IOException e) {
String msg =