use of org.osate.ge.gef.ui.preferences.Preferences 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));
}
Aggregations