Search in sources :

Example 6 with CommandInfo

use of org.jcryptool.core.operations.CommandInfo 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)

Example 7 with CommandInfo

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

the class StartHandler method execute.

public IStatus execute(final ActionView view, final ActionItem startItem, final Shell shell, IProgressMonitor monitor) {
    ActionCascade ac = view.getActionCascade();
    final TableViewer viewer = view.getViewer();
    // $NON-NLS-1$
    LogUtil.logInfo("Running Action Cascade " + ac.getName());
    boolean start = false;
    for (final ActionItem a : ac.getAllItems()) {
        int steps = ac.getSize();
        // Start the execution at the first selected element
        if (a == startItem) {
            start = true;
            monitor.beginTask(a.getActionName(), steps);
        } else
            steps--;
        if (start) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            try {
                boolean executed = false;
                // Set focus on moved row. Just look and feel ...
                display.asyncExec(new Runnable() {

                    public void run() {
                        viewer.setSelection(new StructuredSelection(a), true);
                    }
                });
                OperationsPlugin op = OperationsPlugin.getDefault();
                CommandInfo[] commands = op.getAlgorithmsManager().getShadowAlgorithmCommands();
                // Try to find an CryptoAlgorithm-Plug-in (Classic or Modern)
                for (int i = 0, length = commands.length; i < length; i++) {
                    if (commands[i].getHandler() != null && a.getPluginId().equals(commands[i].getCommandId())) {
                        ((ShadowAlgorithmHandler) commands[i].getHandler()).run(convert(a));
                        executed = true;
                    }
                }
                // Try to find a FlexiProvider algorithm
                if (!executed) {
                    // $NON-NLS-1$
                    LogUtil.logInfo("Trying to execute FlexiProvider algorithm");
                    for (RegistryType rt : RegistryType.values()) {
                        if (rt.getName().equals(a.getPluginId())) {
                            AlgorithmDescriptor descriptor = new AlgorithmDescriptor(a.getActionName(), rt, null);
                            if (a.getParam("algorithm type").equals(RegistryType.BLOCK_CIPHER.getName())) {
                                // $NON-NLS-1$
                                descriptor = new BlockCipherDescriptor(descriptor.getAlgorithmName(), a.getParam("mode"), a.getParam("padding scheme"), null, // $NON-NLS-1$ //$NON-NLS-2$
                                null);
                            } else if (a.getParam("algorithm type").equals(RegistryType.SECURE_RANDOM.getName())) {
                                // $NON-NLS-1$
                                // TODO Get property "alphabet" from item
                                byte[][] alphabet = null;
                                if (alphabet == null) {
                                    descriptor = new SecureRandomDescriptor(descriptor.getAlgorithmName(), // $NON-NLS-1$
                                    Integer.parseInt(a.getParam("random size")));
                                }
                            }
                            final IFlexiProviderOperation operation = new IntegratorOperation(descriptor);
                            operation.setInput(Messages.InputType);
                            operation.setOutput(Messages.StartHandler_6);
                            // $NON-NLS-1$
                            operation.setSignature(a.getParam("signature"));
                            if ("encrypt".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.ENCRYPT);
                            } else if ("decrypt".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.DECRYPT);
                            } else if ("sign".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.SIGN);
                            } else if ("verify".equals(a.getActionType())) {
                                // $NON-NLS-1$
                                operation.setOperation(OperationType.VERIFY);
                            } else {
                                operation.setOperation(OperationType.UNKNOWN);
                            }
                            // $NON-NLS-1$
                            String alias = a.getParam("key alias");
                            if (alias != null) {
                                operation.setKeyStoreAlias(new KeyStoreAlias(alias));
                            }
                            // $NON-NLS-1$
                            String password = a.getParam("key password");
                            if (password != null) {
                                operation.setPassword(password.toCharArray());
                            }
                            display.asyncExec(new Runnable() {

                                public void run() {
                                    PerformOperationManager.getInstance().firePerformOperation(operation);
                                }
                            });
                            executed = true;
                        }
                    }
                }
                if (!executed) {
                    throw new Exception(Messages.StartHandler_2 + a.getPluginId());
                }
            } catch (final Exception e) {
                if (ActionsUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_IGNORE_ERRORS)) {
                    // $NON-NLS-1$
                    LogUtil.logError(ActionsUIPlugin.PLUGIN_ID, a.getActionName() + ": " + e.getMessage(), e, false);
                    if (!ActionsUIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_DONT_SHOW_MESSAGES)) {
                        display.syncExec(new Runnable() {

                            public void run() {
                                MessageDialog.openWarning(shell, Messages.StartHandler_3, e.getMessage());
                            }
                        });
                    }
                } else {
                    display.asyncExec(new Runnable() {

                        public void run() {
                            MessageDialog.openError(shell, Messages.StartHandler_3, e.getMessage() + // $NON-NLS-1$
                            Messages.StartHandler_4);
                        }
                    });
                    return Status.CANCEL_STATUS;
                }
            } finally {
                monitor.worked(ac.getSize() - steps);
            }
        }
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : KeyStoreAlias(org.jcryptool.crypto.keystore.backend.KeyStoreAlias) RegistryType(org.jcryptool.crypto.flexiprovider.types.RegistryType) ShadowAlgorithmHandler(org.jcryptool.core.operations.algorithm.ShadowAlgorithmHandler) ActionItem(org.jcryptool.actions.core.types.ActionItem) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AlgorithmDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.AlgorithmDescriptor) ActionCascade(org.jcryptool.actions.core.types.ActionCascade) OperationsPlugin(org.jcryptool.core.operations.OperationsPlugin) ExecutionException(org.eclipse.core.commands.ExecutionException) SecureRandomDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.SecureRandomDescriptor) CommandInfo(org.jcryptool.core.operations.CommandInfo) BlockCipherDescriptor(org.jcryptool.crypto.flexiprovider.descriptors.algorithms.BlockCipherDescriptor) IntegratorOperation(org.jcryptool.crypto.flexiprovider.integrator.IntegratorOperation) TableViewer(org.eclipse.jface.viewers.TableViewer) IFlexiProviderOperation(org.jcryptool.crypto.flexiprovider.descriptors.IFlexiProviderOperation)

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