01: /*
02: * FindBugs - Find Bugs in Java programs
03: * Copyright (C) 2006, University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19: package edu.umd.cs.findbugs.gui2;
20:
21: import java.io.File;
22: import java.util.ArrayList;
23:
24: import javax.swing.JMenu;
25:
26: /**
27: * @author Dan
28: */
29:
30: public class RecentMenu {
31:
32: private static class LimitedArrayList<T> extends ArrayList<T> {
33: public static final int MAX_ENTRIES = 5;
34:
35: public LimitedArrayList() {
36: super (MAX_ENTRIES);
37: }
38:
39: @Override
40: public boolean add(T element) {
41: if (!this .contains(element)) {
42: super .add(0, element);
43: if (this .size() > MAX_ENTRIES) {
44: this .remove(MAX_ENTRIES);
45: }
46: } else {
47: this .remove(element);
48: super .add(0, element);
49: }
50: return true;
51: }
52: }
53:
54: LimitedArrayList<File> recentFiles;//Originally called recentProjects before merge two lists into one.
55: JMenu recentMenu;
56:
57: public RecentMenu(JMenu menu) {
58: recentFiles = new LimitedArrayList<File>();
59: recentMenu = menu;
60:
61: for (File f : GUISaveState.getInstance().getRecentFiles()) {
62: recentFiles.add(f);
63: }
64:
65: makeRecentMenu();
66: }
67:
68: public void makeRecentMenu() {
69: recentMenu.removeAll();
70: for (File f : recentFiles) {
71: Debug.println(f);
72: if (!f.exists()) {
73: if (MainFrame.DEBUG)
74: System.err
75: .println("a recent project was not found, removing it from menu");
76: continue;
77: }
78:
79: recentMenu.add(MainFrame.getInstance().createRecentItem(f,
80: SaveType.forFile(f)));
81: }
82:
83: }
84:
85: /**
86: * Adds a file to the list of recent files used.
87: * @param f
88: */
89: public void addRecentFile(final File f) {
90: recentFiles.add(f);
91:
92: makeRecentMenu();
93: }
94:
95: }
|