001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import javax.swing.*;
024: import java.util.prefs.*;
025: import java.util.Vector;
026:
027: /**
028: *
029: * @author 'Rodrigo Reyes"
030: */
031: public class RecentFileMenu {
032: private JMenu m_root;
033: private Vector m_recent;
034: private Class m_prefAttach;
035: private RecentFileMenu.Action m_action;
036: private int m_recentCount;
037:
038: public interface Action {
039: public void action(String path);
040: }
041:
042: /** Creates a new instance of RecentFileMenu */
043: public RecentFileMenu(JMenu menuroot, int recentCount,
044: Class prefAttach, RecentFileMenu.Action action) {
045: m_recent = new Vector(recentCount);
046: m_recentCount = recentCount;
047: m_root = menuroot;
048: m_prefAttach = prefAttach;
049: m_action = action;
050:
051: loadRecentPrefs();
052: }
053:
054: private void loadRecentPrefs() {
055: Preferences p = Preferences.systemNodeForPackage(m_prefAttach);
056: int count = p.getInt("recentfilecount", 0);
057:
058: m_recent.removeAllElements();
059: for (int i = 0; i < count; i++) {
060: String rf = p.get("recentfile_" + i, null);
061:
062: if ((rf != null) && (m_recent.size() < m_recentCount)) {
063: m_recent.add(rf);
064: }
065: }
066:
067: buildMenu();
068: }
069:
070: public void savePrefs() {
071: Preferences p = Preferences.systemNodeForPackage(m_prefAttach);
072:
073: for (int i = 0; i < m_recent.size(); i++) {
074: p.put("recentfile_" + i, m_recent.elementAt(i).toString());
075: }
076: p.putInt("recentfilecount", m_recent.size());
077: }
078:
079: public class ActionRecent implements java.awt.event.ActionListener {
080: public int Offset;
081:
082: public void actionPerformed(java.awt.event.ActionEvent evt) {
083: if (m_recent.elementAt(Offset) != null) {
084: RecentFileMenu.this .m_action.action(m_recent.elementAt(
085: Offset).toString());
086: add(m_recent.elementAt(Offset).toString());
087: }
088: }
089: }
090:
091: private void buildMenu() {
092: m_root.removeAll();
093: for (int i = 0; i < Math.min(m_recent.size(), m_recentCount); i++) {
094: JMenuItem item = new JMenuItem(m_recent.elementAt(i)
095: .toString());
096: ActionRecent ar = new ActionRecent();
097: ar.Offset = i;
098: item.addActionListener(ar);
099: m_root.add(item);
100: }
101: m_root.addSeparator();
102: JMenuItem clear = new JMenuItem("Clear");
103: clear.addActionListener(new java.awt.event.ActionListener() {
104: public void actionPerformed(java.awt.event.ActionEvent evt) {
105: m_recent.removeAllElements();
106: buildMenu();
107: }
108: });
109: m_root.add(clear);
110: }
111:
112: public void add(String rec) {
113: m_recent.remove(rec);
114: m_recent.insertElementAt(rec, 0);
115: while (m_recent.size() > m_recentCount)
116: m_recent.remove(m_recent.size() - 1);
117: buildMenu();
118: }
119: }
|