|
/** |
|
* Initializes the Templates menu. |
|
* <p> |
|
* Other components can add templates simply by providing scripts in their |
|
* resources, identified by a path of the form |
|
* {@code /script_templates/<menu path>/<menu label>}. |
|
* </p> |
|
* |
|
* @param templatesMenu the top-level menu to populate |
|
*/ |
|
private void addTemplates(final JMenu templatesMenu) { |
|
final File baseDir = appService.getApp().getBaseDirectory(); |
|
|
|
for (final String templatePath : TEMPLATE_PATHS) { |
|
for (final Map.Entry<String, URL> entry : new TreeMap<>( |
|
FileUtils.findResources(null, templatePath, baseDir)).entrySet()) |
|
{ |
|
final String key = entry.getKey(); |
|
final String ext = FileUtils.getExtension(key); |
|
|
|
// try to determine the scripting language |
|
final ScriptLanguage lang = ext.isEmpty() ? null : |
|
scriptService.getLanguageByExtension(ext); |
|
final String langName = lang == null ? null : lang.getLanguageName(); |
|
final String langSuffix = lang == null ? null : " (" + langName + ")"; |
|
|
|
final String path = adjustPath(key, langName); |
|
|
|
// create a human-readable label |
|
final int labelIndex = path.lastIndexOf('/') + 1; |
|
final String label = ext.isEmpty() ? path.substring(labelIndex) : |
|
path.substring(labelIndex, path.length() - ext.length() - 1); |
|
|
|
final ActionListener menuListener = new ActionListener() { |
|
@Override |
|
public void actionPerformed(final ActionEvent e) { |
|
loadTemplate(entry.getValue()); |
|
} |
|
}; |
|
|
|
// add script to the secondary language-sorted menu structure |
|
if (langName != null) { |
|
final String langPath = "[by language]/" + langName + "/" + path; |
|
final JMenu langMenu = getMenu(templatesMenu, langPath, true); |
|
final JMenuItem langItem = new JMenuItem(label); |
|
langMenu.add(langItem); |
|
langItem.addActionListener(menuListener); |
|
} |
|
|
|
// add script to the primary Templates menu structure |
|
final JMenu menu = getMenu(templatesMenu, path, true); |
|
final JMenuItem item = new JMenuItem(label + langSuffix); |
|
menu.add(item); |
|
item.addActionListener(menuListener); |
|
} |
|
} |
|
} |