From: <her...@us...> - 2024-03-14 07:45:51
|
Revision: 25712 http://sourceforge.net/p/jedit/svn/25712 Author: hertzhaft Date: 2024-03-14 07:45:49 +0000 (Thu, 14 Mar 2024) Log Message: ----------- Bug fixes and improvements for release 5.1.0 Modified Paths: -------------- plugins/Templates/trunk/CHANGES.txt plugins/Templates/trunk/Templates.props plugins/Templates/trunk/actions.xml plugins/Templates/trunk/description.html plugins/Templates/trunk/templates/AcceleratorManager.java plugins/Templates/trunk/templates/AcceleratorOptionPane.java plugins/Templates/trunk/templates/TemplateAction.java plugins/Templates/trunk/templates/TemplateDir.java plugins/Templates/trunk/templates/TemplateDockable.java plugins/Templates/trunk/templates/TemplateFile.java plugins/Templates/trunk/templates/TemplateTree.java plugins/Templates/trunk/templates/TemplatesMenuProvider.java plugins/Templates/trunk/templates/TemplatesOptionPane.java plugins/Templates/trunk/templates/TemplatesPlugin.java Modified: plugins/Templates/trunk/CHANGES.txt =================================================================== --- plugins/Templates/trunk/CHANGES.txt 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/CHANGES.txt 2024-03-14 07:45:49 UTC (rev 25712) @@ -1,5 +1,15 @@ CHANGES file for Templates plugin. +Version 5.1.0 (11-MAR-2024) +============================= +CHANGES: +- fixed problem: keyboard bindings for template actions were not initialized (hertzhaft) +- fixed problem: keyboard shortcuts for templates were not functioning before opening the Plugins->Templates menu (hertzhaft) +- replaced the serial naming scheme for template actions; now shortcuts are no longer reassigned when new templates are added (hertzhaft) +- fixed problem: template accelerators were not saved (hertzhaft) +- added missing title and message properties for accelerator dialogs in dockable (hertzhaft) +- improved usability of accelerator option pane, added "reload accelerators" button (hertzhaft) + Version 5.0.0 (05-JAN-2017) ============================= CHANGES: @@ -165,10 +175,8 @@ Version 0.3.1 (10-JAN-2000) ============================= CHANGES: -- in TemplatesPlugin.createMenuItems(), Templates submenu now added to -menus vector, not menuItems (M. Dillon) -- changed comments style in Templates.props to conform to standard Java -properties file format (M. Dillon) +- in TemplatesPlugin.createMenuItems(), Templates submenu now added to menus vector, not menuItems (M. Dillon) +- changed comments style in Templates.props to conform to standard Java properties file format (M. Dillon) - added missing Templates.html help file back into distribution Version 0.3.0 (20-DEC-1999) Modified: plugins/Templates/trunk/Templates.props =================================================================== --- plugins/Templates/trunk/Templates.props 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/Templates.props 2024-03-14 07:45:49 UTC (rev 25712) @@ -3,7 +3,7 @@ plugin.templates.TemplatesPlugin.activate=startup plugin.templates.TemplatesPlugin.name=Templates plugin.templates.TemplatesPlugin.author=Steve Jakob, Calvin Yu -plugin.templates.TemplatesPlugin.version=5.0.3 +plugin.templates.TemplatesPlugin.version=5.1.0 plugin.sessions.TemplatesPlugin.description=The Templates plugin integrates the Apache Velocity templating engine with jEdit, to allow dynamic text generation based on template files. plugin.templates.TemplatesPlugin.docs=docs/index.html @@ -63,12 +63,18 @@ plugin.TemplatesPlugin.error.buffer-read-only.title=Unable to write plugin.TemplatesPlugin.error.buffer-read-only.message=Unable to write. Buffer is read-only. -#AcceleratorOptionPane Label Properties -plugin.TemplatesPlugin.mode.label=Mode -plugin.TemplatesPlugin.remove-accelerator.label=Remove -plugin.TemplatesPlugin.add-accelerator.label=Add -plugin.TemplatesPlugin.save-accelerator.label=Save +# Messages for the "set accelerator" dialog in the dockable window +plugin.TemplatesPlugin.set-accelerator.input-mode.title=Buffer edit mode +plugin.TemplatesPlugin.set-accelerator.input-mode.message=Enter the edit mode for which the accelerator will be used +plugin.TemplatesPlugin.set-accelerator.input-accelerator.title=Accelerator +plugin.TemplatesPlugin.set-accelerator.input-accelerator.message=Enter the accelerator string +# AcceleratorOptionPane label properties +plugin.TemplatesPlugin.mode.label=Select editor mode +plugin.TemplatesPlugin.new-accelerator.label=Create new accelerator +plugin.TemplatesPlugin.assign-accelerator.label=Assign accelerator to selected template +plugin.TemplatesPlugin.remove-accelerator.label=Remove assigned accelerator +plugin.TemplatesPlugin.reload-accelerator.label=Reload all accelerators from disk + # Templates Tree window templates-tree.title=Templates Tree - Modified: plugins/Templates/trunk/actions.xml =================================================================== --- plugins/Templates/trunk/actions.xml 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/actions.xml 2024-03-14 07:45:49 UTC (rev 25712) @@ -28,7 +28,7 @@ mgr = view.getDockableWindowManager(); mgr.showDockableWindow(VFSBrowser.NAME); mgr.getDockable(VFSBrowser.NAME) - .setDirectory(templates.TemplatesPlugin.getTemplateDir()); + .setDirectory(templates.TemplatesPlugin.getTemplateDirName()); </CODE> </ACTION> </ACTIONS> Modified: plugins/Templates/trunk/description.html =================================================================== --- plugins/Templates/trunk/description.html 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/description.html 2024-03-14 07:45:49 UTC (rev 25712) @@ -4,7 +4,9 @@ files based on template files. The incorporation of the Apache Velocity templating engine as of version 3.0.0 allows for a great deal of flexibility and interactivity. Templates may be accessed through the - Plugins menu or a dockable tree view. + Plugins menu, through expanding inline accelerators (like in the SuperAbbrevs + plugin), through keyboard shortcuts (assignable in GlobalOptions->Shortcuts) + or via a dockable tree view. </p> </html> Modified: plugins/Templates/trunk/templates/AcceleratorManager.java =================================================================== --- plugins/Templates/trunk/templates/AcceleratorManager.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/AcceleratorManager.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -47,10 +47,10 @@ public final static String KEYWORD_MAPPINGS_FILE = "accelerator-mappings"; private static AcceleratorManager instance; - + private static Pattern acceleratorPattern; + private final static String mappingRE = "(.*)\\.(.*)=(.*)"; - private static Pattern acceleratorPattern; - + /** * The mappings member contains a HashMap for each mode that has at least one * defined accelerator. Each of these HashMaps maps the accelerator name to the @@ -69,7 +69,6 @@ */ public AcceleratorManager() { - mappings = new HashMap(); loadAcceleratorMappings(); } @@ -94,6 +93,15 @@ } /** + * Loads all accelerators from mappings file. + */ + public void load() + { + loadAcceleratorMappings(); + Log.log(Log.DEBUG, this, "Reloaded template accelerator mappings"); + } + + /** * Saves all accelerators. */ public void save() @@ -130,7 +138,8 @@ if (modeMap == null) { return Collections.EMPTY_LIST; - } else + } + else { return modeMap.keySet(); } @@ -234,8 +243,7 @@ { System.out.println("AcceleratorManager: accelerator path is " + path); textArea.getBuffer().remove(sel.getStart(), sel.getEnd() - sel.getStart()); - ((TemplatesPlugin) jEdit.getPlugin("templates.TemplatesPlugin")) - .processTemplate(path, textArea); + TemplatesPlugin.processTemplate(path, textArea); } //}}} } @@ -280,7 +288,6 @@ int wordStart = TextUtilities.findWordStart(lineText, pos - 1, textArea.getBuffer().getStringProperty("noWordSep")); String accelerator = lineText.substring(wordStart, pos); - return accelerator; } @@ -303,6 +310,7 @@ */ private void loadAcceleratorMappings() { + mappings = new HashMap(); BufferedReader in = null; File mappingsFile = new File(getMappingsFilePath()); if (!mappingsFile.exists()) @@ -314,19 +322,18 @@ // Create regex pattern for matching accelerators try { - acceleratorPattern = Pattern.compile(mappingRE, - Pattern.CASE_INSENSITIVE); + acceleratorPattern = Pattern.compile(mappingRE, Pattern.CASE_INSENSITIVE); } catch (PatternSyntaxException pe) { // This won't happen if the programmer did his job right. - Log.log(Log.ERROR, this, + Log.log(Log.ERROR, this, "PatternSyntaxException in accelerator RE: " + mappingRE); } catch (IllegalArgumentException iae) { // This won't happen if the programmer did his job right. - Log.log(Log.ERROR, this, + Log.log(Log.ERROR, this, "IllegalArgumentException in accelerator RE: " + mappingRE); } } @@ -424,7 +431,7 @@ private static String getMappingsFilePath() { return MiscUtilities.constructPath( - TemplatesPlugin.getVelocityDirectory(), KEYWORD_MAPPINGS_FILE); + TemplatesPlugin.getVelocityDirectory(), KEYWORD_MAPPINGS_FILE); } } Modified: plugins/Templates/trunk/templates/AcceleratorOptionPane.java =================================================================== --- plugins/Templates/trunk/templates/AcceleratorOptionPane.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/AcceleratorOptionPane.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -41,95 +41,121 @@ implements ListSelectionListener, ActionListener, TreeSelectionListener { - private final static String ADD_ACTION = "add"; - private final static String REMOVE_ACTION = "remove"; - private final static String SAVE_ACTION = "save"; + private final static String ASSIGN_ACTION = "assign"; + private final static String NEW_ACTION = "new"; + private final static String REMOVE_ACTION = "remove"; + private final static String RELOAD_ACTION = "reload"; + private final static String INPUT_PROP = "options.Templates.accelerators.input.accelerator"; - private JComboBox modes; - private JList accelerators; - private TemplateTree templates; - private JButton removeButton; - private JButton saveButton; + private JComboBox modes; + private JList accelerators; + private TemplateTree templates; + private JButton newButton; + private JButton assignButton; + private JButton removeButton; + private JButton reloadButton; + private AcceleratorManager mgr; + /** * Create a new <code>AcceleratorOptionPane</code>. */ public AcceleratorOptionPane() { - super("Templates.accelerators"); + super("Templates.accelerators"); + mgr = AcceleratorManager.getInstance(); } //{{{ TreeSelectionListener Method /** - * Handle a tree selection change. + * save is enabled when a template is highlighted in the tree. */ public void valueChanged(TreeSelectionEvent evt) { - saveButton.setEnabled(templates.isTemplateSelected()); + assignButton.setEnabled(templates.isTemplateSelected()); } //}}} //{{{ ListSelectionListener Method /** - * Accelerator selection value changed. + * A new mode was chosen in the editor mode selector. */ public void valueChanged(ListSelectionEvent evt) { - templates.setEnabled(getSelectedAccelerator() != null); - removeButton.setEnabled(getSelectedAccelerator() != null); - String path = AcceleratorManager.getInstance() - .findTemplatePath(getSelectedMode(), getSelectedAccelerator()); + String acc = getSelectedAccelerator(); + String mode = getSelectedMode(); + String path = mgr.findTemplatePath(mode, acc); templates.setSelectedTemplate(path); - saveButton.setEnabled(false); + templates.setEnabled(true); + newButton.setEnabled(true); + removeButton.setEnabled(acc != null); + assignButton.setEnabled(acc != null); } //}}} //{{{ ActionListener Method /** - * Action performed. + * Perform button or mode selector action */ public void actionPerformed(ActionEvent evt) { - if (ADD_ACTION.equals(evt.getActionCommand())) { - String accelerator = GUIUtilities.input(this, - "options.Templates.accelerators.input.accelerator", - null); - AcceleratorManager.getInstance() - .addAccelerator(getSelectedMode(), accelerator, null); - loadAcceleratorsForMode(getSelectedMode()); - accelerators.setSelectedValue(accelerator, true); - } else if (REMOVE_ACTION.equals(evt.getActionCommand())) { - AcceleratorManager.getInstance() - .removeAccelerator(getSelectedMode(), getSelectedAccelerator()); - loadAcceleratorsForMode(getSelectedMode()); - } else if (SAVE_ACTION.equals(evt.getActionCommand())) { - saveAccelerator(); - } else { - String modeName = getSelectedMode(); - if (modeName == null) { - modes.setSelectedIndex(0); - } - loadAcceleratorsForMode(modeName); - } + String cmd = evt.getActionCommand(); + String mode = getSelectedMode(); + String acc = null; + + if (NEW_ACTION.equals(cmd)) + { + acc = GUIUtilities.input(this, INPUT_PROP, null); + if (acc == null || acc.equals("")) { + return; + } + mgr.addAccelerator(mode, acc, null); + loadAcceleratorsForMode(mode); + accelerators.setSelectedValue(acc, true); + newButton.setEnabled(false); + assignButton.setEnabled(true); + } + else if (ASSIGN_ACTION.equals(cmd)) + { + assignAccelerator(); + } + else if (REMOVE_ACTION.equals(cmd)) + { + mgr.removeAccelerator(mode, getSelectedAccelerator()); + loadAcceleratorsForMode(mode); + } + else if (RELOAD_ACTION.equals(cmd)) + { + mgr.load(); + loadAcceleratorsForMode(mode); + } + else // mode selector changed + { + if (mode == null) + { + modes.setSelectedIndex(0); + } + loadAcceleratorsForMode(mode); + } } //}}} /** - * Save the accelerator. + * Assign the accelerator. */ - public void saveAccelerator() + public void assignAccelerator() { - String accelerator = getSelectedAccelerator(); + String acc = getSelectedAccelerator(); if (!templates.isTemplateSelected()) { GUIUtilities.error(this, "plugin.TemplatesPlugin.error.invalid-template-path", null); } else { String path = templates.getSelectedTemplate(); - AcceleratorManager.getInstance().addAccelerator(getSelectedMode(), - accelerator, - path); + mgr.addAccelerator(getSelectedMode(), acc, path); } - saveButton.setEnabled(false); + newButton.setEnabled(true); + assignButton.setEnabled(false); + removeButton.setEnabled(true); } /** @@ -137,8 +163,9 @@ */ public String getSelectedAccelerator() { - if (accelerators.getModel().getSize() == 0) { - return null; + if (accelerators.getModel().getSize() == 0) + { + return null; } return (String) accelerators.getSelectedValue(); } @@ -148,7 +175,8 @@ */ public String getSelectedMode() { - if (modes.getItemCount() == 0) { + if (modes.getItemCount() == 0) + { return null; } return (String) modes.getSelectedItem(); @@ -161,19 +189,19 @@ { modes = new JComboBox(getModeNames()); modes.addActionListener(this); - addComponent(jEdit.getProperty( - "plugin.TemplatesPlugin.mode.label"), modes); - String currentMode = jEdit.getActiveView().getBuffer().getMode().getName(); - modes.setSelectedItem(currentMode); + addComponent(jEdit.getProperty("plugin.TemplatesPlugin.mode.label"), modes); + String currentMode = jEdit.getActiveView().getBuffer().getMode().getName(); + modes.setSelectedItem(currentMode); JPanel acceleratorPanel = new JPanel(new GridBagLayout()); acceleratorPanel.setBorder(BorderFactory.createEmptyBorder(12, 0, 0, 0)); - GridBagConstraints gbc = new GridBagConstraints(); accelerators = new JList(new DefaultListModel()); accelerators.setVisibleRowCount(7); accelerators.setPrototypeCellValue("XXXXXXXXXXXXXXX"); accelerators.addListSelectionListener(this); + + GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = gbc.WEST; gbc.fill = gbc.BOTH; gbc.gridx = 0; @@ -181,37 +209,52 @@ gbc.insets = new Insets(0, 0, 11, 11); gbc.weightx = .2; gbc.weighty = .998; + acceleratorPanel.add(new JScrollPane(accelerators), gbc); - JButton add = new JButton(jEdit.getProperty( - "plugin.TemplatesPlugin.add-accelerator.label")); - add.setActionCommand(ADD_ACTION); - add.addActionListener(this); + newButton = new JButton( + jEdit.getProperty("plugin.TemplatesPlugin.new-accelerator.label")); + newButton.setActionCommand(NEW_ACTION); + newButton.addActionListener(this); + gbc.fill = gbc.HORIZONTAL; + gbc.weighty = .001; gbc.gridy++; - gbc.weighty = .001; - acceleratorPanel.add(add, gbc); + acceleratorPanel.add(newButton, gbc); - saveButton = new JButton(jEdit.getProperty( - "plugin.TemplatesPlugin.save-accelerator.label")); - saveButton.setActionCommand(SAVE_ACTION); - saveButton.addActionListener(this); - saveButton.setEnabled(false); + assignButton = new JButton( + jEdit.getProperty("plugin.TemplatesPlugin.assign-accelerator.label")); + assignButton.setActionCommand(ASSIGN_ACTION); + assignButton.addActionListener(this); + assignButton.setEnabled(false); + gbc.gridy++; - acceleratorPanel.add(saveButton, gbc); + acceleratorPanel.add(assignButton, gbc); - removeButton = new JButton(jEdit.getProperty( - "plugin.TemplatesPlugin.remove-accelerator.label")); - removeButton.setEnabled(false); + removeButton = new JButton( + jEdit.getProperty("plugin.TemplatesPlugin.remove-accelerator.label")); removeButton.setActionCommand(REMOVE_ACTION); removeButton.addActionListener(this); + removeButton.setEnabled(false); + gbc.gridy++; - gbc.insets = new Insets(0, 0, 0, 11); + // gbc.insets = new Insets(0, 0, 0, 11); acceleratorPanel.add(removeButton, gbc); + reloadButton = new JButton( + jEdit.getProperty("plugin.TemplatesPlugin.reload-accelerator.label")); + reloadButton.setActionCommand(RELOAD_ACTION); + reloadButton.addActionListener(this); + reloadButton.setEnabled(true); + + gbc.gridy++; + gbc.insets = new Insets(0, 0, 0, 11); + acceleratorPanel.add(reloadButton, gbc); + templates = new TemplateTree(); templates.setEnabled(false); templates.addTreeSelectionListener(this); + gbc.fill = gbc.BOTH; gbc.gridy = 0; gbc.gridx++; @@ -225,41 +268,54 @@ GridBagConstraints cons = gridBag.getConstraints(acceleratorPanel); cons.fill = cons.BOTH; gridBag.setConstraints(acceleratorPanel, cons); - + loadAcceleratorsForMode(currentMode); } - /** - * Load the accelerators for the given mode. - */ - private void loadAcceleratorsForMode(String modeName) - { - Collection col = AcceleratorManager.getInstance().getAccelerators(modeName); - if (!col.isEmpty() && accelerators != null) { - accelerators.setEnabled(true); - List list = new ArrayList(col); - Collections.sort(list); - DefaultListModel listModel = (DefaultListModel) accelerators.getModel(); - listModel.clear(); - for (Iterator i = list.iterator(); i.hasNext();) { - listModel.addElement(i.next()); - } - accelerators.setModel(listModel); - accelerators.setSelectedIndex(0); - } - } + /** + * Saves accelerators on exit + */ + protected void _save() + { + mgr.save(); + } - /** - * Returns an array of mode names. - */ - private static String[] getModeNames() - { - Mode[] modes = jEdit.getModes(); - String[] names = new String[modes.length]; - for (int i=0; i<modes.length; i++) { - names[i] = modes[i].getName(); - } - return names; - } + /** + * Load the accelerators for the given mode. + */ + private void loadAcceleratorsForMode(String modeName) + { + if (accelerators == null) { + return; + } + DefaultListModel listModel = (DefaultListModel) accelerators.getModel(); + listModel.clear(); + Collection coll = mgr.getAccelerators(modeName); + if (coll.isEmpty()) { + return; + } + List list = new ArrayList(coll); + Collections.sort(list); + for (Iterator i = list.iterator(); i.hasNext();) + { + listModel.addElement(i.next()); + } + // accelerators.setModel(listModel); + accelerators.setSelectedIndex(0); + accelerators.setEnabled(true); + } + /** + * Returns an array of mode names. + */ + private static String[] getModeNames() + { + Mode[] modes = jEdit.getModes(); + String[] names = new String[modes.length]; + for (int i=0; i<modes.length; i++) { + names[i] = modes[i].getName(); + } + return names; + } + } Modified: plugins/Templates/trunk/templates/TemplateAction.java =================================================================== --- plugins/Templates/trunk/templates/TemplateAction.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplateAction.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -30,12 +30,13 @@ */ public class TemplateAction extends BeanShellAction { - // private static String actionLabel = "Templates.process-template"; + // setup BeanShell code for action private static String code1 = "templates.TemplatesPlugin.processTemplate(\""; private static String code2 = "\", textArea);"; + // phased out MR 2024 private static int serialNum = 0; - //Constructors + // Constructor public TemplateAction(String actionName, String templateName, String filepath) { super(actionName, code1 + filepath.replace('\\','/') + code2, @@ -46,22 +47,21 @@ jEdit.setTemporaryProperty(actionName + ".label", templateName); } + // Constructor from TemplateFile public TemplateAction(TemplateFile file) { - this(TemplateAction.getUniqueActionName(), file.getLabel(), file.getPath()); + this( + // getUniqueActionName(), + file.getActionName(), + file.getLabel(), + file.getRelativePath() + ); } - + + // bad: changes when templates are added, so key bindings would get shifted public static synchronized final String getUniqueActionName() { return "process-template-" + Integer.toString(serialNum++); } - /** - * Over-ride EditAction.getLabel() which requires a property - */ - /* public String getLabel() { - return actionLabel; - } - */ - } /* * Change Log: Modified: plugins/Templates/trunk/templates/TemplateDir.java =================================================================== --- plugins/Templates/trunk/templates/TemplateDir.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplateDir.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -36,18 +36,20 @@ public class TemplateDir extends TemplateFile { private Vector templateFiles; + private ActionSet actionSet; + // private static RE backupFilter; public static Pattern backupPattern; - //Constructors + // Constructors public TemplateDir(TemplateDir parent, File templateFile) { super(parent, templateFile); } - //Accessors & Mutators + // Accessors & Mutators - //Implementors + // Implementors public boolean isDirectory() { return true; @@ -58,7 +60,7 @@ * to template files. Backup files are ignored based on the values of the * backup prefix and suffix in the "Global Options" settings. */ - public void refreshTemplates() + public void readTemplates() { File f; this.templateFiles = new Vector(); @@ -74,15 +76,15 @@ if (f.isDirectory()) { // Add subdirectory as a TemplateDir - TemplateDir submenu = new TemplateDir(this, f); - this.templateFiles.addElement(submenu); - submenu.refreshTemplates(); + TemplateDir subdir = new TemplateDir(this, f); + this.templateFiles.addElement(subdir); + subdir.readTemplates(); } else { Matcher m = backupPattern.matcher(files[i]); if (!m.matches()) - {// if not a backup file + { // if not a backup file TemplateFile tf = new TemplateFile(this, f); this.templateFiles.addElement(tf); } @@ -107,7 +109,7 @@ { String exp = jEdit.getProperty("backup.prefix") + "\\S+" + - jEdit.getProperty("backup.suffix");// RE for jEdit backups + jEdit.getProperty("backup.suffix"); // RE for jEdit backups if (exp.equals("\\S+")) { exp = ""; @@ -115,6 +117,15 @@ // backupFilter = new RE(exp, RE.REG_ICASE); backupPattern = Pattern.compile(exp, Pattern.CASE_INSENSITIVE); } + + private static String fixPath(String path) + { // check for root of templates tree + if (!path.endsWith(File.separator) && !"".equals(path)) + { + path = path + File.separator; + } + return path; + } /** * Add a menu item to the given menu object for each TemplateFile contained @@ -124,62 +135,99 @@ * @param menu The menu to which the new JMenuItem objects will be added. * @param parent The path of the parent directory */ - public void createMenus(JMenu menu, String parent) + public void createMenus(JMenu menu, String parentPath) { - Object o; - JMenu submenu; - TemplateAction myAction; - TemplateDir td; - TemplateFile tf; - Enumeration e; - if (!parent.endsWith(File.separator)) + Log.log(Log.DEBUG, TemplatesPlugin.class, "subMenu: " + getLabel()); + + // PROBLEM #1: Templates actions are not showing up in + // Utilities->Global Options->Shortcuts until after "Refresh Templates" + // is selected. + // PROBLEM #2: When "Refresh Templates" is selected, the old actions + // are not removed. + // solved in v.5.1.0 (hertzhaft) + + Enumeration items = this.templateFiles.elements(); + while (items.hasMoreElements()) { - // check for root of templates tree - if (!"".equals(parent)) - { - parent = parent + File.separator; + Object object = items.nextElement(); + if (object instanceof TemplateDir) + { // add sub-menu + TemplateDir dir = (TemplateDir) object; + JMenu subMenu = new JMenu(dir.getLabel()); + menu.add(subMenu); + dir.createMenus(subMenu, fixPath(parentPath) + dir.getLabel()); } + else + { // add template action as a menu item + TemplateFile file = (TemplateFile) object; + menu.add(GUIUtilities.loadMenuItem( + jEdit.getActionContext(), + file.getActionName(), + false + )); + } } + } + + /** + * Add an ActionSet for this TemplateDir. + * Add an action for each TemplateFile contained within this TemplateDir. + * Recursively process any TemplateDir objects within this TemplateDir. + * + * @param parent The path of the parent directory + */ + public void createActions(String parentPath) + { if (templateFiles == null) { - this.refreshTemplates(); + this.readTemplates(); } - // Create ActionSet for the items in this TemplateDir - StringBuffer sb = new StringBuffer("Templates - "); - if ("".equals(parent)) // Top-level menu - sb.append("(Top-level menu)"); - else - sb.append(this.getFQLabel()); - ActionSet myActions = new ActionSet(sb.toString()); - // PROBLEM #1: Templates actions are not showing up in - // Utilities->Global Options->Shortcuts until after "Refresh Templates" - // is selected. - // PROBLEM #2: When "Refresh Templates" is selected, the old actions - // are not removed. - e = this.templateFiles.elements(); - while (e.hasMoreElements()) + String parent = fixPath(parentPath); + + StringBuffer name = new StringBuffer("Templates: "); + name.append("".equals(parent) + ? "(top-level menu)" + : this.getFQLabel()); + addActionSet( new ActionSet(name.toString())); + + Log.log(Log.DEBUG, TemplatesPlugin.class, "actionSet: " + name); + + Enumeration items = this.templateFiles.elements(); + while (items.hasMoreElements()) { - o = e.nextElement(); - if (o instanceof TemplateDir) - { - td = (TemplateDir) o; - submenu = new JMenu(td.getLabel()); - menu.add(submenu);// Add subdirectory as a sub-menu - td.createMenus(submenu, parent + td.getLabel()); - } else - { - tf = (TemplateFile) o; - String actionName = TemplateAction.getUniqueActionName(); - myAction = new TemplateAction(actionName, - tf.getLabel(), tf.getRelativePath()); - myActions.addAction(myAction); - menu.add(GUIUtilities.loadMenuItem(jEdit.getActionContext(), actionName, false)); + Object object = items.nextElement(); + if (object instanceof TemplateDir) + { // add ActionSet for subdir + TemplateDir dir = (TemplateDir) object; + dir.createActions(parent + dir.getLabel()); } + else + { // add Action for template file + TemplateFile file = (TemplateFile) object; + TemplateAction action = new TemplateAction(file); + actionSet.addAction(action); + } } - jEdit.addActionSet(myActions); + registerActionSet(); } + public ActionSet registerActionSet() + { + actionSet.initKeyBindings(); + jEdit.addActionSet(actionSet); + return actionSet; + } + + public ActionSet addActionSet(ActionSet newActionSet) + { + if (actionSet != null) { + jEdit.removeActionSet(actionSet); + } + actionSet = newActionSet; + return actionSet; + } + public Enumeration children() { return templateFiles.elements(); Modified: plugins/Templates/trunk/templates/TemplateDockable.java =================================================================== --- plugins/Templates/trunk/templates/TemplateDockable.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplateDockable.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -59,7 +59,7 @@ templates.addMouseListener(this); } - + public boolean requestFocusInWindow() { templates.requestFocus(); return true; @@ -102,8 +102,7 @@ */ public void processSelectedTemplate() { - TemplatesPlugin plugin = (TemplatesPlugin) jEdit.getPlugin("templates.TemplatesPlugin"); - plugin.processTemplate(templates.getSelectedTemplate(), view, view.getTextArea()); + TemplatesPlugin.processTemplate(templates.getSelectedTemplate(), view, view.getTextArea()); view.getEditPane().getTextArea().requestFocus(); } @@ -182,34 +181,46 @@ if (RELOAD.equals(evt.getActionCommand())) { TemplatesPlugin.refreshTemplates(); templates.reload(); - } else if (EDIT.equals(evt.getActionCommand())) { - jEdit.openFile(view, MiscUtilities.concatPath(TemplatesPlugin.getTemplateDir(), - templates.getSelectedTemplate())); - } else if (SET_ACCELERATOR.equals(evt.getActionCommand())) { - String mode = - GUIUtilities.input(view, "plugin.TemplatesPlugin.set-accelerator.input-mode", - jEdit.getModes(), view.getEditPane().getBuffer().getMode().getName()); + } + else if (EDIT.equals(evt.getActionCommand())) { + jEdit.openFile(view, MiscUtilities.concatPath( + TemplatesPlugin.getTemplateDirName(), + templates.getSelectedTemplate() + )); + } + else if (SET_ACCELERATOR.equals(evt.getActionCommand())) { + String mode = GUIUtilities.input(view, + "plugin.TemplatesPlugin.set-accelerator.input-mode", + jEdit.getModes(), + view.getEditPane().getBuffer().getMode().getName() + ); if (mode == null) { return; } - String accelerator = - GUIUtilities.input(view, "plugin.TemplatesPlugin.set-accelerator.input-accelerator", null); + String accelerator = GUIUtilities.input(view, + "plugin.TemplatesPlugin.set-accelerator.input-accelerator", + null + ); if (accelerator == null) { return; } - if (AcceleratorManager.getInstance().findTemplatePath(mode, - accelerator) != null) - { - int result = - GUIUtilities.confirm(view, "plugin.TemplatesPlugin.set-accelerator.confirm-overwrite", - new String[] {"accelerator"}, JOptionPane.YES_NO_OPTION, - JOptionPane.WARNING_MESSAGE); - if (result != JOptionPane.YES_OPTION) { - return; - } + + AcceleratorManager mgr = AcceleratorManager.getInstance(); + + if (mgr.findTemplatePath(mode, accelerator) != null) { + int result = GUIUtilities.confirm(view, + "plugin.TemplatesPlugin.set-accelerator.confirm-overwrite", + new String[] {"accelerator"}, + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE + ); + if (result != JOptionPane.YES_OPTION) + { + return; + } } - AcceleratorManager.getInstance().addAccelerator(mode, accelerator, - templates.getSelectedTemplate()); + mgr.addAccelerator(mode, accelerator, templates.getSelectedTemplate()); + mgr.save(); } } //}}} Modified: plugins/Templates/trunk/templates/TemplateFile.java =================================================================== --- plugins/Templates/trunk/templates/TemplateFile.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplateFile.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -43,7 +43,7 @@ private static Pattern templateLabelPattern; private TemplateDir parent; - //Constructors + // Constructor public TemplateFile(TemplateDir parent, File templateFile) { super(); @@ -72,7 +72,7 @@ } } - //Accessors & Mutators + // Accessors & Mutators public String getLabel() { return label; @@ -83,6 +83,11 @@ label = labelVal; } + public String getActionName() + { + return "velocity-" + getRelativePath(); + } + /** * Returns the "fully qualified" label, that is, a hierarchical label * which includes the label of this <code>TemplateFile</code>'s parent. @@ -110,14 +115,14 @@ public String getRelativePath() { String absolutePath = templateFile.getPath(); - if (absolutePath.startsWith(TemplatesPlugin.getTemplateDir())) + if (absolutePath.startsWith(TemplatesPlugin.getTemplateDirName())) { - return absolutePath.substring(TemplatesPlugin.getTemplateDir().length()); + return absolutePath.substring(TemplatesPlugin.getTemplateDirName().length()); } return absolutePath; } - //Implementors + // Implementors public boolean isDirectory() { return false; Modified: plugins/Templates/trunk/templates/TemplateTree.java =================================================================== --- plugins/Templates/trunk/templates/TemplateTree.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplateTree.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -108,7 +108,8 @@ */ public boolean isLastPathComponentATemplate(TreePath path) { - if (path == null) { + if (path == null) + { return false; } TreeNode node = (TreeNode) path.getLastPathComponent(); @@ -123,8 +124,10 @@ // NOTE: the supplied object path is an array of Strings List path = new LinkedList(); path.add(root); - for (int i=0; i<objPath.length; i++) { - if (!findChildNode(objPath, i, path)) { + for (int i=0; i<objPath.length; i++) + { + if (!findChildNode(objPath, i, path)) + { return null; } } @@ -139,7 +142,8 @@ Object target = objPath[idx]; TreeNode parent = (TreeNode) path.get(path.size() - 1); Enumeration children = parent.children(); - while (children.hasMoreElements()) { + while (children.hasMoreElements()) + { TreeNode node = (TreeNode) children.nextElement(); if ((((TemplateFile)node).getRelativePath()).endsWith((String)target)) { path.add(node); Modified: plugins/Templates/trunk/templates/TemplatesMenuProvider.java =================================================================== --- plugins/Templates/trunk/templates/TemplatesMenuProvider.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplatesMenuProvider.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -41,7 +41,7 @@ * Re-create the Templates menu hierarchy. */ public void update(JMenu templatesMenu) { - Log.log(Log.DEBUG,this,"... TemplatesMenu.update()"); + Log.log(Log.DEBUG,this,"TemplatesMenu.update()"); templatesMenu.removeAll(); // Create menu items for the "Refresh" option and a separator JMenuItem mi; Modified: plugins/Templates/trunk/templates/TemplatesOptionPane.java =================================================================== --- plugins/Templates/trunk/templates/TemplatesOptionPane.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplatesOptionPane.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -62,7 +62,7 @@ * @param textFieldStr A string containing the current Templates directory. */ private void initGUI() { - String templateFieldStr = TemplatesPlugin.getTemplateDir(); + String templateFieldStr = TemplatesPlugin.getTemplateDirName(); String velocityFieldStr = TemplatesPlugin.getVelocityDirectory(); boolean accelPassThruFlag = TemplatesPlugin.getAcceleratorPassThruFlag(); setLayout(new BorderLayout()); Modified: plugins/Templates/trunk/templates/TemplatesPlugin.java =================================================================== --- plugins/Templates/trunk/templates/TemplatesPlugin.java 2024-02-18 13:58:02 UTC (rev 25711) +++ plugins/Templates/trunk/templates/TemplatesPlugin.java 2024-03-14 07:45:49 UTC (rev 25712) @@ -35,16 +35,17 @@ import velocity.jEditContext; /** - * A jEdit plugin for adding a templating function. + * A jEdit plugin for using and managing Apache Velocity templates. * * @author Steve Jakob */ public class TemplatesPlugin extends EditPlugin - implements RuntimeConstants, VelocityConstants + implements RuntimeConstants, VelocityConstants { + private static String pluginName; private static String defaultTemplateDir; private static String defaultVelocityDir; - private static String sepChar;// System-dependant separator character + private static String sepChar; // System-dependant separator character private static TemplateDir templates = null; private static VelocityEngine engine; @@ -60,21 +61,11 @@ } /** - * Sets the root TemplateDir object to another value. - * - * @param newTemplates The new TemplateDir object - */ - public static void setTemplates(TemplateDir newTemplates) - { - templates = newTemplates; - } - - /** * Returns the directory where templates are stored * * @return A string containing the template directory path. */ - public static String getTemplateDir() + public static String getTemplateDirName() { String templateDir = jEdit.getProperty("plugin.TemplatesPlugin.templateDir.0", ""); if (templateDir.equals("")) @@ -132,7 +123,7 @@ * throw a more specific exception. */ public static VelocityEngine getEngine() - throws Exception + throws Exception { if (engine == null) { @@ -139,7 +130,7 @@ engine = new VelocityEngine(); Properties props = loadVelocityProperties(); props.setProperty(RUNTIME_LOG, getVelocityLog()); - props.setProperty(FILE_RESOURCE_LOADER_PATH, TemplatesPlugin.getTemplateDir()); + props.setProperty(FILE_RESOURCE_LOADER_PATH, TemplatesPlugin.getTemplateDirName()); engine.init(props); } return engine; @@ -211,18 +202,16 @@ { Properties props = new Properties(); InputStream in = null; - TemplatesPlugin thePlugin = (TemplatesPlugin) jEdit.getPlugin( - "templates.TemplatesPlugin"); // Load the normal Velocity properties try { - in = thePlugin.getClass().getClassLoader(). + in = TemplatesPlugin.class.getClassLoader(). getResourceAsStream("velocity/velocity.properties"); props.load(in); } catch (IOException e) { - Log.log(Log.ERROR, thePlugin, "Error loading normal velocity properties"); + Log.log(Log.ERROR, pluginName, "Error loading normal velocity properties"); } finally { @@ -240,7 +229,7 @@ } catch (IOException e) { - Log.log(Log.ERROR, thePlugin, "Error loading custom velocity properties"); + Log.log(Log.ERROR, pluginName, "Error loading custom velocity properties"); } finally { @@ -251,80 +240,74 @@ //{{{ EditPlugin Methods /** - * Start this plugin. + * Start this plugin. Read templates directory and create actions/actionsets. */ public void start() { + pluginName = TemplatesPlugin.class.getSimpleName(); + sepChar = System.getProperty("file.separator"); - defaultTemplateDir = jEdit.getSettingsDirectory() + sepChar + - "templates" + sepChar; - defaultVelocityDir = jEdit.getSettingsDirectory() + sepChar + - "velocity" + sepChar; + defaultTemplateDir = jEdit.getSettingsDirectory() + sepChar + "templates" + sepChar; + defaultVelocityDir = jEdit.getSettingsDirectory() + sepChar + "velocity" + sepChar; File velocityDir = new File(getVelocityDirectory()); if (velocityDir.isFile()) { - Log.log(Log.DEBUG, this, "'" + getVelocityDirectory() + "' is a file"); + Log.log(Log.DEBUG, pluginName, "'" + getVelocityDirectory() + "' is a file"); } if (!velocityDir.exists() && !velocityDir.mkdirs()) { - Log.log(Log.DEBUG, this, "Cannot make directory '" + getVelocityDirectory() + "'"); + Log.log(Log.DEBUG, pluginName, "Cannot make directory '" + getVelocityDirectory() + "'"); } - File templatesDir = new File(TemplatesPlugin.getTemplateDir()); - if (!templatesDir.exists() && !templatesDir.mkdirs()) + File templateDir = new File(TemplatesPlugin.getTemplateDirName()); + if (!templateDir.exists() && !templateDir.mkdirs()) { - Log.log(Log.DEBUG, this, "Cannot make directory '" + TemplatesPlugin.getTemplateDir() + "'"); + Log.log(Log.DEBUG, pluginName, "Cannot make directory '" + TemplatesPlugin.getTemplateDirName() + "'"); } - templates = new TemplateDir(null, templatesDir); - templates.refreshTemplates(); + templates = new TemplateDir(null, templateDir); + templates.readTemplates(); + templates.createActions(""); // make template shortcuts work from the start } /** - * Not used. + * Scans the templates directory, recreates all actions/actionsets + * and sends an EditBus message to all TemplatesMenu objects + * to update themselves when accessed. + * Backup files are ignored based on the values of the backup prefix and suffix + * in the "Global Options" settings. */ - public void stop() - { - AcceleratorManager.getInstance().save(); - } - - /** - * Scans the templates directory and sends an EditBus message to all - * TemplatesMenu objects to update themselves. Backup files are ignored based - * on the values of the backup prefix and suffix in the "Global Options" - * settings. - */ public static void refreshTemplates() { - String templateDirStr = getTemplateDir(); - File templateDir = new File(templateDirStr); + String dirName = getTemplateDirName(); + File templateDir = new File(dirName); try { if (!templateDir.exists()) - {// If the template directory doesn't exist - Log.log(Log.DEBUG, - jEdit.getPlugin(jEdit.getProperty("plugin.TemplatesPlugin.name")), - "Attempting to create templates directory: " + templateDirStr); - templateDir.mkdir();// then create it + { // If the template directory doesn't exist + Log.log(Log.DEBUG, pluginName, + "Attempting to create templates directory: " + dirName); + templateDir.mkdir(); // then create it if (!templateDir.exists()) - {// If insufficent privileges to create it + { // If insufficent privileges to create it throw new java.lang.SecurityException(); } } - setTemplates(new TemplateDir(null, templateDir)); - getTemplates().refreshTemplates(); - buildAllMenus(); + templates = new TemplateDir(null, templateDir); + templates.readTemplates(); + templates.createActions(""); + sendMenuChanged(); } catch (java.lang.SecurityException se) { - Log.log(Log.ERROR, - jEdit.getPlugin(jEdit.getProperty("plugin.TemplatesPlugin.name")), - jEdit.getProperty("plugin.TemplatesPlugin.error.create-dir") + templateDir - ); + Log.log(Log.ERROR, pluginName, + jEdit.getProperty("plugin.TemplatesPlugin.error.create-dir") + templateDir + ); } } - private static void buildAllMenus() + private static void sendMenuChanged() { + // signal the Plugins->Templates menu tree should be rebuilt on access EditBus.send(new DynamicMenuChanged("plugin.templates.TemplatesPlugin.menu")); } @@ -336,7 +319,7 @@ */ public static void editTemplate(View view) { - JFileChooser chooser = new JFileChooser(getTemplateDir()); + JFileChooser chooser = new JFileChooser(getTemplateDirName()); int retVal = chooser.showOpenDialog(view); if (retVal == JFileChooser.APPROVE_OPTION) { @@ -344,13 +327,11 @@ if (file != null) { try - { - // Load file into jEdit + { // Load file into jEdit jEdit.openFile(view, file.getCanonicalPath()); } catch (IOException e) - { - // shouldn't happen + { // shouldn't happen } } } @@ -364,7 +345,7 @@ */ public static void saveTemplate(View view) { - JFileChooser chooser = new JFileChooser(getTemplateDir()); + JFileChooser chooser = new JFileChooser(getTemplateDirName()); int retVal = chooser.showSaveDialog(view); if (retVal == JFileChooser.APPROVE_OPTION) { @@ -372,13 +353,11 @@ if (file != null) { try - { - // Save file + { // Save file view.getBuffer().save(view, file.getCanonicalPath()); } catch (IOException e) - { - // shouldn't happen + { // shouldn't happen } } } @@ -439,10 +418,8 @@ } catch (Exception e) { - TemplatesPlugin thePlugin = (TemplatesPlugin) jEdit.getPlugin( - "templates.TemplatesPlugin"); - Log.log(Log.ERROR, thePlugin, "Error processing template '" + template + "'"); - Log.log(Log.ERROR, thePlugin, e); + Log.log(Log.ERROR, pluginName, "Error processing template '" + template + "'"); + Log.log(Log.ERROR, pluginName, e); JOptionPane.showMessageDialog(textArea, "An error occurred while attempting to process a template." + "\nRefer to the Activity Log for more details.", This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |