Search in sources :

Example 1 with ShadowAlgorithmHandler

use of org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler in project core by jcryptool.

the class AlgorithmView method doAction.

/**
 * used to trigger actions when an algorithm is drag&dropped to an editor
 *
 * @param algorithmName the name of the algorithm whose action is to be done
 */
public static void doAction(String algorithmName) {
    Iterator<ShadowAlgorithmHandler> it9 = algorithmTypes.iterator();
    ShadowAlgorithmHandler handler = null;
    while (it9.hasNext()) {
        handler = it9.next();
        // TODO the text property doesn't belong in the Handler but in the CommandInfo
        if (algorithmName.equals(handler.getText())) {
            // TODO maybe Command.executeWithChecks() should be called here, which also requires an ExecutionEvent
            handler.execute(null);
        }
    }
}
Also used : ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler)

Example 2 with ShadowAlgorithmHandler

use of org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler 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 ShadowAlgorithmHandler

use of org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler 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 ShadowAlgorithmHandler

use of org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler 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 ShadowAlgorithmHandler

use of org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler in project core by jcryptool.

the class AlgorithmTreeViewer method makeAndAssignActions.

/**
 * creates the actions according to the algorithm extension point and assigns them to the
 * viewers double click listener
 */
private void makeAndAssignActions() {
    doubleClickHandler = new AbstractHandler() {

        public Object execute(ExecutionEvent event) {
            TreeObject treeObject = (TreeObject) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
            if (editorReferences.length == 0 && (!treeObject.getParent().getName().equals(org.jcryptool.core.Messages.applicationActionBarAdvisor_Menu_Algorithms_PRNG))) {
                AlgorithmView.showMessage(Messages.AlgorithmView_warning_message_no_active_editor);
            } 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 && treeObject.getName().equals(handler.getText())) {
                        Command command = commandService.getCommand(commandId);
                        try {
                            return command.executeWithChecks(event);
                        } catch (Exception ex) {
                            LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                            return (null);
                        }
                    }
                }
            }
            return (null);
        }
    };
    addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(final DoubleClickEvent event) {
            Object obj = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (obj instanceof TreeParent) {
                if (viewer.getTree().getSelection()[0].getExpanded()) {
                    viewer.collapseToLevel(obj, 1);
                } else {
                    viewer.expandToLevel(obj, 1);
                }
            } else if (obj instanceof TreeObject) {
                try {
                    final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
                    IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
                    ExecutionEvent executionEvent = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
                    // run assigned action
                    doubleClickHandler.execute(executionEvent);
                } catch (ExecutionException ex) {
                    LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                }
            }
        }
    });
    addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(final SelectionChangedEvent event) {
            Object treeObject = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (treeObject instanceof TreeParent) {
                // $NON-NLS-1$
                PlatformUI.getWorkbench().getHelpSystem().displayHelp(ViewsPlugin.PLUGIN_ID + ".algorithmsView");
                getControl().setFocus();
            } else if (treeObject instanceof TreeObject) {
                AlgorithmView.showContextHelp(extensionPointId, ((TreeObject) treeObject).getName());
                getControl().setFocus();
            }
        }
    });
}
Also used : ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler) TreeParent(org.jcryptool.core.views.content.structure.TreeParent) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AbstractHandler(org.eclipse.core.commands.AbstractHandler) ICommandService(org.eclipse.ui.commands.ICommandService) PartInitException(org.eclipse.ui.PartInitException) 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) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) Iterator(java.util.Iterator) TreeObject(org.jcryptool.core.views.content.structure.TreeObject) TreeObject(org.jcryptool.core.views.content.structure.TreeObject) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

ShadowAlgorithmHandler (org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler)6 CommandInfo (org.jcryptool.core.operations.CommandInfo)5 ExecutionException (org.eclipse.core.commands.ExecutionException)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 AbstractHandler (org.eclipse.core.commands.AbstractHandler)2 Command (org.eclipse.core.commands.Command)2 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)2 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)2 ICommandService (org.eclipse.ui.commands.ICommandService)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 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 PaletteDrawer (org.eclipse.gef.palette.PaletteDrawer)1 PaletteEntry (org.eclipse.gef.palette.PaletteEntry)1 PaletteRoot (org.eclipse.gef.palette.PaletteRoot)1 SelectionToolEntry (org.eclipse.gef.palette.SelectionToolEntry)1