public void add(String path) { if (path.isEmpty()) { return; } String value = Stream .concat(Stream.of(path), getAsStream().filter(p -> !p.equals(path))) .collect(Collectors.joining(File.pathSeparator)); preferences.put(key, value); flush(); }
@Override public JMenuItem getMenuPresenter() { List<File> openedFiles = OpenProductAction.getOpenedProductFiles(); List<String> pathList = getRecentProductPaths().get(); final Preferences preference = SnapApp.getDefault().getPreferences(); int maxFileList = preference.getInt(UiBehaviorController.PREFERENCE_KEY_LIST_FILES_TO_REOPEN, DEFAULT_MAX_FILE_LIST_REOPEN); // Add "open recent product file" actions JMenu menu = new JMenu(Bundle.CTL_ReopenProductActionMenuText()); pathList.stream().limit(maxFileList).forEach(path->{ if (!openedFiles.contains(new File(path))) { JMenuItem menuItem = new JMenuItem(path); OpenProductAction openProductAction = new OpenProductAction(); openProductAction.setFile(new File(path)); menuItem.addActionListener(openProductAction); menu.add(menuItem); } }); // Add "Clear List" action if (menu.getComponentCount() > 0 || pathList.size() > 0) { menu.addSeparator(); JMenuItem menuItem = new JMenuItem(Bundle.CTL_ClearListActionMenuText()); menuItem.addActionListener(e -> getRecentProductPaths().clear()); menu.add(menuItem); } return menu; }
static RecentPaths getRecentProductPaths() { return new RecentPaths(SnapApp.getDefault().getPreferences(), PREFERENCES_KEY_RECENTLY_OPENED_PRODUCTS, true); }
@Override public void run() { try { ph.start(); ph.switchToIndeterminate(); Product product = ProductIO.readProduct(file, formatName); boolean interrupted = Thread.interrupted(); if (!interrupted) { if (product == null) { SwingUtilities.invokeLater( () -> Dialogs.showError(Bundle.LBL_NoReaderFoundText() + String.format("%nFile '%s' can not be opened.", file))); } else { OpenProductAction.getRecentProductPaths().add(file.getPath()); SwingUtilities.invokeLater(() -> SnapApp.getDefault().getProductManager().addProduct(product)); } } } catch (IOException problem) { SwingUtilities.invokeLater(() -> Dialogs.showError(Bundle.CTL_OpenProductActionName(), problem.getMessage())); } finally { ph.finish(); } }
@Test public void testEmpty() throws Exception { List<String> paths = recentPaths.get(); assertNotNull(paths); assertTrue(paths.isEmpty()); }
public List<String> get() { return getAsStream().collect(Collectors.toList()); }
public void clear() { preferences.remove(key); flush(); }
@Before public void setUp() throws Exception { Preferences preferences = Preferences.userNodeForPackage(RecentPathsTest.class).node("test"); preferences.remove("recents"); recentPaths = new RecentPaths(Preferences.userNodeForPackage(RecentPathsTest.class).node("test"), "recents", false); }
OpenProductAction.getRecentProductPaths().add(fileLocation.getPath()); } else { if (metadataRoot != null) {