@@ -1753,6 +1753,7 @@ protected boolean handleOpenInternal(File sketchFile) {
1753
1753
1754
1754
} else {
1755
1755
String properParent = fileName .substring (0 , fileName .length () - 4 );
1756
+ File properFolder ;
1756
1757
1757
1758
Object [] options = {tr ("OK" ), tr ("Cancel" )};
1758
1759
String prompt = I18n .format (tr ("The file \" {0}\" needs to be inside\n " +
@@ -1761,40 +1762,51 @@ protected boolean handleOpenInternal(File sketchFile) {
1761
1762
fileName ,
1762
1763
properParent );
1763
1764
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 );
1765
1766
1766
- if (result != JOptionPane .YES_OPTION ) {
1767
- return false ;
1768
- }
1767
+ int result ;
1768
+ if (parentFolderContainsSketchName (sketchFile , properParent )) {
1769
1769
1770
- // create properly named folder
1771
- File properFolder ;
1772
- if (onlyContainsSketchFiles (sketchFile .getParent ())) {
1773
- // Move the whole folder, so properFolder needs to be created one level above
1770
+ // properFolder needs to be created one level above
1774
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 ("Moving" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1775
1784
} else {
1776
- properFolder = new File (sketchFile .getParent (), properParent );
1785
+ result = JOptionPane .showOptionDialog (this , prompt , tr ("Moving" ), JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
1786
+ }
1787
+
1788
+ if (result != JOptionPane .YES_OPTION ) {
1789
+ return false ;
1777
1790
}
1778
1791
1779
1792
if (properFolder .exists ()) {
1780
1793
Base .showWarning (tr ("Error" ), I18n .format (tr ("A folder named \" {0}\" already exists. " +
1781
1794
"Can't open sketch." ), properParent ), null );
1782
1795
return false ;
1783
1796
}
1784
- if (!properFolder .mkdirs ()) {
1785
- //throw new IOException("Couldn't create sketch folder");
1786
- Base .showWarning (tr ("Error" ), tr ("Could not create the sketch folder." ), null );
1787
- return false ;
1788
- }
1797
+
1789
1798
// copy the sketch inside
1790
1799
File properPdeFile = new File (properFolder , sketchFile .getName ());
1791
1800
try {
1792
- if (onlyContainsSketchFiles (sketchFile . getParent () )) {
1801
+ if (parentFolderContainsSketchName (sketchFile , properParent )) {
1793
1802
File dir = new File (sketchFile .getParent ());
1794
- FileUtils .copy (dir , properFolder );
1795
- // remove the original folder, so user doesn't get confused
1796
- FileUtils .recursiveDelete (dir );
1803
+ dir .renameTo (properFolder );
1797
1804
} else {
1805
+ if (!properFolder .mkdirs ()) {
1806
+ //throw new IOException("Couldn't create sketch folder");
1807
+ Base .showWarning (tr ("Error" ), tr ("Could not create the sketch folder." ), null );
1808
+ return false ;
1809
+ }
1798
1810
Base .copyFile (sketchFile , properPdeFile );
1799
1811
// remove the original file, so user doesn't get confused
1800
1812
sketchFile .delete ();
@@ -1826,38 +1838,10 @@ protected boolean handleOpenInternal(File sketchFile) {
1826
1838
return true ;
1827
1839
}
1828
1840
1829
- private boolean allowedExtension (String fileName , String [] validExtensions ) {
1830
- int i = fileName .lastIndexOf ('.' );
1831
- if (i > 0 ) {
1832
- String ext = fileName .substring (i +1 ).toLowerCase ();
1833
- if (!Arrays .asList (validExtensions ).contains (ext )) {
1834
- return false ;
1835
- }
1836
- }
1837
- // no extension or valid extension
1838
- return true ;
1839
- }
1840
-
1841
- private boolean onlyContainsSketchFiles (String path ) {
1842
- File folder = new File (path );
1843
- File [] listOfFiles = folder .listFiles ();
1844
-
1845
- for (int i = 0 ; i < listOfFiles .length ; i ++) {
1846
- String name = listOfFiles [i ].getName ();
1847
- if (listOfFiles [i ].isFile ()) {
1848
- // allowed files extensions are only .ino, .h, .hpp, .c, .cpp (all cases)
1849
- String [] valid = {"ino" , "h" , "hpp" , "c" , "cpp" , "pde" };
1850
- if (!allowedExtension (name , valid )) {
1851
- return false ;
1852
- }
1853
- } else if (listOfFiles [i ].isDirectory ()) {
1854
- // allowed dir names are only src and extras , plus source control folders
1855
- if (name != "src" && name != "extras" && !FileUtils .SOURCE_CONTROL_FOLDERS .contains (name )) {
1856
- return false ;
1857
- }
1858
- }
1859
- }
1860
- return true ;
1841
+ private boolean parentFolderContainsSketchName (File sketchFile , String sketchName ) {
1842
+ String dir = sketchFile .getParent ().toLowerCase ();
1843
+ System .out .println (dir + " " + sketchName );
1844
+ return dir .contains (sketchName .toLowerCase ());
1861
1845
}
1862
1846
1863
1847
public void updateTitle () {
0 commit comments