/** * Creates a new RecentFileManager object * * @param recentFileHandler the recent file handler object reference * @param jMenu the menu component object reference onto which the recent files JMenuItems are to * be added * @param configFileName the recent files config save file name. the file will be created in the * same folder as that of the project * @param maxRecords the maximum no of recent files to be stored * @param fileIconTypes the file icon type array. specifies what icon to be displayed for * different files. the file type is recognized by their extension. */ public RecentFilesManager( RecentFileHandler recentFileHandler, JMenu jMenu, String configFileName, int maxRecords, FileIconTypes[] fileIconTypes) throws FileNotFoundException, IOException { // set the recent file handler object this.recentFileHandler = recentFileHandler; // set the jmenu reference this.jmenu = jMenu; // create the config file from the config file name this.configFile = new File(configFileName); // if file doesnt exists the ncreate one if (!configFile.exists()) { this.configFile.createNewFile(); } // set the maximun no of records this.maxRecords = maxRecords; // set the file icon types reference this.fileIconTypesArray = fileIconTypes; // create a new recent files list recentFilesList = new ArrayList<>(); // create a properties object this.properties = new Properties(); // load the properties from the config file this.properties.load(new FileInputStream(configFile)); // the recent file name string String fileName; // the recent file attribute string String attribute; // the recent file attribute list ArrayList<String> attributeList; // for the maximun no of recent records for (int i = 0; i < maxRecords; i++) { // the value for key 'x0' gives the file name fileName = properties.getProperty(String.valueOf(i) + 0); // if file name is not equal to null then if (fileName != null) { // create a new recent file object RecentFile recentFile = new RecentFile(); // create a file object based on the recent file name // and set it to the recent file object recentFile.setFile(new File(fileName)); // create the attribute list attributeList = new ArrayList<String>(); // the value of j starts with 1 as the value 0 is already used for the file name // we dont know how many attributes are present so we will try to search for // all the possibilities till the integer max value for (int j = 1; j < Integer.MAX_VALUE; j++) { // get the attribute attribute = properties.getProperty(String.valueOf(i) + String.valueOf(j)); // if the returned attribute is not null then if (attribute != null) { // add the attribute to the attribute list attributeList.add(attribute); } else { // else if the attribute is null then it means // the attribute for the file specified by the value 'i' is over // so then break the search for the attribute for the file break; } } // if the attribute size is greater than zero then if (attributeList.size() > 0) { // convert the attributes list into attributes array String[] attributesArray = (String[]) attributeList.toArray(new String[0]); // set the attributes array to the recent file recentFile.setAttributes(attributesArray); } else { // set the attribute array as null recentFile.setAttributes(null); } // add the recent file to the recent files list recentFilesList.add(recentFile); } } // load the jmenu with the recent files JMenuItems loadRecentFilesMenuItems(); }