Class RecentFiles


  • public class RecentFiles
    extends java.lang.Object
    A class to save recent file history in preference using java Preference api.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Queue<java.lang.String> filePaths  
      private static java.lang.String HISTORY_LENGTH  
      private static java.lang.String KEY  
      private int maximum  
      private static java.lang.String PATH_KEY  
      private static java.lang.String PIECES_LENGTH_KEY  
      private java.util.prefs.Preferences pref  
    • Constructor Summary

      Constructors 
      Constructor Description
      RecentFiles​(java.lang.Class className, int maximumFile)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFile​(java.lang.String path)
      Add a new file in recent file history.
      private java.lang.String[] breakString​(java.lang.String fullPath)  
      void close()
      This method save the present recent file history in the preference.
      java.util.List<java.lang.String> getFiles()
      This gives the file in descending order where order is according to the time it is added.
      boolean isEmpty()
      Check if file history is empty.
      private java.util.Queue<java.lang.String> readHistoryFromPref()  
      void removeAll()
      Clear the previous recent file history.
      void removeFile​(java.lang.String path)
      Remove a file from recent file history.
      private void writeHistoryToPref​(java.util.Queue<java.lang.String> filePaths)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PIECES_LENGTH_KEY

        private static final java.lang.String PIECES_LENGTH_KEY
        See Also:
        Constant Field Values
      • pref

        private final java.util.prefs.Preferences pref
      • filePaths

        private java.util.Queue<java.lang.String> filePaths
      • maximum

        private final int maximum
    • Constructor Detail

      • RecentFiles

        public RecentFiles​(java.lang.Class className,
                           int maximumFile)
        Constructor.
        Parameters:
        className - the class for which this Recentfiles object is created and it will be used to create preference instance.
        maximumFile - the number of recent files to remember.
    • Method Detail

      • removeAll

        public void removeAll()
        Clear the previous recent file history.
      • isEmpty

        public boolean isEmpty()
        Check if file history is empty.
        Returns:
        if history is empty return true otherwise return false.
      • addFile

        public void addFile​(java.lang.String path)
        Add a new file in recent file history.
        Parameters:
        path - path to the file. this path means File#getPath() method returned String.
      • removeFile

        public void removeFile​(java.lang.String path)
        Remove a file from recent file history.
        Parameters:
        path - path string to the file. this path means File#getPath() method returned String.
      • getFiles

        public java.util.List<java.lang.String> getFiles()
        This gives the file in descending order where order is according to the time it is added. This checks for file's existence in file history.
        Returns:
        return the file paths in a List.
      • close

        public void close()
                   throws java.io.IOException
        This method save the present recent file history in the preference. To get the recent file history in next session this method must be called.
        Throws:
        java.io.IOException - if saving in preference doesn't success.
      • breakString

        private java.lang.String[] breakString​(java.lang.String fullPath)
      • writeHistoryToPref

        private void writeHistoryToPref​(java.util.Queue<java.lang.String> filePaths)
      • readHistoryFromPref

        private java.util.Queue<java.lang.String> readHistoryFromPref()