Search in sources :

Example 1 with CommandInfo

use of org.jcryptool.core.operations.CommandInfo in project core by jcryptool.

the class CryptoContributionItem method fill.

public void fill(Menu menu, int index) {
    algorithmsMenu = new Menu(menu);
    Comparator<String> menuStringsComparator = new Comparator<String>() {

        public int compare(String o1, String o2) {
            return o1.toLowerCase().compareTo(o2.toLowerCase());
        }
    };
    SortedMap<String, Menu> typeMap = new TreeMap<String, Menu>(menuStringsComparator);
    SortedMap<String, HashMap<String, CommandInfo>> commandMap = new TreeMap<String, HashMap<String, CommandInfo>>(menuStringsComparator);
    CommandInfo[] algorithmCommands = OperationsPlugin.getDefault().getAlgorithmsManager().getShadowAlgorithmCommands();
    for (final CommandInfo commandInfo : algorithmCommands) {
        String translatedType = ApplicationActionBarAdvisor.getTypeTranslation(OperationsPlugin.getDefault().getAlgorithmsManager().getAlgorithmType(commandInfo));
        if (!typeMap.containsKey(translatedType)) {
            typeMap.put(translatedType, new Menu(algorithmsMenu));
        }
        HashMap<String, CommandInfo> map = new HashMap<String, CommandInfo>(1);
        map.put(translatedType, commandInfo);
        String text = null;
        text = commandInfo.getText();
        commandMap.put(text, map);
    }
    for (String subMenuKey : typeMap.keySet()) {
        MenuItem item = new MenuItem(algorithmsMenu, SWT.CASCADE);
        item.setText(subMenuKey);
        item.setMenu(typeMap.get(subMenuKey));
    }
    for (HashMap<String, CommandInfo> algorithmItems : commandMap.values()) {
        String translatedType = algorithmItems.keySet().iterator().next();
        final CommandInfo commandInfo = algorithmItems.get(translatedType);
        // get the menu
        Menu typeMenu = typeMap.get(translatedType);
        // create an item for the algorithm
        MenuItem item = new MenuItem(typeMenu, SWT.CASCADE);
        final String commandId = commandInfo.getCommandId();
        item.setText(commandInfo.getText());
        item.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {
                run(commandId);
            }
        });
        // update the menu
        typeMap.put(translatedType, typeMenu);
    }
    MenuItem item = new MenuItem(menu, SWT.CASCADE, index);
    item.setText(Messages.CryptoContributionItem_0);
    item.setMenu(algorithmsMenu);
}
Also used : HashMap(java.util.HashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) TreeMap(java.util.TreeMap) Comparator(java.util.Comparator) CommandInfo(org.jcryptool.core.operations.CommandInfo) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu)

Example 2 with CommandInfo

use of org.jcryptool.core.operations.CommandInfo in project core by jcryptool.

the class AlgorithmPaletteViewer method createTree.

/**
 * creates a tree representation of the algorithm structure
 *
 * @param needles
 *            a search string to filter the algorithms
 */
