Search in sources :

Example 6 with BusinessObjectHandler

use of org.osate.ge.businessobjecthandling.BusinessObjectHandler in project osate2 by osate.

the class AgeContentOutlinePage method createControl.

@Override
public void createControl(final Composite parent) {
    super.createControl(parent);
    final TreeViewer viewer = getTreeViewer();
    ContextHelpUtil.setHelp(viewer.getControl(), ContextHelpUtil.OUTLINE_VIEW);
    // A comparator is set to allow comparing tree elements of different types in a way where they will be equal if the relative reference is equal.
    // This is needed so that tree node will be preserved when elements are hidden and shown and the underlying object type changes.
    // If link with editor is enabled, selection may not be retained.
    viewer.setComparer(new IElementComparer() {

        @Override
        public int hashCode(final Object element) {
            if (element == null) {
                return 0;
            }
            return Objects.hashCode(getRelativeReferenceForElement(element)) + hashCode(getElementParent(element));
        }

        @Override
        public boolean equals(final Object element1, final Object element2) {
            final Object ref1 = getRelativeReferenceForElement(element1);
            final Object ref2 = getRelativeReferenceForElement(element2);
            final boolean referencesAreEqual = Objects.equals(ref1, ref2);
            if (!referencesAreEqual) {
                return false;
            }
            if (element1 == null || element2 == null) {
                return element1 == element2;
            }
            return equals(getElementParent(element1), getElementParent(element2));
        }
    });
    viewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {
        }

        @Override
        public Object[] getElements(final Object inputElement) {
            if (inputElement instanceof InternalDiagramEditor) {
                final InternalDiagramEditor editor = (InternalDiagramEditor) inputElement;
                return getChildren(editor.getDiagram());
            }
            return new BusinessObjectContext[0];
        }

        @Override
        public Object[] getChildren(final Object parentElement) {
            if (parentElement instanceof BusinessObjectContext) {
                final BusinessObjectContext parent = ((BusinessObjectContext) parentElement);
                final List<BusinessObjectContext> children = new ArrayList<>();
                // DiagramNodes represent elements which are part of the diagram
                if (parent instanceof DiagramNode) {
                    final DiagramNode parentNode = (DiagramNode) parent;
                    // Add child diagram nodes
                    parentNode.getChildren().stream().filter((de) -> !Strings.isNullOrEmpty(de.getUserInterfaceName()) || de.getBusinessObject() instanceof EObject).forEach(children::add);
                    // Add children which are hidden based on user preference
                    if (showHiddenElementsAction.isChecked()) {
                        // If the diagram is a contextless diagram, create a business object context which uses the current project as the business object
                        final BusinessObjectContext parentForRetrieval;
                        if (parentElement instanceof AgeDiagram && ((AgeDiagram) parentElement).getConfiguration().getContextBoReference() == null) {
                            parentForRetrieval = new BusinessObjectContext() {

                                @Override
                                public Collection<? extends BusinessObjectContext> getChildren() {
                                    return parentNode.getChildren();
                                }

                                @Override
                                public BusinessObjectContext getParent() {
                                    return parent.getParent();
                                }

                                @Override
                                public Object getBusinessObject() {
                                    return projectProvider.getProject();
                                }
                            };
                        } else {
                            parentForRetrieval = parent;
                        }
                        // Add children based on the objects returns by the business object provider for business objects which are not currently in the
                        // diagram.
                        getChildContextsFromProvider(parent, parentForRetrieval, childRef -> {
                            return !children.stream().map(AgeContentOutlinePage.this::getRelativeReferenceForElement).anyMatch(childRef::equals);
                        }).filter(this::includeHiddenBusinessObjectContext).forEachOrdered(children::add);
                    }
                } else if (parent instanceof BusinessObjectContext) {
                    // Add children which are hidden based on user preference
                    if (showHiddenElementsAction.isChecked()) {
                        getChildContextsFromProvider(parent, parent, childRef -> true).filter(this::includeHiddenBusinessObjectContext).forEachOrdered(children::add);
                    }
                }
                return children.toArray();
            }
            return new BusinessObjectContext[0];
        }

        /**
         * Filter returns whether a hidden business object context should be shown. This is implemented to be consistent with
         * handling for diagram elements.
         */
        private boolean includeHiddenBusinessObjectContext(final BusinessObjectContext boc) {
            final Object bo = boc.getBusinessObject();
            if (bo instanceof EObject) {
                return true;
            }
            final BusinessObjectHandler boh = extRegistry.getApplicableBusinessObjectHandler(bo);
            return boh != null && !Strings.isNullOrEmpty(boh.getName(new GetNameContext(bo)));
        }

        /**
         * Creates a stream of business object contexts representing the children returned by the business object provider.
         * Such contexts do not have a valid isChildren() method.
         * @param parent is the context to use as the parent of returned contexts. This should be the DiagramNode if one exists.
         * @param parentForRetrieval is the context to use when requesting children from the business object provider
         * @param filterPredicate is a filter that can be used to filter results by the relative reference before the context is created
         * @return A stream of child business object contexts
         */
        private Stream<BusinessObjectContext> getChildContextsFromProvider(final BusinessObjectContext parent, final BusinessObjectContext parentForRetrieval, final Predicate<RelativeBusinessObjectReference> filterPredicate) {
            return bopHelper.getChildBusinessObjects(parentForRetrieval).stream().map(childBo -> {
                if (childBo instanceof BusinessObjectProxy) {
                    return ((BusinessObjectProxy) childBo).resolve(referenceService);
                } else {
                    return childBo;
                }
            }).filter(Objects::nonNull).filter(childBo -> {
                final RelativeBusinessObjectReference childRef = referenceService.getRelativeReference(childBo);
                return childRef != null && filterPredicate.test(childRef);
            }).map(childBo -> new BusinessObjectContext() {

                @Override
                public Collection<? extends BusinessObjectContext> getChildren() {
                    // Returns an empty list. Shouldn't be needed. All children are hidden and such children will be provided by the content provider.
                    return Collections.emptyList();
                }

                @Override
                public BusinessObjectContext getParent() {
                    return parent;
                }

                @Override
                public Object getBusinessObject() {
                    return childBo;
                }
            });
        }

        @Override
        public Object getParent(final Object element) {
            return getElementParent(element);
        }

        @Override
        public boolean hasChildren(final Object element) {
            if (element instanceof BusinessObjectContext) {
                return getChildren(element).length > 0;
            }
            return false;
        }
    });
    // Create a label provider that will be used by the tree's actual label provider which will be a StyledCellLabelProvider.
    // This label provider is also used by the custom comparator because the default one does not support text provided by the styled cell label provider.
    final LabelProvider innerLabelProvider = new LabelProvider() {

        @Override
        public String getText(final Object element) {
            if (element instanceof BusinessObjectContext) {
                final BusinessObjectContext boc = (BusinessObjectContext) element;
                return UiUtil.getDescription(boc, extRegistry);
            }
            return super.getText(element);
        }

        @Override
        public Image getImage(final Object element) {
            if (element instanceof DiagramElement) {
                final DiagramElement de = (DiagramElement) element;
                return UiUtil.getImage(de.getBusinessObjectHandler(), de.getBusinessObject()).orElse(null);
            } else if (element instanceof BusinessObjectContext) {
                final BusinessObjectContext boc = (BusinessObjectContext) element;
                final Object bo = boc.getBusinessObject();
                return UiUtil.getImage(extRegistry, bo).orElse(null);
            }
            return null;
        }
    };
    viewer.setLabelProvider(new StyledCellLabelProvider(StyledCellLabelProvider.COLORS_ON_SELECTION) {

        @Override
        public void update(final ViewerCell cell) {
            final Object element = cell.getElement();
            cell.setText(innerLabelProvider.getText(element));
            cell.setForeground(element instanceof DiagramNode ? null : Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
            cell.setImage(innerLabelProvider.getImage(element));
        }
    });
    viewer.setComparator(new ViewerComparator() {

        @Override
        public int compare(final Viewer viewer, final Object e1, final Object e2) {
            final String t1 = innerLabelProvider.getText(e1);
            final String t2 = innerLabelProvider.getText(e2);
            return getComparator().compare(t1, t2);
        }
    });
    viewer.addDoubleClickListener(event -> UiUtil.openPropertiesView());
    final MenuManager menuMgr = new MenuManager();
    menuMgr.setRemoveAllWhenShown(true);
    final Tree tree = viewer.getTree();
    final Menu menu = menuMgr.createContextMenu(tree);
    tree.setMenu(menu);
    // Allow contributions
    getSite().registerContextMenu("org.osate.ge.editor.AgeDiagramEditor", menuMgr, viewer);
    editor.addSelectionChangedListener(event -> updateOutlineSelectionIfLinked());
    editor.getDiagram().addModificationListener(diagramModificationListener);
    viewer.addSelectionChangedListener(this);
    viewer.setInput(editor);
}
Also used : BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) ContentOutlinePage(org.eclipse.ui.views.contentoutline.ContentOutlinePage) IStatusLineManager(org.eclipse.jface.action.IStatusLineManager) IToolBarManager(org.eclipse.jface.action.IToolBarManager) ModificationsCompletedEvent(org.osate.ge.internal.diagram.runtime.ModificationsCompletedEvent) TreePath(org.eclipse.jface.viewers.TreePath) BusinessObjectContext(org.osate.ge.BusinessObjectContext) ViewerCell(org.eclipse.jface.viewers.ViewerCell) Composite(org.eclipse.swt.widgets.Composite) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) DiagramModificationListener(org.osate.ge.internal.diagram.runtime.DiagramModificationListener) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) ProjectProvider(org.osate.ge.internal.services.ProjectProvider) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) PlatformUI(org.eclipse.ui.PlatformUI) Predicate(java.util.function.Predicate) Collection(java.util.Collection) MenuManager(org.eclipse.jface.action.MenuManager) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) Display(org.eclipse.swt.widgets.Display) ContextHelpUtil(org.osate.ge.internal.ui.util.ContextHelpUtil) BusinessObjectProxy(org.osate.ge.internal.model.BusinessObjectProxy) Objects(java.util.Objects) IElementComparer(org.eclipse.jface.viewers.IElementComparer) ISharedImages(org.eclipse.ui.ISharedImages) List(java.util.List) Stream(java.util.stream.Stream) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) UiUtil(org.osate.ge.internal.ui.util.UiUtil) Tree(org.eclipse.swt.widgets.Tree) IPageSite(org.eclipse.ui.part.IPageSite) SWT(org.eclipse.swt.SWT) TreeViewer(org.eclipse.jface.viewers.TreeViewer) BusinessObjectProviderHelper(org.osate.ge.internal.util.BusinessObjectProviderHelper) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) ProjectReferenceService(org.osate.ge.internal.services.ProjectReferenceService) Image(org.eclipse.swt.graphics.Image) ExtensionRegistryService(org.osate.ge.internal.services.ExtensionRegistryService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) TreeSelection(org.eclipse.jface.viewers.TreeSelection) GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) Activator(org.osate.ge.internal.Activator) IPreferenceChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener) Viewer(org.eclipse.jface.viewers.Viewer) Action(org.eclipse.jface.action.Action) IActionBars(org.eclipse.ui.IActionBars) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) IMenuManager(org.eclipse.jface.action.IMenuManager) ActionFactory(org.eclipse.ui.actions.ActionFactory) DiagramModificationAdapter(org.osate.ge.internal.diagram.runtime.DiagramModificationAdapter) Menu(org.eclipse.swt.widgets.Menu) Collections(java.util.Collections) LabelProvider(org.eclipse.jface.viewers.LabelProvider) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) TreeViewer(org.eclipse.jface.viewers.TreeViewer) IElementComparer(org.eclipse.jface.viewers.IElementComparer) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) BusinessObjectProxy(org.osate.ge.internal.model.BusinessObjectProxy) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) EObject(org.eclipse.emf.ecore.EObject) Tree(org.eclipse.swt.widgets.Tree) List(java.util.List) ArrayList(java.util.ArrayList) Stream(java.util.stream.Stream) Menu(org.eclipse.swt.widgets.Menu) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) ViewerCell(org.eclipse.jface.viewers.ViewerCell) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) EObject(org.eclipse.emf.ecore.EObject) BusinessObjectContext(org.osate.ge.BusinessObjectContext) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Example 7 with BusinessObjectHandler

