Example usage for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment.

Prototype

IPackageFragment getPackageFragment(String packageName);

Source Link

Document

Returns the package fragment with the given package name.

Usage

From source file:com.iw.plugins.spindle.ui.wizards.factories.LibraryFactory.java

License:Mozilla Public License

/**
 * Method createLibrary.//w  ww  .j a  v  a 2 s  .  com
 * 
 * @param root
 * @param pack
 * @param appname
 * @param monitor
 * @return IFile
 */
public static IFile createLibrary(IPackageFragmentRoot root, IPackageFragment pack, String libraryName,
        IProgressMonitor monitor) throws CoreException, InterruptedException {

    monitor.beginTask(UIPlugin.getString("ApplicationFactory.operationdesc", libraryName), 10);
    if (pack == null) {
        pack = root.getPackageFragment("");
    }
    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, null);
        pack.save(new SubProgressMonitor(monitor, 1), true);
    }
    monitor.worked(1);
    IContainer folder = (IContainer) pack.getUnderlyingResource();
    IFile file = folder.getFile(new Path(libraryName + ".library"));

    InputStream contents = new ByteArrayInputStream(getLibraryContent().getBytes());
    file.create(contents, false, new SubProgressMonitor(monitor, 1));
    monitor.worked(1);
    monitor.done();
    return file;
}

From source file:com.iw.plugins.spindle.ui.wizards.fields.PackageDialogField.java

License:Mozilla Public License

public IStatus packageChanged() {
    SpindleStatus status = new SpindleStatus();
    checkButtonEnabled();/*from w w w. j  a  v a2 s. c  om*/
    String packName = getTextValue();
    if (!"".equals(packName)) {
        IStatus val = JavaConventions.validatePackageName(packName);
        if (val.getSeverity() == IStatus.ERROR) {
            status.setError(UIPlugin.getString(name + ".error.InvalidPackageName", val.getMessage()));
            return status;
        } else if (val.getSeverity() == IStatus.WARNING) {
            status.setWarning(UIPlugin.getString(name + ".warning.DiscouragedPackageName", val.getMessage()));
            // continue
        }
    } else {

        status.setError(UIPlugin.getString(name + ".error.defaultPackage"));
        return status;
    }

    IPackageFragmentRoot root;
    if (container == null) {
        root = null;
    } else {
        root = container.getPackageFragmentRoot();
    }
    if (root != null) {
        IPackageFragment pack = root.getPackageFragment(packName);
        try {
            if (fSourcePackagesOnly && root.getKind() == IPackageFragmentRoot.K_BINARY) {
                status.setError(UIPlugin.getString(name + ".error.PackageMustBeSource"));
                return status;
            }

            IPath rootPath = root.getPath();
            IPath outputPath = root.getJavaProject().getOutputLocation();
            if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                // if the bin folder is inside of our root, dont allow to name a
                // package
                // like the bin folder
                IPath packagePath = pack.getUnderlyingResource().getFullPath();
                if (outputPath.isPrefixOf(packagePath)) {
                    status.setError(UIPlugin.getString(name + ".error.ClashOutputLocation"));
                    return status;
                }
            }
        } catch (JavaModelException e) {
            UIPlugin.log(e);
            // let pass
        }

        currentPackage = pack;
        if (currentPackage != null && nameField != null) {
            try {
                IContainer folder = (IContainer) getPackageFragment().getUnderlyingResource();

                boolean isComponent = nameField.getKind() == AbstractNameField.COMPONENT_NAME;
                IFile file = folder
                        .getFile(new Path(nameField.getTextValue() + (isComponent ? ".jwc" : ".page")));
                if (file.exists()) {

                    status.setError(UIPlugin.getString(name + ".error.ComponentAlreadyExists",
                            file.getFullPath().toString()));
                    return status;
                }
            } catch (JavaModelException e) {
                UIPlugin.log(e);
            }
        }
    } else {
        status.setError("");
    }
    return status;
}

From source file:com.iw.plugins.spindle.ui.wizards.TypeChooseWizardPage.java

License:Mozilla Public License

public void init(IJavaElement initElement) {
    IDialogSettings settings = getDialogSettings();

    if (initElement != null) {
        IJavaProject jproject = initElement.getJavaProject();

        restoreRootAndPackageSettings(jproject);

        IPackageFragmentRoot settingsRoot = getPackageFragmentRoot();
        IPackageFragment settingsPackage = getPackageFragment();

        IPackageFragmentRoot root = CoreUtils.getPackageFragmentRoot(initElement);
        if (root != null) {
            setPackageFragmentRoot(root, true);
            IPackageFragment pack = (IPackageFragment) CoreUtils.findElementOfKind(initElement,
                    IJavaElement.PACKAGE_FRAGMENT);
            // its a diff
            if (pack == null && settingsPackage != null) {
                pack = root.getPackageFragment(settingsPackage.getElementName());
            }//from   www .j a  va2  s .c  om
            setPackageFragment(pack, true);

        } else if (settingsRoot == null) {
            root = getInitialRoot(
                    (IJavaProject) CoreUtils.findElementOfKind(initElement, IJavaElement.JAVA_PROJECT));
            setPackageFragmentRoot(root, true);
            setPackageFragment(null, true);
        }
    }

}

