Search in sources :

Example 1 with Result

use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result in project usbdm-eclipse-plugins by podonoghue.

the class ProcessProjectActions method process.

public static void process(final Wizard wizard, final IProject projectHandle, final Device device, final ProjectActionList actionList, final Map<String, String> variableMap, final IProgressMonitor monitor) throws Exception {
    // System.err.println("ProcessProjectActions.process("+device.getName()+") =============================================");
    if (actionList == null) {
        return;
    }
    final ApplyOptions applyOptions = new ApplyOptions(projectHandle);
    class MyVisitor implements ProjectActionList.Visitor {

        @Override
        public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
            // System.err.println("ProjectCustomAction: "+action.toString());
            try {
                if (action instanceof FileAction) {
                    new AddTargetFiles().process(projectHandle, variableMap, (FileAction) action, monitor);
                } else if (action instanceof DeleteResourceAction) {
                    new DeleteResource().process(projectHandle, variableMap, (DeleteResourceAction) action, monitor);
                } else if (action instanceof CreateFolderAction) {
                    ProjectUtilities.createFolder(projectHandle, variableMap, (CreateFolderAction) action, monitor);
                } else if (action instanceof ProjectOption) {
                    applyOptions.process(variableMap, (ProjectOption) action, monitor);
                } else if (action instanceof ExcludeAction) {
                    ProjectUtilities.excludeItem(projectHandle, (ExcludeAction) action, monitor);
                } else if (action instanceof ProjectActionList) {
                    ProjectActionList projectActionList = (ProjectActionList) action;
                    return new Result(projectActionList.appliesTo(device, variableMap) ? Status.CONTINUE : Status.PRUNE);
                } else if (action instanceof ProjectCustomAction) {
                    ProjectCustomAction customAction = (ProjectCustomAction) action;
                    Class<?> actionClass = Class.forName(customAction.getclassName());
                    Object clsInstance = actionClass.newInstance();
                    if (!(clsInstance instanceof CustomAction)) {
                        throw new Exception("Custom action does not implement required interface");
                    }
                    ((CustomAction) clsInstance).action(projectHandle, variableMap, monitor, customAction.getValue());
                } else if (action instanceof ProjectConstant) {
                // Ignore as already added to paramMap
                } else if (action instanceof WizardGroup) {
                // Ignore as already added to paramMap
                } else if (action instanceof ProjectVariable) {
                // Ignore as already added to paramMap
                } else if (action instanceof WizardPageInformation) {
                // Ignore as only applicable to wizard dialogues
                } else {
                    throw new Exception("Unexpected action class: " + action.getClass());
                }
            } catch (Exception e) {
                // e.printStackTrace();
                StringBuffer sb = new StringBuffer();
                sb.append("Unable to process Action " + action.toString() + "\n");
                sb.append("Action id = " + action.getId() + "\n");
                sb.append("Action owned by = " + action.getOwnerId() + "\n");
                sb.append(e.getMessage());
                return new Result(new Exception(sb.toString()));
            }
            return new Result(Status.CONTINUE);
        }
    }
    ;
    MyVisitor visitor = new MyVisitor();
    Result res = actionList.visit(visitor, null, monitor);
    if (res.getStatus() == Status.EXCEPTION) {
        throw res.getException();
    }
}
Also used : AddTargetFiles(net.sourceforge.usbdm.cdt.utilties.AddTargetFiles) Result(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result) WizardPageInformation(net.sourceforge.usbdm.packageParser.WizardPageInformation) ProjectAction(net.sourceforge.usbdm.packageParser.ProjectAction) CreateFolderAction(net.sourceforge.usbdm.packageParser.CreateFolderAction) ProjectCustomAction(net.sourceforge.usbdm.packageParser.ProjectCustomAction) ProjectVariable(net.sourceforge.usbdm.packageParser.ProjectVariable) ProjectConstant(net.sourceforge.usbdm.packageParser.ProjectConstant) WizardGroup(net.sourceforge.usbdm.packageParser.WizardGroup) DeleteResourceAction(net.sourceforge.usbdm.packageParser.DeleteResourceAction) ProjectCustomAction(net.sourceforge.usbdm.packageParser.ProjectCustomAction) ApplyOptions(net.sourceforge.usbdm.cdt.utilties.ApplyOptions) DeleteResource(net.sourceforge.usbdm.cdt.utilties.DeleteResource) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Value(net.sourceforge.usbdm.packageParser.ProjectActionList.Value) ProjectOption(net.sourceforge.usbdm.packageParser.ProjectOption) ExcludeAction(net.sourceforge.usbdm.packageParser.ExcludeAction) ProjectActionList(net.sourceforge.usbdm.packageParser.ProjectActionList) FileAction(net.sourceforge.usbdm.packageParser.FileAction)

