Search in sources :

Example 1 with CommandArgument

use of org.yamcs.studio.commanding.stack.xml.CommandStack.Command.CommandArgument in project yamcs-studio by yamcs.

the class ImportCommandStackHandler method parseCommandStack.

public List<StackedCommand> parseCommandStack(String fileName) {
    try {
        final JAXBContext jc = JAXBContext.newInstance(org.yamcs.studio.commanding.stack.xml.CommandStack.class);
        final Unmarshaller unmarshaller = jc.createUnmarshaller();
        final org.yamcs.studio.commanding.stack.xml.CommandStack commandStack = (org.yamcs.studio.commanding.stack.xml.CommandStack) unmarshaller.unmarshal(new FileReader(fileName));
        List<StackedCommand> importedStack = new LinkedList<StackedCommand>();
        for (CommandStack.Command c : commandStack.getCommand()) {
            StackedCommand sc = new StackedCommand();
            CommandInfo mc = CommandingCatalogue.getInstance().getCommandInfo(c.getQualifiedName());
            if (mc == null) {
                MessageDialog.openError(Display.getCurrent().getActiveShell(), "Import Command Stack", "Command " + c.getQualifiedName() + " does not exist in MDB.");
                return null;
            }
            sc.setMetaCommand(mc);
            sc.setSelectedAliase(c.getSelectedAlias());
            sc.setComment(c.getComment());
            for (CommandArgument ca : c.getCommandArgument()) {
                ArgumentInfo a = getArgumentFromYamcs(mc, ca.getArgumentName());
                if (a == null) {
                    MessageDialog.openError(Display.getCurrent().getActiveShell(), "Import Command Stack", "In command " + c.getQualifiedName() + ", argument " + ca.getArgumentName() + " does not exist in MDB.");
                    return null;
                }
                sc.addAssignment(a, ca.getArgumentValue());
            }
            importedStack.add(sc);
        }
        return importedStack;
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unable to load command stack for importation. Check the XML file is correct. Details:\n" + e.toString());
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Import Command Stack", "Unable to load command stack for importation. Check the XML file is correct. Details:\n" + e.toString());
        return null;
    }
}
Also used : CommandStack(org.yamcs.studio.commanding.stack.xml.CommandStack) CommandArgument(org.yamcs.studio.commanding.stack.xml.CommandStack.Command.CommandArgument) JAXBContext(javax.xml.bind.JAXBContext) LinkedList(java.util.LinkedList) ExecutionException(org.eclipse.core.commands.ExecutionException) CommandInfo(org.yamcs.protobuf.Mdb.CommandInfo) CommandStack(org.yamcs.studio.commanding.stack.xml.CommandStack) FileReader(java.io.FileReader) Unmarshaller(javax.xml.bind.Unmarshaller) ArgumentInfo(org.yamcs.protobuf.Mdb.ArgumentInfo)

Example 2 with CommandArgument

use of org.yamcs.studio.commanding.stack.xml.CommandStack.Command.CommandArgument in project yamcs-studio by yamcs.

the class ExportCommandStackHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    // Get current command stack
    Collection<StackedCommand> scs = org.yamcs.studio.commanding.stack.CommandStack.getInstance().getCommands();
    if (scs == null || scs.isEmpty()) {
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Export Command Stack", "Current command stack is empty. No command to export.");
        return null;
    }
    FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
    dialog.setFilterExtensions(new String[] { "*.xml" });
    // dialog.setFilterPath("c:\\temp");
    String exportFile = dialog.open();
    System.out.println("export file choosen: " + exportFile);
    if (exportFile == null) {
        // cancelled
        return null;
    }
    // Build model
    org.yamcs.studio.commanding.stack.xml.CommandStack exportCommandStack = new org.yamcs.studio.commanding.stack.xml.CommandStack();
    List<org.yamcs.studio.commanding.stack.xml.CommandStack.Command> exportedCommands = exportCommandStack.getCommand();
    for (StackedCommand sc : scs) {
        org.yamcs.studio.commanding.stack.xml.CommandStack.Command c = new org.yamcs.studio.commanding.stack.xml.CommandStack.Command();
        c.setQualifiedName(sc.getMetaCommand().getQualifiedName());
        c.setSelectedAlias(sc.getSelectedAlias());
        c.setComment(sc.getComment());
        exportedCommands.add(c);
        List<CommandArgument> cas = c.getCommandArgument();
        Iterator<Entry<ArgumentInfo, String>> it = sc.getAssignments().entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<ArgumentInfo, String> pair = it.next();
            String argName = pair.getKey().getName();
            String argValue = pair.getValue();
            CommandArgument ca = new CommandArgument();
            ca.setArgumentName(argName);
            ca.setArgumentValue(argValue);
            cas.add(ca);
        }
    }
    // Write model to file
    try {
        File file = new File(exportFile);
        JAXBContext jaxbContext = JAXBContext.newInstance(org.yamcs.studio.commanding.stack.xml.CommandStack.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(exportCommandStack, file);
        jaxbMarshaller.marshal(exportCommandStack, System.out);
    } catch (Exception e) {
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Export Command Stack", "Unable to perform command stack export.\nDetails:" + e.getMessage());
        return null;
    }
    MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Export Command Stack", "Command stack exported successfully.");
    return null;
}
Also used : CommandArgument(org.yamcs.studio.commanding.stack.xml.CommandStack.Command.CommandArgument) JAXBContext(javax.xml.bind.JAXBContext) Entry(java.util.Map.Entry) ArgumentInfo(org.yamcs.protobuf.Mdb.ArgumentInfo) Marshaller(javax.xml.bind.Marshaller) ExecutionException(org.eclipse.core.commands.ExecutionException) FileDialog(org.eclipse.swt.widgets.FileDialog) Map(java.util.Map) File(java.io.File)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 ArgumentInfo (org.yamcs.protobuf.Mdb.ArgumentInfo)2 CommandArgument (org.yamcs.studio.commanding.stack.xml.CommandStack.Command.CommandArgument)2 File (java.io.File)1 FileReader (java.io.FileReader)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 FileDialog (org.eclipse.swt.widgets.FileDialog)1 CommandInfo (org.yamcs.protobuf.Mdb.CommandInfo)1 CommandStack (org.yamcs.studio.commanding.stack.xml.CommandStack)1