Search in sources :

Example 1 with IImportAsModelDialog

use of org.whole.lang.ui.dialogs.IImportAsModelDialog in project whole by wholeplatform.

the class HandlersBehavior method pasteAs.

public static void pasteAs(IBindingManager bm) {
    IEntityPartViewer viewer = (IEntityPartViewer) bm.wGetValue("viewer");
    IEntity focusEntity = bm.wGet("focusEntity");
    RunnableWithResult<IImportAsModelDialog> dialogRunnable = RunnableWithResult.create(() -> {
        Shell shell = viewer.getControl().getShell();
        IEclipseContext eclipseContext = (IEclipseContext) bm.wGetValue("eclipse#eclipseContext");
        IImportAsModelDialog dialog = eclipseContext.get(IImportAsModelDialogFactory.class).createImplicitElementImportAsModelDialog(shell, "Paste As", EntityUtils.isComposite(focusEntity));
        dialog.show();
        return dialog;
    });
    IImportAsModelDialog dialog = E4Utils.syncExec(bm, dialogRunnable).get();
    if (!dialog.isConfirmed())
        return;
    IPersistenceKit persistenceKit = dialog.getPersistenceKit();
    RunnableWithResult<IEntity> entityRunnable = RunnableWithResult.create(() -> {
        try {
            return ClipboardUtils.parseClipboardContents(persistenceKit, bm);
        } catch (Exception e) {
            IEclipseContext context = (IEclipseContext) bm.wGetValue("eclipse#eclipseContext");
            E4Utils.reportError(context, "Write Model errors", "Parse failed using the selected persistence.", e);
            return CommonsEntityFactory.instance.createResolver();
        }
    });
    IEntity entity = E4Utils.syncExec(bm, entityRunnable).get();
    boolean adding = dialog.isForceAdding();
    IEntityIterator<IEntity> iterator;
    if (bm.wIsSet("syntheticRoot")) {
        IEntity syntheticRoot = bm.wGet("syntheticRoot");
        adding |= syntheticRoot.wSize() > 1;
        if (adding && !EntityUtils.isComposite(focusEntity)) {
            adding = false;
            iterator = IteratorFactory.selfIterator();
        } else
            iterator = IteratorFactory.childReverseIterator();
        iterator.reset(syntheticRoot);
    } else {
        iterator = IteratorFactory.selfIterator();
        iterator.reset(entity);
    }
    EntityDescriptor<?> stage = dialog.getStage();
    while (iterator.hasNext()) {
        IEntity clipboardEntity = EntityUtils.clone(iterator.next());
        if (!adding) {
            if (!CommonsEntityDescriptorEnum.SameStageFragment.equals(stage) || !EntityUtils.isReplaceable(focusEntity, clipboardEntity))
                clipboardEntity = CommonsEntityFactory.instance.create(stage, clipboardEntity);
            IEntity parent = focusEntity.wGetParent();
            parent.wSet(focusEntity, clipboardEntity);
            break;
        } else {
            if (!CommonsEntityDescriptorEnum.SameStageFragment.equals(stage) || !EntityUtils.isAddable(focusEntity, clipboardEntity))
                clipboardEntity = CommonsEntityFactory.instance.create(stage, clipboardEntity);
            if (bm.wIsSet("hilightPosition"))
                focusEntity.wAdd(bm.wIntValue("hilightPosition"), clipboardEntity);
            else
                focusEntity.wAdd(clipboardEntity);
        }
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) IImportAsModelDialogFactory(org.whole.lang.ui.dialogs.IImportAsModelDialogFactory) OperationCanceledException(org.whole.lang.operations.OperationCanceledException) Shell(org.eclipse.swt.widgets.Shell) IImportAsModelDialog(org.whole.lang.ui.dialogs.IImportAsModelDialog) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IPersistenceKit(org.whole.lang.codebase.IPersistenceKit) IEntityPartViewer(org.whole.lang.ui.viewers.IEntityPartViewer)

Example 2 with IImportAsModelDialog

use of org.whole.lang.ui.dialogs.IImportAsModelDialog in project whole by wholeplatform.

the class HandlersBehavior method importEntity.

public static void importEntity(IBindingManager bm) {
    IEntityPartViewer viewer = (IEntityPartViewer) bm.wGetValue("viewer");
    IEntity focusEntity = bm.wGet("focusEntity");
    RunnableWithResult<IImportAsModelDialog> runnable = RunnableWithResult.create(() -> {
        Shell shell = viewer.getControl().getShell();
        IEclipseContext eclipseContext = (IEclipseContext) bm.wGetValue("eclipse#eclipseContext");
        IImportAsModelDialog dialog = eclipseContext.get(IImportAsModelDialogFactory.class).createImportAsModelDialog(shell, "Import model", EntityUtils.isComposite(focusEntity));
        dialog.show();
        return dialog;
    });
    E4Utils.syncExec(bm, runnable);
    IImportAsModelDialog dialog = runnable.get();
    if (!dialog.isConfirmed())
        return;
    Object[] files = dialog.getSelection();
    IPersistenceKit persistenceKit = dialog.getPersistenceKit();
    EntityDescriptor<?> stage = dialog.getStage();
    boolean adding = dialog.isForceAdding() || files.length > 1;
    for (int i = files.length - 1; i >= 0; i--) {
        IPersistenceProvider pp = new IFilePersistenceProvider((IFile) files[i]);
        try {
            IEntity importedEntity = persistenceKit.readModel(pp);
            if (!adding) {
                if (!CommonsEntityDescriptorEnum.SameStageFragment.equals(stage) || !EntityUtils.isReplaceable(focusEntity, importedEntity))
                    importedEntity = CommonsEntityFactory.instance.create(stage, importedEntity);
                IEntity parent = focusEntity.wGetParent();
                parent.wSet(focusEntity, importedEntity);
                break;
            } else {
                if (!CommonsEntityDescriptorEnum.SameStageFragment.equals(stage) || !EntityUtils.isAddable(focusEntity, importedEntity))
                    importedEntity = CommonsEntityFactory.instance.create(stage, importedEntity);
                if (bm.wIsSet("hilightPosition"))
                    focusEntity.wAdd(bm.wIntValue("hilightPosition"), importedEntity);
                else
                    focusEntity.wAdd(importedEntity);
            }
        } catch (Exception e) {
        // fail silently
        }
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) IImportAsModelDialogFactory(org.whole.lang.ui.dialogs.IImportAsModelDialogFactory) OperationCanceledException(org.whole.lang.operations.OperationCanceledException) Shell(org.eclipse.swt.widgets.Shell) IImportAsModelDialog(org.whole.lang.ui.dialogs.IImportAsModelDialog) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IFilePersistenceProvider(org.whole.lang.codebase.IFilePersistenceProvider) IPersistenceProvider(org.whole.lang.codebase.IPersistenceProvider) IPersistenceKit(org.whole.lang.codebase.IPersistenceKit) IEntityPartViewer(org.whole.lang.ui.viewers.IEntityPartViewer)

Example 3 with IImportAsModelDialog

use of org.whole.lang.ui.dialogs.IImportAsModelDialog in project whole by wholeplatform.

the class TextTransferDropTargetListener method getCommand.

@Override
protected Command getCommand() {
    String text = (String) getCurrentEvent().data;
    Shell shell = getViewer().getControl().getShell();
    IImportAsModelDialog dialog = factory.createImplicitElementImportAsModelDialog(shell, "Drop As", false);
    if (!dialog.show())
        return null;
    IPersistenceKit persistenceKit = dialog.getPersistenceKit();
    EntityDescriptor<?> stage = dialog.getStage();
    try {
        IBindingManager bm = BindingManagerFactory.instance.createBindingManager();
        bm.wDefValue("parseFragments", true);
        IEntity entity = persistenceKit.readModel(new StringPersistenceProvider(text, bm));
        // FIXME workaround for selections with multiple entities
        if (EntityUtils.isTuple(entity))
            bm.wDef("syntheticRoot", entity);
        boolean hasSyntheticRoot = bm.wIsSet("syntheticRoot");
        IEntityIterator<IEntity> iterator;
        boolean needsCompositeTarget = false;
        if (hasSyntheticRoot) {
            IEntity syntheticRoot = bm.wGet("syntheticRoot");
            iterator = IteratorFactory.childIterator();
            iterator.reset(syntheticRoot);
            needsCompositeTarget = syntheticRoot.wSize() > 1;
        } else {
            iterator = IteratorFactory.selfIterator();
            iterator.reset(entity);
        }
        List<IEntityPart> editParts = new ArrayList<IEntityPart>();
        while (iterator.hasNext()) {
            IEntity stageEntity = EntityUtils.clone(iterator.next());
            stageEntity = ClipboardUtils.conditionalStageAdd(getTargetEditPart(), stage, stageEntity, needsCompositeTarget);
            editParts.add(ClipboardUtils.createEditPart(getViewer().getEditPartFactory(), stageEntity));
        }
        ChangeBoundsRequest request = (ChangeBoundsRequest) getTargetRequest();
        request.setEditParts(editParts);
        return super.getCommand();
    } catch (Exception e) {
        return null;
    }
}
Also used : StringPersistenceProvider(org.whole.lang.codebase.StringPersistenceProvider) IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList) ChangeBoundsRequest(org.eclipse.gef.requests.ChangeBoundsRequest) Shell(org.eclipse.swt.widgets.Shell) IImportAsModelDialog(org.whole.lang.ui.dialogs.IImportAsModelDialog) IBindingManager(org.whole.lang.bindings.IBindingManager) IPersistenceKit(org.whole.lang.codebase.IPersistenceKit) IEntityPart(org.whole.lang.ui.editparts.IEntityPart)

Example 4 with IImportAsModelDialog

use of org.whole.lang.ui.dialogs.IImportAsModelDialog in project whole by wholeplatform.

the class FileTransferDropTargetListener method getCommand.

@Override
protected Command getCommand() {
    String[] filePaths = (String[]) getCurrentEvent().data;
    List<EditPart> editParts = new ArrayList<EditPart>();
    Shell shell = getViewer().getControl().getShell();
    IImportAsModelDialog dialog = factory.createElementListImportAsModelDialog(shell, "DnD Import", false);
    String[] fileNames = new String[filePaths.length];
    for (int i = 0; i < fileNames.length; i++) fileNames[i] = filePaths[i].substring(filePaths[i].lastIndexOf(File.separatorChar) + 1);
    dialog.setSelection(fileNames);
    if (!dialog.show())
        return null;
    IPersistenceKit persistenceKit = dialog.getPersistenceKit();
    EntityDescriptor<?> stage = dialog.getStage();
    boolean needsCompositeTarget = filePaths.length > 1;
    for (String filePath : filePaths) {
        try {
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            File file = new File(filePath);
            IFile[] files = workspace.getRoot().findFilesForLocationURI(file.toURI());
            IPersistenceProvider persistenceProvider = (files.length == 1) ? new IFilePersistenceProvider(files[0]) : new FilePersistenceProvider(file);
            IEntity entity = persistenceKit.readModel(persistenceProvider);
            entity = ClipboardUtils.conditionalStageAdd(getTargetEditPart(), stage, entity, needsCompositeTarget);
            editParts.add(ClipboardUtils.createEditPart(getViewer().getEditPartFactory(), entity));
        } catch (Exception e) {
        // fail silently
        }
    }
    if (!editParts.isEmpty()) {
        ChangeBoundsRequest request = (ChangeBoundsRequest) getTargetRequest();
        request.setEditParts(editParts);
    } else
        return null;
    return super.getCommand();
}
Also used : IFile(org.eclipse.core.resources.IFile) IEntity(org.whole.lang.model.IEntity) EditPart(org.eclipse.gef.EditPart) ArrayList(java.util.ArrayList) ChangeBoundsRequest(org.eclipse.gef.requests.ChangeBoundsRequest) Shell(org.eclipse.swt.widgets.Shell) IImportAsModelDialog(org.whole.lang.ui.dialogs.IImportAsModelDialog) FilePersistenceProvider(org.whole.lang.codebase.FilePersistenceProvider) IFilePersistenceProvider(org.whole.lang.codebase.IFilePersistenceProvider) IWorkspace(org.eclipse.core.resources.IWorkspace) IFilePersistenceProvider(org.whole.lang.codebase.IFilePersistenceProvider) IPersistenceProvider(org.whole.lang.codebase.IPersistenceProvider) IPersistenceKit(org.whole.lang.codebase.IPersistenceKit) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

Shell (org.eclipse.swt.widgets.Shell)4 IPersistenceKit (org.whole.lang.codebase.IPersistenceKit)4 IEntity (org.whole.lang.model.IEntity)4 IImportAsModelDialog (org.whole.lang.ui.dialogs.IImportAsModelDialog)4 ArrayList (java.util.ArrayList)2 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)2 ChangeBoundsRequest (org.eclipse.gef.requests.ChangeBoundsRequest)2 IFilePersistenceProvider (org.whole.lang.codebase.IFilePersistenceProvider)2 IPersistenceProvider (org.whole.lang.codebase.IPersistenceProvider)2 OperationCanceledException (org.whole.lang.operations.OperationCanceledException)2 IImportAsModelDialogFactory (org.whole.lang.ui.dialogs.IImportAsModelDialogFactory)2 IEntityPartViewer (org.whole.lang.ui.viewers.IEntityPartViewer)2 File (java.io.File)1 IFile (org.eclipse.core.resources.IFile)1 IWorkspace (org.eclipse.core.resources.IWorkspace)1 EditPart (org.eclipse.gef.EditPart)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 FilePersistenceProvider (org.whole.lang.codebase.FilePersistenceProvider)1 StringPersistenceProvider (org.whole.lang.codebase.StringPersistenceProvider)1 IEntityPart (org.whole.lang.ui.editparts.IEntityPart)1