use of net.sourceforge.usbdm.packageParser.ProjectVariable 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();
}
}
use of net.sourceforge.usbdm.packageParser.ProjectVariable in project usbdm-eclipse-plugins by podonoghue.
the class UsbdmOptionsPanel method getButtonData.
public void getButtonData(final Map<String, String> paramMap) {
if (fButtonMap == null) {
// Button not yet created
return;
}
// Check all buttons
for (Entry<String, Button> entry : fButtonMap.entrySet()) {
Button button = entry.getValue();
ProjectVariable projectVariable = fVariableMap.get(entry.getKey());
// System.err.println(String.format("getButtonData() %s => %s", projectVariable.getId(), button.isEnabled() && button.getSelection()));
paramMap.put(projectVariable.getId(), (button.isEnabled() && button.getSelection()) ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
}
}
use of net.sourceforge.usbdm.packageParser.ProjectVariable in project usbdm-eclipse-plugins by podonoghue.
the class UsbdmOptionsPanel method createOptionsControl.
/**
* Populate the Options controls
*
* @param parent
*
* @return Composite created
* @throws Exception
*/
private Composite createOptionsControl(Composite parent) {
final Composite comp = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, true);
// GridBagLayout layout = new GridBagLayout();
comp.setLayout(layout);
if (fProjectActionList == null) {
// Should be impossible
new Label(comp, SWT.NONE).setText("No device options");
return comp;
}
/*
* Collect variables for dialogue
*/
fVariableMap = new HashMap<String, ProjectVariable>();
final ArrayList<ProjectVariable> variableList = new ArrayList<ProjectVariable>();
final HashMap<String, WizardGroup> wizardGroups = new HashMap<String, WizardGroup>();
Visitor visitor = new ProjectActionList.Visitor() {
@Override
public Result applyTo(ProjectAction action, ProjectActionList.Value result, IProgressMonitor monitor) {
if (action instanceof ProjectActionList) {
ProjectActionList pal = (ProjectActionList) action;
if (!pal.appliesTo(fDevice, fOptionMap)) {
return PRUNE;
}
} else if (action instanceof ProjectVariable) {
ProjectVariable projectVariable = (ProjectVariable) action;
fVariableMap.put(projectVariable.getId(), projectVariable);
variableList.add(projectVariable);
// System.err.println(String.format("Adding %s", projectVariable));
} else if (action instanceof WizardPageInformation) {
WizardPageInformation wizardPageInfo = (WizardPageInformation) action;
// System.err.println("WizardPage found : " + wizardPageInfo);
if (fWizardPageInfo.getId().equals(wizardPageInfo.getId())) {
ArrayList<WizardGroup> groups = wizardPageInfo.getGroups();
// System.err.println("Processing WizardPage : " + wizardPageInfo);
for (WizardGroup group : groups) {
wizardGroups.put(group.getId(), group);
// System.err.println(" WizardGroup found : " + group);
}
}
}
return CONTINUE;
}
};
fProjectActionList.visit(visitor, null);
if (fVariableMap.size() == 0) {
Group defaultGroup = new Group(comp, SWT.NONE);
defaultGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
defaultGroup.setLayout(new GridLayout(2, false));
Label label = new Label(defaultGroup, SWT.NONE);
// $NON-NLS-1$
label.setText("No Options");
} else {
SelectionListener listener = new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
fHasChanged = true;
notifyListeners(SWT.CHANGED, null);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
notifyListeners(SWT.CHANGED, null);
}
};
// Group created dynamically from XML
HashMap<String, Group> groups = new HashMap<String, Group>();
fGroupList = new ArrayList<Group>();
// Create buttons
fButtonMap = new HashMap<String, Button>();
for (ProjectVariable variable : variableList) {
if ((variable.getName() == null) || (variable.getName().length() == 0)) {
throw new RuntimeException("Variable without name " + variable.getId());
}
String groupId = variable.getButtonGroupId();
int buttonStyle = variable.getButtonStyle();
Group group = groups.get(groupId);
if (group == null) {
WizardGroup wizardGroup = wizardGroups.get(groupId);
if (wizardGroup == null) {
continue;
}
group = new Group(comp, SWT.NONE);
fGroupList.add(group);
String groupName = MacroSubstitute.substitute(wizardGroup.getName(), fOptionMap);
group.setText(groupName);
GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
gd.horizontalSpan = wizardGroup.getSpan();
group.setLayoutData(gd);
group.setLayout(new GridLayout(wizardGroup.getWidth(), false));
groups.put(groupId, group);
}
Button button = new Button(group, buttonStyle);
fButtonMap.put(variable.getId(), button);
button.setText(" " + variable.getName());
boolean value = getSetting(variable.getId(), Boolean.valueOf(variable.getDefaultValue()));
button.setSelection(value);
button.setToolTipText(variable.getDescription().replaceAll("\\\\n", "\n"));
button.setData(variable);
button.addSelectionListener(listener);
}
}
return comp;
}
use of net.sourceforge.usbdm.packageParser.ProjectVariable 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();
}
}
use of net.sourceforge.usbdm.packageParser.ProjectVariable in project usbdm-eclipse-plugins by podonoghue.
the class UsbdmOptionsPanel method validate.
/**
* Validates control & returns error message
*
* @return
*
* @return Error message (null if none)
*
* @throws Exception
*/
public String validate() {
if (fButtonMap == null) {
// Button not yet created
return null;
}
// Propagate dependencies between buttons
// Bit of a hack - do the page multiple times to propagate dependency changes
Set<Entry<String, Button>> buttonSet = fButtonMap.entrySet();
for (int i = 0; i < 5; i++) {
// Check and update all buttons and record if any changed
boolean noChanges = true;
for (Entry<String, Button> entry : buttonSet) {
Button button = entry.getValue();
ProjectVariable projectVariable = fVariableMap.get(entry.getKey());
boolean enabled = true;
try {
ApplyWhenCondition applyWhenCondition = projectVariable.getRequirement();
// if (projectVariable.getId().equals("projectOptionValue.KSDK-usb-audio-generator")) {
// applyWhenCondition.setVerbose(true);
// }
enabled = applyWhenCondition.enabled(fDevice, fOptionMap, fButtonMap);
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
if (button.isEnabled() != enabled) {
button.setEnabled(enabled);
noChanges = false;
}
if (!button.isEnabled()) {
button.setSelection(false);
}
}
if (noChanges) {
break;
}
}
// Check for at least one radio button selected in each group
for (Group group : fGroupList) {
boolean radioButtonSelected = false;
boolean radioButtonPresent = false;
for (Control child : group.getChildren()) {
if (child instanceof Button) {
Button button = (Button) child;
if (button.isEnabled() && (button.getStyle() & SWT.RADIO) != 0) {
radioButtonPresent = true;
if (radioButtonSelected) {
// Clear multiple button selection
button.setSelection(false);
}
radioButtonSelected = radioButtonSelected || button.getSelection();
}
}
}
if (radioButtonPresent && !radioButtonSelected) {
// Prompt to select a radio button
return "Select " + group.getText();
}
}
return null;
}
Aggregations