Skip to content

Commit deb30c4

Browse files
committed
change up handleOpen() to better handle example sketches for different Modes
1 parent 76f2205 commit deb30c4

File tree

3 files changed

+40
-79
lines changed

3 files changed

+40
-79
lines changed

app/src/processing/app/Base.java

Lines changed: 27 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ public void handleNew() {
12071207
}
12081208

12091209
String path = newbieFile.getAbsolutePath();
1210-
handleOpen(path, true);
1210+
handleOpenUntitled(path);
12111211

12121212
} catch (IOException e) {
12131213
Messages.showWarning("That's new to me",
@@ -1307,7 +1307,7 @@ private Editor openSketchBundle(String path) {
13071307
if (fileList.length == 1) {
13081308
File sketchFile = Sketch.findMain(fileList[0], getModeList());
13091309
if (sketchFile != null) {
1310-
return handleOpen(sketchFile.getAbsolutePath(), true);
1310+
return handleOpenUntitled(sketchFile.getAbsolutePath());
13111311
}
13121312
} else {
13131313
System.err.println("Expecting one folder inside " +
@@ -1441,6 +1441,7 @@ private File moveLikeSketchFolder(File pdeFile, String baseName) throws IOExcept
14411441
return null;
14421442
}
14431443

1444+
14441445
/**
14451446
* Open a sketch from the path specified. Do not use for untitled sketches.
14461447
* Note that the user may have selected/double-clicked any .pde in a sketch.
@@ -1516,11 +1517,11 @@ public Editor handleOpen(String path) {
15161517
// for now, post a warning if the main was different
15171518
System.out.println(path + " selected, but main is " + mainPath);
15181519
}
1519-
return handleOpen(mainPath, false);
1520+
return handleOpenInternal(mainPath, false);
15201521

15211522
} else {
15221523
// if no main specified, use the passed-in path as the main
1523-
return handleOpen(path, false);
1524+
return handleOpenInternal(path, false);
15241525
}
15251526
} else {
15261527
// No properties file, so do some checks to make sure the file
@@ -1603,7 +1604,7 @@ public Editor handleOpen(String path) {
16031604
nextMode = mode;
16041605
}
16051606
*/
1606-
handleOpen(pdeFile.getAbsolutePath(), false);
1607+
handleOpenInternal(pdeFile.getAbsolutePath(), false);
16071608
}
16081609
} catch (IOException e) {
16091610
Messages.showWarning("sketch.properties",
@@ -1614,84 +1615,36 @@ public Editor handleOpen(String path) {
16141615

16151616

16161617
/**
1617-
* Open a sketch in a new window.
1618-
* @param path Path to the pde file for the sketch in question
1619-
* @return the Editor object, so that properties (like 'untitled')
1620-
* can be set by the caller
1618+
* Open a (vetted) sketch location using a particular Mode. Used by the
1619+
* Examples window, because Modes like Python and Android do not have
1620+
* "sketch.properties" files in each example folder.
16211621
*/
1622-
protected Editor handleOpen(String path, boolean untitled) {
1623-
return handleOpen(path, untitled, EditorState.nextEditor(editors));
1622+
public Editor handleOpen(String path, Mode mode) {
1623+
nextMode = mode;
1624+
return handleOpenInternal(path, false);
16241625
}
16251626

16261627

1627-
protected Editor handleOpen(String path, boolean untitled,
1628-
EditorState state) {
1629-
/*
1630-
return handleOpen(path, untitled, state, nextMode);
1628+
/**
1629+
* Open the sketch associated with this .pde file in a new window
1630+
* as an "Untitled" sketch.
1631+
* @param path Path to the pde file for the sketch in question
1632+
* @return the Editor object, so that properties (like 'untitled')
1633+
* can be set by the caller
1634+
*/
1635+
protected Editor handleOpenUntitled(String path) {
1636+
return handleOpenInternal(path, true);
16311637
}
16321638

1633-
protected Editor handleOpen(String path, boolean untitled,
1634-
EditorState state, Mode mode) {
1635-
try {
1636-
final File file = new File(path);
1637-
if (!file.exists()) {
1638-
return null;
1639-
}
1640-
1641-
// Cycle through open windows to make sure that it's not already open.
1642-
for (Editor editor : editors) {
1643-
// User may have double-clicked any PDE in the sketch folder,
1644-
// so we have to check each open tab (not just the main one).
1645-
// https://github.com/processing/processing/issues/2506
1646-
for (SketchCode tab : editor.getSketch().getCode()) {
1647-
if (tab.getFile().equals(file)) {
1648-
editor.toFront();
1649-
// move back to the top of the recent list
1650-
Recent.append(editor);
1651-
return editor;
1652-
}
1653-
}
1654-
}
1655-
1656-
// read the sketch.properties file if it exists
1657-
File parentFolder = new File(path).getParentFile();
1658-
Settings props = loadSketchProperties(parentFolder);
1659-
1660-
// if the Mode is set in this file, use that to determine next
1661-
if (props != null) {
1662-
String modeIdentifier = props.get("mode.id");
1663-
if (modeIdentifier != null) {
1664-
Mode mode = findMode(modeIdentifier);
1665-
if (mode != null) {
1666-
nextMode = mode;
1667-
} else {
1668-
Messages.showWarning("Missing Mode",
1669-
"You must first install " + props.get("mode") + " Mode to use this sketch.");
1670-
ContributionManager.openModes();
1671-
return null;
1672-
}
1673-
}
1674-
}
1675-
1676-
if (!Sketch.isSanitaryName(file.getName())) {
1677-
Messages.showWarning("You're tricky, but not tricky enough",
1678-
file.getName() + " is not a valid name for a sketch.\n" +
1679-
"Better to stick to ASCII, no spaces, and make sure\n" +
1680-
"it doesn't start with a number.", null);
1681-
return null;
1682-
}
1683-
1684-
if (!nextMode.canEdit(file)) {
1685-
final Mode mode = selectMode(file);
1686-
if (mode == null) {
1687-
return null;
1688-
}
1689-
nextMode = mode;
1690-
}
1691-
*/
16921639

1640+
/**
1641+
* Internal function to actually open the sketch. At this point, the
1642+
* sketch file/folder must have been vetted, and nextMode set properly.
1643+
*/
1644+
protected Editor handleOpenInternal(String path, boolean untitled) {
16931645
try {
16941646
try {
1647+
EditorState state = EditorState.nextEditor(editors);
16951648
Editor editor = nextMode.createEditor(this, path, state);
16961649

16971650
// opened successfully, let's go to work

app/src/processing/app/ui/ExamplesFrame.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void mouseClicked(MouseEvent e) {
117117
//if (node != null && node.isLeaf() && node.getPath().equals(selPath)) {
118118
if (node != null && node.isLeaf() && selRow != -1) {
119119
SketchReference sketch = (SketchReference) node.getUserObject();
120-
base.handleOpen(sketch.getPath());
120+
base.handleOpen(sketch.getPath(), mode);
121121
}
122122
}
123123
}
@@ -134,7 +134,7 @@ public void keyTyped(KeyEvent e) {
134134
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
135135
if (node != null && node.isLeaf()) {
136136
SketchReference sketch = (SketchReference) node.getUserObject();
137-
base.handleOpen(sketch.getPath());
137+
base.handleOpen(sketch.getPath(), mode);
138138
}
139139
}
140140
}

todo.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ X You must first install tweak Mode to use this sketch
55
X https://github.com/processing/processing4/issues/415
66
X Change straight quotes to smart quotes in the PDE.properties file
77
X look for other uses of Util.deleteFile() and replace with Platform calls
8-
_ test with Python Mode before release
9-
_ opening an example throws an NPE
10-
_ add changes for the eawt mess?
8+
X change handleOpen() to take a Mode object
9+
X allows Python and Android Mode to open example sketches in that Mode,
10+
X without (retroactively) needing sketch.properties files in all folders
11+
12+
python
13+
X add JavaFX handler
14+
X opening an example throws an NPE
15+
_ add changes for the eawt mess?
1116

1217
sketchbook/open/deletions
1318
X test "obvious" sketch folder (and whether it prompts)
@@ -34,6 +39,9 @@ _ examples handling is less than ideal
3439
_ examples not mentioned in warning dialog when installing
3540
_ doesn't show in examples window for p5jsMode
3641
_ the extra "Contributed Examples" subfolder is awkward
42+
_ perhaps more important, examples need to specify their Mode
43+
_ not ideal for Android since there's some crossover, but ok for Example sets
44+
_ otherwise impossible to know how to populate the Examples window
3745

3846

3947
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

0 commit comments

Comments
 (0)