PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/RecentFilesMenu.java

#
Java | 131 lines | 82 code | 17 blank | 32 comment | 8 complexity | ba3f33c65ac0f49b8f4a9e93dbdbe788 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * RecentFilesMenu.java - Recent file list menu
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import javax.swing.event.*;
  25. import javax.swing.*;
  26. import java.awt.event.*;
  27. import java.util.Vector;
  28. import org.gjt.sp.jedit.browser.FileCellRenderer;
  29. import org.gjt.sp.jedit.io.VFSManager;
  30. import org.gjt.sp.jedit.io.VFS;
  31. import org.gjt.sp.jedit.*;
  32. //}}}
  33. public class RecentFilesMenu extends EnhancedMenu implements MenuListener
  34. {
  35. //{{{ RecentFilesMenu constructor
  36. public RecentFilesMenu()
  37. {
  38. super("recent-files");
  39. addMenuListener(this);
  40. } //}}}
  41. //{{{ menuSelected() method
  42. public void menuSelected(MenuEvent evt)
  43. {
  44. final View view = GUIUtilities.getView(this);
  45. if(getMenuComponentCount() != 0)
  46. removeAll();
  47. //{{{ ActionListener...
  48. ActionListener actionListener = new ActionListener()
  49. {
  50. public void actionPerformed(ActionEvent evt)
  51. {
  52. jEdit.openFile(view,evt.getActionCommand());
  53. view.getStatus().setMessage(null);
  54. }
  55. }; //}}}
  56. //{{{ MouseListener...
  57. MouseListener mouseListener = new MouseAdapter()
  58. {
  59. public void mouseEntered(MouseEvent evt)
  60. {
  61. view.getStatus().setMessage(
  62. ((JMenuItem)evt.getSource())
  63. .getActionCommand());
  64. }
  65. public void mouseExited(MouseEvent evt)
  66. {
  67. view.getStatus().setMessage(null);
  68. }
  69. }; //}}}
  70. Vector recentVector = BufferHistory.getBufferHistory();
  71. if(recentVector.size() == 0)
  72. {
  73. add(GUIUtilities.loadMenuItem("no-recent-files"));
  74. return;
  75. }
  76. Vector menuItems = new Vector();
  77. boolean sort = jEdit.getBooleanProperty("sortRecent");
  78. /*
  79. * While recentVector has 50 entries or so, we only display
  80. * a few of those in the menu (otherwise it will be way too
  81. * long)
  82. */
  83. int recentFileCount = Math.min(recentVector.size(),
  84. jEdit.getIntegerProperty("history",25));
  85. for(int i = recentVector.size() - 1;
  86. i >= recentVector.size() - recentFileCount;
  87. i--)
  88. {
  89. String path = ((BufferHistory.Entry)recentVector
  90. .elementAt(i)).path;
  91. VFS vfs = VFSManager.getVFSForPath(path);
  92. JMenuItem menuItem = new JMenuItem(vfs.getFileName(path));
  93. menuItem.setActionCommand(path);
  94. menuItem.addActionListener(actionListener);
  95. menuItem.addMouseListener(mouseListener);
  96. menuItem.setIcon(FileCellRenderer.fileIcon);
  97. if(sort)
  98. menuItems.addElement(menuItem);
  99. else
  100. add(menuItem);
  101. }
  102. if(sort)
  103. {
  104. MiscUtilities.quicksort(menuItems,
  105. new MiscUtilities.MenuItemCompare());
  106. for(int i = 0; i < menuItems.size(); i++)
  107. {
  108. add((JMenuItem)menuItems.elementAt(i));
  109. }
  110. }
  111. } //}}}
  112. public void menuDeselected(MenuEvent e) {}
  113. public void menuCanceled(MenuEvent e) {}
  114. }