/plugins/Templates/tags/rel-3_1_0/templates/TemplateDir.java

# · Java · 228 lines · 112 code · 19 blank · 97 comment · 15 complexity · a00cba3344d2dba6ac3f3b8d1e0451a8 MD5 · raw file

  1. // $Id: TemplateDir.java 7441 2002-08-16 15:15:12Z sjakob $
  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. package templates;
  22. import javax.swing.*;
  23. import java.io.File;
  24. import java.util.*;
  25. import javax.swing.tree.TreeNode;
  26. import gnu.regexp.*;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.jedit.gui.EnhancedMenuItem;
  29. import org.gjt.sp.util.Log;
  30. /**
  31. * A TemplateDir is a type of TemplateFile which is a container for other
  32. * TemplateFiles. In this way we can create a tree of TemplateFiles similar
  33. * to a directory tree or menu hierarchy.
  34. * @author Steve Jakob
  35. */
  36. public class TemplateDir extends TemplateFile
  37. {
  38. private Vector templateFiles;
  39. private static RE backupFilter;
  40. //Constructors
  41. public TemplateDir(TemplateDir parent, File templateFile) {
  42. super(parent, templateFile);
  43. }
  44. //Accessors & Mutators
  45. //Implementors
  46. public boolean isDirectory() { return true; }
  47. /**
  48. * Scans the templates directory and creates a Hashtable
  49. * mapping template names to template files. Backup files are ignored
  50. * based on the values of the backup prefix and suffix in the "Global
  51. * Options" settings.
  52. */
  53. public void refreshTemplates() {
  54. File f;
  55. this.templateFiles = new Vector();
  56. try {
  57. // Make sure we always have an up to date backup filter
  58. createBackupFilter();
  59. String[] files = this.templateFile.list();
  60. for (int i = 0; i < files.length; i++) {
  61. f = new File(this.templateFile, files[i]);
  62. if (f.isDirectory()) {
  63. TemplateDir submenu = new TemplateDir(this, f);
  64. this.templateFiles.addElement(submenu); // Add subdirectory as a TemplateDir
  65. submenu.refreshTemplates();
  66. }
  67. else if (!backupFilter.isMatch(files[i])) { // if not a backup file
  68. TemplateFile tf = new TemplateFile(this, f);
  69. this.templateFiles.addElement(tf);
  70. }
  71. }
  72. } catch (gnu.regexp.REException ree) {
  73. Log.log(Log.ERROR,this,jEdit.getProperty("plugin.TemplatesPlugin.error.bad-backup-filter"));
  74. // System.out.println("Templates: Bad RegExp creating backup filter.");
  75. }
  76. }
  77. private static void createBackupFilter() throws gnu.regexp.REException {
  78. String exp = jEdit.getProperty("backup.prefix") +
  79. "\\S+" +
  80. jEdit.getProperty("backup.suffix"); // RE for jEdit backups
  81. if (exp.equals("\\S+")) {
  82. exp = "";
  83. }
  84. backupFilter = new RE(exp,RE.REG_ICASE);
  85. }
  86. /**
  87. * Add a menu item to the given menu object for each TemplateFile contained
  88. * within this TemplateDir. Recursively process any TemplateDir objects
  89. * contained within this TemplateDir.
  90. * @param menu The menu to which the new JMenuItem objects will be added.
  91. */
  92. public void createMenus(JMenu menu, String parent) {
  93. Object o;
  94. JMenu submenu;
  95. EnhancedMenuItem mi;
  96. TemplateAction myAction;
  97. TemplateDir td;
  98. TemplateFile tf;
  99. Enumeration e;
  100. if (!parent.endsWith(File.separator)) {
  101. if (!"".equals(parent)) { // check for root of templates tree
  102. parent = parent + File.separator;
  103. }
  104. }
  105. if (templateFiles == null) this.refreshTemplates();
  106. e = this.templateFiles.elements();
  107. while (e.hasMoreElements()) {
  108. o = e.nextElement();
  109. if (o instanceof TemplateDir) {
  110. td = (TemplateDir) o;
  111. submenu = new JMenu(td.getLabel());
  112. menu.add(submenu); // Add subdirectory as a sub-menu
  113. td.createMenus(submenu, parent + td.getLabel());
  114. }
  115. else {
  116. tf = (TemplateFile) o;
  117. myAction = new TemplateAction(tf.getLabel(), tf.getRelativePath());
  118. mi = new EnhancedMenuItem(tf.getLabel(), myAction);
  119. menu.add(mi);
  120. }
  121. }
  122. }
  123. public Enumeration children() {
  124. return templateFiles.elements();
  125. }
  126. public boolean getAllowsChildren() {
  127. return true;
  128. }
  129. public TreeNode getChildAt(int index) {
  130. return (TreeNode)templateFiles.elementAt(index);
  131. }
  132. public int getChildCount() {
  133. return templateFiles.size();
  134. }
  135. public int getIndex(TreeNode child) {
  136. return templateFiles.indexOf(child);
  137. }
  138. public boolean isLeaf() {
  139. return false;
  140. }
  141. /**
  142. * Generates a string representation of the template hierarchy. This
  143. * method is intended as a debugging tool.
  144. * @return A String representation of the template hierarchy, with each
  145. * directory and filename on a separate line.
  146. */
  147. public String printDir() {
  148. String newLine = System.getProperty("line.separator");
  149. StringBuffer retStr = new StringBuffer("Dir: " + this.getLabel() + newLine);
  150. Enumeration kids = this.children();
  151. while (kids.hasMoreElements()) {
  152. TemplateFile f = (TemplateFile) kids.nextElement();
  153. if (f.isDirectory()) {
  154. retStr.append(((TemplateDir)f).printDir());
  155. } else {
  156. retStr.append(f.getRelativePath() + newLine);
  157. }
  158. }
  159. return retStr.toString();
  160. }
  161. }
  162. /*
  163. * Change Log:
  164. * $Log$
  165. * Revision 1.5 2002/08/16 15:15:12 sjakob
  166. * Added debugging method printDir() which returns a String representation of the
  167. * template hierarchy.
  168. *
  169. * Revision 1.4 2002/08/13 14:47:31 sjakob
  170. * BUG FIX: If backup filename prefix and suffix were both blank, the regular expression used to
  171. * filter backup files would filter all files.
  172. *
  173. * Revision 1.3 2002/05/07 04:08:33 sjakob
  174. * BUG FIX: Fixed problem where template menu items stopped working
  175. * when we started using relative, rather than absolute, file paths.
  176. *
  177. * Revision 1.2 2002/05/07 03:28:10 sjakob
  178. * Added support for template labelling via "#template=" command.
  179. *
  180. * Revision 1.1 2002/04/30 19:26:10 sjakob
  181. * Integrated Calvin Yu's Velocity plugin into Templates to support dynamic templates.
  182. *
  183. * Revision 1.3 2002/02/22 02:34:36 sjakob
  184. * Updated Templates for jEdit 4.0 actions API changes.
  185. * Selection of template menu items can now be recorded in macros.
  186. *
  187. * Revision 1.2 2001/02/23 19:31:39 sjakob
  188. * Added "Edit Template" function to Templates menu.
  189. * Some Javadoc cleanup.
  190. *
  191. * Revision 1.1.1.1 2000/04/21 05:05:44 sjakob
  192. * Initial import of rel-1.0.0
  193. *
  194. * Revision 1.3 2000/03/08 15:46:49 sjakob
  195. * Updated README, CHANGES, to-do files.
  196. * Use properties for error messages, rather than hard-coded strings.
  197. *
  198. * Revision 1.2 2000/03/08 06:55:46 sjakob
  199. * Use org.gjt.sp.util.Log instead of System.out.println.
  200. * Update documentation.
  201. * Add sample template files to project.
  202. *
  203. * Revision 1.1 2000/03/03 06:25:43 sjakob
  204. * Redesigned the plugin to fix a bug where only the most recent view had a
  205. * Templates menu. Added TemplateFile and TemplateDir classes to handle
  206. * files and directories in the Templates directory tree. Templates menus for
  207. * all views are refreshed simultaneously.
  208. *
  209. */