private void createTree(String[] needles) {
    invisibleRoot = new PaletteRoot();
    TreeMap<String, PaletteDrawer> types = new TreeMap<String, PaletteDrawer>();
    TreeMap<String, SelectionToolEntry> sortList = new TreeMap<String, SelectionToolEntry>();
    Iterator<CommandInfo> it = algorithmList.iterator();
    CommandInfo info = null;
    while (it.hasNext()) {
        info = it.next();
        String text = "";
        String type = "";
        String toolTipText = "";
        boolean isFlexiProviderAlgorithm = false;
        ShadowAlgorithmHandler handler = (ShadowAlgorithmHandler) info.getHandler();
        text = handler.getText();
        type = handler.getType();
        toolTipText = handler.getToolTipText();
        isFlexiProviderAlgorithm = handler.isFlexiProviderAlgorithm();
        // filter
        boolean show = true;
        for (String needle : needles) {
            if (!text.toLowerCase().matches(// $NON-NLS-1$ //$NON-NLS-2$
            ".*" + needle.toLowerCase() + ".*"))
                show = false;
        }
        if (show) {
            // Create Category
            if (types.get(type) == null) {
                // translate
                type = ApplicationActionBarAdvisor.getTypeTranslation(type);
                PaletteDrawer paletteDrawer = new PaletteDrawer(type);
                paletteDrawer.setSmallIcon(ViewsPlugin.getImageDescriptor(TreeView.ICON_FOLDER));
                paletteDrawer.setLargeIcon(ViewsPlugin.getImageDescriptor(TreeView.ICON_FOLDER));
                types.put(type, paletteDrawer);
            }
            // Add element
            SelectionToolEntry paletteEntry = new SelectionToolEntry(text, toolTipText);
            if (isFlexiProviderAlgorithm) {
                // FlexiProvider item
                paletteEntry.setSmallIcon(ViewsPlugin.getImageDescriptor(TreeView.ICON_ITEM_FLEXI));
                paletteEntry.setLargeIcon(ViewsPlugin.getImageDescriptor(TreeView.ICON_ITEM_FLEXI));
            } else {
                // JCrypTool item
                paletteEntry.setSmallIcon(ViewsPlugin.getImageDescriptor(TreeView.ICON_ITEM_JCT));
                paletteEntry.setLargeIcon(ViewsPlugin.getImageDescriptor(TreeView.ICON_ITEM_JCT));
            }
            paletteEntry.setUserModificationPermission(PaletteEntry.PERMISSION_NO_MODIFICATION);
            paletteEntry.setType(type);
            // temporary save in list
            sortList.put(paletteEntry.getLabel(), paletteEntry);
        }
    }
    ArrayList<PaletteDrawer> parents = new ArrayList<PaletteDrawer>(types.values());
    for (SelectionToolEntry paletteEntry : sortList.values()) {
        // read from sorted list
        // put sorted into palette
        types.get(paletteEntry.getType()).add(paletteEntry);
    }
    // attach tree to the root element
    Iterator<PaletteDrawer> parentIterator2 = parents.iterator();
    while (parentIterator2.hasNext()) {
        invisibleRoot.add(parentIterator2.next());
    }
}
Also used : PaletteRoot(org.eclipse.gef.palette.PaletteRoot) ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) CommandInfo(org.jcryptool.core.operations.CommandInfo) SelectionToolEntry(org.eclipse.gef.palette.SelectionToolEntry) PaletteDrawer(org.eclipse.gef.palette.PaletteDrawer)

Example 3 with CommandInfo

use of org.jcryptool.core.operations.CommandInfo in project core by jcryptool.

the class AlgorithmPaletteViewer method makeAndAssignActions.

/**
 * Constructs the actions according to the algorithm extension point and
 * assigns the actions to the doubleclick listener of the viewer
 */
private void makeAndAssignActions() {
    doubleClickHandler = new AbstractHandler() {

        public Object execute(ExecutionEvent event) {
            Object selection = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (selection instanceof PaletteEditPart) {
                PaletteEditPart paletteEditPart = (PaletteEditPart) selection;
                Object model = paletteEditPart.getModel();
                IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
                if (editorReferences.length == 0 && (!((PaletteEntry) model).getParent().getLabel().equals(org.jcryptool.core.Messages.applicationActionBarAdvisor_Menu_Algorithms_PRNG))) {
                    AlgorithmView.showMessage(Messages.AlgorithmPaletteViewer_0);
                } else {
                    final ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
                    Iterator<CommandInfo> it9 = algorithmList.iterator();
                    CommandInfo commandInfo = null;
                    while (it9.hasNext()) {
                        commandInfo = it9.next();
                        ShadowAlgorithmHandler handler = (ShadowAlgorithmHandler) commandInfo.getHandler();
                        String commandId = commandInfo.getCommandId();
                        if (commandId != null && model.toString().equals("Palette Entry (" + handler.getText() + ")")) {
                            // $NON-NLS-1$ //$NON-NLS-2$
                            Command command = commandService.getCommand(commandId);
                            try {
                                return command.executeWithChecks(event);
                            } catch (Exception ex) {
                                LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                                return (null);
                            }
                        }
                    }
                }
            }
            return (null);
        }
    };
    viewer.getControl().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDoubleClick(final MouseEvent e) {
            if (e.button == 1) {
                // only left button double clicks
                final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
                IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
                ExecutionEvent event = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
                try {
                    // run assigned action
                    doubleClickHandler.execute(event);
                } catch (ExecutionException ex) {
                    LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                }
            }
        }

        @Override
        public void mouseDown(final MouseEvent e) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            Object obj = selection.getFirstElement();
            if (obj instanceof PaletteEditPart) {
                AlgorithmView.showContextHelp(extensionPointId, ((PaletteEntry) ((PaletteEditPart) obj).getModel()).getLabel());
                viewer.getControl().setFocus();
                viewer.setSelection(selection);
            }
        }
    });
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler) MouseAdapter(org.eclipse.swt.events.MouseAdapter) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) PaletteEntry(org.eclipse.gef.palette.PaletteEntry) PaletteEditPart(org.eclipse.gef.ui.palette.editparts.PaletteEditPart) AbstractHandler(org.eclipse.core.commands.AbstractHandler) ICommandService(org.eclipse.ui.commands.ICommandService) ExecutionException(org.eclipse.core.commands.ExecutionException) CommandInfo(org.jcryptool.core.operations.CommandInfo) IHandlerService(org.eclipse.ui.handlers.IHandlerService) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) Iterator(java.util.Iterator) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 4 with CommandInfo

