/plugins/Templates/tags/rel-1_0_2/TemplateDir.java

# · Java · 137 lines · 68 code · 11 blank · 58 comment · 9 complexity · b94ba25597d832e8281cccb4fc83539d MD5 · raw file

  1. // $Id: TemplateDir.java 7393 2000-12-04 20:17:46Z $
  2. /*
  3. * TemplateDir.java - Represents a directory within the templates
  4. * directory hierarchy.
  5. * Copyright (C) 1999 Steve Jakob
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. import javax.swing.*;
  22. import java.io.File;
  23. import java.util.*;
  24. import gnu.regexp.*;
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.util.Log;
  27. /**
  28. * A TemplateDir is a type of TemplateFile which is a container for other TemplateFiles.
  29. * In this way we can create a tree of TemplateFiles similar to a directory tree or
  30. * menu hierarchy.
  31. * @author Steve Jakob
  32. */
  33. public class TemplateDir extends TemplateFile
  34. {
  35. private Hashtable templateFiles;
  36. private static RE backupFilter;
  37. //Constructors
  38. public TemplateDir(File templateFile) {
  39. super(templateFile);
  40. }
  41. //Accessors & Mutators
  42. //Implementors
  43. public boolean isDirectory() { return true; }
  44. /**
  45. * Scans the templates directory and creates a Hashtable
  46. * mapping template names to template files. Backup files are ignored
  47. * based on the values of the backup prefix and suffix in the "Global
  48. * Options" settings.
  49. */
  50. public void refreshTemplates() {
  51. File f;
  52. this.templateFiles = new Hashtable();
  53. try {
  54. if (backupFilter == null)
  55. createBackupFilter();
  56. String[] files = this.templateFile.list();
  57. for (int i = 0; i < files.length; i++) {
  58. f = new File(this.templateFile, files[i]);
  59. if (f.isDirectory()) {
  60. TemplateDir submenu = new TemplateDir(f);
  61. this.templateFiles.put(files[i], submenu); // Add subdirectory as a TemplateDir
  62. submenu.refreshTemplates();
  63. }
  64. else if (!backupFilter.isMatch(files[i])) { // if not a backup file
  65. TemplateFile tf = new TemplateFile(f);
  66. this.templateFiles.put(files[i], tf);
  67. }
  68. }
  69. } catch (gnu.regexp.REException ree) {
  70. Log.log(Log.ERROR,this,jEdit.getProperty("plugin.TemplatesPlugin.error.bad-backup-filter"));
  71. // System.out.println("Templates: Bad RegExp creating backup filter.");
  72. }
  73. }
  74. private static void createBackupFilter() throws gnu.regexp.REException {
  75. String exp = jEdit.getProperty("backup.prefix") +
  76. "\\S+" +
  77. jEdit.getProperty("backup.suffix"); // RE for jEdit backups
  78. backupFilter = new RE(exp,RE.REG_ICASE);
  79. }
  80. public void createMenus(JMenu menu) {
  81. Object o;
  82. JMenu submenu;
  83. JMenuItem mi;
  84. TemplateDir td;
  85. TemplateFile tf;
  86. Enumeration e;
  87. e = this.templateFiles.elements();
  88. while (e.hasMoreElements()) {
  89. o = e.nextElement();
  90. if (o instanceof TemplateDir) {
  91. td = (TemplateDir) o;
  92. submenu = new JMenu(td.getLabel());
  93. menu.add(submenu); // Add subdirectory as a sub-menu
  94. td.createMenus(submenu);
  95. }
  96. else {
  97. tf = (TemplateFile) o;
  98. mi = new JMenuItem(tf.getLabel());
  99. mi.addActionListener(tf);
  100. menu.add(mi);
  101. }
  102. }
  103. }
  104. }
  105. /*
  106. * Change Log:
  107. * $Log$
  108. * Revision 1.1 2000/04/21 05:05:44 sjakob
  109. * Initial revision
  110. *
  111. * Revision 1.3 2000/03/08 15:46:49 sjakob
  112. * Updated README, CHANGES, to-do files.
  113. * Use properties for error messages, rather than hard-coded strings.
  114. *
  115. * Revision 1.2 2000/03/08 06:55:46 sjakob
  116. * Use org.gjt.sp.util.Log instead of System.out.println.
  117. * Update documentation.
  118. * Add sample template files to project.
  119. *
  120. * Revision 1.1 2000/03/03 06:25:43 sjakob
  121. * Redesigned the plugin to fix a bug where only the most recent view had a
  122. * Templates menu. Added TemplateFile and TemplateDir classes to handle
  123. * files and directories in the Templates directory tree. Templates menus for
  124. * all views are refreshed simultaneously.
  125. *
  126. */