Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,7 @@ public JMenu initDefaultFileMenu() {
item.addActionListener(e -> handleNew());
defaultFileMenu.add(item);

item = Toolkit.newJMenuItem(Language.text("menu.file.open"), 'O');
item.addActionListener(e -> handleOpenPrompt());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realised that the file->open menu item certainly should not be deleted here

defaultFileMenu.add(item);


item = Toolkit.newJMenuItemShift(Language.text("menu.file.sketchbook"), 'K');
item.addActionListener(e -> showSketchbookFrame());
Expand All @@ -598,6 +596,10 @@ public JMenu initDefaultFileMenu() {
item.addActionListener(e -> thinkDifferentExamples());
defaultFileMenu.add(item);

item = Toolkit.newJMenuItem(Language.text("menu.file.templates"), 'T');
//item.addActionListener(e -> handleTemplates());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more comment

defaultFileMenu.add(item);

return defaultFileMenu;
}

Expand Down Expand Up @@ -1095,16 +1097,15 @@ public void modeRemoved(Mode mode) {
/**
* Create a new untitled document in a new sketch window.
*/
public void handleNew() {
// long t1 = System.currentTimeMillis();
public Editor handleNew() {
try {
// In 0126, untitled sketches will begin in the temp folder,
// and then moved to a new location because Save will default to Save As.
//File sketchbookDir = getSketchbookFolder();
File newbieDir = SketchName.nextFolder(untitledFolder);

// User was told to go outside or other problem happened inside naming.
if (newbieDir == null) return;
if (newbieDir == null) return null;

// Make the directory for the new sketch
if (!newbieDir.mkdirs()) {
Expand All @@ -1124,16 +1125,16 @@ public void handleNew() {
}

String path = newbieFile.getAbsolutePath();
handleOpenUntitled(path);
return handleOpenUntitled(path);

} catch (IOException e) {
Messages.showTrace("That's new to me",
"A strange and unexplainable error occurred\n" +
"while trying to create a new sketch.", e, false);
"A strange and unexplainable error occurred\n" +
"while trying to create a new sketch.", e, false);
return null;
}
}


/**
* Prompt for a sketch to open, and open it in a new window.
*/
Expand Down Expand Up @@ -1288,6 +1289,8 @@ private void openContribBundle(String path) {
});
}

// Open templates to start with any work
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another



/**
* Return true if it's an obvious sketch folder: only .pde files,
Expand Down
104 changes: 104 additions & 0 deletions app/src/processing/app/ui/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
import java.nio.file.Files;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
Expand Down Expand Up @@ -722,6 +723,12 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
item.addActionListener(e -> handleSaveAs());
fileMenu.add(item);

JMenu templatesMenu = new JMenu(Language.text("menu.file.templates"));
Messages.log("templatesMenu");
loadTemplates(templatesMenu);
fileMenu.add(templatesMenu);


if (exportItems != null) {
for (JMenuItem ei : exportItems) {
fileMenu.add(ei);
Expand Down Expand Up @@ -761,6 +768,101 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
}
return fileMenu;
}
// Load templates from both the repository and the user's Sketchbook folder
private void loadTemplates(JMenu templatesMenu) {
List<File> allTemplates = new ArrayList<>();

// Load predefined templates from the repository
File repoTemplatesDir = Platform.getContentFile("lib/templates");
addTemplatesFromDirectory(repoTemplatesDir, allTemplates);

// Load user-defined templates from the Sketchbook folder
File userTemplatesDir = new File(Base.getSketchbookFolder(), "templates");
addTemplatesFromDirectory(userTemplatesDir, allTemplates);


addTemplatesToMenu(allTemplates, templatesMenu);
}

private void addTemplatesFromDirectory(File directory, List<File> allTemplates) {
if (directory.exists() && directory.isDirectory()) {
File[] templateFiles = directory.listFiles();
if (templateFiles != null) {
for (File templateFile : templateFiles) {
allTemplates.add(templateFile);
}
} else {
Messages.log("No template files found in directory: " + directory.getAbsolutePath());
}
} else {
Messages.log("Directory does not exist or is not a directory: " + directory.getAbsolutePath());
}
}

// Add templates to the menu
// Add templates to the menu
private void addTemplatesToMenu(List<File> templates, JMenu templatesMenu) {
templatesMenu.removeAll(); // Clear existing menu items
for (File templateFile : templates) {
String templateName;
if (templateFile.isDirectory()) {
// Handle directory case - might be a sketch folder
templateName = templateFile.getName();
} else if (templateFile.getName().toLowerCase().endsWith(".pde")) {
templateName = templateFile.getName().replace(".pde", "");
} else {
templateName = templateFile.getName();
}

JMenuItem templateItem = new JMenuItem(templateName);
templateItem.addActionListener(e -> {
try {
String templateCode = "";

if (templateFile.isDirectory()) {
// It's a sketch folder, find the main .pde file with the same name as the folder
File mainPdeFile = new File(templateFile, templateFile.getName() + ".pde");
if (mainPdeFile.exists()) {
templateCode = new String(Files.readAllBytes(mainPdeFile.toPath()));

} else {
// If main file doesn't exist, try to read any .pde file in the directory
File[] pdeFiles = templateFile.listFiles((dir, name) -> name.toLowerCase().endsWith(".pde"));
if (pdeFiles != null && pdeFiles.length > 0) {
templateCode = new String(Files.readAllBytes(pdeFiles[0].toPath()));

}
}
} else if (templateFile.getName().toLowerCase().endsWith(".pde")) {

templateCode = new String(Files.readAllBytes(templateFile.toPath()));

} else {
templateCode = new String(Files.readAllBytes(templateFile.toPath()));
Messages.log("Unrecognized file type: " + templateFile.getAbsolutePath());
}

Editor newEditor = base.handleNew();
if (newEditor != null) {

newEditor.insertText(templateCode);
} else {
Messages.log("Failed to create new editor.");
}
} catch (IOException ex) {
Messages.log("Error reading template file: " + ex.getMessage());
ex.printStackTrace();
} catch (Exception ex) {
Messages.log("Unexpected error: " + ex.getMessage());
ex.printStackTrace();
}
});
templatesMenu.add(templateItem);
}
}





protected JMenu buildEditMenu() {
Expand Down Expand Up @@ -2302,7 +2404,9 @@ protected void handleSaveImpl() {
statusNotice(Language.text("editor.status.saving"));
try {
if (sketch.save()) {

statusNotice(Language.text("editor.status.saving.done"));

} else {
statusEmpty();
}
Expand Down
1 change: 1 addition & 0 deletions build/shared/lib/languages/PDE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ menu.file.recent = Open Recent
menu.file.sketchbook = Sketchbook...
menu.file.sketchbook.empty = Empty Sketchbook
menu.file.examples = Examples...
menu.file.templates=Templates
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be menu.file.templates=New from Template

cc @Stefterv

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay sir

menu.file.close = Close
menu.file.save = Save
menu.file.save_as = Save As...
Expand Down
9 changes: 9 additions & 0 deletions build/shared/lib/templates/Template1.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Basic Template
void setup() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and @SableRaf named these in his original suggestion, so please change the file names to those of something else more appropriate

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

size(800, 600);
background(255);
}

void draw() {
// Drawing code here
}
13 changes: 13 additions & 0 deletions build/shared/lib/templates/Template2.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Shape Drawing Template
void setup() {
size(800, 600);
background(255);
}

void draw() {
fill(150, 0, 0);
rect(100, 100, 200, 200);

fill(0, 150, 0);
ellipse(400, 300, 150, 150);
}
17 changes: 17 additions & 0 deletions build/shared/lib/templates/Template3.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Animation Template
float x = 0;
float speed = 2;

void setup() {
size(800, 600);
background(255);
}

void draw() {
background(255);
ellipse(x, height/2, 50, 50);
x += speed;
if (x > width || x < 0) {
speed *= -1;
}
}
15 changes: 15 additions & 0 deletions build/shared/lib/templates/Template4.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Interactive Template
void setup() {
size(800, 600);
background(255);
}

void draw() {
background(255);
fill(0, 0, 150);
ellipse(mouseX, mouseY, 50, 50);
}

void mousePressed() {
background(0, 255, 0);
}