Example 2 with Result

use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result in project usbdm-eclipse-plugins by podonoghue.

the class UsbdmNewProjectWizard method updateMapConstants.

/**
 * Visits the nodes of the projectActionList and add constants found
 *
 * @param paramMap            Map to add constants to
 * @param projectActionList   Action list to visit
 */
void updateMapConstants(final Map<String, String> paramMap, ProjectActionList projectActionList) {
    Visitor visitor = new Visitor() {

        @Override
        public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
            try {
                if (action instanceof ProjectActionList) {
                    ProjectActionList projectActionList = (ProjectActionList) action;
                    return projectActionList.appliesTo(fUsbdmProjectParametersPage_2.getDevice(), paramMap) ? CONTINUE : PRUNE;
                } else if (action instanceof ProjectConstant) {
                    ProjectConstant projectConstant = (ProjectConstant) action;
                    // System.err.println(String.format("updateMapConstants(): Found constant %s => %s",  projectConstant.getId(), projectConstant.getValue()));
                    String value = paramMap.get(projectConstant.getId());
                    if (value != null) {
                        if (projectConstant.isWeak()) {
                            // Ignore - assume constant is a default that has been overwritten
                            return CONTINUE;
                        }
                        if (!projectConstant.doOverwrite() && !value.equals(projectConstant.getValue())) {
                            return new Result(new Exception("paramMap already contains constant " + projectConstant.getId()));
                        }
                    }
                    paramMap.put(projectConstant.getId(), projectConstant.getValue());
                }
                return CONTINUE;
            } catch (Exception e) {
                return new Result(e);
            }
        }
    };
    // Visit all enabled actions and collect constants
    Result result = fProjectActionList.visit(visitor, null);
    if (result.getStatus() == Status.EXCEPTION) {
        result.getException().printStackTrace();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Visitor(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor) ProjectAction(net.sourceforge.usbdm.packageParser.ProjectAction) ProjectConstant(net.sourceforge.usbdm.packageParser.ProjectConstant) Value(net.sourceforge.usbdm.packageParser.ProjectActionList.Value) ProjectActionList(net.sourceforge.usbdm.packageParser.ProjectActionList) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result)

Example 3 with Result

use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result in project usbdm-eclipse-plugins by podonoghue.

the class ProjectActionList method visit.

/**
 * Visit each action recursively
 *
 * @param  visitor  Method to apply
 * @param  value    Object to hold progress/return value
 * @param  monitor  Monitor to indicate progress
 *
 * @throws Exception
 */
public Result visit(Visitor visitor, Value value, IProgressMonitor progressMonitor) {
    SubMonitor monitor = SubMonitor.convert(progressMonitor);
    monitor.beginTask("Visiting", (1 + fProjectActionList.size()));
    Result control = visitor.applyTo(this, value, monitor.newChild(1));
    switch(control.getStatus()) {
        case EXCEPTION:
            // No more actions applied
            return control;
        case PRUNE:
            // No children visited
            return new Result(Status.CONTINUE);
        case CONTINUE:
            // Keep going
            break;
    }
    for (ProjectAction action : fProjectActionList) {
        if (action instanceof ProjectActionList) {
            control = ((ProjectActionList) action).visit(visitor, value, monitor.newChild(1));
        } else {
            control = visitor.applyTo(action, value, monitor.newChild(1));
        }
        switch(control.getStatus()) {
            case EXCEPTION:
                // No more actions applied
                return control;
            case PRUNE:
            case CONTINUE:
                // Keep going
                break;
        }
    }
    return new Result(Status.CONTINUE);
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor) Result(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result)

Example 4 with Result

use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result in project usbdm-eclipse-plugins by podonoghue.

the class UsbdmOptionsPanel method getPageData.

/**
 *   Add settings to paramMap
 *
 *   @param paramMap
 *   @throws Exception
 */
