Search in sources :

Example 1 with AlgorithmNode

use of org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.AlgorithmNode in project core by jcryptool.

the class FlexiProviderAlgorithmsView method hookActions.

/**
 * Adds an listener, which will provide the context help for a selected algorithm. If for a child is no context help
 * available, it will use the parents context help
 */
@SuppressWarnings("unchecked")
private void hookActions(String xmlfile) {
    doubleClickHandler = new AbstractHandler() {

        public Object execute(ExecutionEvent event) {
            ISelection selection = viewer.getSelection();
            Object obj = ((IStructuredSelection) selection).getFirstElement();
            if (obj instanceof CategoryNode || obj instanceof FolderNode) {
                if (viewer.getTree().getSelection()[0].getExpanded()) {
                    viewer.collapseToLevel(obj, 1);
                } else {
                    viewer.expandToLevel(obj, 1);
                }
            } else if (obj instanceof AlgorithmNode) {
                AlgorithmsManager.getInstance().algorithmCalled(((AlgorithmNode) obj).getAlgorithm());
            }
            return (null);
        }
    };
    URL xmlFile = FlexiProviderAlgorithmsPlugin.getDefault().getBundle().getEntry(xmlfile);
    SAXBuilder builder = new SAXBuilder();
    // Build a lookup table for the names defined in xml/help_algorithmis.xml
    // to the according context id (in $nl$/contexts_algorithms.xml)
    final Hashtable<String, String> contextIdLookup = new Hashtable<String, String>();
    try {
        Document doc = builder.build(xmlFile);
        Element root = doc.getRootElement();
        List<Element> helpEntries = root.getChildren("helpentry");
        for (Element helpEntry : helpEntries) {
            String mainname = helpEntry.getAttributeValue("mainname");
            String contextid = helpEntry.getAttributeValue("contextid");
            contextIdLookup.put(mainname, contextid);
            Element aliasesRoot = helpEntry.getChild("aliases");
            if (aliasesRoot != null) {
                List<Element> aliases = aliasesRoot.getChildren("alias");
                for (Element alias : aliases) {
                    contextIdLookup.put(alias.getValue(), contextid);
                }
            }
        }
        viewer.getControl().addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDoubleClick(final MouseEvent e) {
                if (e.button == 1) {
                    // only left button double clicks
                    try {
                        // run assigned action
                        doubleClickHandler.execute(null);
                    } catch (ExecutionException ex) {
                        LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, ex);
                    }
                }
            }

            @Override
            public void mouseDown(final MouseEvent event) {
                IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
                Object obj = ((IStructuredSelection) selection).getFirstElement();
                if (obj instanceof ITreeNode) {
                    ITreeNode node = (ITreeNode) obj;
                    // display help of parent entry if no context help is available
                    do {
                        String name = node.toString();
                        if (contextIdLookup.containsKey(name)) {
                            String contextId = FlexiProviderAlgorithmsPlugin.PLUGIN_ID + "." + contextIdLookup.get(name);
                            PlatformUI.getWorkbench().getHelpSystem().displayHelp(contextId);
                            break;
                        }
                        node = node.getParent();
                    } while (node != null);
                    viewer.getControl().setFocus();
                    viewer.setSelection(selection);
                }
            }
        });
    } catch (JDOMException e) {
        LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, e);
    } catch (IOException e) {
        LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, e);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) MouseEvent(org.eclipse.swt.events.MouseEvent) FolderNode(org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.FolderNode) AlgorithmNode(org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.AlgorithmNode) Hashtable(java.util.Hashtable) Element(org.jdom.Element) MouseAdapter(org.eclipse.swt.events.MouseAdapter) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) AbstractHandler(org.eclipse.core.commands.AbstractHandler) URL(java.net.URL) CategoryNode(org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.CategoryNode) ITreeNode(org.jcryptool.crypto.flexiprovider.ui.nodes.ITreeNode) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) ISelection(org.eclipse.jface.viewers.ISelection) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 2 with AlgorithmNode

use of org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.AlgorithmNode in project core by jcryptool.

the class FlexiProviderAlgorithmsViewContentProvider method init.

private static CategoryNode init(String label, List<IMetaAlgorithm> algorithms) {
    CategoryNode category = new CategoryNode(label);
    // should be sorted already. just to be on the save side.
    Collections.sort(algorithms);
    // top level entries
    for (IMetaAlgorithm meta : algorithms) {
        if (!meta.getClassName().contains("$")) {
            // $NON-NLS-1$
            category.addChild(new AlgorithmNode(meta));
        }
    }
    // sub level entries
    for (IMetaAlgorithm meta : algorithms) {
        if (meta.getClassName().contains("$")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String primaryPrefix = meta.getClassName().substring(0, meta.getClassName().indexOf("$"));
            if (containsClassName(primaryPrefix, algorithms)) {
                // recursive entries
                buildAlgorithmHierarchy(category, meta);
            } else {
                // folder entries
                // $NON-NLS-1$ //$NON-NLS-2$
                String prefix = meta.getClassName().substring(meta.getClassName().lastIndexOf(".") + 1, meta.getClassName().indexOf("$"));
                ITreeNode folder = new FolderNode(prefix);
                for (IMetaAlgorithm meta2 : algorithms) {
                    if (meta2.getClassName().contains("$")) {
                        // $NON-NLS-1$
                        if (meta2.getClassName().contains("." + prefix)) {
                            // $NON-NLS-1$
                            folder.addChild(new AlgorithmNode(meta2));
                        }
                    }
                }
                category.addChild(folder);
            }
        }
    }
    return category;
}
Also used : CategoryNode(org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.CategoryNode) ITreeNode(org.jcryptool.crypto.flexiprovider.ui.nodes.ITreeNode) IMetaAlgorithm(org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaAlgorithm) FolderNode(org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.FolderNode) AlgorithmNode(org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.AlgorithmNode)

Aggregations

AlgorithmNode (org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.AlgorithmNode)2 CategoryNode (org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.CategoryNode)2 FolderNode (org.jcryptool.crypto.flexiprovider.algorithms.ui.views.nodes.FolderNode)2 ITreeNode (org.jcryptool.crypto.flexiprovider.ui.nodes.ITreeNode)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Hashtable (java.util.Hashtable)1 AbstractHandler (org.eclipse.core.commands.AbstractHandler)1 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 MouseAdapter (org.eclipse.swt.events.MouseAdapter)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 IMetaAlgorithm (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaAlgorithm)1 Document (org.jdom.Document)1 Element (org.jdom.Element)1 JDOMException (org.jdom.JDOMException)1 SAXBuilder (org.jdom.input.SAXBuilder)1