source: josm/trunk/src/org/openstreetmap/josm/gui/io/RecentlyOpenedFilesMenu.java@ 13691

Last change on this file since 13691 was 13647, checked in by Don-vip, 6 years ago

see #16204 - Allow to start and close JOSM in WebStart sandbox mode (where every external access is denied). This was very useful to reproduce some very tricky bugs that occured in real life but were almost impossible to diagnose.

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.io.File;
9import java.util.Collection;
10import java.util.Collections;
11
12import javax.swing.AbstractAction;
13import javax.swing.JMenu;
14import javax.swing.JMenuItem;
15import javax.swing.JSeparator;
16import javax.swing.event.MenuEvent;
17import javax.swing.event.MenuListener;
18
19import org.openstreetmap.josm.actions.OpenFileAction.OpenFileTask;
20import org.openstreetmap.josm.gui.MainApplication;
21import org.openstreetmap.josm.spi.preferences.Config;
22import org.openstreetmap.josm.tools.ImageProvider;
23
24/**
25 * Show list of recently opened files
26 */
27public class RecentlyOpenedFilesMenu extends JMenu {
28 private ClearAction clearAction;
29
30 /**
31 * Constructs a new {@code RecentlyOpenedFilesMenu}.
32 */
33 public RecentlyOpenedFilesMenu() {
34 super(tr("Open Recent"));
35 setToolTipText(tr("List of recently opened files"));
36 setIcon(new ImageProvider("openrecent").setOptional(true).setSize(ImageProvider.ImageSizes.MENU).get());
37 putClientProperty("help", ht("/Action/OpenRecent"));
38
39 // build dynamically
40 addMenuListener(new MenuListener() {
41 @Override
42 public void menuSelected(MenuEvent e) {
43 rebuild();
44 }
45
46 @Override
47 public void menuDeselected(MenuEvent e) {
48 // Do nothing
49 }
50
51 @Override
52 public void menuCanceled(MenuEvent e) {
53 // Do nothing
54 }
55 });
56 }
57
58 private void rebuild() {
59 removeAll();
60 Collection<String> fileHistory = Config.getPref().getList("file-open.history");
61
62 for (final String file : fileHistory) {
63 add(new OpenRecentAction(file));
64 }
65 add(new JSeparator());
66 if (clearAction == null) {
67 clearAction = new ClearAction();
68 }
69 JMenuItem clearItem = new JMenuItem(clearAction);
70 clearItem.setEnabled(!fileHistory.isEmpty());
71 add(clearItem);
72 }
73
74 static final class OpenRecentAction extends AbstractAction {
75 private final String file;
76
77 OpenRecentAction(String file) {
78 this.file = file;
79 putValue(NAME, file);
80 putValue("help", ht("/Action/OpenRecent"));
81 putValue("toolbar", Boolean.FALSE);
82 }
83
84 @Override
85 public void actionPerformed(ActionEvent e) {
86 OpenFileTask task = new OpenFileTask(Collections.singletonList(new File(file)), null);
87 task.setRecordHistory(true);
88 MainApplication.worker.submit(task);
89 }
90 }
91
92 private static class ClearAction extends AbstractAction {
93
94 ClearAction() {
95 super(tr("Clear"));
96 putValue(SHORT_DESCRIPTION, tr("Clear the list of recently opened files"));
97 putValue("help", ht("/Action/OpenRecent"));
98 putValue("toolbar", "recentlyopenedfiles/clear");
99 }
100
101 @Override
102 public void actionPerformed(ActionEvent e) {
103 Config.getPref().putList("file-open.history", null);
104 }
105 }
106}
Note: See TracBrowser for help on using the repository browser.