use of org.jcryptool.core.operations.CommandInfo in project core by jcryptool.

the class AlgorithmTreeViewer method createTree.

/**
 * creates a tree for the algorithm structure
 *
 * @param needles the search string to filter the algorithms
 */
private void createTree(String[] needles) {
    HashMap<String, TreeParent> types = new HashMap<String, TreeParent>();
    Iterator<CommandInfo> it = algorithmList.iterator();
    CommandInfo info = null;
    while (it.hasNext()) {
        info = it.next();
        String text = "";
        String type = "";
        boolean isFlexiProviderAlgorithm = false;
        ShadowAlgorithmHandler handler = (ShadowAlgorithmHandler) info.getHandler();
        text = handler.getText();
        type = handler.getType();
        isFlexiProviderAlgorithm = handler.isFlexiProviderAlgorithm();
        // filter
        boolean show = true;
        for (String needle : needles) {
            if (// $NON-NLS-1$ //$NON-NLS-2$
            !text.toLowerCase().matches(".*" + needle.toLowerCase() + ".*"))
                show = false;
        }
        if (show) {
            // Create Category
            if (types.get(type) == null) {
                // translate
                String translatedType = ApplicationActionBarAdvisor.getTypeTranslation(type);
                types.put(type, new TreeParent(translatedType));
            }
            // Add element
            TreeObject object = new TreeObject(text);
            if (isFlexiProviderAlgorithm)
                object.setIsFlexiProviderAlgorithm();
            types.get(type).addChild(object);
        }
    }
    ArrayList<TreeParent> parents = new ArrayList<TreeParent>(types.values());
    // attach categories to root element
    // $NON-NLS-1$
    invisibleRoot = new TreeParent("");
    Iterator<TreeParent> parentIterator2 = parents.iterator();
    while (parentIterator2.hasNext()) {
        invisibleRoot.addChild(parentIterator2.next());
    }
}
Also used : CommandInfo(org.jcryptool.core.operations.CommandInfo) HashMap(java.util.HashMap) ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler) TreeParent(org.jcryptool.core.views.content.structure.TreeParent) ArrayList(java.util.ArrayList) TreeObject(org.jcryptool.core.views.content.structure.TreeObject)

Example 5 with CommandInfo

use of org.jcryptool.core.operations.CommandInfo in project core by jcryptool.

the class ApplicationActionBarAdvisor method createAlgorithmMenu.

/**
 * Creates the algorithm menu.
 *
 * @return the menu manager
 */
