@@ -1793,6 +1793,7 @@ protected boolean handleOpenInternal(File sketchFile) {
17931793
17941794 } else {
17951795 String properParent = fileName .substring (0 , fileName .length () - 4 );
1796+ File properFolder ;
17961797
17971798 Object [] options = {tr ("OK" ), tr ("Cancel" )};
17981799 String prompt = I18n .format (tr ("The file \" {0}\" needs to be inside\n " +
@@ -1801,36 +1802,61 @@ protected boolean handleOpenInternal(File sketchFile) {
18011802 fileName ,
18021803 properParent );
18031804
1804- int result = JOptionPane .showOptionDialog (this , prompt , tr ("Moving" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1805+ properFolder = new File (sketchFile .getParent (), properParent );
1806+
1807+ int result ;
1808+ if (parentFolderContainsSketchName (sketchFile , properParent )) {
1809+
1810+ // properFolder needs to be created one level above
1811+ properFolder = new File (new File (sketchFile .getParent ()).getParent (), properParent );
1812+
1813+ // ask for different confirmation
1814+ prompt = I18n .format (tr ("The file \" {0}\" needs to be inside\n " +
1815+ "a sketch folder named \" {1}\" .\n " +
1816+ "Renaming folder \" {2}\" into \" {3}\" \n " +
1817+ "Continue?" ),
1818+ fileName ,
1819+ properParent ,
1820+ sketchFile .getParent (),
1821+ properFolder );
1822+
1823+ result = JOptionPane .showOptionDialog (this , prompt , tr ("Renaming" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1824+ } else {
1825+ result = JOptionPane .showOptionDialog (this , prompt , tr ("Moving" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1826+ }
18051827
18061828 if (result != JOptionPane .YES_OPTION ) {
18071829 return false ;
18081830 }
18091831
1810- // create properly named folder
1811- File properFolder = new File (sketchFile .getParent (), properParent );
18121832 if (properFolder .exists ()) {
18131833 Base .showWarning (tr ("Error" ), I18n .format (tr ("A folder named \" {0}\" already exists. " +
18141834 "Can't open sketch." ), properParent ), null );
18151835 return false ;
18161836 }
1817- if (!properFolder .mkdirs ()) {
1818- //throw new IOException("Couldn't create sketch folder");
1819- Base .showWarning (tr ("Error" ), tr ("Could not create the sketch folder." ), null );
1820- return false ;
1821- }
1837+
18221838 // copy the sketch inside
18231839 File properPdeFile = new File (properFolder , sketchFile .getName ());
18241840 try {
1825- Base .copyFile (sketchFile , properPdeFile );
1841+ if (parentFolderContainsSketchName (sketchFile , properParent )) {
1842+ File dir = new File (sketchFile .getParent ());
1843+ dir .renameTo (properFolder );
1844+ } else {
1845+ // Create folder
1846+ if (!properFolder .mkdirs ()) {
1847+ //throw new IOException("Couldn't create sketch folder");
1848+ Base .showWarning (tr ("Error" ), tr ("Could not create the sketch folder." ), null );
1849+ return false ;
1850+ }
1851+ Base .copyFile (sketchFile , properPdeFile );
1852+ // remove the original file, so user doesn't get confused
1853+ sketchFile .delete ();
1854+ }
18261855 } catch (IOException e ) {
18271856 Base .showWarning (tr ("Error" ), tr ("Could not copy to a proper location." ), e );
18281857 return false ;
18291858 }
18301859
1831- // remove the original file, so user doesn't get confused
1832- sketchFile .delete ();
1833-
18341860 // update with the new path
18351861 file = properPdeFile ;
18361862
@@ -1853,6 +1879,11 @@ protected boolean handleOpenInternal(File sketchFile) {
18531879 return true ;
18541880 }
18551881
1882+ private boolean parentFolderContainsSketchName (File sketchFile , String sketchName ) {
1883+ String dir = sketchFile .getParent ().toLowerCase ();
1884+ return dir .contains (sketchName .toLowerCase ());
1885+ }
1886+
18561887 public void updateTitle () {
18571888 if (sketchController == null ) {
18581889 return ;
0 commit comments