@@ -1793,6 +1793,7 @@ protected boolean handleOpenInternal(File sketchFile) {
1793
1793
1794
1794
} else {
1795
1795
String properParent = fileName .substring (0 , fileName .length () - 4 );
1796
+ File properFolder ;
1796
1797
1797
1798
Object [] options = {tr ("OK" ), tr ("Cancel" )};
1798
1799
String prompt = I18n .format (tr ("The file \" {0}\" needs to be inside\n " +
@@ -1801,36 +1802,61 @@ protected boolean handleOpenInternal(File sketchFile) {
1801
1802
fileName ,
1802
1803
properParent );
1803
1804
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
+ }
1805
1827
1806
1828
if (result != JOptionPane .YES_OPTION ) {
1807
1829
return false ;
1808
1830
}
1809
1831
1810
- // create properly named folder
1811
- File properFolder = new File (sketchFile .getParent (), properParent );
1812
1832
if (properFolder .exists ()) {
1813
1833
Base .showWarning (tr ("Error" ), I18n .format (tr ("A folder named \" {0}\" already exists. " +
1814
1834
"Can't open sketch." ), properParent ), null );
1815
1835
return false ;
1816
1836
}
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
+
1822
1838
// copy the sketch inside
1823
1839
File properPdeFile = new File (properFolder , sketchFile .getName ());
1824
1840
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
+ }
1826
1855
} catch (IOException e ) {
1827
1856
Base .showWarning (tr ("Error" ), tr ("Could not copy to a proper location." ), e );
1828
1857
return false ;
1829
1858
}
1830
1859
1831
- // remove the original file, so user doesn't get confused
1832
- sketchFile .delete ();
1833
-
1834
1860
// update with the new path
1835
1861
file = properPdeFile ;
1836
1862
@@ -1853,6 +1879,11 @@ protected boolean handleOpenInternal(File sketchFile) {
1853
1879
return true ;
1854
1880
}
1855
1881
1882
+ private boolean parentFolderContainsSketchName (File sketchFile , String sketchName ) {
1883
+ String dir = sketchFile .getParent ().toLowerCase ();
1884
+ return dir .contains (sketchName .toLowerCase ());
1885
+ }
1886
+
1856
1887
public void updateTitle () {
1857
1888
if (sketchController == null ) {
1858
1889
return ;
0 commit comments