use of org.osate.ge.internal.services.ActionExecutor in project osate2 by osate.
the class AgeEditor method createPartControl.
@Override
public void createPartControl(final Composite parent) {
//
// Create the FX canvas which is an SWT widget for embedding JavaFX content.
//
fxCanvas = new FXCanvas(parent, SWT.NONE);
fxCanvas.addDisposeListener(e -> {
fxCanvas.getScene().setRoot(new Group());
fxCanvas.setScene(null);
});
fxCanvas.addPaintListener(paintListener);
// Suppress SWT key press handling when interaction is active
fxCanvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final org.eclipse.swt.events.KeyEvent e) {
if (activeInteraction != null) {
e.doit = false;
}
}
});
fxCanvas.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
deactivateInteraction();
}
});
// Create the context menu
contextMenuManager = new MenuManager(MENU_ID, MENU_ID);
contextMenuManager.setRemoveAllWhenShown(true);
final Menu contextMenu = contextMenuManager.createContextMenu(fxCanvas);
fxCanvas.setMenu(contextMenu);
getEditorSite().registerContextMenu(MENU_ID, contextMenuManager, selectionProvider, true);
// Create the action executor. It will append an action to activate the editor when undoing and redoing actions.
actionExecutor = (label, mode, action) -> {
final boolean reverseActionWasSpecified = actionService.execute(label, mode, action);
// This will ensure that when the action is undone, the editor will be switched to the one in which the action was performed.
if (isEditorActive() && reverseActionWasSpecified && !actionService.isActionExecuting() && mode == ExecutionMode.NORMAL) {
actionService.execute("Activate Editor", ExecutionMode.APPEND_ELSE_HIDE, new ActivateAgeEditorAction(AgeEditor.this));
}
fireDirtyPropertyChangeEvent();
return reverseActionWasSpecified;
};
// Initialize the palette model
final AgeEditorPaletteModel.ImageProvider imageProvider = id -> {
final RegisteredImage img = extRegistry.getImageMap().get(id);
if (img == null) {
return Optional.empty();
}
final URI imageUri = URI.createPlatformPluginURI("/" + img.plugin + "/" + img.path, true);
if (CommonPlugin.asLocalURI(imageUri).isFile()) {
return Optional.of(new Image(imageUri.toString()));
} else {
return Optional.empty();
}
};
Object diagramBo = AgeDiagramUtil.getConfigurationContextBusinessObject(diagram, projectReferenceService);
if (diagramBo == null) {
diagramBo = project;
}
this.paletteModel = new AgeEditorPaletteModel(extRegistry.getPaletteContributors(), diagramBo, imageProvider);
// If the palette item changes while an interaction is active, deactivate the interaction.
this.paletteModel.activeItemProperty().addListener((javafx.beans.value.ChangeListener<SimplePaletteItem>) (observable, oldValue, newValue) -> deactivateInteraction());
// Initialize the JavaFX nodes based on the diagram
canvas = new InfiniteCanvas();
// Set show grid based on preferences
canvas.setShowGrid(preferenceStore.getBoolean(Preferences.SHOW_GRID));
final Scene scene = new Scene(new DiagramEditorNode(paletteModel, canvas));
fxCanvas.setScene(scene);
gefDiagram = new GefAgeDiagram(diagram, coloringService);
// Create a wrapper around the diagram's scene node.
final Group wrapper = new DiagramNodeWrapper(gefDiagram.getSceneNode());
// Add the wrapper to the canvas
canvas.getContentGroup().getChildren().add(wrapper);
gefDiagram.updateDiagramFromSceneGraph(false);
// Treat the current state of the diagram as clean.
cleanDiagramChangeNumber = diagram.getCurrentChangeNumber();
adapterMap.put(LayoutInfoProvider.class, gefDiagram);
// Create overlays
overlays = new Overlays(gefDiagram);
selectionProvider.addSelectionChangedListener(overlays);
canvas.getScrolledOverlayGroup().getChildren().add(overlays);
// Perform the initial incremental layout
diagram.modify("Incremental Layout", m -> DiagramElementLayoutUtil.layoutIncrementally(diagram, m, gefDiagram));
// Set action executor after initial load. This occurs after the incremental layout to prevent the loading and initial layout from being undoable
diagram.setActionExecutor(actionExecutor);
// Refresh the dirty state whenever an operation occurs
final IOperationHistory history = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory();
history.addOperationHistoryListener(operationHistoryListener);
canvas.setOnScroll(e -> {
if (e.isControlDown()) {
// Adjust zoom
if (e.getDeltaY() < 0.0) {
zoomOut();
} else {
zoomIn();
}
} else {
if (e.isShiftDown()) {
// Scroll in X direction
canvas.setHorizontalScrollOffset(canvas.getHorizontalScrollOffset() - e.getDeltaY());
} else {
// Scroll
canvas.setHorizontalScrollOffset(canvas.getHorizontalScrollOffset() - e.getDeltaX());
canvas.setVerticalScrollOffset(canvas.getVerticalScrollOffset() + e.getDeltaY());
}
}
});
//
// Listeners to handle tooltips
//
canvas.addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET, e -> {
if (e.getTarget() instanceof Node && activeInteraction == null && tooltipManager != null && gefDiagram != null) {
final DiagramElement de = gefDiagram.getDiagramElement((Node) e.getTarget());
if (de != null) {
tooltipManager.mouseEnter(de);
}
}
});
canvas.addEventHandler(MouseEvent.MOUSE_EXITED_TARGET, e -> {
if (e.getTarget() instanceof Node && activeInteraction == null && tooltipManager != null && gefDiagram != null) {
final DiagramElement de = gefDiagram.getDiagramElement((Node) e.getTarget());
if (de != null) {
tooltipManager.mouseExit(de);
}
}
});
//
// General input handlers
//
// Event handler. Delegates to input event handlers or the active interaction as appropriate
final EventHandler<? super InputEvent> handleInput = e -> {
if (activeInteraction == null) {
// Delegate processing of the event to the input event handlers
for (final InputEventHandler inputEventHandler : inputEventHandlers) {
final InputEventHandler.HandledEvent r = inputEventHandler.handleEvent(e);
if (r != null) {
activeInteraction = r.newInteraction;
if (activeInteraction != null) {
canvas.setCursor(activeInteraction.getCursor());
if (tooltipManager != null) {
tooltipManager.hideTooltip();
}
}
break;
}
}
} else {
if (activeInteraction.handleEvent(e) == InteractionState.COMPLETE) {
deactivateInteraction();
}
canvas.setCursor(activeInteraction == null ? null : activeInteraction.getCursor());
}
};
// Handle mouse button presses
canvas.addEventFilter(MouseEvent.MOUSE_PRESSED, handleInput);
canvas.addEventFilter(MouseEvent.MOUSE_DRAGGED, handleInput);
canvas.addEventFilter(MouseEvent.MOUSE_RELEASED, handleInput);
scene.addEventFilter(KeyEvent.KEY_PRESSED, handleInput);
canvas.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {
if (activeInteraction == null) {
Cursor cursor = Cursor.DEFAULT;
for (final InputEventHandler inputEventHandler : inputEventHandlers) {
final Cursor overrideCursor = inputEventHandler.getCursor(e);
if (overrideCursor != null) {
cursor = overrideCursor;
break;
}
}
canvas.setCursor(cursor);
}
handleInput.handle(e);
});
// Create input event handlers
inputEventHandlers.add(new OpenPropertiesViewInputEventHandler(this));
inputEventHandlers.add(new ResizeInputEventHandler(this));
inputEventHandlers.add(new MarqueeSelectInputEventHandler(this));
inputEventHandlers.add(new MoveConnectionPointTool(this));
inputEventHandlers.add(new RenameInputEventHandler(this));
inputEventHandlers.add(new SelectInputEventHandler(this));
inputEventHandlers.add(new MoveInputEventHandler(this));
inputEventHandlers.add(new PaletteCommandInputEventHandler(this));
}
use of org.osate.ge.internal.services.ActionExecutor in project osate2 by osate.
the class EditorRenameUtil method rename.
public static void rename(final DiagramElement de, final String value, final InternalDiagramEditor editor) {
final ReferenceBuilderService referenceBuilder = Objects.requireNonNull(editor.getAdapter(ReferenceBuilderService.class), "Unable to retrieve reference builder service");
final DiagramService diagramService = Objects.requireNonNull(editor.getAdapter(DiagramService.class), "Unable to retrieve diagram service");
final AadlModificationService aadlModService = Objects.requireNonNull(editor.getAdapter(AadlModificationService.class), "Unable to retrieve AADL modification service");
final ProjectProvider projectProvider = Objects.requireNonNull(editor.getAdapter(ProjectProvider.class), "Unable to retrieve project provider");
final ActionExecutor actionExecutor = editor.getActionExecutor();
final ModelChangeNotifier modelChangeNotifier = Objects.requireNonNull(editor.getAdapter(ModelChangeNotifier.class), "Unable to retrieve model change notifier");
final EObject bo = (EObject) de.getBusinessObject();
final BusinessObjectHandler handler = de.getBusinessObjectHandler();
final String initialValue = getCurrentName(de);
// If the business object handler provides a Rename method, then use it to rename the element instead of using LTK refactoring.
if (RenameUtil.supportsNonLtkRename(handler)) {
final CanonicalBusinessObjectReference canonicalRef = referenceBuilder.getCanonicalReference(bo);
final IProject project = ProjectUtil.getProjectOrNull(EcoreUtil.getURI(bo));
final ReferenceCollection references;
if (canonicalRef == null || project == null) {
references = null;
} else {
final Set<IProject> relevantProjects = ProjectUtil.getAffectedProjects(project, new HashSet<>());
references = diagramService.getReferences(relevantProjects, Collections.singleton(canonicalRef));
}
aadlModService.modify(bo, (boToModify) -> {
RenameUtil.performNonLtkRename(boToModify, handler, value);
// Update diagram references. This is done in the modify block because the project is build and the diagram is updated before the modify()
// function returns.
final CanonicalBusinessObjectReference newCanonicalRef = referenceBuilder.getCanonicalReference(boToModify);
final RelativeBusinessObjectReference newRelRef = referenceBuilder.getRelativeReference(boToModify);
if (newCanonicalRef != null && newRelRef != null) {
references.update(new UpdatedReferenceValueProvider() {
@Override
public CanonicalBusinessObjectReference getNewCanonicalReference(final CanonicalBusinessObjectReference originalCanonicalReference) {
if (originalCanonicalReference.equals(canonicalRef)) {
return newCanonicalRef;
}
return null;
}
@Override
public RelativeBusinessObjectReference getNewRelativeReference(final CanonicalBusinessObjectReference originalCanonicalReference) {
if (originalCanonicalReference.equals(canonicalRef)) {
return newRelRef;
}
return null;
}
});
}
});
} else {
// Rename using LTK
actionExecutor.execute("Rename Element " + initialValue + " to " + value, ActionExecutor.ExecutionMode.NORMAL, new LtkRenameAction(projectProvider, modelChangeNotifier, new BoSupplier(de), value, initialValue));
}
}
use of org.osate.ge.internal.services.ActionExecutor in project osate2 by osate.
the class UiBusinessObjectSelection method getActionExecutor.
/**
* Gets the action executor that should be used to modify the model.
* @return
*/
private static ActionExecutor getActionExecutor() {
final InternalDiagramEditor editor = UiUtil.getActiveDiagramEditor();
ActionExecutor executor = null;
if (editor != null) {
executor = editor.getActionExecutor();
}
if (executor == null) {
executor = Objects.requireNonNull(PlatformUI.getWorkbench().getService(ActionService.class), "Unable to retrieve action service");
}
return executor;
}
Aggregations