Search in sources :

Example 1 with EditableEmbeddedTextValue

use of org.osate.ge.ba.ui.properties.EditableEmbeddedTextValue 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)

Aggregations

Objects (java.util.Objects)1 Optional (java.util.Optional)1 Adapters (org.eclipse.core.runtime.Adapters)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 NamedElement (org.osate.aadl2.NamedElement)1 EditableEmbeddedTextValue (org.osate.ge.ba.ui.properties.EditableEmbeddedTextValue)1 BehaviorAnnexSelectionUtil (org.osate.ge.ba.util.BehaviorAnnexSelectionUtil)1