Examples of IPackageFragment


Examples of org.eclipse.jdt.core.IPackageFragment

                }
                TextEdit rootEdit = fileChange.getEdit();

                /* loop over all renames to perform */
                for (Map.Entry<IPackageFragment,RenameArguments> entry : pkgFragments.entrySet()) {
                    IPackageFragment pkgFragment = entry.getKey();
                    RenameArguments arguments = entry.getValue();

                    final String oldName = pkgFragment.getElementName();
                    final String newName = arguments.getNewName();

                    Pattern pattern = Pattern.compile(
                    /* match start boundary */"(^|" + grammarSeparator + ")" +
                    /* match itself / package name */"(" + Pattern.quote(oldName) + ")" +
                    /* match end boundary */"(" + grammarSeparator + "|" + Pattern.quote(".*") + "|" + Pattern.quote("\\") + "|$)");

                    /* find all matches to replace and add them to the root edit */
                    Matcher matcher = pattern.matcher(bndFileText);
                    while (matcher.find()) {
                        rootEdit.addChild(new ReplaceEdit(matcher.start(2), matcher.group(2).length(), newName));
                    }

                    pattern = Pattern.compile(
                    /* match start boundary */"(^|" + grammarSeparator + ")" +
                    /* match bundle activator */"(Bundle-Activator\\s*:\\s*)" +
                    /* match itself / package name */"(" + Pattern.quote(oldName) + ")" +
                    /* match class name */"(\\.[^\\.]+)" +
                    /* match end boundary */"(" + grammarSeparator + "|" + Pattern.quote("\\") + "|$)");

                    /* find all matches to replace and add them to the root edit */
                    matcher = pattern.matcher(bndFileText);
                    while (matcher.find()) {
                        rootEdit.addChild(new ReplaceEdit(matcher.start(3), matcher.group(3).length(), newName));
                    }
                }

                /*
                 * only store the changes when no changes were stored before for this file and when there are actually
                 * changes.
                 */
                if (fileChangeIsNew && rootEdit.hasChildren()) {
                    fileChanges.put(resource, fileChange);
                }

                return false;
            }
        };

        /* determine which projects have to be visited */
        Set<IProject> projectsToVisit = new HashSet<IProject>();
        for (IPackageFragment pkgFragment : pkgFragments.keySet()) {
            projectsToVisit.add(pkgFragment.getResource().getProject());
            for (IProject projectToVisit : pkgFragment.getResource().getProject().getReferencingProjects()) {
                projectsToVisit.add(projectToVisit);
            }
            for (IProject projectToVisit : pkgFragment.getResource().getProject().getReferencedProjects()) {
                projectsToVisit.add(projectToVisit);
            }
        }

        /* visit the projects */
 
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

        } else if (data instanceof IStructuredSelection) {
            Iterator< ? > iterator = ((IStructuredSelection) data).iterator();
            while (iterator.hasNext()) {
                Object element = iterator.next();
                if (element instanceof IPackageFragment) {
                    IPackageFragment pkg = (IPackageFragment) element;
                    newEntries.add(createNewEntry(pkg.getElementName()));
                } else if (element instanceof ImportPackage) {
                    ImportPackage pkg = (ImportPackage) element;
                    newEntries.add(createNewEntry(pkg.getName()));
                }
            }
        }
        addRows(insertionIndex, newEntries);
        return true;
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

    protected void initTypePage(IJavaElement elem) {
        String initSuperclass = "java.lang.Object"; //$NON-NLS-1$
        ArrayList<String> initSuperinterfaces = new ArrayList<String>(5);

        IJavaProject project = null;
        IPackageFragment pack = null;
        IType enclosingType = null;

        if (elem != null) {
            // evaluate the enclosing type
            project = elem.getJavaProject();
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

        }
    }

    private void typePageChangeControlPressed(DialogField field) {
        if (field == fPackageDialogField) {
            IPackageFragment pack = choosePackage();
            if (pack != null) {
                fPackageDialogField.setText(pack.getElementName());
            }
        } else if (field == fEnclosingTypeDialogField) {
            IType type = chooseEnclosingType();
            if (type != null) {
                fEnclosingTypeDialogField.setText(type.getFullyQualifiedName('.'));
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

    public IResource getModifiedResource() {
        IType enclosing = getEnclosingType();
        if (enclosing != null) {
            return enclosing.getResource();
        }
        IPackageFragment pack = getPackageFragment();
        if (pack != null) {
            String cuName = getCompilationUnitName(getTypeNameWithoutParameters());
            return pack.getCompilationUnit(cuName).getResource();
        }
        return null;
    }
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

            // continue checking
        }

        // must not exist
        if (!isEnclosingTypeSelected()) {
            IPackageFragment pack = getPackageFragment();
            if (pack != null) {
                ICompilationUnit cu = pack.getCompilationUnit(getCompilationUnitName(typeName));
                fCurrType = cu.getType(typeName);
                IResource resource = cu.getResource();

                if (resource.exists()) {
                    status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists);
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

        dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_description);
        dialog.setEmptyListMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_empty);
        dialog.setElements(packages);
        dialog.setHelpAvailable(false);

        IPackageFragment pack = getPackageFragment();
        if (pack != null) {
            dialog.setInitialSelections(new Object[] {
                pack
            });
        }
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

        }

        monitorInternal.beginTask(NewWizardMessages.NewTypeWizardPage_operationdesc, 8);

        IPackageFragmentRoot root = getPackageFragmentRoot();
        IPackageFragment pack = getPackageFragment();
        if (pack == null) {
            pack = root.getPackageFragment(""); //$NON-NLS-1$
        }

        if (!pack.exists()) {
            String packName = pack.getElementName();
            pack = root.createPackageFragment(packName, true, new SubProgressMonitor(monitorInternal, 1));
        } else {
            monitorInternal.worked(1);
        }

        boolean needsSave;
        ICompilationUnit connectedCU = null;

        try {
            String typeName = getTypeNameWithoutParameters();

            boolean isInnerClass = isEnclosingTypeSelected();

            IType createdType;
            ImportsManager imports;
            int indent = 0;

            Set<String> existingImports;

            String lineDelimiter = null;
            if (!isInnerClass) {
                lineDelimiter = StubUtility.getLineDelimiterUsed(pack.getJavaProject());

                String cuName = getCompilationUnitName(typeName);
                ICompilationUnit parentCU = pack.createCompilationUnit(cuName, "", false, new SubProgressMonitor(monitorInternal, 2)); //$NON-NLS-1$
                // create a working copy with a new owner

                needsSave = true;
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitorInternal, 1)); // cu is now a (primary) working copy
                connectedCU = parentCU;

                IBuffer buffer = parentCU.getBuffer();

                String simpleTypeStub = constructSimpleTypeStub();
                String cuContent = constructCUContent(parentCU, simpleTypeStub, lineDelimiter);
                buffer.setContents(cuContent);

                CompilationUnit astRoot = createASTForImports(parentCU);
                existingImports = getExistingImports(astRoot);

                imports = new ImportsManager(astRoot);
                // add an import that will be removed again. Having this import solves 14661
                imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName));

                String typeContent = constructTypeStub(parentCU, imports, lineDelimiter);

                int index = cuContent.lastIndexOf(simpleTypeStub);
                if (index == -1) {
                    AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0);
                    int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition();
                    int end = typeNode.getStartPosition() + typeNode.getLength();
                    buffer.replace(start, end - start, typeContent);
                } else {
                    buffer.replace(index, simpleTypeStub.length(), typeContent);
                }

                createdType = parentCU.getType(typeName);
            } else {
                IType enclosingType = getEnclosingType();

                ICompilationUnit parentCU = enclosingType.getCompilationUnit();

                needsSave = !parentCU.isWorkingCopy();
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitorInternal, 1)); // cu is now for sure (primary) a working copy
                connectedCU = parentCU;

                CompilationUnit astRoot = createASTForImports(parentCU);
                imports = new ImportsManager(astRoot);
                existingImports = getExistingImports(astRoot);

                // add imports that will be removed again. Having the imports solves 14661
                IType[] topLevelTypes = parentCU.getTypes();
                for (int i = 0; i < topLevelTypes.length; i++) {
                    imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
                }

                lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
                StringBuffer content = new StringBuffer();

                String comment = getTypeComment(parentCU, lineDelimiter);
                if (comment != null) {
                    content.append(comment);
                    content.append(lineDelimiter);
                }

                content.append(constructTypeStub(parentCU, imports, lineDelimiter));
                IJavaElement sibling = null;
                if (enclosingType.isEnum()) {
                    IField[] fields = enclosingType.getFields();
                    if (fields.length > 0) {
                        for (int i = 0, max = fields.length; i < max; i++) {
                            if (!fields[i].isEnumConstant()) {
                                sibling = fields[i];
                                break;
                            }
                        }
                    }
                } else {
                    IJavaElement[] elems = enclosingType.getChildren();
                    sibling = elems.length > 0 ? elems[0] : null;
                }

                createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitorInternal, 2));

                indent = StubUtility.getIndentUsed(enclosingType) + 1;
            }
            if (monitorInternal.isCanceled()) {
                throw new InterruptedException();
            }

            // add imports for superclass/interfaces, so types can be resolved correctly

            ICompilationUnit cu = createdType.getCompilationUnit();

            imports.create(false, new SubProgressMonitor(monitorInternal, 1));

            JavaModelUtil.reconcile(cu);

            if (monitorInternal.isCanceled()) {
                throw new InterruptedException();
            }

            // set up again
            CompilationUnit astRoot = createASTForImports(imports.getCompilationUnit());
            imports = new ImportsManager(astRoot);

            createTypeMembers(createdType, imports, new SubProgressMonitor(monitorInternal, 1));

            // add imports
            imports.create(false, new SubProgressMonitor(monitorInternal, 1));

            removeUnusedImports(cu, existingImports, false);

            JavaModelUtil.reconcile(cu);

            ISourceRange range = createdType.getSourceRange();

            IBuffer buf = cu.getBuffer();
            String originalContent = buf.getText(range.getOffset(), range.getLength());

            String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, lineDelimiter, pack.getJavaProject());
            formattedContent = Strings.trimLeadingTabsAndSpaces(formattedContent);
            buf.replace(range.getOffset(), range.getLength(), formattedContent);
            if (!isInnerClass) {
                String fileComment = getFileComment(cu);
                if (fileComment != null && fileComment.length() > 0) {
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

     * @since 2.1
     */
    protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
        String fileComment = getFileComment(cu, lineDelimiter);
        String typeComment = getTypeComment(cu, lineDelimiter);
        IPackageFragment pack = (IPackageFragment) cu.getParent();
        String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
        if (content != null) {
            ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
            parser.setProject(cu.getJavaProject());
            parser.setSource(content.toCharArray());
            CompilationUnit unit = (CompilationUnit) parser.createAST(null);
            if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
                return content;
            }
        }
        StringBuffer buf = new StringBuffer();
        if (!pack.isDefaultPackage()) {
            buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
        }
        buf.append(lineDelimiter).append(lineDelimiter);
        if (typeComment != null) {
            buf.append(typeComment).append(lineDelimiter);
        }
View Full Code Here

Examples of org.eclipse.jdt.core.IPackageFragment

                if (Flags.isAbstract(type.getFlags())) {
                    omit = true;
                }

                if (!includeNonSource) {
                    IPackageFragment pkgFragment = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                    if (pkgFragment.getCompilationUnits().length == 0) {
                        omit = true;
                    }
                }

            } catch (JavaModelException e) {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.