From source file:com.iw.plugins.spindle.ui.wizards.TypeChooseWizardPage.java

License:Mozilla Public License

public void restoreRootAndPackageSettings(IJavaProject project) {
    try {/*from ww  w.ja  v  a2s .com*/
        IDialogSettings settings = getDialogSettings();
        String rootSetting = settings.get(PACKAGE_ROOT_KEY);
        if (rootSetting == null)
            return;
        IPackageFragmentRoot foundRoot = null;
        IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            String path = roots[i].getPath().makeRelative().toString();
            if (path.equals(rootSetting)) {
                foundRoot = roots[i];
                setPackageFragmentRoot(roots[i], true);
                break;
            }
        }
        String packageSetting = settings.get(PACKAGE_NAME_KEY);
        if (foundRoot == null || packageSetting == null
                || (packageSetting = packageSetting.trim()).length() == 0)
            return;
        setPackageFragment(foundRoot.getPackageFragment(packageSetting), true);
    } catch (JavaModelException e) {
        UIPlugin.log(e);
    }
}

From source file:com.iw.plugins.spindle.util.lookup.TapestryLookup.java

License:Mozilla Public License

private IPackageFragment[] getPackageFragmentsInRoots(IPackageFragmentRoot[] roots, IJavaProject project) {

    ArrayList frags = new ArrayList();
    for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot root = roots[i];
        try {//from  w ww .j  a va  2 s.  co m
            IJavaElement[] children = root.getChildren();

            int length = children.length;
            if (length == 0)
                continue;
            if (children[0].getParent().getParent().equals(project)) {
                for (int j = 0; j < length; j++) {
                    frags.add(children[j]);
                }
            } else {
                for (int j = 0; j < length; j++) {
                    frags.add(root.getPackageFragment(children[j].getElementName()));
                }
            }
        } catch (JavaModelException e) {
            // do nothing
        }
    }
    IPackageFragment[] fragments = new IPackageFragment[frags.size()];
    frags.toArray(fragments);
    return fragments;
}

From source file:com.iw.plugins.spindle.wizards.factories.ApplicationClassFactory.java

License:Mozilla Public License

static public IType createClass(IPackageFragmentRoot root, IPackageFragment pack, String classname,
        IType superClass, IMethodEvaluator methodEvaluator, IProgressMonitor monitor)
        throws CoreException, InterruptedException {

    IType createdType;/*  w w  w.  ja  va 2s.  c o  m*/
    ImportsStructure imports;
    int indent = 0;
    String superclassName = (superClass == null ? "java.lang.Object" : superClass.getElementName());

    monitor.beginTask(MessageUtil.getFormattedString("ClassFactory.operationdesc", classname), 10);
    if (pack == null) {
        pack = root.getPackageFragment("");
    }
    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, null);
    }

    monitor.worked(1);

    String lineDelimiter = null;
    ICompilationUnit parentCU = pack.getCompilationUnit(classname + ".java");
    imports = getImports(parentCU);
    lineDelimiter = StubUtility.getLineDelimiterUsed(parentCU);
    String content = createClassBody(classname, superClass, imports, lineDelimiter);
    createdType = parentCU.createType(content, null, false, new SubProgressMonitor(monitor, 5));
    // add imports for sclass, so the type can be parsed correctly
    if (imports != null) {
        imports.create(true, new SubProgressMonitor(monitor, 1));
    }
    createAllNewMethods(methodEvaluator, createdType, imports, new SubProgressMonitor(monitor, 1));

    monitor.worked(1);

    String formattedContent = StubUtility.codeFormat(createdType.getSource(), indent, lineDelimiter);

    save(createdType, formattedContent, monitor);
    monitor.done();
    return createdType;
}

From source file:com.iw.plugins.spindle.wizards.factories.ApplicationFactory.java

License:Mozilla Public License

