/jEdit/tags/jedit-4-4-2/org/gjt/sp/jedit/menu/DirectoryProvider.java

# · Java · 177 lines · 110 code · 26 blank · 41 comment · 22 complexity · 1379bf715d9dbc2d2164cc8fc3c2a3de MD5 · raw file

  1. /*
  2. * DirectoryProvider.java - File list menu
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2003 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.menu;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.event.*;
  26. import java.io.File;
  27. import java.util.Arrays;
  28. import org.gjt.sp.jedit.browser.*;
  29. import org.gjt.sp.jedit.io.FileVFS;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.util.StandardUtilities;
  32. //}}}
  33. /**
  34. * @author Slava Pestov
  35. * @version $Id: DirectoryProvider.java 16207 2009-09-17 17:54:14Z shlomy $
  36. */
  37. public class DirectoryProvider implements DynamicMenuProvider
  38. {
  39. //{{{ DirectoryProvider constructor
  40. public DirectoryProvider(String dir)
  41. {
  42. this.dir = dir;
  43. } //}}}
  44. //{{{ updateEveryTime() method
  45. public boolean updateEveryTime()
  46. {
  47. return true;
  48. } //}}}
  49. //{{{ update() method
  50. public void update(JMenu menu)
  51. {
  52. final View view = GUIUtilities.getView(menu);
  53. String path;
  54. if(dir == null)
  55. {
  56. path = view.getBuffer().getDirectory();
  57. }
  58. else
  59. path = dir;
  60. JMenuItem mi = new JMenuItem(path + ':');
  61. mi.setActionCommand(path);
  62. mi.setIcon(FileCellRenderer.openDirIcon);
  63. //{{{ ActionListeners
  64. ActionListener fileListener = new ActionListener()
  65. {
  66. public void actionPerformed(ActionEvent evt)
  67. {
  68. jEdit.openFile(view,evt.getActionCommand());
  69. }
  70. };
  71. ActionListener dirListener = new ActionListener()
  72. {
  73. public void actionPerformed(ActionEvent evt)
  74. {
  75. VFSBrowser.browseDirectory(view,
  76. evt.getActionCommand());
  77. }
  78. }; //}}}
  79. mi.addActionListener(dirListener);
  80. menu.add(mi);
  81. menu.addSeparator();
  82. if(dir == null && !(view.getBuffer().getVFS() instanceof FileVFS))
  83. {
  84. mi = new JMenuItem(jEdit.getProperty(
  85. "directory.not-local"));
  86. mi.setEnabled(false);
  87. menu.add(mi);
  88. return;
  89. }
  90. File directory = new File(path);
  91. JMenu current = menu;
  92. // for filtering out backups
  93. String backupPrefix = jEdit.getProperty("backup.prefix");
  94. String backupSuffix = jEdit.getProperty("backup.suffix");
  95. File[] list = directory.listFiles();
  96. if(list == null || list.length == 0)
  97. {
  98. mi = new JMenuItem(jEdit.getProperty(
  99. "directory.no-files"));
  100. mi.setEnabled(false);
  101. menu.add(mi);
  102. }
  103. else
  104. {
  105. int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
  106. Arrays.sort(list,
  107. new StandardUtilities.StringCompare<File>(true));
  108. for(int i = 0; i < list.length; i++)
  109. {
  110. File file = list[i];
  111. String name = file.getName();
  112. // skip marker files
  113. if(name.endsWith(".marks"))
  114. continue;
  115. // skip autosave files
  116. if(name.startsWith("#") && name.endsWith("#"))
  117. continue;
  118. // skip backup files
  119. if((backupPrefix.length() != 0
  120. && name.startsWith(backupPrefix))
  121. || (backupSuffix.length() != 0
  122. && name.endsWith(backupSuffix)))
  123. continue;
  124. // skip directories
  125. //if(file.isDirectory())
  126. // continue;
  127. mi = new JMenuItem(name);
  128. mi.setActionCommand(file.getPath());
  129. mi.addActionListener(file.isDirectory()
  130. ? dirListener
  131. : fileListener);
  132. mi.setIcon(file.isDirectory()
  133. ? FileCellRenderer.dirIcon
  134. : FileCellRenderer.fileIcon);
  135. if(current.getItemCount() >= maxItems && i != list.length - 1)
  136. {
  137. //current.addSeparator();
  138. JMenu newCurrent = new JMenu(
  139. jEdit.getProperty(
  140. "common.more"));
  141. current.add(newCurrent);
  142. current = newCurrent;
  143. }
  144. current.add(mi);
  145. }
  146. }
  147. } //}}}
  148. //{{{ Private members
  149. private final String dir;
  150. //}}}
  151. }