use of org.osate.ge.businessobjecthandling.BusinessObjectHandler 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));
    }
}
Also used : ActionExecutor(org.osate.ge.internal.services.ActionExecutor) ModelChangeNotifier(org.osate.ge.internal.services.ModelChangeNotifier) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) IProject(org.eclipse.core.resources.IProject) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) UpdatedReferenceValueProvider(org.osate.ge.internal.services.DiagramService.UpdatedReferenceValueProvider) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) EObject(org.eclipse.emf.ecore.EObject) LtkRenameAction(org.osate.ge.internal.ui.LtkRenameAction) DiagramService(org.osate.ge.internal.services.DiagramService) ReferenceCollection(org.osate.ge.internal.services.DiagramService.ReferenceCollection) ProjectProvider(org.osate.ge.internal.services.ProjectProvider)

Example 8 with BusinessObjectHandler

use of org.osate.ge.businessobjecthandling.BusinessObjectHandler in project osate2 by osate.

the class UiUtil method getDescription.

/**
 * Returns a user friendly description for a business object context. This method determines the appropriate business object handler
 * and uses it to to retrieve the name.
 * This method adds descriptive prefixes in some cases.
 * It is advised to use other methods when working with diagram elements in order to avoid calling the business object handlers.
 * @param boc the business object context for which to retrieve the description
 * @param extService the extension registry
 * @return the description of the business object context
 */
