Skip to content

Commit db4b6b9

Browse files
committed
Add template in file->Templates 2.0
1 parent f727013 commit db4b6b9

File tree

7 files changed

+166
-31
lines changed

7 files changed

+166
-31
lines changed

app/src/processing/app/Base.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,16 +1099,15 @@ public void modeRemoved(Mode mode) {
10991099
/**
11001100
* Create a new untitled document in a new sketch window.
11011101
*/
1102-
public void handleNew() {
1103-
// long t1 = System.currentTimeMillis();
1102+
public Editor handleNew() {
11041103
try {
11051104
// In 0126, untitled sketches will begin in the temp folder,
11061105
// and then moved to a new location because Save will default to Save As.
11071106
//File sketchbookDir = getSketchbookFolder();
11081107
File newbieDir = SketchName.nextFolder(untitledFolder);
11091108

11101109
// User was told to go outside or other problem happened inside naming.
1111-
if (newbieDir == null) return;
1110+
if (newbieDir == null) return null;
11121111

11131112
// Make the directory for the new sketch
11141113
if (!newbieDir.mkdirs()) {
@@ -1128,16 +1127,16 @@ public void handleNew() {
11281127
}
11291128

11301129
String path = newbieFile.getAbsolutePath();
1131-
handleOpenUntitled(path);
1130+
return handleOpenUntitled(path);
11321131

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

1140-
11411140
/**
11421141
* Prompt for a sketch to open, and open it in a new window.
11431142
*/

app/src/processing/app/ui/Editor.java

Lines changed: 105 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Timer;
4040
import java.util.TimerTask;
4141
import java.util.stream.Collectors;
42+
import java.nio.file.Files;
4243

4344
import javax.swing.*;
4445
import javax.swing.border.EmptyBorder;
@@ -722,11 +723,10 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
722723
item.addActionListener(e -> handleSaveAs());
723724
fileMenu.add(item);
724725

725-
item = Toolkit.newJMenuItemShift(Language.text("menu.file.templates"), 'T');
726-
//JMenuItem finalItem = item;
727-
item.addActionListener(e -> handleTemplate());
728-
fileMenu.add(item);
729-
726+
JMenu templatesMenu = new JMenu(Language.text("menu.file.templates"));
727+
System.out.println("templatesMenu");
728+
loadTemplates(templatesMenu);
729+
fileMenu.add(templatesMenu);
730730

731731

732732
if (exportItems != null) {
@@ -768,34 +768,115 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
768768
}
769769
return fileMenu;
770770
}
771-
// Define the handleTemplate method to show different templates
772-
private void handleTemplate() {
773-
// Create a popup menu for templates
774-
JPopupMenu templatesMenu = new JPopupMenu("Templates");
771+
// Load templates from both the repository and the user's Sketchbook folder
772+
private void loadTemplates(JMenu templatesMenu) {
773+
List<File> allTemplates = new ArrayList<>();
775774

776-
JMenuItem template1 = new JMenuItem("Animation Sketch");
777-
template1.addActionListener(e -> insertTemplateCode(Template.animationSketchCode));
778-
templatesMenu.add(template1);
775+
// Load predefined templates from the repository
776+
File repoTemplatesDir = Platform.getContentFile("lib/templates");
777+
System.out.println("repoTemplatesDir: " + repoTemplatesDir);
778+
System.out.println("Loading templates from repository directory: " + repoTemplatesDir.getAbsolutePath());
779+
addTemplatesFromDirectory(repoTemplatesDir, allTemplates);
779780

780-
JMenuItem template2 = new JMenuItem("Interactive Sketch");
781-
template2.addActionListener(e -> insertTemplateCode(Template.interactiveSketchCode));
782-
templatesMenu.add(template2);
781+
// Load user-defined templates from the Sketchbook folder
782+
File userTemplatesDir = new File(Base.getSketchbookFolder(), "templates");
783783

784-
JMenuItem template3 = new JMenuItem("Fullscreen Sketch");
785-
template3.addActionListener(e -> insertTemplateCode(Template.fullscreenSketchCode));
786-
templatesMenu.add(template3);
784+
System.out.println("Loading templates from user sketchbook directory: " + userTemplatesDir.getAbsolutePath());
785+
addTemplatesFromDirectory(userTemplatesDir, allTemplates);
787786

788-
JMenuItem template4 = new JMenuItem("Resizeable Sketch");
789-
template4.addActionListener(e -> insertTemplateCode(Template.resizeableSketchCode));
790-
templatesMenu.add(template4);
787+
// Print all templates to the console
788+
System.out.println("All loaded templates:");
789+
System.out.println(allTemplates.size());
790+
for (File template : allTemplates) {
791+
System.out.println("Template: " + template.getAbsolutePath());
792+
}
791793

792-
// Show the popup menu at the location where the "Templates" menu item was clicked
793-
templatesMenu.show(this, 0, 0);
794+
// Add all templates to the menu
795+
addTemplatesToMenu(allTemplates, templatesMenu);
794796
}
795797

798+
private void addTemplatesFromDirectory(File directory, List<File> allTemplates) {
799+
if (directory.exists() && directory.isDirectory()) {
800+
File[] templateFiles = directory.listFiles();
801+
if (templateFiles != null) {
802+
for (File templateFile : templateFiles) {
803+
System.out.println("Found template file: " + templateFile.getAbsolutePath());
804+
allTemplates.add(templateFile);
805+
}
806+
} else {
807+
System.out.println("No template files found in directory: " + directory.getAbsolutePath());
808+
}
809+
} else {
810+
System.out.println("Directory does not exist or is not a directory: " + directory.getAbsolutePath());
811+
}
812+
}
813+
814+
// Add templates to the menu
815+
// Add templates to the menu
816+
private void addTemplatesToMenu(List<File> templates, JMenu templatesMenu) {
817+
templatesMenu.removeAll(); // Clear existing menu items
818+
for (File templateFile : templates) {
819+
String templateName;
820+
if (templateFile.getName().toLowerCase().endsWith(".pde")) {
821+
templateName = templateFile.getName().replace(".pde", "");
822+
} else {
823+
templateName = templateFile.getName();
824+
}
825+
JMenuItem templateItem = new JMenuItem(templateName);
826+
templateItem.addActionListener(e -> {
827+
try {
828+
String templateCode;
829+
if (templateFile.getName().toLowerCase().endsWith(".pde")) {
830+
templateCode = new String(Files.readAllBytes(templateFile.toPath()));
831+
System.out.println("Template code read from file: " + templateCode);
832+
} else {
833+
templateCode = "";
834+
// System.out.println("Addtemplatestomenu--suserdefned"+templateFile);// Or any other action needed for non-.pde files
835+
System.out.println("Non-.pde file selected: " + templateFile.getAbsolutePath());
836+
}
837+
838+
Editor newEditor = base.handleNew();
839+
if (newEditor != null) {
840+
System.out.println("New editor created. Inserting template code.");
841+
newEditor.insertText(templateCode);
842+
} else {
843+
System.err.println("Failed to create new editor.");
844+
}
845+
} catch (IOException ex) {
846+
ex.printStackTrace();
847+
}
848+
});
849+
templatesMenu.add(templateItem);
850+
}
851+
}
852+
853+
854+
// Method to save user-defined templates in the sketchbook/templates directory
855+
// public void saveUserTemplate(String templateName, String templateCode) {
856+
// File userTemplatesDir = new File(Base.getSketchbookFolder(), "templates");
857+
// if (!userTemplatesDir.exists()) {
858+
// userTemplatesDir.mkdirs();
859+
// }
860+
//
861+
// File templateFile = new File(userTemplatesDir, templateName + ".pde");
862+
// try {
863+
// Files.write(templateFile.toPath(), templateCode.getBytes());
864+
// System.out.println("User template saved: " + templateFile.getAbsolutePath());
865+
//
866+
// // Reload templates after saving a new one
867+
// JMenu templatesMenu = new JMenu(Language.text("menu.file.templates"));
868+
// loadTemplates(templatesMenu);
869+
//
870+
// // Update the File menu with the new templates menu
871+
// JMenu fileMenu = buildFileMenu(null);
872+
// fileMenu.add(templatesMenu);
873+
// } catch (IOException e) {
874+
// e.printStackTrace();
875+
// }
876+
// }
796877
// Add this method to insert template code into the editor
797878
private void insertTemplateCode(String templateCode) {
798-
textarea.setText("");
879+
//textarea.setText("");
799880
insertText(templateCode);
800881
}
801882

build/shared/lib/languages/PDE.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ menu.file.recent = Open Recent
2121
menu.file.sketchbook = Sketchbook...
2222
menu.file.sketchbook.empty = Empty Sketchbook
2323
menu.file.examples = Examples...
24+
menu.file.templates=Templates
2425
menu.file.close = Close
2526
menu.file.save = Save
2627
menu.file.save_as = Save As...
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Basic Template
2+
void setup() {
3+
size(800, 600);
4+
background(255);
5+
}
6+
7+
void draw() {
8+
// Drawing code here
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Shape Drawing Template
2+
void setup() {
3+
size(800, 600);
4+
background(255);
5+
}
6+
7+
void draw() {
8+
fill(150, 0, 0);
9+
rect(100, 100, 200, 200);
10+
11+
fill(0, 150, 0);
12+
ellipse(400, 300, 150, 150);
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Animation Template
2+
float x = 0;
3+
float speed = 2;
4+
5+
void setup() {
6+
size(800, 600);
7+
background(255);
8+
}
9+
10+
void draw() {
11+
background(255);
12+
ellipse(x, height/2, 50, 50);
13+
x += speed;
14+
if (x > width || x < 0) {
15+
speed *= -1;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Interactive Template
2+
void setup() {
3+
size(800, 600);
4+
background(255);
5+
}
6+
7+
void draw() {
8+
background(255);
9+
fill(0, 0, 150);
10+
ellipse(mouseX, mouseY, 50, 50);
11+
}
12+
13+
void mousePressed() {
14+
background(0, 255, 0);
15+
}

0 commit comments

Comments
 (0)