Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide functionality to find and locate template scripts #6

Open
imagejan opened this issue Dec 6, 2016 · 1 comment
Open

Provide functionality to find and locate template scripts #6

imagejan opened this issue Dec 6, 2016 · 1 comment

Comments

@imagejan
Copy link
Member

imagejan commented Dec 6, 2016

It would be helpful to have a tool to quickly search for a specific script template in the Templates menu, as well as locating its source (i.e. from which jar file, from which update site).

Maybe some code of the ImageJ CommandFinder can be re-used for this, at it provides the same functionality for menu commands?

@imagejan
Copy link
Member Author

imagejan commented Jan 8, 2018

With the new SearchActionFactory interface in place, it would probably sufficient to factor out the logic from here:

/**
* 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);
}
}
}

to generate a list of available templates that is usable by both the current script editor (when creating its Templates menu) and a newly created OpenTemplateActionFactory or some such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant