Search in sources :

Example 1 with SimplePaletteItem

use of org.osate.ge.gef.palette.SimplePaletteItem 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));
}
Also used : DiagramModifier(org.osate.ge.internal.diagram.runtime.DiagramModifier) Tool(org.osate.ge.internal.ui.tools.Tool) CoreException(org.eclipse.core.runtime.CoreException) FocusEvent(org.eclipse.swt.events.FocusEvent) DiagramElementLayoutUtil(org.osate.ge.internal.diagram.runtime.layout.DiagramElementLayoutUtil) InteractionState(org.osate.ge.gef.ui.editor.Interaction.InteractionState) BusinessObjectContext(org.osate.ge.BusinessObjectContext) DefaultQueryService(org.osate.ge.services.impl.DefaultQueryService) Composite(org.eclipse.swt.widgets.Composite) PartInitException(org.eclipse.ui.PartInitException) Map(java.util.Map) CommonPlugin(org.eclipse.emf.common.CommonPlugin) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) StatusManager(org.eclipse.ui.statushandlers.StatusManager) ProjectProvider(org.osate.ge.internal.services.ProjectProvider) Transform(javafx.scene.transform.Transform) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) GefAgeDiagram(org.osate.ge.gef.ui.diagram.GefAgeDiagram) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) KeyAdapter(org.eclipse.swt.events.KeyAdapter) ChangeListener(org.osate.ge.internal.services.ModelChangeNotifier.ChangeListener) IEditorInput(org.eclipse.ui.IEditorInput) PlatformUI(org.eclipse.ui.PlatformUI) MenuManager(org.eclipse.jface.action.MenuManager) Status(org.eclipse.core.runtime.Status) Rectangle(javafx.scene.shape.Rectangle) AgeGefUiPlugin(org.osate.ge.gef.ui.AgeGefUiPlugin) KeyEvent(javafx.scene.input.KeyEvent) UiService(org.osate.ge.internal.services.UiService) Group(javafx.scene.Group) IContentOutlinePage(org.eclipse.ui.views.contentoutline.IContentOutlinePage) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IOperationHistoryListener(org.eclipse.core.commands.operations.IOperationHistoryListener) ReferenceService(org.osate.ge.internal.services.ReferenceService) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) Overlays(org.osate.ge.gef.ui.editor.overlays.Overlays) SWT(org.eclipse.swt.SWT) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) EditorPart(org.eclipse.ui.part.EditorPart) AgeDiagramProvider(org.osate.ge.internal.AgeDiagramProvider) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) Bounds(javafx.geometry.Bounds) PaintListener(org.eclipse.swt.events.PaintListener) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) URI(org.eclipse.emf.common.util.URI) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) ExtensionRegistryService(org.osate.ge.internal.services.ExtensionRegistryService) ListenerList(org.eclipse.core.runtime.ListenerList) IEditorSite(org.eclipse.ui.IEditorSite) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IPropertySheetPage(org.eclipse.ui.views.properties.IPropertySheetPage) ArrayList(java.util.ArrayList) IFileEditorInput(org.eclipse.ui.IFileEditorInput) ModelChangeNotifier(org.osate.ge.internal.services.ModelChangeNotifier) AgeContentOutlinePage(org.osate.ge.internal.ui.editor.AgeContentOutlinePage) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) ITabbedPropertySheetPageContributor(org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor) IProject(org.eclipse.core.resources.IProject) IResourceDelta(org.eclipse.core.resources.IResourceDelta) ReferenceResolutionService(org.osate.ge.services.ReferenceResolutionService) IFile(org.eclipse.core.resources.IFile) BusinessObjectTreeUpdater(org.osate.ge.internal.diagram.runtime.updating.BusinessObjectTreeUpdater) FXCanvas(javafx.embed.swt.FXCanvas) Color(javafx.scene.paint.Color) IPreferenceChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener) FileEditorInput(org.eclipse.ui.part.FileEditorInput) SelectAllAction(org.osate.ge.internal.ui.editor.actions.SelectAllAction) Node(javafx.scene.Node) ContentOutline(org.eclipse.ui.views.contentoutline.ContentOutline) TabbedPropertySheetPage(org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage) ProjectReferenceServiceProxy(org.osate.ge.internal.services.impl.ProjectReferenceServiceProxy) Cursor(javafx.scene.Cursor) QueryService(org.osate.ge.services.QueryService) IPartListener(org.eclipse.ui.IPartListener) IContextService(org.eclipse.ui.contexts.IContextService) InputEvent(javafx.scene.input.InputEvent) DiagramModificationAdapter(org.osate.ge.internal.diagram.runtime.DiagramModificationAdapter) LayoutInfoProvider(org.osate.ge.internal.diagram.runtime.layout.LayoutInfoProvider) Preferences(org.osate.ge.gef.ui.preferences.Preferences) Image(javafx.scene.image.Image) EventHandler(javafx.event.EventHandler) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Affine(javafx.scene.transform.Affine) IAction(org.eclipse.jface.action.IAction) ErrorDialog(org.eclipse.jface.dialogs.ErrorDialog) DefaultColoringService(org.osate.ge.internal.services.impl.DefaultColoringService) AgeGefRuntimeException(org.osate.ge.gef.AgeGefRuntimeException) ModificationsCompletedEvent(org.osate.ge.internal.diagram.runtime.ModificationsCompletedEvent) ActionService(org.osate.ge.internal.services.ActionService) IncrementalProjectBuilder(org.eclipse.core.resources.IncrementalProjectBuilder) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) InfiniteCanvas(org.eclipse.gef.fx.nodes.InfiniteCanvas) ISelectionListener(org.eclipse.ui.ISelectionListener) DiagramModificationListener(org.osate.ge.internal.diagram.runtime.DiagramModificationListener) SimplePaletteItem(org.osate.ge.gef.palette.SimplePaletteItem) BusinessObjectNodeFactory(org.osate.ge.internal.diagram.runtime.updating.BusinessObjectNodeFactory) DefaultReferenceResolutionService(org.osate.ge.services.impl.DefaultReferenceResolutionService) Bundle(org.osgi.framework.Bundle) ActionExecutor(org.osate.ge.internal.services.ActionExecutor) UndoRedoActionGroup(org.eclipse.ui.operations.UndoRedoActionGroup) RegisteredImage(org.osate.ge.internal.services.ExtensionRegistryService.RegisteredImage) Collection(java.util.Collection) Display(org.eclipse.swt.widgets.Display) DiagramSerialization(org.osate.ge.internal.diagram.runtime.DiagramSerialization) Collectors(java.util.stream.Collectors) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Objects(java.util.Objects) DefaultActionService(org.osate.ge.internal.services.impl.DefaultActionService) List(java.util.List) DeactivatedEvent(org.osate.ge.internal.ui.tools.DeactivatedEvent) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) Optional(java.util.Optional) ISelection(org.eclipse.jface.viewers.ISelection) DiagramEditorNode(org.osate.ge.gef.DiagramEditorNode) DefaultDiagramElementGraphicalConfigurationProvider(org.osate.ge.internal.diagram.runtime.updating.DefaultDiagramElementGraphicalConfigurationProvider) SystemInstanceLoadingService(org.osate.ge.internal.services.SystemInstanceLoadingService) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) CopyAction(org.osate.ge.internal.ui.editor.actions.CopyAction) EclipseContextFactory(org.eclipse.e4.core.contexts.EclipseContextFactory) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Scene(javafx.scene.Scene) IOperationHistory(org.eclipse.core.commands.operations.IOperationHistory) ProjectReferenceService(org.osate.ge.internal.services.ProjectReferenceService) MouseEvent(javafx.scene.input.MouseEvent) HashMap(java.util.HashMap) DoubleProperty(javafx.beans.property.DoubleProperty) DiagramContextChecker(org.osate.ge.internal.ui.editor.DiagramContextChecker) ColoringService(org.osate.ge.internal.services.ColoringService) ImmutableList(com.google.common.collect.ImmutableList) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) DiagramUpdater(org.osate.ge.internal.diagram.runtime.updating.DiagramUpdater) ActivatedEvent(org.osate.ge.internal.ui.tools.ActivatedEvent) IWorkbenchSite(org.eclipse.ui.IWorkbenchSite) ActivateAgeEditorAction(org.osate.ge.internal.ui.editor.ActivateAgeEditorAction) DefaultBusinessObjectTreeUpdater(org.osate.ge.internal.diagram.runtime.updating.DefaultBusinessObjectTreeUpdater) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) AgeDiagramUtil(org.osate.ge.internal.diagram.runtime.AgeDiagramUtil) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ExecutionMode(org.osate.ge.internal.services.ActionExecutor.ExecutionMode) AgeHandlerUtil(org.osate.ge.internal.ui.handlers.AgeHandlerUtil) IResource(org.eclipse.core.resources.IResource) Menu(org.eclipse.swt.widgets.Menu) FocusAdapter(org.eclipse.swt.events.FocusAdapter) PasteAction(org.osate.ge.internal.ui.editor.actions.PasteAction) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) Group(javafx.scene.Group) UndoRedoActionGroup(org.eclipse.ui.operations.UndoRedoActionGroup) FocusAdapter(org.eclipse.swt.events.FocusAdapter) ActivateAgeEditorAction(org.osate.ge.internal.ui.editor.ActivateAgeEditorAction) RegisteredImage(org.osate.ge.internal.services.ExtensionRegistryService.RegisteredImage) KeyAdapter(org.eclipse.swt.events.KeyAdapter) DiagramEditorNode(org.osate.ge.gef.DiagramEditorNode) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) Node(javafx.scene.Node) DiagramEditorNode(org.osate.ge.gef.DiagramEditorNode) Image(javafx.scene.image.Image) RegisteredImage(org.osate.ge.internal.services.ExtensionRegistryService.RegisteredImage) SimplePaletteItem(org.osate.ge.gef.palette.SimplePaletteItem) Cursor(javafx.scene.Cursor) FocusEvent(org.eclipse.swt.events.FocusEvent) URI(org.eclipse.emf.common.util.URI) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) InfiniteCanvas(org.eclipse.gef.fx.nodes.InfiniteCanvas) IOperationHistory(org.eclipse.core.commands.operations.IOperationHistory) Menu(org.eclipse.swt.widgets.Menu) FXCanvas(javafx.embed.swt.FXCanvas) Scene(javafx.scene.Scene) Overlays(org.osate.ge.gef.ui.editor.overlays.Overlays) GefAgeDiagram(org.osate.ge.gef.ui.diagram.GefAgeDiagram) MenuManager(org.eclipse.jface.action.MenuManager)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 DoubleProperty (javafx.beans.property.DoubleProperty)1 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)1 FXCanvas (javafx.embed.swt.FXCanvas)1 EventHandler (javafx.event.EventHandler)1 Bounds (javafx.geometry.Bounds)1 Cursor (javafx.scene.Cursor)1 Group (javafx.scene.Group)1 Node (javafx.scene.Node)1 Scene (javafx.scene.Scene)1 Image (javafx.scene.image.Image)1