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);
}
}
}
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
}
}
}
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;
}
}
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();
}
Aggregations