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