Ignore:
Timestamp:
05/01/08 12:26:53 (16 years ago)
Author:
ra33
Message:

New expeditee version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gui/FrameIO.java

    r4 r7  
    44import java.io.BufferedWriter;
    55import java.io.File;
     6import java.io.FileInputStream;
     7import java.io.FileOutputStream;
    68import java.io.FileReader;
    79import java.io.FileWriter;
     
    130132        public static Frame LoadFrame(String frameName) {
    131133                // 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);
    134136                        frameName = DisplayIO.getCurrentFrame().getFramesetNameAdjusted()
    135137                                        + frameName;
     
    160162
    161163                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();
    162185        }
    163186
     
    267290
    268291        public static Frame LoadPrevious(Frame current) {
     292                checkTDFC(current);
     293
    269294                // the current name and number
    270295                String name = current.getFramesetNameAdjusted();
     
    292317         */
    293318        public static Frame LoadNext(Frame current) {
     319                checkTDFC(current);
    294320
    295321                // the current name and number
     
    318344                FrameGraphics.DisplayMessage("This is the last frame in the frameset");
    319345                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                }
    320361        }
    321362
     
    331372                }
    332373
    333                 String adjustedName = Frame.GetFramesetNameAdjusted(framesetName);
     374                String adjustedName = FrameUtils.GetFramesetNameAdjusted(framesetName);
    334375
    335376                // loop backwards until a frame that exists is found
     
    368409         */
    369410        public static boolean DeleteFrame(Frame toDelete) throws IOException {
     411                if (toDelete == null)
     412                        return false;
     413
    370414                SaveFrame(toDelete);
    371415
     
    374418                int lastNumber = FrameIO.getLastNumber(DELETED_FRAMES);
    375419                String framePath;
    376                 if (lastNumber < 0) {
     420                try{
    377421                        // create the new frameset
    378422                        Frame one = FrameIO.CreateFrameset(DELETED_FRAMES, toDelete.path);
    379423                        framePath = one.path;
    380424                        lastNumber = 0;
    381                 } else {
     425                } catch(Exception e){
    382426                        Frame zero = FrameIO.LoadFrame(DELETED_FRAMES + "0");
    383427                        framePath = zero.path;
     
    426470                SuspendCache();
    427471
    428                 String adjustFramesetName = Frame.GetFramesetNameAdjusted(frameset);
     472                String adjustFramesetName = FrameUtils
     473                                .GetFramesetNameAdjusted(frameset);
    429474                Frame template = null;
    430475                if (templateFrame == null)
     
    520565         * @return The first Frame of the new Frameset (Frame.1)
    521566         */
    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);
    584569        }
    585570
     
    600585
    601586                // 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)))
    603588                                || !Character.isDigit(frameName.charAt(frameName.length() - 1)))
    604589                        return false;
    605590
    606                 int dotIndex = frameName.indexOf(".");
    607                 // All the characters inbetween first and last must be letters
    608                 // or digits
     591                int dotIndex = frameName.lastIndexOf('.');
     592                // All the characters between first and last must be letters
     593                // or digits or period
    609594                for (int i = 1; i < frameName.length(); i++) {
    610595                        if (dotIndex > 0 && i > dotIndex) {
     
    645630
    646631                if (toSave == null)
     632                        return "";
     633
     634                if (!toSave.hasChanged() || toSave.isSaved())
    647635                        return "";
    648636
     
    713701                        }
    714702                } catch (IOException ioe) {
    715                         ioe.printStackTrace();
     703                        // ioe.printStackTrace();
    716704                        Logger.Log(ioe);
     705                        return null;
    717706                }
    718707
     
    724713        }
    725714
    726         public static Frame CreateNewProfile(String username) {
     715        public static Frame CreateNewProfile(String username) throws Exception{
    727716                Frame profile = CreateFrameset(username, PROFILE_PATH);
    728717                FrameUtils.CreateDefaultProfile(profile);
     
    782771        }
    783772
    784         private static String NameValidation(String toValidate) {
     773        private static String NameValidation(String toValidate) throws Exception{
    785774                String result = "";
    786775
     
    791780                        // check for illegal characters
    792781                        if (ILLEGAL_CHARS.contains("" + cur)) {
    793                                 FrameGraphics
    794                                                 .DisplayMessage("Frameset name contains illegal character '"
     782                                throw new Exception("Frameset name contains illegal character '"
    795783                                                                + cur + "' at position " + (i + 1));
    796                                 return null;
    797784                        }
    798785
     
    808795
    809796                                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 "
    812798                                                                        + MAX_NAME_LENGTH + " characters)");
    813                                         return null;
    814799                                }
    815800                        }
     
    832817                // do auto shrinking of the title
    833818                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()) {
    836822                        titleItem.setSize(titleItem.getSize() - 1);
    837823                }
    838                        
    839824
    840825                return newFrame;
     
    850835         *         frame)
    851836         */
    852         public static Frame CreateNewFrameset(String name) {
     837        public static Frame CreateNewFrameset(String name) throws Exception{
    853838                String path = DisplayIO.getCurrentFrame().path;
    854839
     
    896881        }
    897882
     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
    8981032}
Note: See TracChangeset for help on using the changeset viewer.