@@ -1753,6 +1753,7 @@ protected boolean handleOpenInternal(File sketchFile) {
17531753
17541754 } else {
17551755 String properParent = fileName .substring (0 , fileName .length () - 4 );
1756+ File properFolder ;
17561757
17571758 Object [] options = {tr ("OK" ), tr ("Cancel" )};
17581759 String prompt = I18n .format (tr ("The file \" {0}\" needs to be inside\n " +
@@ -1761,36 +1762,61 @@ protected boolean handleOpenInternal(File sketchFile) {
17611762 fileName ,
17621763 properParent );
17631764
1764- int result = JOptionPane .showOptionDialog (this , prompt , tr ("Moving" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1765+ properFolder = new File (sketchFile .getParent (), properParent );
1766+
1767+ int result ;
1768+ if (parentFolderContainsSketchName (sketchFile , properParent )) {
1769+
1770+ // properFolder needs to be created one level above
1771+ properFolder = new File (new File (sketchFile .getParent ()).getParent (), properParent );
1772+
1773+ // ask for different confirmation
1774+ prompt = I18n .format (tr ("The file \" {0}\" needs to be inside\n " +
1775+ "a sketch folder named \" {1}\" .\n " +
1776+ "Renaming folder \" {2}\" into \" {3}\" \n " +
1777+ "Continue?" ),
1778+ fileName ,
1779+ properParent ,
1780+ sketchFile .getParent (),
1781+ properFolder );
1782+
1783+ result = JOptionPane .showOptionDialog (this , prompt , tr ("Renaming" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1784+ } else {
1785+ result = JOptionPane .showOptionDialog (this , prompt , tr ("Moving" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1786+ }
17651787
17661788 if (result != JOptionPane .YES_OPTION ) {
17671789 return false ;
17681790 }
17691791
1770- // create properly named folder
1771- File properFolder = new File (sketchFile .getParent (), properParent );
17721792 if (properFolder .exists ()) {
17731793 Base .showWarning (tr ("Error" ), I18n .format (tr ("A folder named \" {0}\" already exists. " +
17741794 "Can't open sketch." ), properParent ), null );
17751795 return false ;
17761796 }
1777- if (!properFolder .mkdirs ()) {
1778- //throw new IOException("Couldn't create sketch folder");
1779- Base .showWarning (tr ("Error" ), tr ("Could not create the sketch folder." ), null );
1780- return false ;
1781- }
1797+
17821798 // copy the sketch inside
17831799 File properPdeFile = new File (properFolder , sketchFile .getName ());
17841800 try {
1785- FileUtils .copy (new File (sketchFile .getParent ()), properFolder );
1801+ if (parentFolderContainsSketchName (sketchFile , properParent )) {
1802+ File dir = new File (sketchFile .getParent ());
1803+ dir .renameTo (properFolder );
1804+ } else {
1805+ // Create folder
1806+ if (!properFolder .mkdirs ()) {
1807+ //throw new IOException("Couldn't create sketch folder");
1808+ Base .showWarning (tr ("Error" ), tr ("Could not create the sketch folder." ), null );
1809+ return false ;
1810+ }
1811+ Base .copyFile (sketchFile , properPdeFile );
1812+ // remove the original file, so user doesn't get confused
1813+ sketchFile .delete ();
1814+ }
17861815 } catch (IOException e ) {
17871816 Base .showWarning (tr ("Error" ), tr ("Could not copy to a proper location." ), e );
17881817 return false ;
17891818 }
17901819
1791- // remove the original file, so user doesn't get confused
1792- sketchFile .delete ();
1793-
17941820 // update with the new path
17951821 file = properPdeFile ;
17961822
@@ -1813,6 +1839,11 @@ protected boolean handleOpenInternal(File sketchFile) {
18131839 return true ;
18141840 }
18151841
1842+ private boolean parentFolderContainsSketchName (File sketchFile , String sketchName ) {
1843+ String dir = sketchFile .getParent ().toLowerCase ();
1844+ return dir .contains (sketchName .toLowerCase ());
1845+ }
1846+
18161847 public void updateTitle () {
18171848 if (sketchController == null ) {
18181849 return ;
0 commit comments