Changeset 7 for trunk/src/org/expeditee/gui/FrameIO.java
- Timestamp:
- 05/01/08 12:26:53 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/gui/FrameIO.java
r4 r7 4 4 import java.io.BufferedWriter; 5 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 6 8 import java.io.FileReader; 7 9 import java.io.FileWriter; … … 130 132 public static Frame LoadFrame(String frameName) { 131 133 // If the frame name is a positive integer add the frameset name 132 if (isPositiveInteger(frameName)) {133 assert (false);134 if (isPositiveInteger(frameName)) { 135 assert (false); 134 136 frameName = DisplayIO.getCurrentFrame().getFramesetNameAdjusted() 135 137 + frameName; … … 160 162 161 163 return loaded; 164 } 165 166 /** 167 * Gets a list of all the framesets available to the user 168 * 169 * @return a string containing a list of all the available framesets on 170 * separate lines 171 */ 172 public static String getFramesetList() { 173 StringBuffer list = new StringBuffer(); 174 175 for (String path : UserSettings.FrameDirs) { 176 for (File f : (new File(path)).listFiles()) { 177 if (f.isDirectory()) { 178 list.append(f.getName()).append('\n'); 179 } 180 } 181 } 182 // remove the final new line char 183 list.deleteCharAt(list.length() - 1); 184 return list.toString(); 162 185 } 163 186 … … 267 290 268 291 public static Frame LoadPrevious(Frame current) { 292 checkTDFC(current); 293 269 294 // the current name and number 270 295 String name = current.getFramesetNameAdjusted(); … … 292 317 */ 293 318 public static Frame LoadNext(Frame current) { 319 checkTDFC(current); 294 320 295 321 // the current name and number … … 318 344 FrameGraphics.DisplayMessage("This is the last frame in the frameset"); 319 345 return null; 346 } 347 348 /** 349 * This method checks if the current frame has just been created with TDFC. 350 * If it has the frame is saved regardless of whether it has been edited or 351 * not and the TDFC item property is cleared. This is to ensure that the 352 * link is saved on the parent frame. 353 * 354 * @param current 355 */ 356 private static void checkTDFC(Frame current) { 357 if (FrameMouseActions.getTdfcItem() != null) { 358 FrameMouseActions.setTdfcItem(null); 359 current.change(); 360 } 320 361 } 321 362 … … 331 372 } 332 373 333 String adjustedName = Frame .GetFramesetNameAdjusted(framesetName);374 String adjustedName = FrameUtils.GetFramesetNameAdjusted(framesetName); 334 375 335 376 // loop backwards until a frame that exists is found … … 368 409 */ 369 410 public static boolean DeleteFrame(Frame toDelete) throws IOException { 411 if (toDelete == null) 412 return false; 413 370 414 SaveFrame(toDelete); 371 415 … … 374 418 int lastNumber = FrameIO.getLastNumber(DELETED_FRAMES); 375 419 String framePath; 376 if (lastNumber < 0){420 try{ 377 421 // create the new frameset 378 422 Frame one = FrameIO.CreateFrameset(DELETED_FRAMES, toDelete.path); 379 423 framePath = one.path; 380 424 lastNumber = 0; 381 } else{425 } catch(Exception e){ 382 426 Frame zero = FrameIO.LoadFrame(DELETED_FRAMES + "0"); 383 427 framePath = zero.path; … … 426 470 SuspendCache(); 427 471 428 String adjustFramesetName = Frame.GetFramesetNameAdjusted(frameset); 472 String adjustFramesetName = FrameUtils 473 .GetFramesetNameAdjusted(frameset); 429 474 Frame template = null; 430 475 if (templateFrame == null) … … 520 565 * @return The first Frame of the new Frameset (Frame.1) 521 566 */ 522 public static Frame CreateFrameset(String frameset, String path) { 523 String conversion = frameset + " --> "; 524 525 // ensure the framename is valid 526 frameset = NameValidation(frameset); 527 528 if (frameset == null) 529 return null; 530 531 conversion += frameset; 532 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Frameset Name: " 533 + conversion); 534 conversion = frameset; 535 536 /** 537 * TODO: Update this to exclude any\all invalid filename characters 538 */ 539 // ignore annotation character 540 if (frameset.startsWith("@")) 541 frameset = frameset.substring(1); 542 543 conversion += " --> " + frameset; 544 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Name: " + conversion); 545 546 // create the new Frameset directory 547 File dir = new File(path + frameset.toLowerCase() + File.separator); 548 549 dir.mkdirs(); 550 551 // create the new INF file 552 try { 553 WriteINF(path, frameset, frameset + getFramesetExtension(frameset)); 554 } catch (IOException ioe) { 555 ioe.printStackTrace(); 556 Logger.Log(ioe); 557 } 558 559 SuspendCache(); 560 // copy the default .0 and .1 files 561 Frame base; 562 try { 563 base = LoadFrame(UserSettings.DefaultFrame); 564 } catch (Exception e) { 565 base = new Frame(); 566 } 567 ResumeCache(); 568 569 base.resetDateCreated(); 570 base.setFrameset(frameset); 571 base.setFrameNumber(0); 572 base.path = path; 573 SaveFrame(base, false); 574 575 base.resetDateCreated(); 576 base.setFrameNumber(1); 577 base.setTitle(frameset); 578 SaveFrame(base, true); 579 580 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Created new frameset: " 581 + frameset); 582 583 return base; 567 public static Frame CreateFrameset(String frameset, String path) throws Exception{ 568 return CreateFrameset(frameset, path, false); 584 569 } 585 570 … … 600 585 601 586 // String must begin with a letter and end with a digit 602 if (! Character.isLetter(frameName.charAt(0))587 if (!(Character.isLetter(frameName.charAt(0)) || Character.isDigit(frameName.charAt(0))) 603 588 || !Character.isDigit(frameName.charAt(frameName.length() - 1))) 604 589 return false; 605 590 606 int dotIndex = frameName. indexOf(".");607 // All the characters inbetween first and last must be letters608 // or digits 591 int dotIndex = frameName.lastIndexOf('.'); 592 // All the characters between first and last must be letters 593 // or digits or period 609 594 for (int i = 1; i < frameName.length(); i++) { 610 595 if (dotIndex > 0 && i > dotIndex) { … … 645 630 646 631 if (toSave == null) 632 return ""; 633 634 if (!toSave.hasChanged() || toSave.isSaved()) 647 635 return ""; 648 636 … … 713 701 } 714 702 } catch (IOException ioe) { 715 ioe.printStackTrace();703 // ioe.printStackTrace(); 716 704 Logger.Log(ioe); 705 return null; 717 706 } 718 707 … … 724 713 } 725 714 726 public static Frame CreateNewProfile(String username) {715 public static Frame CreateNewProfile(String username) throws Exception{ 727 716 Frame profile = CreateFrameset(username, PROFILE_PATH); 728 717 FrameUtils.CreateDefaultProfile(profile); … … 782 771 } 783 772 784 private static String NameValidation(String toValidate) {773 private static String NameValidation(String toValidate) throws Exception{ 785 774 String result = ""; 786 775 … … 791 780 // check for illegal characters 792 781 if (ILLEGAL_CHARS.contains("" + cur)) { 793 FrameGraphics 794 .DisplayMessage("Frameset name contains illegal character '" 782 throw new Exception("Frameset name contains illegal character '" 795 783 + cur + "' at position " + (i + 1)); 796 return null;797 784 } 798 785 … … 808 795 809 796 if (result.length() >= MAX_NAME_LENGTH) { 810 FrameGraphics 811 .DisplayMessage("Frameset name is too long (Max " 797 throw new Exception("Frameset name is too long (Max " 812 798 + MAX_NAME_LENGTH + " characters)"); 813 return null;814 799 } 815 800 } … … 832 817 // do auto shrinking of the title 833 818 Text titleItem = newFrame.getTitle(); 834 835 while(titleItem.getBoundsWidth() + titleItem.getX() > newFrame.getFrameNameItem().getX() ) { 819 820 while (titleItem.getBoundsWidth() + titleItem.getX() > newFrame 821 .getFrameNameItem().getX()) { 836 822 titleItem.setSize(titleItem.getSize() - 1); 837 823 } 838 839 824 840 825 return newFrame; … … 850 835 * frame) 851 836 */ 852 public static Frame CreateNewFrameset(String name) {837 public static Frame CreateNewFrameset(String name) throws Exception{ 853 838 String path = DisplayIO.getCurrentFrame().path; 854 839 … … 896 881 } 897 882 883 public static Boolean DoesFramesetExist(String framesetName) { 884 String zeroFrame = FrameUtils.GetFramesetNameAdjusted(framesetName) + '0'; 885 return FrameIO.DoesFrameExist(zeroFrame); 886 } 887 888 public static Frame CreateFrameset(String frameset, String path, 889 boolean recreate) throws Exception{ 890 String conversion = frameset + " --> "; 891 892 // ensure the framename is valid 893 frameset = NameValidation(frameset); 894 895 if (frameset == null) 896 throw new Exception("Invalid frameset name"); 897 898 if (!recreate && FrameIO.DoesFramesetExist(frameset)) { 899 throw new Exception("A frameset called " + frameset + " already exists."); 900 } 901 902 conversion += frameset; 903 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Frameset Name: " 904 + conversion); 905 conversion = frameset; 906 907 /** 908 * TODO: Update this to exclude any\all invalid filename characters 909 */ 910 // ignore annotation character 911 if (frameset.startsWith("@")) 912 frameset = frameset.substring(1); 913 914 conversion += " --> " + frameset; 915 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Name: " + conversion); 916 917 // create the new Frameset directory 918 File dir = new File(path + frameset.toLowerCase() + File.separator); 919 920 dir.mkdirs(); 921 922 // create the new INF file 923 try { 924 WriteINF(path, frameset, frameset + getFramesetExtension(frameset)); 925 } catch (IOException ioe) { 926 ioe.printStackTrace(); 927 Logger.Log(ioe); 928 } 929 930 SuspendCache(); 931 // copy the default .0 and .1 files 932 Frame base; 933 try { 934 base = LoadFrame(UserSettings.DefaultFrame); 935 } catch (Exception e) { 936 base = new Frame(); 937 } 938 ResumeCache(); 939 940 base.resetDateCreated(); 941 base.setFrameset(frameset); 942 base.setFrameNumber(0); 943 base.setTitle(base.getFramesetNameAdjusted() + "0"); 944 base.path = path; 945 base.change(); 946 SaveFrame(base, false); 947 948 base.resetDateCreated(); 949 base.setFrameNumber(1); 950 base.setTitle(frameset); 951 base.change(); 952 SaveFrame(base, true); 953 954 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Created new frameset: " 955 + frameset); 956 957 return base; 958 } 959 960 public static boolean DeleteFrameset(String framesetName) { 961 if (!FrameIO.DoesFramesetExist(framesetName)) 962 return false; 963 964 for (String path : UserSettings.FrameDirs) { 965 String source = path + framesetName.toLowerCase() + File.separator; 966 File framesetDirectory = new File(source); 967 if (framesetDirectory.exists()) { 968 for (File f : framesetDirectory.listFiles()) 969 f.delete(); 970 return framesetDirectory.delete(); 971 } 972 } 973 return false; 974 } 975 976 public static boolean CopyFrameset(String framesetToCopy, 977 String copiedFrameset) throws Exception { 978 if (!FrameIO.DoesFramesetExist(framesetToCopy)) 979 return false; 980 if (FrameIO.DoesFramesetExist(copiedFrameset)) 981 return false; 982 // search through all the directories to find the frameset we are 983 // copying 984 for (String path : UserSettings.FrameDirs) { 985 String source = path + framesetToCopy.toLowerCase() 986 + File.separator; 987 File framesetDirectory = new File(source); 988 if (framesetDirectory.exists()) { 989 // copy the frameset 990 File copyFramesetDirectory = new File(path 991 + copiedFrameset.toLowerCase() + File.separator); 992 if (!copyFramesetDirectory.mkdirs()) 993 return false; 994 // copy each of the frames 995 for (File f : framesetDirectory.listFiles()) { 996 String copyPath = copyFramesetDirectory.getAbsolutePath() 997 + File.separator + f.getName(); 998 FrameIO.copyFile(f.getAbsolutePath(), copyPath); 999 } 1000 return true; 1001 } 1002 } 1003 return false; 1004 } 1005 1006 public static void copyFile(String existingFile, String newFileName) 1007 throws Exception { 1008 FileInputStream is = new FileInputStream(existingFile); 1009 FileOutputStream os = new FileOutputStream(newFileName, false); 1010 int data; 1011 while ((data = is.read()) != -1) { 1012 os.write(data); 1013 } 1014 os.flush(); 1015 os.close(); 1016 is.close(); 1017 } 1018 1019 /** 1020 * Saves a frame regardless of whether or not the frame is marked as having 1021 * been changed. 1022 * 1023 * @param frame 1024 * the frame to save 1025 * @return the contents of the frame or null if it could not be saved 1026 */ 1027 public static String ForceSaveFrame(Frame frame) { 1028 frame.change(); 1029 return SaveFrame(frame, false); 1030 } 1031 898 1032 }
Note:
See TracChangeset
for help on using the changeset viewer.