Search in sources :

Example 1 with SimpleCommandContext

use of edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext 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();
                        }
                    }
                }
            });
        }
    }
}
Also used : Action(org.eclipse.jface.action.Action) SimpleCommandContext(edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext) CommandContext(edu.uah.rsesc.aadlsimulator.ui.ext.CommandContext) Rectangle(org.eclipse.swt.graphics.Rectangle) RGB(org.eclipse.swt.graphics.RGB) Point(org.eclipse.swt.graphics.Point) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint) MessageBox(org.eclipse.swt.widgets.MessageBox) SimulationEngine(edu.uah.rsesc.aadlsimulator.SimulationEngine) InstanceObject(org.osate.aadl2.instance.InstanceObject) Shell(org.eclipse.swt.widgets.Shell) ColorDialog(org.eclipse.swt.widgets.ColorDialog) SimpleCommandContext(edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext) Command(edu.uah.rsesc.aadlsimulator.ui.ext.Command) MenuManager(org.eclipse.jface.action.MenuManager) InstanceObject(org.osate.aadl2.instance.InstanceObject) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor)

Example 2 with SimpleCommandContext

use of edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext in project AGREE by loonwerks.

the class PropertiesView method createView.

@PostConstruct
void createView(final Composite parent) {
    propertiesStateViewer = new PropertiesStateViewer(parent);
    propertiesStateViewer.addPropertyMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(final IMenuManager manager) {
            // Only populate the menu if a single property is selected
            final IStructuredSelection selection = propertiesStateViewer.getSelection();
            if (selection.size() == 1 && engineState != null && engine != null) {
                final Object selectedProperty = selection.getFirstElement();
                // Populate context menu using command extensions
                final CommandContext cmdCtx = new SimpleCommandContext(selectedProperty, engineState, engine);
                for (final Command cmd : extService.getCommands()) {
                    if (cmd.isAvailable(cmdCtx)) {
                        manager.add(new Action(cmd.getLabel(cmdCtx)) {

                            @Override
                            public boolean isEnabled() {
                                return cmd.isActivatable(cmdCtx);
                            }

                            @Override
                            public void run() {
                                cmd.activate(cmdCtx);
                            }
                        });
                    }
                }
            }
        }
    });
    // Add listener and populate UI with current state
    simulationUiService.addStateChangeListener(stateListener);
    stateListener.onSimulatorStateChanged(simulationUiService.getCurrentState());
}
Also used : Action(org.eclipse.jface.action.Action) SimpleCommandContext(edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext) CommandContext(edu.uah.rsesc.aadlsimulator.ui.ext.CommandContext) SimpleCommandContext(edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext) Command(edu.uah.rsesc.aadlsimulator.ui.ext.Command) IMenuManager(org.eclipse.jface.action.IMenuManager) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IMenuListener(org.eclipse.jface.action.IMenuListener) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Command (edu.uah.rsesc.aadlsimulator.ui.ext.Command)2 CommandContext (edu.uah.rsesc.aadlsimulator.ui.ext.CommandContext)2 SimpleCommandContext (edu.uah.rsesc.aadlsimulator.ui.ext.SimpleCommandContext)2 Action (org.eclipse.jface.action.Action)2 SimulationEngine (edu.uah.rsesc.aadlsimulator.SimulationEngine)1 InputConstraint (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint)1 PostConstruct (javax.annotation.PostConstruct)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 Point (org.eclipse.swt.graphics.Point)1 RGB (org.eclipse.swt.graphics.RGB)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 ColorDialog (org.eclipse.swt.widgets.ColorDialog)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 Shell (org.eclipse.swt.widgets.Shell)1 InstanceObject (org.osate.aadl2.instance.InstanceObject)1