public static IMenuManager createAlgorithmMenu() {
    ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
    MenuManager menu = new MenuManager(Messages.applicationActionBarAdvisor_Menu_Algorithms, // $NON-NLS-1$
    CorePlugin.PLUGIN_ID + ".algorithms");
    // id->compare-relevant-name map
    final Map<String, String> idNameMap = new HashMap<String, String>();
    // id-comparator (compares the names derived from the ids over the map (above)
    Comparator<String> idComparator = new Comparator<String>() {

        public int compare(String id1, String id2) {
            return menuStringsComparator.compare(idNameMap.get(id1), idNameMap.get(id2));
        }
    };
    // put existing id->name pairs
    // $NON-NLS-1$
    idNameMap.put("classic", Messages.applicationActionBarAdvisor_Menu_Algorithms_Classic);
    // $NON-NLS-1$
    idNameMap.put("symmetric", Messages.applicationActionBarAdvisor_Menu_Algorithms_Symmetric);
    // $NON-NLS-1$
    idNameMap.put("asymmetric", Messages.applicationActionBarAdvisor_Menu_Algorithms_Asymmetric);
    // $NON-NLS-1$
    idNameMap.put("hybrid", Messages.applicationActionBarAdvisor_Menu_Algorithms_Hybrid);
    // $NON-NLS-1$
    idNameMap.put("hash", Messages.applicationActionBarAdvisor_Menu_Algorithms_Hash);
    // $NON-NLS-1$
    idNameMap.put("misc", Messages.applicationActionBarAdvisor_Menu_Algorithms_Misc);
    // $NON-NLS-1$
    idNameMap.put("mac", Messages.applicationActionBarAdvisor_Menu_Algorithms_Mac);
    // $NON-NLS-1$
    idNameMap.put("prng", Messages.applicationActionBarAdvisor_Menu_Algorithms_PRNG);
    // $NON-NLS-1$
    idNameMap.put("signature", Messages.applicationActionBarAdvisor_Menu_Algorithms_Signature);
    // $NON-NLS-1$
    idNameMap.put("xml", Messages.applicationActionBarAdvisor_Menu_Algorithms_XML_Security);
    // Sorted submenus map
    SortedMap<String, SortedMap<String, CommandInfo>> sortedSubMenus = new TreeMap<String, SortedMap<String, CommandInfo>>(idComparator);
    // fill sub menu sorting map
    for (CommandInfo algorithmCommand : algorithmCommands) {
        String type = OperationsPlugin.getDefault().getAlgorithmsManager().getAlgorithmType(algorithmCommand);
        if (!sortedSubMenus.containsKey(type)) {
            // algorithm type) when id has no textual mapping
            if (!idNameMap.containsKey(type)) {
                idNameMap.put(type, type);
            }
            // make a new submenu
            sortedSubMenus.put(type, new TreeMap<String, CommandInfo>(menuStringsComparator));
        }
        sortedSubMenus.get(type).put(algorithmCommand.getText(), algorithmCommand);
    }
    for (String subMenuKey : sortedSubMenus.keySet()) {
        MenuManager subMenu = new MenuManager(idNameMap.get(subMenuKey), subMenuKey);
        menu.add(subMenu);
        Map<String, CommandInfo> subMenuActions = sortedSubMenus.get(subMenuKey);
        for (String subMenuActionKey : subMenuActions.keySet()) {
            CommandInfo commandInfo = subMenuActions.get(subMenuActionKey);
            Command cmd = commandService.getCommand(commandInfo.getCommandId());
            cmd.define(commandInfo.getText(), null, commandService.getCategory(CommandManager.AUTOGENERATED_CATEGORY_ID));
            cmd.setHandler(commandInfo.getHandler());
            subMenu.add(createContributionItem(PlatformUI.getWorkbench(), null, commandInfo.getCommandId()));
        }
    }
    menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    return menu;
}
Also used : HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) ICommandService(org.eclipse.ui.commands.ICommandService) Comparator(java.util.Comparator) CommandInfo(org.jcryptool.core.operations.CommandInfo) Command(org.eclipse.core.commands.Command) SortedMap(java.util.SortedMap) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) Separator(org.eclipse.jface.action.Separator)

Aggregations

CommandInfo (org.jcryptool.core.operations.CommandInfo)7 ShadowAlgorithmHandler (org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler)5 HashMap (java.util.HashMap)3 TreeMap (java.util.TreeMap)3 Command (org.eclipse.core.commands.Command)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 ICommandService (org.eclipse.ui.commands.ICommandService)3 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 Iterator (java.util.Iterator)2 AbstractHandler (org.eclipse.core.commands.AbstractHandler)2 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)2 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)2 IHandlerService (org.eclipse.ui.handlers.IHandlerService)2 TreeObject (org.jcryptool.core.views.content.structure.TreeObject)2 TreeParent (org.jcryptool.core.views.content.structure.TreeParent)2 SortedMap (java.util.SortedMap)1 PaletteDrawer (org.eclipse.gef.palette.PaletteDrawer)1 PaletteEntry (org.eclipse.gef.palette.PaletteEntry)1