Search in sources :

Example 16 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class JDTJavaSourcePersistenceKit method doReadModel.

@Override
protected IEntity doReadModel(IPersistenceProvider pp) throws Exception {
    String fileStr = StringUtils.readAsString(pp.getInputStream(), pp.getEncoding());
    IBindingManager bm = pp.getBindings();
    ASTNode ast = null;
    // TODO remove when parse/unparse are added
    if (bm.wIsSet("parseFragments") && bm.wBooleanValue("parseFragments")) {
        IJavaEntity entity = JDTTransformerVisitor.transform(fileStr, JDTUtils.parse(fileStr));
        if (Matcher.matchImpl(JavaEntityDescriptorEnum.ClassDeclaration, entity))
            bm.wDef("syntheticRoot", ((ClassDeclaration) entity).getBodyDeclarations());
        else if (Matcher.matchImpl(JavaEntityDescriptorEnum.Block, entity))
            bm.wDef("syntheticRoot", entity);
        return entity;
    } else {
        if (bm.wIsSet("entityURI")) {
            String entityURI = bm.wStringValue("entityURI");
            if (ResourceUtils.hasFragmentPart(entityURI)) {
                EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.getEntityDescriptor(entityURI, true, bm);
                if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Expression)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.EXPRESSION);
                } else if (ed.equals(JavaEntityDescriptorEnum.Block)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.STATEMENTS);
                } else if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Statement)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.STATEMENTS);
                } else if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.BodyDeclaration)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.CLASS_BODY_DECLARATIONS);
                } else if (ed.equals(JavaEntityDescriptorEnum.BodyDeclarations)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.CLASS_BODY_DECLARATIONS);
                } else if (ed.equals(JavaEntityDescriptorEnum.CompilationUnit))
                    ast = JDTUtils.parseAsCompilationUnit(fileStr);
            }
        }
        if (ast == null)
            ast = JDTUtils.parse(fileStr);
        IEntity result = JDTTransformerVisitor.transform(fileStr, ast);
        if (bm.wIsSet("entityURI")) {
            String entityURI = bm.wStringValue("entityURI");
            if (ResourceUtils.hasFragmentPart(entityURI)) {
                EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.getEntityDescriptor(entityURI, true, bm);
                if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Expression)) {
                    if (!result.wGetEntityDescriptor().isLanguageSubtypeOf(ed))
                        throw new IllegalArgumentException("");
                } else if (!ed.equals(JavaEntityDescriptorEnum.Block) && ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Statement)) {
                    if (result.wSize() != 1 || !(result = result.wGet(0)).wGetEntityDescriptor().isLanguageSubtypeOf(ed))
                        throw new IllegalArgumentException("");
                } else if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.BodyDeclaration)) {
                    if (result.wSize() != 1 || !(result = result.wGet(0)).wGetEntityDescriptor().isLanguageSubtypeOf(ed))
                        throw new IllegalArgumentException("");
                }
            } else if (result.wGetEntityDescriptor().equals(JavaEntityDescriptorEnum.Block) && result.wSize() == 1)
                result = result.wGet(0);
        }
        return result;
    }
}
Also used : ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) IEntity(org.whole.lang.model.IEntity) IJavaEntity(org.whole.lang.java.model.IJavaEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 17 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class MergeResourcesAction method performWorkspaceResourceSelection.

protected IEntity performWorkspaceResourceSelection(Shell shell, IEntity entity) {
    IResource input;
    if (Matcher.matchImpl(ArtifactsEntityDescriptorEnum.Workspace, entity.wGetParent()))
        input = ResourcesPlugin.getWorkspace().getRoot();
    else {
        IBindingManager bm = BindingManagerFactory.instance.createBindingManager();
        ArtifactsWorkspaceUtils.bindPath(entity.wGetParent(), bm, false);
        input = (IResource) (bm.wIsSet("folder") ? bm.wGetValue("folder") : bm.wGetValue("project"));
    }
    IJavaElement javaInput = JavaCore.create(input);
    LabelProvider labelProvider = javaInput != null ? new JavaElementLabelProvider() : new WorkbenchLabelProvider();
    ITreeContentProvider contentProvider = javaInput != null ? new StandardJavaElementContentProvider() : new WorkbenchContentProvider();
    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, labelProvider, contentProvider);
    dialog.setInput(javaInput != null ? javaInput : input);
    dialog.setTitle(SELECT_RESOURCE_MSG);
    dialog.setMessage("Choose a resource");
    if (javaInput != null)
        dialog.addFilter(new ViewerFilter() {

            public boolean select(Viewer viewer, Object parentElement, Object element) {
                try {
                    return !(element instanceof IPackageFragmentRoot) || ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE;
                } catch (Exception e) {
                    return false;
                }
            }
        });
    if (dialog.open() != IDialogConstants.OK_ID)
        return NullEntity.instance;
    IEntity result = null;
    for (Object resource : dialog.getResult()) {
        IEntity artifactsPath;
        if (resource instanceof IJavaElement) {
            IJavaElement toJavaElement = (IJavaElement) resource;
            artifactsPath = ArtifactsWorkspaceUtils.toArtifactsPath(javaInput, toJavaElement);
        } else {
            IResource toResource = (IResource) resource;
            IJavaElement parentJavaElement = JavaCore.create(toResource.getParent());
            if (javaInput == null || parentJavaElement == null)
                artifactsPath = ArtifactsWorkspaceUtils.toArtifactsPath(input, toResource);
            else
                artifactsPath = ArtifactsWorkspaceUtils.toArtifactsPath(javaInput, toResource);
        }
        result = result == null ? artifactsPath : EntityUtils.merge(result, artifactsPath, createEntityComparator(), false);
    }
    int index = Matcher.match(ArtifactsEntityDescriptorEnum.Workspace, result) ? result.wIndexOf(ArtifactsFeatureDescriptorEnum.projects) : result.wIndexOf(ArtifactsFeatureDescriptorEnum.artifacts);
    IEntity tree = result.wGet(index);
    result.wRemove(index);
    return tree;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) IEntity(org.whole.lang.model.IEntity) Viewer(org.eclipse.jface.viewers.Viewer) IEntityPartViewer(org.whole.lang.ui.viewers.IEntityPartViewer) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) JavaElementLabelProvider(org.eclipse.jdt.ui.JavaElementLabelProvider) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) StandardJavaElementContentProvider(org.eclipse.jdt.ui.StandardJavaElementContentProvider) IBindingManager(org.whole.lang.bindings.IBindingManager) JavaElementLabelProvider(org.eclipse.jdt.ui.JavaElementLabelProvider) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) LabelProvider(org.eclipse.jface.viewers.LabelProvider) IResource(org.eclipse.core.resources.IResource)