public void getPageData(final Map<String, String> paramMap, ProjectActionList projectActionLists) {
    Visitor visitor = new Visitor() {

        @Override
        public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
            try {
                if (action instanceof ProjectActionList) {
                    ProjectActionList projectActionList = (ProjectActionList) action;
                    return projectActionList.appliesTo(fDevice, paramMap) ? CONTINUE : PRUNE;
                } else if (action instanceof ProjectVariable) {
                    ProjectVariable projectVariable = (ProjectVariable) action;
                    Button button = fButtonMap.get(projectVariable.getId());
                    if (button == null) {
                        return new Result(new Exception("Can't find button for var : " + projectVariable + " from " + action.getId()));
                    }
                    Boolean value = button.getSelection();
                    // System.err.println("UsbdmOptionsPanel.getPageData() projectVariable = " + projectVariable.toString() + ", value = " + value);
                    paramMap.put(projectVariable.getId(), value.toString());
                } else if (action instanceof ProjectConstant) {
                    ProjectConstant projectConstant = (ProjectConstant) action;
                    // System.err.println(String.format("UsbdmOptionsPanel.getPageData(): Adding constant %s => %s",  projectConstant.getId(), projectConstant.getValue()));
                    String value = paramMap.get(projectConstant.getId());
                    if (value != null) {
                        if (projectConstant.isWeak()) {
                            // Ignore - assume constant is a default that has been overwritten
                            return CONTINUE;
                        }
                        if (!projectConstant.doOverwrite() && !value.equals(projectConstant.getValue())) {
                            return new Result(new Exception("paramMap already contains constant " + projectConstant.getId()));
                        }
                    }
                    paramMap.put(projectConstant.getId(), projectConstant.getValue());
                }
                return CONTINUE;
            } catch (Exception e) {
                return new Result(e);
            }
        }
    };
    // Visit all enabled actions and collect variables and constants
    Result result = fProjectActionList.visit(visitor, null);
    if (result.getStatus() == Status.EXCEPTION) {
        result.getException().printStackTrace();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProjectVariable(net.sourceforge.usbdm.packageParser.ProjectVariable) Visitor(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor) ProjectAction(net.sourceforge.usbdm.packageParser.ProjectAction) ProjectConstant(net.sourceforge.usbdm.packageParser.ProjectConstant) Button(org.eclipse.swt.widgets.Button) Value(net.sourceforge.usbdm.packageParser.ProjectActionList.Value) ProjectActionList(net.sourceforge.usbdm.packageParser.ProjectActionList) Result(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result)

Example 5 with Result

use of net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result in project usbdm-eclipse-plugins by podonoghue.

the class ProcessProjectActions method process.

/**
 * Process a project action list
 *
 * @param projectHandle Project being manipulated
 * @param actionList    Actions to do
 * @param variableMap   Variables that may be needed
 * @param monitor       Progress monitor
 *
 * @throws Exception
 */
public void process(final IProject projectHandle, final ProjectActionList actionList, final Map<String, String> variableMap, final IProgressMonitor monitor) throws Exception {
    if (actionList == null) {
        return;
    }
    System.err.println("ProcessProjectActions.process " + actionList.getId());
    final ApplyOptions applyOptions = new ApplyOptions(projectHandle);
    class MyVisitor implements ProjectActionList.Visitor {

        @Override
        public Result applyTo(ProjectAction action, Value result, IProgressMonitor monitor) {
            SubMonitor subMonitor = SubMonitor.convert(monitor);
            subMonitor.subTask(action.toString());
            // System.err.println("ProjectCustomAction: "+action.toString());
            try {
                if (action instanceof FileAction) {
                    new AddTargetFiles().process(projectHandle, variableMap, (FileAction) action, subMonitor);
                } else if (action instanceof DeleteResourceAction) {
                    new DeleteResource().process(projectHandle, variableMap, (DeleteResourceAction) action, subMonitor);
                } else if (action instanceof CreateFolderAction) {
                // ProjectUtilities.createFolder(projectHandle, variableMap, (CreateFolderAction)action, subMonitor);
                } else if (action instanceof ProjectOption) {
                    applyOptions.process(variableMap, (ProjectOption) action, subMonitor);
                } else if (action instanceof ExcludeAction) {
                    ProjectUtilities.excludeItem(projectHandle, (ExcludeAction) action, subMonitor);
                } else if (action instanceof ProjectActionList) {
                    ProjectActionList projectActionList = (ProjectActionList) action;
                    if (projectActionList.isDoOnceOnly()) {
                        if (previousActions.contains(projectActionList.getId())) {
                            // Don't repeat action
                            System.err.println("ProcessProjectActions.process - not repeating action " + projectActionList.getId());
                            return new Result(Status.PRUNE);
                        }
                        previousActions.add(projectActionList.getId());
                    }
                    return new Result(projectActionList.applies(variableMap) ? Status.CONTINUE : Status.PRUNE);
                } else if (action instanceof ProjectCustomAction) {
                    ProjectCustomAction customAction = (ProjectCustomAction) action;
                    Class<?> actionClass = Class.forName(customAction.getclassName());
                    Object clsInstance = actionClass.newInstance();
                    if (!(clsInstance instanceof CustomAction)) {
                        throw new Exception("Custom action does not implement required interface");
                    }
                    ((CustomAction) clsInstance).action(projectHandle, variableMap, subMonitor, customAction.getValue());
                } else if (action instanceof ProjectConstant) {
                // Ignore as already added to paramMap
                } else if (action instanceof WizardGroup) {
                // Ignore as already added to paramMap
                } else if (action instanceof ProjectVariable) {
                // Ignore as already added to paramMap
                } else if (action instanceof WizardPageInformation) {
                // Ignore as only applicable to wizard dialogues
                } else {
                    throw new Exception("Unexpected action class: " + action.getClass());
                }
            } catch (Exception e) {
                // e.printStackTrace();
                StringBuffer sb = new StringBuffer();
                sb.append("Unable to process Action " + action.toString() + "\n");
                sb.append("Action id = " + action.getId() + "\n");
                sb.append("Action owned by = " + action.getOwnerId() + "\n");
                sb.append(e.getMessage());
                return new Result(new Exception(sb.toString(), e));
            }
            return new Result(Status.CONTINUE);
        }
    }
    ;
    MyVisitor visitor = new MyVisitor();
    Result res = actionList.visit(visitor, null, monitor);
    if (res.getStatus() == Status.EXCEPTION) {
        throw res.getException();
    }
}
Also used : AddTargetFiles(net.sourceforge.usbdm.cdt.utilties.AddTargetFiles) Result(net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result) WizardPageInformation(net.sourceforge.usbdm.packageParser.WizardPageInformation) ProjectAction(net.sourceforge.usbdm.packageParser.ProjectAction) CreateFolderAction(net.sourceforge.usbdm.packageParser.CreateFolderAction) ProjectCustomAction(net.sourceforge.usbdm.packageParser.ProjectCustomAction) ProjectVariable(net.sourceforge.usbdm.packageParser.ProjectVariable) ProjectConstant(net.sourceforge.usbdm.packageParser.ProjectConstant) WizardGroup(net.sourceforge.usbdm.packageParser.WizardGroup) DeleteResourceAction(net.sourceforge.usbdm.packageParser.DeleteResourceAction) CustomAction(net.sourceforge.usbdm.packageParser.CustomAction) ProjectCustomAction(net.sourceforge.usbdm.packageParser.ProjectCustomAction) ApplyOptions(net.sourceforge.usbdm.cdt.utilties.ApplyOptions) SubMonitor(org.eclipse.core.runtime.SubMonitor) DeleteResource(net.sourceforge.usbdm.cdt.utilties.DeleteResource) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Value(net.sourceforge.usbdm.packageParser.ProjectActionList.Value) ProjectOption(net.sourceforge.usbdm.packageParser.ProjectOption) ExcludeAction(net.sourceforge.usbdm.packageParser.ExcludeAction) ProjectActionList(net.sourceforge.usbdm.packageParser.ProjectActionList) FileAction(net.sourceforge.usbdm.packageParser.FileAction)

Aggregations

Result (net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor.Result)5 ProjectAction (net.sourceforge.usbdm.packageParser.ProjectAction)4 ProjectActionList (net.sourceforge.usbdm.packageParser.ProjectActionList)4 Value (net.sourceforge.usbdm.packageParser.ProjectActionList.Value)4 ProjectConstant (net.sourceforge.usbdm.packageParser.ProjectConstant)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 ProjectVariable (net.sourceforge.usbdm.packageParser.ProjectVariable)3 AddTargetFiles (net.sourceforge.usbdm.cdt.utilties.AddTargetFiles)2 ApplyOptions (net.sourceforge.usbdm.cdt.utilties.ApplyOptions)2 DeleteResource (net.sourceforge.usbdm.cdt.utilties.DeleteResource)2 CreateFolderAction (net.sourceforge.usbdm.packageParser.CreateFolderAction)2 DeleteResourceAction (net.sourceforge.usbdm.packageParser.DeleteResourceAction)2 ExcludeAction (net.sourceforge.usbdm.packageParser.ExcludeAction)2 FileAction (net.sourceforge.usbdm.packageParser.FileAction)2 Visitor (net.sourceforge.usbdm.packageParser.ProjectActionList.Visitor)2 ProjectCustomAction (net.sourceforge.usbdm.packageParser.ProjectCustomAction)2 ProjectOption (net.sourceforge.usbdm.packageParser.ProjectOption)2 WizardGroup (net.sourceforge.usbdm.packageParser.WizardGroup)2 WizardPageInformation (net.sourceforge.usbdm.packageParser.WizardPageInformation)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2