use of org.osate.aadl2.instance.InstanceObject in project AGREE by loonwerks.
the class VariablesView method fillContextMenu.
// Fill context menu dynamically
private void fillContextMenu(final MenuManager contextMenu, final Tree tree) {
if (treeSelectionTracker.isSelectionValid()) {
final int selectedColumnIndex = treeSelectionTracker.getSelectedColumnIndex();
final int valueColumnsSize = valueColumns.size();
final int valueColumnIndex = selectedColumnIndex - (treeViewer.getTree().getColumnCount() - valueColumnsSize);
final SimulationEngine engine = simulationUiService.getCurrentState().getSimulationEngine();
// Create the Show in Graphical View Menu Item
if (selectedColumnIndex == VARIABLE_NAME_COLUMN_INDEX) {
final Object element = treeSelectionTracker.getSelectedTreeItemData();
final InstanceObject io = (engine != null && engine.getCurrentState() != null) ? engine.getCurrentState().getElementInstanceObject(element) : null;
if (io != null) {
contextMenu.add(new Action("Show in Graphical View") {
@Override
public void run() {
showInGraphicalView(io);
}
});
}
// Populate context menu using command extensions
final CommandContext cmdCtx = new SimpleCommandContext(element, engine.getCurrentState(), engine);
for (final Command cmd : extService.getCommands()) {
if (cmd.isAvailable(cmdCtx)) {
contextMenu.add(new Action(cmd.getLabel(cmdCtx)) {
@Override
public boolean isEnabled() {
return cmd.isActivatable(cmdCtx);
}
@Override
public void run() {
cmd.activate(cmdCtx);
}
});
}
}
}
// Highlight Menu
final MenuManager subMenu = new MenuManager("Highlight");
if (valueColumnIndex >= 0) {
final int frameIndex = valueColumns.get(valueColumnIndex).getFrameIndex();
final Object treeItemData = treeSelectionTracker.getSelectedTreeItemData();
ImageDescriptor imageDesc = ImageDescriptor.createFromImage(greenImage);
subMenu.add(new Action("Green", imageDesc) {
@Override
public void run() {
highlightCell(treeItemData, frameIndex, green);
}
});
imageDesc = ImageDescriptor.createFromImage(redImage);
subMenu.add(new Action("Red", imageDesc) {
@Override
public void run() {
highlightCell(treeItemData, frameIndex, red);
}
});
imageDesc = ImageDescriptor.createFromImage(yellowImage);
subMenu.add(new Action("Yellow", imageDesc) {
@Override
public void run() {
highlightCell(treeItemData, frameIndex, yellow);
}
});
if (customRgb != null) {
fillColorIconImage(customColorImage, customRgb);
imageDesc = ImageDescriptor.createFromImage(customColorImage);
subMenu.add(new Action("Previous", imageDesc) {
@Override
public void run() {
highlightCell(treeItemData, frameIndex, customRgb);
}
});
}
subMenu.add(new Action("Custom...") {
@Override
public void run() {
final Shell shell = new Shell();
final Rectangle rect = tree.getParent().getShell().getBounds();
// offset width and height of color dialog box from center of screen/parent shell
shell.setLocation((rect.x + rect.width / 2) - 150, (rect.y + rect.height / 2) - 250);
final ColorDialog colorDialog = new ColorDialog(shell);
colorDialog.open();
if (colorDialog.getRGB() != null) {
final RGB cellColor = colorDialog.getRGB();
highlightCell(treeItemData, frameIndex, cellColor);
customRgb = cellColor;
}
}
});
contextMenu.add(subMenu);
final CellColorInfo cellColorInfo = getCellColorInfo(treeItemData, frameIndex);
if (cellColorInfo != null) {
final CellColorInfo removeCellColorInfo = cellColorInfo;
contextMenu.add(new Action("Reset Highlight") {
@Override
public void run() {
if (treeViewer != null) {
cellColorInfos.remove(removeCellColorInfo);
treeViewer.getTree().redraw();
}
}
});
}
}
// Reset all Highlights
if (!cellColorInfos.isEmpty()) {
// Resets tree, erases highlights
contextMenu.add(new Action("Reset All Highlights") {
@Override
public void run() {
final MessageBox dialog = new MessageBox(tree.getShell(), SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
dialog.setText("Reset All Highlights");
dialog.setMessage("Are you sure you want to reset all highlighted cells?");
if (dialog.open() == SWT.OK) {
if (treeViewer != null) {
cellColorInfos.clear();
treeViewer.getTree().redraw();
}
}
}
});
}
}
}
use of org.osate.aadl2.instance.InstanceObject in project AGREE by loonwerks.
the class SimulatorTooltipContributor method addTooltipContents.
@Override
public void addTooltipContents(final TooltipContributorContext ctx) {
final Object bo = ctx.getBusinessObjectContext().getBusinessObject();
final SimulationUIService simulationUiService = Objects.requireNonNull(EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()).get(SimulationUIService.class), "unable to get simulation UI service");
final SimulatorState simulatorState = simulationUiService.getCurrentState();
if (simulatorState.getEngineState() != null) {
final SimulationEngineState engineState = simulatorState.getEngineState();
if (engineState.getNumberOfFrames() > 0) {
if (bo instanceof FeatureInstance || bo instanceof ComponentInstance) {
final InstanceObject io = (InstanceObject) bo;
// Compare the references from the diagram and simulation system instances. Only show tooltip if they match
// This will ensure that the tooltips only appear for diagrams of instances of the same component implementation
final URI diagramSystemInstanceUri = getResourceUri(io.getSystemInstance());
final URI simulationSystemInstanceUri = getResourceUri(simulatorState.getSimulationEngine().getSystemInstance());
if (diagramSystemInstanceUri != null && diagramSystemInstanceUri.equals(simulationSystemInstanceUri)) {
final int frameIndex = simulatorState.getSelectedFrameIndex() == SimulatorState.NO_FRAME_INDEX_SELECTED ? engineState.getNumberOfFrames() - 1 : simulatorState.getSelectedFrameIndex();
if (frameIndex < engineState.getNumberOfFrames()) {
// Handle the root instance object as a special case because there will not be a state element for it.
if (io == io.getSystemInstance()) {
final StyledText st = new StyledText(ctx.getTooltip(), SWT.NONE);
st.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
addTooltipInfo(st, engineState, frameIndex, io.getName(), null, engineState.getRootElements(), io, INDENT_SIZE);
} else {
final Object stateElement = engineState.findElement(io);
if (stateElement != null) {
final StyledText st = new StyledText(ctx.getTooltip(), SWT.NONE);
st.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
addStateElementsInfo(st, engineState, frameIndex, Collections.singleton(stateElement), io, INDENT_SIZE);
}
}
}
}
}
}
}
}
use of org.osate.aadl2.instance.InstanceObject in project AGREE by loonwerks.
the class ShowInSimulationVariablesViewHandler method calculateEnabled.
private boolean calculateEnabled(final Object evaluationContext) {
final SimulationUIService simulationUiService = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()).get(SimulationUIService.class);
if (simulationUiService == null) {
return false;
}
final List<BusinessObjectContext> selectedBocs = getSelectedDiagramElementsFromContext(evaluationContext);
if (simulationUiService.getCurrentState().getEngineState() == null || selectedBocs.size() != 1) {
return false;
}
final Object bo = selectedBocs.get(0).getBusinessObject();
// Make the command available if there is only one business object selected and it is not the root System instance
if ((bo instanceof FeatureInstance || bo instanceof ComponentInstance) && bo != ((InstanceObject) bo).getSystemInstance()) {
final InstanceObject io = (InstanceObject) bo;
// Compare the references from the diagram and simulation system instances. Only show the command if they match
final URI diagramSystemInstanceUri = EcoreUtil.getURI(io.getSystemInstance());
final URI simulationSystemInstanceUri = EcoreUtil.getURI(simulationUiService.getCurrentState().getSimulationEngine().getSystemInstance());
return diagramSystemInstanceUri != null && diagramSystemInstanceUri.equals(simulationSystemInstanceUri);
}
return false;
}
use of org.osate.aadl2.instance.InstanceObject in project AGREE by loonwerks.
the class VariablesView method createView.
@PostConstruct
void createView(final Composite parent) {
final Display display = parent.getDisplay();
colorManager = new DeviceResourceManager(display);
unchangedValueColor = colorManager.createColor(new RGB(192, 192, 192));
constraintErrorColor = colorManager.createColor(red);
greenImage = createColorIconImage(display, green);
redImage = createColorIconImage(display, red);
yellowImage = createColorIconImage(display, yellow);
customColorImage = createColorIconImage(display, customRgb);
// Create root composite
final Composite root = new Composite(parent, SWT.NONE);
final GridLayout rootLayout = new GridLayout(1, false);
root.setLayout(rootLayout);
// Create the variable tree viewer
treeViewer = new VariablesTreeViewer(root, SWT.FULL_SELECTION);
// Enable tooltip support
ColumnViewerToolTipSupport.enableFor(treeViewer);
treeSelectionTracker = new TreeSelectionTracker(treeViewer);
GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
treeViewer.setUseHashlookup(true);
final VariableContentProvider contentProvider = new VariableContentProvider();
treeViewer.setContentProvider(contentProvider);
treeViewer.setInput(null);
treeViewer.setComparator(new ViewerComparator() {
@Override
public int compare(final Viewer viewer, final Object e1, final Object e2) {
if (contentProvider.currentState == null) {
return 0;
}
// Sort component instances before other variables
final InstanceObject io1 = contentProvider.currentState.getEngineState().getElementInstanceObject(e1);
final InstanceObject io2 = contentProvider.currentState.getEngineState().getElementInstanceObject(e2);
if (io1 instanceof ComponentInstance && !(io2 instanceof ComponentInstance)) {
return -1;
} else if (io2 instanceof ComponentInstance && !(io1 instanceof ComponentInstance)) {
return 1;
}
// Sort feature instances before the remainder
if (io1 instanceof FeatureInstance && !(io2 instanceof FeatureInstance)) {
return -1;
} else if (io2 instanceof FeatureInstance && !(io1 instanceof FeatureInstance)) {
return 1;
}
final String n1 = contentProvider.currentState.getEngineState().getElementName(e1);
final String n2 = contentProvider.currentState.getEngineState().getElementName(e2);
return getComparator().compare(n1, n2);
}
});
treeViewer.setFilters(new ViewerFilter[] { new ViewerFilter() {
@Override
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
return !contentProvider.currentState.getEngineState().isElementHidden(element);
}
} });
// Create columns
final TreeViewerColumn nameColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
nameColumn.getColumn().setWidth(200);
nameColumn.getColumn().setText("Name");
nameColumn.getColumn().addControlListener(resizeListener);
nameColumn.setLabelProvider(new StyledCellLabelProvider() {
@Override
public void update(final ViewerCell cell) {
cell.setText(getText(cell.getElement()));
cell.setImage(getImage(cell.getElement()));
}
private String getText(final Object element) {
// Get the name of the frame state element
final SimulatorState state = contentProvider.getCurrentState();
if (state == null) {
return "";
}
return state.getEngineState().getElementName(element);
}
private Image getImage(final Object element) {
final SimulatorState state = contentProvider.getCurrentState();
if (state == null) {
return null;
}
final InstanceObject instanceObject = state.getEngineState().getElementInstanceObject(element);
return instanceObject == null ? null : UiUtil.getModelElementLabelProvider().getImage(instanceObject);
}
});
final TreeViewerColumn nextValueColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
nextValueColumn.getColumn().setWidth(100);
nextValueColumn.getColumn().setText("Next Value");
nextValueColumn.setLabelProvider(new OwnerDrawLabelProvider() {
@Override
public void initialize(final ColumnViewer viewer, final ViewerColumn column) {
super.initialize(viewer, column, true);
}
@Override
public void update(final ViewerCell cell) {
final String constraintTxt = getConstraintText(cell.getElement());
final String txt;
final Color foreground;
if (constraintTxt == null) {
// Attempt to retrieve the value for the next frame from from the engine state.
final SimulatorState state = contentProvider.getCurrentState();
if (state == null) {
txt = "";
} else {
final SimulationEngineState engineState = state.getEngineState();
txt = ValueFormatter.formatValue(engineState.getElementValue(engineState.getNumberOfFrames(), cell.getElement()));
}
foreground = unchangedValueColor;
} else {
txt = constraintTxt;
foreground = null;
}
cell.setText(txt);
cell.setForeground(foreground);
super.update(cell);
}
private String getConstraintText(final Object element) {
// Return the invalid value if the input that was entered is not correct
final ConstraintError constraintError = elementToConstraintErrorMap.get(element);
if (constraintError != null) {
return constraintError.constraint;
}
final SimulationEngineState engineState = simulationUiService.getCurrentState().getEngineState();
if (engineState == null) {
return null;
}
final InputConstraint ic = engineState.getElementInputConstraintForNextFrame(element);
if (ic == null) {
return null;
}
return inputConstraintHelper.unparse(ic);
}
@Override
public String getToolTipText(final Object element) {
final ConstraintError constraintError = elementToConstraintErrorMap.get(element);
if (constraintError == null) {
return super.getToolTipText(element);
} else {
return constraintError.errorMessage;
}
}
@Override
protected void measure(final Event event, final Object element) {
}
@Override
protected void erase(final Event event, final Object element) {
// Color cells which contain invalid values
if (elementToConstraintErrorMap.containsKey(element)) {
final GC gc = event.gc;
final Color oldBackground = event.gc.getBackground();
gc.setBackground(constraintErrorColor);
final Rectangle bounds = event.getBounds();
gc.fillRectangle(bounds);
gc.setBackground(oldBackground);
// Ensure that selection and hover indicators are not drawn for the highlighted cell
event.detail &= ~SWT.SELECTED;
event.detail &= ~SWT.HOT;
}
// Disable drawing foreground. paint() will draw the foreground text in order to fix coloring issues when the row is selected.
event.detail &= ~SWT.FOREGROUND;
}
@Override
protected void paint(final Event event, final Object element) {
// Draw just the text with the appropriate background color
final TreeItem item = (TreeItem) event.item;
final Rectangle textBounds = item.getTextBounds(event.index);
final GC gc = event.gc;
final String txt = item.getText(event.index);
gc.setForeground(item.getForeground(event.index));
String drawTxt = txt;
// Check the width of the text and truncate it as necessary
final int maxWidth = Math.max(gc.getClipping().width - 6, textBounds.width);
if (maxWidth > 0) {
String truncatedTxt = txt;
while (gc.textExtent(drawTxt).x > maxWidth && truncatedTxt.length() > 0) {
truncatedTxt = truncatedTxt.substring(0, truncatedTxt.length() - 1);
drawTxt = truncatedTxt + "...";
}
gc.drawString(drawTxt, textBounds.x, textBounds.y, true);
}
}
});
nextValueEditingSupport = new NextValueEditingSupport(simulationUiService, treeViewer);
nextValueColumn.setEditingSupport(nextValueEditingSupport);
nextValueColumn.getColumn().addControlListener(resizeListener);
// Make headers and lines visible
final Tree tree = treeViewer.getTree();
tree.setHeaderVisible(true);
tree.setLinesVisible(true);
createContextMenu(tree);
// Listen for tree resizing
tree.addControlListener(resizeListener);
// Create the slider
frameSlider = new Slider(root, SWT.HORIZONTAL);
frameSlider.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
if (!editingTreeViewer) {
startEditingTreeViewer();
frameScrollOffset = frameSlider.getMaximum() - frameSlider.getSelection() - frameSlider.getThumb();
update();
stopEditingTreeViewer();
}
}
});
GridDataFactory.fillDefaults().grab(true, false).applyTo(frameSlider);
// Add listener and populate UI with current state
simulationUiService.addStateChangeListener(stateListener);
stateListener.onSimulatorStateChanged(simulationUiService.getCurrentState());
}
use of org.osate.aadl2.instance.InstanceObject in project osate2 by osate.
the class InterpreterUtil method resolve.
/**
* Resolve a model element reference relative to an instance object
*/
static InstanceObject resolve(AModelReference ref, NamedElement root) {
if (!(root instanceof InstanceObject)) {
return null;
}
if (ref.getPrev() == null) {
return (InstanceObject) root;
} else {
InstanceObject io = resolve(ref.getPrev(), root);
EObject result = io.eContents().stream().filter(it -> (it instanceof InstanceObject) ? ((InstanceObject) it).getName().equalsIgnoreCase(ref.getModelElement().getName()) : false).findFirst().get();
return (InstanceObject) result;
}
}
Aggregations