/*
 * Copyright ©1998-2020 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 2.0. If a copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

package com.trollworks.gcs.menu.file;

import com.trollworks.gcs.preferences.Preferences;
import com.trollworks.gcs.utility.I18n;
import com.trollworks.gcs.utility.PathUtils;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

/** The standard "Recent Files" menu. */
public class RecentFilesMenu extends JMenu implements MenuListener {
    private int mLastSeenRecentFilesUpdateCounter;

    /** Creates a new {@link RecentFilesMenu}. */
    public RecentFilesMenu() {
        super(I18n.Text("Recent Files"));
        addMenuListener(this);
        mLastSeenRecentFilesUpdateCounter = Preferences.getInstance().getLastRecentFilesUpdateCounter() - 1;
    }

    @Override
    public void menuCanceled(MenuEvent event) {
        // Nothing to do.
    }

    @Override
    public void menuDeselected(MenuEvent event) {
        // Nothing to do.
    }

    @Override
    public void menuSelected(MenuEvent event) {
        Preferences prefs                        = Preferences.getInstance();
        int         lastRecentFilesUpdateCounter = prefs.getLastRecentFilesUpdateCounter();
        if (mLastSeenRecentFilesUpdateCounter != lastRecentFilesUpdateCounter) {
            mLastSeenRecentFilesUpdateCounter = lastRecentFilesUpdateCounter;
            removeAll();
            List<Path> list = new ArrayList<>();
            for (Path path : prefs.getRecentFiles()) {
                if (Files.isReadable(path)) {
                    list.add(path);
                    add(new JMenuItem(new OpenDataFileCommand(PathUtils.getLeafName(path, false), path)));
                    if (list.size() == Preferences.MAX_RECENT_FILES) {
                        break;
                    }
                }
            }
            prefs.setRecentFiles(list);
            if (!list.isEmpty()) {
                addSeparator();
            }
            JMenuItem item = new JMenuItem(ClearRecentFilesMenuCommand.INSTANCE);
            ClearRecentFilesMenuCommand.INSTANCE.adjust();
            add(item);
        }
    }
}

@@ -683,19 +683,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0Aimpor
@@ -1935,14 +1935,26 @@
 ath%3E
+ 
  list 
+           
 = ne
@@ -1966,24 +1966,142 @@
 ayList%3C%3E();%0A
+            Set%3CString%3E set             = new HashSet%3C%3E();%0A            Set%3CString%3E needFullPathSet = new HashSet%3C%3E();%0A
             
@@ -2249,95 +2249,245 @@
     
-add(new JMenuItem(new OpenDataFileCommand(PathUtils.getLeafName(path, false), path))
+String leaf = PathUtils.getLeafName(path, false);%0A                    if (set.contains(leaf)) %7B%0A                        needFullPathSet.add(leaf);%0A                    %7D else %7B%0A                        set.add(leaf
 );%0A
+                    %7D%0A
     
@@ -2630,32 +2630,359 @@
 %7D%0A            %7D%0A
+            for (Path path : list) %7B%0A                String title = PathUtils.getLeafName(path, false);%0A                if (needFullPathSet.contains(title)) %7B%0A                    title = path.toAbsolutePath().toString();%0A                %7D%0A                add(new JMenuItem(new OpenDataFileCommand(title, path)));%0A            %7D%0A
             pref

Use full path for files with the same leaf name in the recent files menu