@@ -724,7 +724,7 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
724724 fileMenu .add (item );
725725
726726 JMenu templatesMenu = new JMenu (Language .text ("menu.file.templates" ));
727- System . out . println ("templatesMenu" );
727+ Messages . log ("templatesMenu" );
728728 loadTemplates (templatesMenu );
729729 fileMenu .add (templatesMenu );
730730
@@ -774,24 +774,13 @@ private void loadTemplates(JMenu templatesMenu) {
774774
775775 // Load predefined templates from the repository
776776 File repoTemplatesDir = Platform .getContentFile ("lib/templates" );
777- System .out .println ("repoTemplatesDir: " + repoTemplatesDir );
778- System .out .println ("Loading templates from repository directory: " + repoTemplatesDir .getAbsolutePath ());
779777 addTemplatesFromDirectory (repoTemplatesDir , allTemplates );
780778
781779 // Load user-defined templates from the Sketchbook folder
782780 File userTemplatesDir = new File (Base .getSketchbookFolder (), "templates" );
783-
784- System .out .println ("Loading templates from user sketchbook directory: " + userTemplatesDir .getAbsolutePath ());
785781 addTemplatesFromDirectory (userTemplatesDir , allTemplates );
786782
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- }
793783
794- // Add all templates to the menu
795784 addTemplatesToMenu (allTemplates , templatesMenu );
796785 }
797786
@@ -800,14 +789,13 @@ private void addTemplatesFromDirectory(File directory, List<File> allTemplates)
800789 File [] templateFiles = directory .listFiles ();
801790 if (templateFiles != null ) {
802791 for (File templateFile : templateFiles ) {
803- System .out .println ("Found template file: " + templateFile .getAbsolutePath ());
804792 allTemplates .add (templateFile );
805793 }
806794 } else {
807- System . out . println ("No template files found in directory: " + directory .getAbsolutePath ());
795+ Messages . log ("No template files found in directory: " + directory .getAbsolutePath ());
808796 }
809797 } else {
810- System . out . println ("Directory does not exist or is not a directory: " + directory .getAbsolutePath ());
798+ Messages . log ("Directory does not exist or is not a directory: " + directory .getAbsolutePath ());
811799 }
812800 }
813801
@@ -817,32 +805,55 @@ private void addTemplatesToMenu(List<File> templates, JMenu templatesMenu) {
817805 templatesMenu .removeAll (); // Clear existing menu items
818806 for (File templateFile : templates ) {
819807 String templateName ;
820- if (templateFile .getName ().toLowerCase ().endsWith (".pde" )) {
808+ if (templateFile .isDirectory ()) {
809+ // Handle directory case - might be a sketch folder
810+ templateName = templateFile .getName ();
811+ } else if (templateFile .getName ().toLowerCase ().endsWith (".pde" )) {
821812 templateName = templateFile .getName ().replace (".pde" , "" );
822813 } else {
823814 templateName = templateFile .getName ();
824815 }
816+
825817 JMenuItem templateItem = new JMenuItem (templateName );
826818 templateItem .addActionListener (e -> {
827819 try {
828- String templateCode ;
829- if (templateFile .getName ().toLowerCase ().endsWith (".pde" )) {
820+ String templateCode = "" ;
821+
822+ if (templateFile .isDirectory ()) {
823+ // It's a sketch folder, find the main .pde file with the same name as the folder
824+ File mainPdeFile = new File (templateFile , templateFile .getName () + ".pde" );
825+ if (mainPdeFile .exists ()) {
826+ templateCode = new String (Files .readAllBytes (mainPdeFile .toPath ()));
827+
828+ } else {
829+ // If main file doesn't exist, try to read any .pde file in the directory
830+ File [] pdeFiles = templateFile .listFiles ((dir , name ) -> name .toLowerCase ().endsWith (".pde" ));
831+ if (pdeFiles != null && pdeFiles .length > 0 ) {
832+ templateCode = new String (Files .readAllBytes (pdeFiles [0 ].toPath ()));
833+
834+ }
835+ }
836+ } else if (templateFile .getName ().toLowerCase ().endsWith (".pde" )) {
837+
830838 templateCode = new String (Files .readAllBytes (templateFile .toPath ()));
831- System . out . println ( "Template code read from file: " + templateCode );
839+
832840 } 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 ());
841+ templateCode = new String (Files .readAllBytes (templateFile .toPath ()));
842+ Messages .log ("Unrecognized file type: " + templateFile .getAbsolutePath ());
836843 }
837844
838845 Editor newEditor = base .handleNew ();
839846 if (newEditor != null ) {
840- System . out . println ( "New editor created. Inserting template code." );
847+
841848 newEditor .insertText (templateCode );
842849 } else {
843- System . err . println ("Failed to create new editor." );
850+ Messages . log ("Failed to create new editor." );
844851 }
845852 } catch (IOException ex ) {
853+ Messages .log ("Error reading template file: " + ex .getMessage ());
854+ ex .printStackTrace ();
855+ } catch (Exception ex ) {
856+ Messages .log ("Unexpected error: " + ex .getMessage ());
846857 ex .printStackTrace ();
847858 }
848859 });
@@ -851,34 +862,7 @@ private void addTemplatesToMenu(List<File> templates, JMenu templatesMenu) {
851862 }
852863
853864
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- // }
877- // Add this method to insert template code into the editor
878- private void insertTemplateCode (String templateCode ) {
879- //textarea.setText("");
880- insertText (templateCode );
881- }
865+
882866
883867
884868 protected JMenu buildEditMenu () {
@@ -2420,7 +2404,9 @@ protected void handleSaveImpl() {
24202404 statusNotice (Language .text ("editor.status.saving" ));
24212405 try {
24222406 if (sketch .save ()) {
2407+
24232408 statusNotice (Language .text ("editor.status.saving.done" ));
2409+
24242410 } else {
24252411 statusEmpty ();
24262412 }
0 commit comments