1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.io.File;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 | import javax.swing.JMenu;
|
---|
13 | import javax.swing.JMenuItem;
|
---|
14 | import javax.swing.JSeparator;
|
---|
15 | import javax.swing.event.MenuEvent;
|
---|
16 | import javax.swing.event.MenuListener;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.OpenFileAction.OpenFileTask;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Show list of recently opened files
|
---|
24 | */
|
---|
25 | public class RecentlyOpenedFilesMenu extends JMenu {
|
---|
26 | ClearAction clearAction;
|
---|
27 |
|
---|
28 | public RecentlyOpenedFilesMenu() {
|
---|
29 | super(tr("Open Recent"));
|
---|
30 | setToolTipText(tr("List of recently opened files"));
|
---|
31 | setIcon(ImageProvider.get("openrecent.png"));
|
---|
32 | putClientProperty("help", ht("/Action/OpenRecent"));
|
---|
33 |
|
---|
34 | // build dynamically
|
---|
35 | addMenuListener(new MenuListener() {
|
---|
36 | @Override
|
---|
37 | public void menuSelected(MenuEvent e) {
|
---|
38 | rebuild();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void menuDeselected(MenuEvent e) {
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void menuCanceled(MenuEvent e) {
|
---|
47 | }
|
---|
48 | });
|
---|
49 | }
|
---|
50 |
|
---|
51 | private void rebuild() {
|
---|
52 | removeAll();
|
---|
53 | Collection<String> fileHistory = Main.pref.getCollection("file-open.history");
|
---|
54 |
|
---|
55 | for (final String file : fileHistory) {
|
---|
56 | add(new AbstractAction() {
|
---|
57 | {
|
---|
58 | putValue(NAME, file);
|
---|
59 | putValue("help", ht("/Action/OpenRecent"));
|
---|
60 | }
|
---|
61 | @Override
|
---|
62 | public void actionPerformed(ActionEvent e) {
|
---|
63 | File f = new File(file);
|
---|
64 | OpenFileTask task = new OpenFileTask(Collections.singletonList(f), null);
|
---|
65 | task.setRecordHistory(true);
|
---|
66 | Main.worker.submit(task);
|
---|
67 | }
|
---|
68 | });
|
---|
69 | }
|
---|
70 | add(new JSeparator());
|
---|
71 | if (clearAction == null) {
|
---|
72 | clearAction = new ClearAction();
|
---|
73 | }
|
---|
74 | JMenuItem clearItem = new JMenuItem(clearAction);
|
---|
75 | clearItem.setEnabled(!fileHistory.isEmpty());
|
---|
76 | add(clearItem);
|
---|
77 | }
|
---|
78 |
|
---|
79 | private static class ClearAction extends AbstractAction {
|
---|
80 |
|
---|
81 | public ClearAction() {
|
---|
82 | super(tr("Clear"));
|
---|
83 | putValue(SHORT_DESCRIPTION, tr("Clear the list of recently opened files"));
|
---|
84 | putValue("help", ht("/Action/OpenRecent"));
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | Main.pref.putCollection("file-open.history", null);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|