PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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