Example 18 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class ReplaceWithClassNameAction method update.

@Override
public void update() {
    ESelectionService selectionService = getContext().get(ESelectionService.class);
    if (selectionService.getSelection() instanceof IBindingManager) {
        IBindingManager bm = (IBindingManager) selectionService.getSelection();
        IJavaProject javaProject = (IJavaProject) bm.wGetValue("javaProject");
        setEnabled(javaProject != null && HandlersBehavior.isValidFocusEntityPart(bm));
    } else
        setEnabled(false);
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IBindingManager(org.whole.lang.bindings.IBindingManager) ESelectionService(org.eclipse.e4.ui.workbench.modeling.ESelectionService)

Example 19 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class ReplaceWithResourceAction method run.

@Override
public void run() {
    ESelectionService selectionService = getContext().get(ESelectionService.class);
    IBindingManager bm = (IBindingManager) selectionService.getSelection();
    IEntity focusEntity = bm.wGet("focusEntity");
    ResourceKind resourceKind = getResourceKind(focusEntity);
    Shell shell = (Shell) getContext().get(IServiceConstants.ACTIVE_SHELL);
    boolean selectionPerformed = false;
    switch(resourceKind) {
        case WORKSPACE:
            selectionPerformed = performWorkspaceResourceSelection(shell, focusEntity);
            break;
        case CLASSPATH:
            selectionPerformed = performClasspathResourceSelection(shell, focusEntity);
            break;
        case FILE_SYSTEM:
        case URL:
            selectionPerformed = performFilesystemSelection(shell, focusEntity, resourceKind == ResourceKind.URL);
            break;
    }
    if (!selectionPerformed)
        return;
    IEntity replacement = GenericEntityFactory.instance.create(ed, path);
    ModelTransactionCommand mtc = new ModelTransactionCommand(focusEntity);
    try {
        mtc.setLabel("replace with class name");
        mtc.begin();
        performReplace(focusEntity, replacement);
        mtc.commit();
        if (mtc.canUndo()) {
            IEntityPartViewer viewer = (IEntityPartViewer) bm.wGetValue("viewer");
            CommandStack commandStack = viewer.getEditDomain().getCommandStack();
            commandStack.execute(mtc);
        }
    } catch (RuntimeException e) {
        mtc.rollbackIfNeeded();
        throw e;
    }
}
Also used : ModelTransactionCommand(org.whole.lang.ui.commands.ModelTransactionCommand) CommandStack(org.eclipse.gef.commands.CommandStack) Shell(org.eclipse.swt.widgets.Shell) IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) ESelectionService(org.eclipse.e4.ui.workbench.modeling.ESelectionService) IEntityPartViewer(org.whole.lang.ui.viewers.IEntityPartViewer)

Example 20 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class PojoJavaModelGeneratorVisitor method visit.

public void visit(Library entity) {
    IBindingManager bm = getBindings();
    bm.wEnterScope();
    ArtifactsGeneratorOperation.generate(entity, bm);
    bm.wExitScope();
}
Also used : IBindingManager(org.whole.lang.bindings.IBindingManager)

Aggregations

IBindingManager (org.whole.lang.bindings.IBindingManager)223 IEntity (org.whole.lang.model.IEntity)141 Test (org.junit.Test)91 PathExpression (org.whole.lang.queries.model.PathExpression)49 ITemplateManager (org.whole.lang.templates.ITemplateManager)40 Grammar (org.whole.lang.grammars.model.Grammar)28 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)27 ESelectionService (org.eclipse.e4.ui.workbench.modeling.ESelectionService)17 Model (org.whole.lang.models.model.Model)15 Production (org.whole.lang.grammars.model.Production)14 IEntityPartViewer (org.whole.lang.ui.viewers.IEntityPartViewer)14 File (java.io.File)12 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)12 XmlModel (org.whole.lang.models.codebase.XmlModel)11 SimpleEntity (org.whole.lang.models.model.SimpleEntity)10 VisitException (org.whole.lang.visitors.VisitException)9 IBindingScope (org.whole.lang.bindings.IBindingScope)6 ModelBuilderOperation (org.whole.lang.builders.ModelBuilderOperation)6 HashMap (java.util.HashMap)5 IFile (org.eclipse.core.resources.IFile)5