- /**
- * <p>Copyright: Copyright (c) 2002-2004</p>
- * <p>Company: JavaResearch(http://www.javaresearch.org)</p>
- */
-
- package org.jr.swing;
-
- import javax.swing.*;
- import java.util.*;
- import java.util.prefs.*;
- import java.awt.event.*;
-
- /**
- * 最近文件列表。
- * <p>最后更新日期:2003年6月2日
- * @author cherami@163.net
- * @since 0.6
- */
-
- public class RecentFiles {
- public static final int POPUP=0;
- public static final int LIST=1;
- public static final int AUTO=0;
- public static final int FULLPATH=1;
- public static final int FILENAME=1;
- private static final int MAX_SIZE=20;
-
- public static final String PREFERENCE_ROOT="recentfiles";
-
- int style;
- int contentType;
- int position;
- Preferences preference;
- ArrayList list;
- HashMap menuItemMap;
- int capacity;
- int size;
- JMenu parentMenu;
- RecentFileProcesser processer;
- boolean autoUpdatePreference;
- /**
- * 构造一个列表大小为10的RecentFilesManager。
- * @param parentMenu 列表菜单
- */
- public RecentFiles(JMenu parentMenu) {
- this(10, parentMenu, true);
- }
-
- /**
- * 根据指定的列表大小构造一个RecentFilesManager。
- * 如果指定的容量小于0则设置为缺省的10。
- * @param capacity 列表容量
- * @param parentMenu 列表菜单
- */
- public RecentFiles(int capacity, JMenu parentMenu) {
- this(capacity, parentMenu, true);
- }
-
- /**
- * 根据指定的列表大小和是否从参数选项里面读取列表构造一个RecentFilesManager。
- * @param capacity 列表容量
- * @param parentMenu 列表菜单
- * @param getFiles 是否从参数选项里面读取列表,为true是读取,否则不读取
- */
- public RecentFiles(int capacity, JMenu parentMenu, boolean getFiles) {
- if (capacity < 0 || capacity > MAX_SIZE) {
- capacity = 10;
- }
- list = new ArrayList(capacity);
- menuItemMap = new HashMap(capacity * 2);
- this.capacity = capacity;
- size = 0;
- this.parentMenu = parentMenu;
- if (getFiles == true) {
- getPreference();
- }
- }
-
- public RecentFiles(JMenu parentMenu, int style,int contentType,boolean getFiles) {
- this.parentMenu = parentMenu;
- this.style=style;
- this.contentType=contentType;
- if (getFiles == true) {
- getPreference();
- }
- }
-
- public void setProcesser(RecentFileProcesser processer) {
- this.processer=processer;
- }
-
- public RecentFileProcesser getProcesser() {
- return processer;
- }
-
- /**
- * 增加一项到最近文件列表中
- * @param filePath 文件的绝对路径
- */
- private void add(String filePath) {
- list.add(0, filePath);
- size++;
- if (size == 1) {
- parentMenu.addSeparator();
- }
- JMenuItem menuItem = createMenuItem(filePath);
- menuItemMap.put(filePath, menuItem);
- parentMenu.add(menuItem, position + 1);
- }
-
- /**
- * 从最近列表中删除一项
- * @param filePath 文件的绝对路径
- */
- private void remove(String filePath) {
- list.remove(filePath);
- size--;
- if (size == 0) {
- parentMenu.remove(position);
- }
- JMenuItem menuItem = (JMenuItem) menuItemMap.get(filePath);
- menuItemMap.remove(filePath);
- parentMenu.remove(menuItem);
- }
-
- /**
- * 将文件列表最后的一项删除。
- */
- public void removeLast() {
- if (size > 0) {
- String lastFile = (String) list.get(size - 1);
- remove(lastFile);
- }
- }
-
- /**
- * 添加一个文件到列表。
- * @param filePath 文件的绝对路径
- */
- public void addFile(String filePath) {
- if (list.size() < capacity && filePath != null) {
- if (!list.contains(filePath)) {
- add(filePath);
- refreshPreference();
- }
- else {
- remove(filePath);
- add(filePath);
- refreshPreference();
- }
- }
- else {
- if (!list.contains(filePath)) {
- removeLast();
- add(filePath);
- refreshPreference();
- }
- else {
- remove(filePath);
- add(filePath);
- refreshPreference();
- }
- }
- }
-
- /**
- * 清空文件列表。
- */
- public void clear() {
- if (size > 0) {
- list.clear();
- menuItemMap.clear();
- for (int i = position; i < position + size; i++) {
- parentMenu.remove(i);
- }
- size = 0;
- refreshPreference();
- }
- }
-
- /**
- * 重新设置列表的容量。
- * 如果新的容量小于已有文件的数量则会删除那些最旧的文件项。
- * @param capacity 新容量
- */
- public void setCapacity(int capacity) {
- if (capacity > 0) {
- this.capacity = capacity;
- if (size > capacity) {
- for (int i = size; i > capacity; i++) {
- removeLast();
- }
- size = capacity;
- refreshPreference();
- }
-
- }
- }
-
- /**
- * 返回文件列表数组,最新的文件在最前面。
- * @return 文件列表数组
- */
- public String[] getLists() {
- String[] result = new String[size];
- for (int i = 0; i < list.size(); i++) {
- result[i] = (String) list.get(i);
- }
- return result;
- }
-
- /**
- * 更新参数选项中的文件列表。
- */
- private void refreshPreference() {
- int i = 0;
- for (; i < list.size(); i++) {
- preference.put("recentfiles" + i, (String) list.get(i));
- }
- while (!preference.get("recentfiles" + i, "").equals("")) {
- preference.remove("recentfiles" + i);
- i++;
- }
- }
-
- /**
- * 从参数选项中读取文件列表。
- */
- private void getPreference() {
- int i = 0;
- String filePath = preference.get("recentfiles" + i, "");
- while (!filePath.equals("")) {
- add(filePath);
- i++;
- filePath = preference.get("recentfiles" + i, "");
- }
- preference.remove("recentfiles" + i);
- }
-
- /**
- * 创建一个最近文件菜单项
- * @param filePath 文件的绝对路径
- * @return 对应的菜单项
- */
- private JMenuItem createMenuItem(String filePath) {
- JMenuItem menuItem = new JMenuItem(getShortRecentName(filePath));
- menuItem.addActionListener(new RecentFileAction(filePath));
- menuItem.setToolTipText(filePath);
- return menuItem;
- }
-
- /**
- * 得到文件路径的缩短的形式。
- * @param filePath 文件路径
- * @return文件路径的缩短的形式
- */
- private String getShortRecentName(String filePath) {
- String result = filePath;
- /*if (filePath.length() > PATH_LENGTH) {
- String fileName = FileUtil.getFileName(filePath);
- int length = fileName.length();
- result = filePath.substring(0, 5);
- result = result + "...";
- result = result +
- filePath.substring(filePath.length() - PATH_LENGTH + 5);
- }*/
- return result;
- }
- /**
- * 最近打开文件事件处理器。
- */
- class RecentFileAction
- implements ActionListener {
- String filePath;
- public RecentFileAction(String filePath) {
- this.filePath = filePath;
- }
-
- public void actionPerformed(ActionEvent e) {
- if (processer!=null) {
- processer.dealWith(filePath);
- }
- }
- }
- }
-