Search in sources :

Example 1 with ModelChangeNotifier

use of org.osate.ge.internal.services.ModelChangeNotifier in project osate2 by osate.

the class EmbeddedTextEditor method createControls.

private void createControls(final GridData styledTextLayoutData) {
    // Create styled text
    styledText = new StyledText(this, styledTextStyle);
    styledText.setEditable(false);
    // Set empty caret so that the caret will not show.
    // Makes sure users know it is not editable.
    // Note: If caret is set to null, exception may occur
    // when used with the StyledTextXtextAdapter.
    final Caret emptyCaret = new Caret(getShell(), SWT.NONE);
    styledText.setCaret(emptyCaret);
    styledText.setLayoutData(styledTextLayoutData);
    styledText.addDisposeListener(e -> emptyCaret.dispose());
    editBtn = new Button(this, SWT.PUSH);
    editBtn.setText("Edit...");
    editBtn.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
        final EditableEmbeddedTextValue embeddedTextValue = xtextAdapter.getEmbeddedTextValue();
        final NamedElement ne = embeddedTextValue.getElementToModify();
        final EditEmbeddedTextDialog dlg = new EditEmbeddedTextDialog(Display.getCurrent().getActiveShell(), embeddedTextValue, styledTextStyle, styledTextLayoutData);
        if (dlg.open() == Window.OK) {
            // Edit condition
            BehaviorAnnexSelectionUtil.getActiveEditor().ifPresent(editorPart -> {
                final ActionService actionService = Adapters.adapt(editorPart, ActionService.class);
                final ModelChangeNotifier modelChangeNotifier = Objects.requireNonNull(editorPart.getAdapter(ModelChangeNotifier.class), "Unable to get model change notifier");
                final IXtextDocument xtextDocument = getXtextDocument(ne).orElse(null);
                if (xtextDocument != null) {
                    // Execute modification with xtext document
                    actionService.execute(embeddedTextValue.getModificationLabel(), ExecutionMode.NORMAL, new EmbeddedTextModificationAction(xtextDocument, modelChangeNotifier, dlg.getResult().getFullSource()));
                } else {
                    final XtextResource xtextResource = getXtextResource(ne).orElseThrow();
                    embeddedTextValue.setEditableText(dlg.getResult().getPartialSource());
                    // Execute modification with xtext resource
                    actionService.execute(embeddedTextValue.getModificationLabel(), ExecutionMode.NORMAL, new EmbeddedTextModificationAction(xtextResource, modelChangeNotifier, embeddedTextValue));
                }
            });
        }
    }));
}
Also used : SwtUtil(org.osate.ge.swt.SwtUtil) StyledText(org.eclipse.swt.custom.StyledText) ActionService(org.osate.ge.internal.services.ActionService) ModelChangeNotifier(org.osate.ge.internal.services.ModelChangeNotifier) Composite(org.eclipse.swt.widgets.Composite) AgeXtextUtil(org.osate.ge.internal.ui.xtext.AgeXtextUtil) GridData(org.eclipse.swt.layout.GridData) BehaviorAnnexSelectionUtil(org.osate.ge.ba.util.BehaviorAnnexSelectionUtil) XtextResource(org.eclipse.xtext.resource.XtextResource) Button(org.eclipse.swt.widgets.Button) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) Caret(org.eclipse.swt.widgets.Caret) Display(org.eclipse.swt.widgets.Display) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) Objects(java.util.Objects) Adapters(org.eclipse.core.runtime.Adapters) Window(org.eclipse.jface.window.Window) EditableEmbeddedTextValue(org.osate.ge.ba.ui.properties.EditableEmbeddedTextValue) ExecutionMode(org.osate.ge.internal.services.ActionExecutor.ExecutionMode) SWT(org.eclipse.swt.SWT) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) NamedElement(org.osate.aadl2.NamedElement) SelectionListener(org.eclipse.swt.events.SelectionListener) StyledText(org.eclipse.swt.custom.StyledText) Button(org.eclipse.swt.widgets.Button) ModelChangeNotifier(org.osate.ge.internal.services.ModelChangeNotifier) XtextResource(org.eclipse.xtext.resource.XtextResource) EditableEmbeddedTextValue(org.osate.ge.ba.ui.properties.EditableEmbeddedTextValue) NamedElement(org.osate.aadl2.NamedElement) Caret(org.eclipse.swt.widgets.Caret) ActionService(org.osate.ge.internal.services.ActionService) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 2 with ModelChangeNotifier

use of org.osate.ge.internal.services.ModelChangeNotifier 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)

Aggregations

ModelChangeNotifier (org.osate.ge.internal.services.ModelChangeNotifier)2 Objects (java.util.Objects)1 Optional (java.util.Optional)1 IProject (org.eclipse.core.resources.IProject)1 Adapters (org.eclipse.core.runtime.Adapters)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 GridDataFactory (org.eclipse.jface.layout.GridDataFactory)1 GridLayoutFactory (org.eclipse.jface.layout.GridLayoutFactory)1 Window (org.eclipse.jface.window.Window)1 SWT (org.eclipse.swt.SWT)1 StyledText (org.eclipse.swt.custom.StyledText)1 SelectionListener (org.eclipse.swt.events.SelectionListener)1 GridData (org.eclipse.swt.layout.GridData)1 Button (org.eclipse.swt.widgets.Button)1 Caret (org.eclipse.swt.widgets.Caret)1 Composite (org.eclipse.swt.widgets.Composite)1 Display (org.eclipse.swt.widgets.Display)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1