public static String getDescription(final BusinessObjectContext boc, final ExtensionRegistryService extService) {
    // Build a prefix based on the business object type
    final Object bo = boc.getBusinessObject();
    final String prefix;
    if (bo instanceof EObject) {
        prefix = StringUtil.camelCaseToUser(((EObject) bo).eClass().getName()) + " ";
    } else {
        prefix = "";
    }
    // Call the business object handler's GetName method
    String baseName;
    if (bo instanceof BusinessObjectProxy) {
        baseName = ((BusinessObjectProxy) bo).getName();
    } else {
        final BusinessObjectHandler boh = extService.getApplicableBusinessObjectHandler(bo);
        if (boh == null) {
            baseName = null;
        } else {
            baseName = boh.getName(new GetNameContext(bo));
        }
    }
    if (Strings.isNullOrEmpty(baseName)) {
        final String typeName = StringUtil.camelCaseToUser(bo instanceof EObject ? ((EObject) bo).eClass().getName() : bo.getClass().getName());
        return typeName;
    }
    return prefix + baseName;
}
Also used : GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) BusinessObjectProxy(org.osate.ge.internal.model.BusinessObjectProxy)

Aggregations

BusinessObjectHandler (org.osate.ge.businessobjecthandling.BusinessObjectHandler)8 EObject (org.eclipse.emf.ecore.EObject)5 RelativeBusinessObjectReference (org.osate.ge.RelativeBusinessObjectReference)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 CanonicalBusinessObjectReference (org.osate.ge.CanonicalBusinessObjectReference)2 GetNameContext (org.osate.ge.businessobjecthandling.GetNameContext)2 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)2 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)2 DiagramNode (org.osate.ge.internal.diagram.runtime.DiagramNode)2 BusinessObjectProxy (org.osate.ge.internal.model.BusinessObjectProxy)2 EmbeddedBusinessObject (org.osate.ge.internal.model.EmbeddedBusinessObject)2 ActionExecutor (org.osate.ge.internal.services.ActionExecutor)2 ProjectProvider (org.osate.ge.internal.services.ProjectProvider)2