/** * the overridden equals method of the object class * * @param obj the object to be compared with * @return true if the files of the two object are the same, else returns false. here we are * only concerned with the file object equality as for the same file there will be different * attributes */ @Override public boolean equals(Object obj) { // check whether the passed object is a kind of RecentFile if (obj instanceof RecentFile) { // cast the passed object into a RecentFile object RecentFile rf = (RecentFile) obj; // check whether the current objects file matches the passed objects file if (file.getAbsolutePath().contentEquals(rf.getFile().getAbsolutePath())) { // if yes then return true return true; } else { // else return false return false; } } else { // if compared with any other object kind return false return false; } }
/** * method to save the recent file with its attributes to the config file * * @throws FileNotFoundException * @throws IOException */ private void save() throws FileNotFoundException, IOException { // the recent file object RecentFile recentFile; // the index i represents the file index int i = 0; // the index j represents the attribute index int j = 0; // clear all properties properties.clear(); // from the recent file list create properties object for (i = 0; i < recentFilesList.size(); i++) { // get the recent file object recentFile = recentFilesList.get(i); // reset the j index value for iterations j = 0; // set the file property - the key for file will be '00' // file absolute path is saved as value for the file key properties.setProperty( String.valueOf(i) + String.valueOf(j), recentFile.getFile().getAbsolutePath()); // save the attributres for the file // only if the recent file have any attribute if (recentFile.getAttributes() != null) { for (String attributes : recentFile.getAttributes()) { // increment the attrbutes index j++; // set the attribute for the file properties.setProperty(String.valueOf(i) + String.valueOf(j), attributes); } } } // save the properties object to the config file properties.store(new FileOutputStream(configFile), null); }
/** method to load the recent files as JmenuItem to the JMenu reference object */ private void loadRecentFilesMenuItems() { // first clear all previous recent files JMenuItems jmenu.removeAll(); // now for all recent files in the recent files list for (final RecentFile recentFile : recentFilesList) { // create a JmenuItem with the recent file name JMenuItem jMenuItem = new JMenuItem(recentFile.getFile().getName()); // add action listner to the JMenuItem jMenuItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // on action performed call the recent file handler's onRecentFileSelection method // and get the method result to a refernce boolean result = recentFileHandler.onRecentFileSelection( recentFile.getFile(), recentFile.getAttributes()); // if the result is false (ie. the file handling failed) if (!result) { // remove the recent file from the recent file list recentFilesList.remove(recentFile); // save the current recent files list to config file try { save(); } catch (FileNotFoundException ex) { Logger.getLogger(RecentFilesManager.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(RecentFilesManager.class.getName()).log(Level.SEVERE, null, ex); } // load the recent files JMenuItems to recent files JMenu loadRecentFilesMenuItems(); } } }); // if the file icon type array is not null then if (fileIconTypesArray != null) { // for all file icon types in the array for (FileIconTypes fileIconTypes : fileIconTypesArray) { // get the recent file extension from file name and check whether it matches // the file icon type's file extension if (recentFile.getFile().getName().endsWith(fileIconTypes.getFileType())) { // on match set the JMenuItem icon as the file icon types icon jMenuItem.setIcon(fileIconTypes.getIcon()); // break the search for the file icon type break; } } } // add the JMenuItem to the JMenu jmenu.add(jMenuItem); } }