static public IFile createApplication(IPackageFragmentRoot root, IPackageFragment pack, String appname,
        String qualifiedEngineClassname, IProgressMonitor monitor) throws CoreException, InterruptedException {

    monitor.beginTask(MessageUtil.getFormattedString("ApplicationFactory.operationdesc", appname), 10);
    if (pack == null) {
        pack = root.getPackageFragment("");
    }//from   ww w  .  j  a  v  a  2  s.c  o m
    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, null);
        pack.save(new SubProgressMonitor(monitor, 1), true);
    }
    monitor.worked(1);
    IContainer folder = (IContainer) pack.getUnderlyingResource();
    IFile file = folder.getFile(new Path(appname + ".application"));

    InputStream contents = new ByteArrayInputStream(
            getApplicationContent(appname, qualifiedEngineClassname, pack.getElementName()).getBytes());
    file.create(contents, false, new SubProgressMonitor(monitor, 1));
    monitor.worked(1);
    monitor.done();
    return file;
}

From source file:com.iw.plugins.spindle.wizards.factories.ClassFactory.java

License:Mozilla Public License

public IType createClass(IPackageFragmentRoot root, IPackageFragment pack, String classname, IType superClass,
        IType[] interfaces, IMethodEvaluator methodEvaluator, IProgressMonitor monitor)
        throws CoreException, InterruptedException {

    IType createdType;//from w w  w  . j  a  va 2 s.  c  om
    ImportsStructure imports;
    int indent = 0;
    String superclassName = (superClass == null ? "java.lang.Object" : superClass.getElementName());

    monitor.beginTask(MessageUtil.getFormattedString("ClassFactory.operationdesc", classname), 10);
    if (pack == null) {
        pack = root.getPackageFragment("");
    }
    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, null);
    }

    monitor.worked(1);

    String lineDelimiter = null;
    ICompilationUnit parentCU = pack.getCompilationUnit(classname + ".java");
    imports = getImports(parentCU);
    lineDelimiter = StubUtility.getLineDelimiterUsed(parentCU);
    String content = createClassBody(classname, superClass, interfaces, imports, lineDelimiter);
    createdType = parentCU.createType(content, null, false, new SubProgressMonitor(monitor, 5));
    // add imports for sclass, so the type can be parsed correctly
    if (imports != null) {
        imports.create(true, new SubProgressMonitor(monitor, 1));
    }
    createAllNewMethods(methodEvaluator, createdType, imports, new SubProgressMonitor(monitor, 1));

    monitor.worked(1);

    String formattedContent = StubUtility.codeFormat(createdType.getSource(), indent, lineDelimiter);

    save(createdType, formattedContent, monitor);
    monitor.done();
    return createdType;
}

From source file:com.iw.plugins.spindle.wizards.factories.ComponentFactory.java

License:Mozilla Public License

public static IFile createComponent(IPackageFragmentRoot root, IPackageFragment pack, String componentName,
        IType specClass, IProgressMonitor monitor) throws CoreException, InterruptedException {

    monitor.beginTask(MessageUtil.getFormattedString("ApplicationFactory.operationdesc", componentName), 10);
    if (pack == null) {
        pack = root.getPackageFragment("");
    }//from   ww  w . j  a  v  a  2 s .  com
    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, null);
        pack.save(new SubProgressMonitor(monitor, 1), true);
    }
    monitor.worked(1);
    IContainer folder = (IContainer) pack.getUnderlyingResource();
    IFile file = folder.getFile(new Path(componentName + ".jwc"));

    String qualifiedSpecClassname = specClass.getFullyQualifiedName();
    InputStream contents = new ByteArrayInputStream(getComponentContent(qualifiedSpecClassname).getBytes());
    file.create(contents, false, new SubProgressMonitor(monitor, 1));
    monitor.worked(1);
    monitor.done();
    return file;
}

From source file:com.iw.plugins.spindle.wizards.factories.LibraryFactory.java

License:Mozilla Public License

/**
 * Method createLibrary./*from   w  w  w.j ava  2 s .c  o  m*/
 * @param root
 * @param pack
 * @param appname
 * @param monitor
 * @return IFile
 */
public static IFile createLibrary(IPackageFragmentRoot root, IPackageFragment pack, String libraryName,
        IProgressMonitor monitor) throws CoreException, InterruptedException {

    monitor.beginTask(MessageUtil.getFormattedString("ApplicationFactory.operationdesc", libraryName), 10);
    if (pack == null) {
        pack = root.getPackageFragment("");
    }
    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, null);
        pack.save(new SubProgressMonitor(monitor, 1), true);
    }
    monitor.worked(1);
    IContainer folder = (IContainer) pack.getUnderlyingResource();
    IFile file = folder.getFile(new Path(libraryName + ".library"));

    InputStream contents = new ByteArrayInputStream(getLibraryContent().getBytes());
    file.create(contents, false, new SubProgressMonitor(monitor, 1));
    monitor.worked(1);
    monitor.done();
    return file;
}