use of org.eclipse.gef.fx.nodes.InfiniteCanvas 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.eclipse.gef.fx.nodes.InfiniteCanvas in project osate2 by osate.
the class GefTest method main.
/**
* Entry point for the test application
* @param args command line arguments
*/
public static void main(final String[] args) {
try (final ImageManager images = new ImageManager()) {
NodeApplication.run(() -> {
final DiagramRootNode root = new DiagramRootNode();
//
// Create Top Level Node
//
final ContainerShape top1 = new ContainerShape();
top1.setGraphic(new RectangleNode(true));
root.getChildren().setAll(top1);
//
// Create Labels
//
final LabelNode label1 = new LabelNode("Top Node");
final LabelNode label2 = new LabelNode("Longer single-line label. This should be truncated if there isn't space.");
final LabelNode label3 = new LabelNode("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
label3.setWrapText(true);
top1.getPrimaryLabels().setAll(label1);
top1.getSecondaryLabels().setAll(label2, label3);
//
// Left Features
//
final DockedShape lfg1 = createDockedShape("lfg1", new FeatureGroupNode());
top1.getLeftChildren().add(lfg1);
lfg1.setConfiguredWidth(400.0);
lfg1.setConfiguredHeight(50.0);
PreferredPosition.set(lfg1, new Point2D(0, 300));
final DockedShape lfg1Child1 = createDockedShape("lfg1Child1", new PortNode(false, true, FeatureDirection.IN));
lfg1Child1.getSecondaryLabels().add(new LabelNode("<internal>"));
PreferredPosition.set(lfg1Child1, new Point2D(0, 20));
lfg1.getNestedChildren().add(lfg1Child1);
final DockedShape lfg1Child2 = createDockedShape("lfg1Child2", new PortNode(false, true, FeatureDirection.OUT));
PreferredPosition.set(lfg1Child2, new Point2D(0, 50));
lfg1.getNestedChildren().add(lfg1Child2);
// Nested feature groups
final DockedShape nestedFg = createDockedShape("nestedFg", new FeatureGroupNode());
lfg1.getNestedChildren().add(nestedFg);
nestedFg.getNestedChildren().add(createDockedShape("c1", new PortNode(false, true, FeatureDirection.IN_OUT)));
nestedFg.getNestedChildren().add(createDockedShape("c2", new PortNode(false, true, FeatureDirection.IN)));
//
// Right Features
//
final DockedShape rf1 = createDockedShape("rf1", new PortNode(false, true, FeatureDirection.OUT));
rf1.getSecondaryLabels().add(new LabelNode("<internal>"));
top1.getRightChildren().add(rf1);
PreferredPosition.set(rf1, new Point2D(0, 230));
final DockedShape rf2 = createDockedShape("rf2", new PortNode(false, true, FeatureDirection.OUT));
top1.getRightChildren().add(rf2);
//
// Create Free Children
//
// mode
final ContainerShape mode1 = new ContainerShape();
final ModeNode mode1Graphic = new ModeNode();
mode1Graphic.setInitialMode(true);
mode1.setGraphic(mode1Graphic);
mode1.getPrimaryLabels().setAll(new LabelNode("m1"));
mode1.getSecondaryLabels().setAll(new LabelNode("<initial>"));
PreferredPosition.set(mode1, new Point2D(400, 200));
top1.getFreeChildren().add(mode1);
// subcomponent
final ContainerShape sc1 = new ContainerShape();
sc1.apply(new FxStyle.Builder().horizontalLabelPosition(LabelPosition.CENTER).verticalLabelPosition(LabelPosition.BEGINNING).build());
sc1.setGraphic(new DeviceNode());
sc1.getPrimaryLabels().setAll(new LabelNode("sys1"));
top1.getFreeChildren().add(sc1);
PreferredPosition.set(sc1, new Point2D(200, 300));
sc1.setConfiguredWidth(310);
sc1.setConfiguredHeight(100);
final DockedShape sys1In1 = createDockedShape("in1", new PortNode(true, true, FeatureDirection.IN));
sc1.getLeftChildren().add(sys1In1);
final DockedShape sys1In2 = createDockedShape("in2", new PortNode(true, true, FeatureDirection.IN));
sc1.getLeftChildren().add(sys1In2);
final DockedShape sys1Out1 = createDockedShape("out1", new PortNode(false, true, FeatureDirection.OUT));
sc1.getRightChildren().add(sys1Out1);
//
// Connections
//
// Flow Indicator
final FlowIndicatorNode fsnk1 = new FlowIndicatorNode();
fsnk1.getPrimaryLabels().setAll(new LabelNode("fsnk1"));
fsnk1.setStartDecoration(new PolygonNode(new Dimension2D(20.0, 12.0), 1.0, 1.0, 0.0, 0.5, 1.0, 0.0));
fsnk1.setEndDecoration(new PolylineNode(new Dimension2D(0.0, 16.0), 0.0, 0.0, 0.0, 1.0));
PreferredPosition.set(fsnk1, new Point2D(80.0, 30.0));
fsnk1.setStartAnchor(sys1In1.getInteriorAnchor());
sc1.getFreeChildren().add(fsnk1);
// Connection
final ConnectionNode c1 = new ConnectionNode();
c1.setStartAnchor(sys1In2.getExteriorAnchor());
c1.setEndAnchor(lfg1Child1.getInteriorAnchor());
c1.getPrimaryLabels().setAll(new LabelNode("c1"));
c1.getMidpointDecorations().setAll(new PolygonNode(new Dimension2D(20.0, 12.0), 1.0, 1.0, 0.0, 0.5, 1.0, 0.0), new PolygonNode(new Dimension2D(10.0, 12.0), 1.0, 1.0, 0.0, 0.5, 1.0, 0.0));
c1.setStartDecoration(new PolygonNode(new Dimension2D(8.0, 12.0), 1.0, 1.0, 0.0, 0.5, 1.0, 0.0));
c1.setEndDecoration(new PolygonNode(new Dimension2D(20.0, 10.0), 1.0, 1.0, 0.0, 0.5, 1.0, 0.0));
root.getChildren().add(c1);
final ConnectionNode fp1 = new ConnectionNode();
fp1.getPrimaryLabels().setAll(new LabelNode("fp1"));
fp1.getSecondaryLabels().setAll(new LabelNode("Latency := 30ms>"));
fp1.setStartAnchor(sys1In2.getInteriorAnchor());
fp1.setEndAnchor(sys1Out1.getInteriorAnchor());
fp1.getInnerConnection().setControlPoints(ImmutableList.of(new org.eclipse.gef.geometry.planar.Point(350, 380), new org.eclipse.gef.geometry.planar.Point(350, 330)));
root.getChildren().add(fp1);
final ConnectionNode c2 = new ConnectionNode();
c2.setStartAnchor(sys1Out1.getExteriorAnchor());
c2.setEndAnchor(rf2.getInteriorAnchor());
root.getChildren().add(c2);
final BaseConnectionNode c3 = new ConnectionNode();
c3.setStartAnchor(mode1.getAnchor());
c3.setEndAnchor(c2.getMidpointAnchor());
root.getChildren().add(c3);
mode1.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
PreferredPosition.set(mode1, new Point2D(300, 200));
event.consume();
});
final InfiniteCanvas canvas = new InfiniteCanvas();
canvas.getContentGroup().getChildren().add(root);
canvas.boundsInLocalProperty().addListener((ChangeListener<Bounds>) (observable, oldValue, newValue) -> {
top1.setConfiguredWidth(newValue.getWidth());
top1.setConfiguredHeight(newValue.getHeight());
});
// Start a service which will reload images
ScheduledService<Void> svc = new ScheduledService<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() {
images.refreshImages();
return null;
}
};
}
};
svc.setPeriod(Duration.seconds(1));
svc.start();
final ImageReference testImage = args.length > 0 ? images.getImageReference(Paths.get(args[0])) : null;
//
// Style test
//
final Font f1 = Font.font("Arial", FontWeight.NORMAL, 14.0);
final FxStyle s1 = new FxStyle.Builder().primaryLabelsVisible(true).font(f1).image(testImage).build();
final FxStyle s2 = new FxStyle.Builder().outlineColor(Color.BLUE).primaryLabelsVisible(true).lineWidth(4).build();
StyleRoot.set(top1, true);
StyleRoot.set(mode1, true);
StyleRoot.set(sc1, true);
FxStyleApplier.applyStyle(mode1, s2);
FxStyleApplier.applyStyle(sc1, s2);
FxStyleApplier.applyStyle(top1, s1);
return canvas;
});
}
}
Aggregations