List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment
IPackageFragment getPackageFragment(String packageName);
From source file:ar.com.tadp.xml.rinzo.jdt.actions.CreateClassAction.java
License:Open Source License
public void run(IAction action) { if (!Utils.isEmpty(this.getSelection())) { IWorkbench workbench = RinzoJDTPlugin.getDefault().getWorkbench(); Shell shell = workbench.getActiveWorkbenchWindow().getShell(); NewClassCreationWizard wizard = new NewClassCreationWizard(); String className = this.getSelection().substring(this.getSelection().lastIndexOf(".") + 1); String packageName = this.getSelection().substring(0, this.getSelection().lastIndexOf(".")); wizard.init(workbench, new StructuredSelection(className)); WizardDialog dialog = new WizardDialog(shell, wizard); PixelConverter converter = new PixelConverter(shell); dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20)); dialog.create();/*from w ww . ja va 2 s. c om*/ NewClassWizardPage page = (NewClassWizardPage) wizard.getPage("NewClassWizardPage"); page.setTypeName(className, true); page.setAddComments(true, true); IPackageFragmentRoot sourceFolder = this.getSourceFolder(); page.setPackageFragmentRoot(sourceFolder, true); page.setPackageFragment(sourceFolder.getPackageFragment(packageName), true); dialog.open(); } }
From source file:at.bestsolution.javafx.ide.jdt.internal.JavaClassResourceService.java
License:Open Source License
IResource handleElementCreation(IContainer container, String packageName, String className) { // FIXME This is temporary if (!container.getProject().isOpen()) { try {//from w w w. jav a 2 s. com container.getProject().open(new NullProgressMonitor()); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } } IProject pr = container.getProject(); IJavaElement jElement = getInitialJavaElement(container); IPackageFragmentRoot jRoot = getFragmentRoot(jElement); try { IJavaProject jProject = JavaCore.create(pr); jProject.open(new NullProgressMonitor()); jRoot.open(new NullProgressMonitor()); IPackageFragment fragment = jRoot.getPackageFragment(packageName); if (!fragment.exists()) { ((IFolder) fragment.getResource()).create(true, true, null); } ICompilationUnit u = fragment.getCompilationUnit(className + ".java"); IFile f = (IFile) u.getResource(); ByteArrayInputStream in = new ByteArrayInputStream( new String("public class " + className + " {\n}").getBytes()); f.create(in, IFile.FORCE | IFile.KEEP_HISTORY, new NullProgressMonitor()); in.close(); // pr.build(IncrementalProjectBuilder.FULL_BUILD, new // NullProgressMonitor()); return f; } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.err.println(jRoot); return null; }
From source file:at.spardat.xma.gui.projectw.AbstractOpenXmaProjectContentProvider.java
License:Open Source License
public void renameJavaPackages(IRunnableContext context, IProject project, String oldPackageName, String newPackageName) {//from www. j a va 2 s. c o m IJavaProject javaProject = JavaCore.create(project); try { IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; if (root.getKind() == IPackageFragmentRoot.K_SOURCE && root.getPackageFragment(oldPackageName).exists()) { IPackageFragment packageFragment = root.getPackageFragment(oldPackageName); renamePackage(context, packageFragment, newPackageName); } } } catch (Exception exception) { throw new WrappedException(exception); } }
From source file:byke.tests.workspaceutils.JavaProject.java
License:Open Source License
public ICompilationUnit createCompilationUnit(final IPackageFragmentRoot sourceFolder, String packageName, String cuName, String contents) throws JavaModelException, CoreException { IPackageFragment packageFragment = sourceFolder.getPackageFragment(packageName); if (!packageFragment.exists()) { packageFragment = sourceFolder.createPackageFragment(packageName, false, null); }//from w ww . ja v a2s. co m return createCompilationUnit(packageFragment, cuName, contents); }
From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaSequenceContentProvider.java
License:Open Source License
public Object getContainingGroup(Object lifelineOrGroup) { if (lifelineOrGroup instanceof JavaObject) { IJavaElement element = ((JavaObject) lifelineOrGroup).getJavaElement(); if (element instanceof IType) { return ((IType) element).getPackageFragment(); }/*from w w w . j a va 2 s . c o m*/ } else if (lifelineOrGroup instanceof IPackageFragment) { IPackageFragment fragment = (IPackageFragment) lifelineOrGroup; IPackageFragmentRoot root = (IPackageFragmentRoot) fragment.getParent(); int lastDot = fragment.getElementName().lastIndexOf('.'); if (lastDot > 0) { String name = fragment.getElementName().substring(0, lastDot); return root.getPackageFragment(name); } } return null; }
From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaSequenceContentProvider.java
License:Open Source License
public boolean hasContainingGroup(Object lifelineOrGroup) { if (lifelineOrGroup instanceof JavaObject) { IJavaElement element = ((JavaObject) lifelineOrGroup).getJavaElement(); if (element instanceof IType) { return ((IType) element).getPackageFragment() != null; }//from w w w. j a v a 2 s . co m } else if (lifelineOrGroup instanceof IPackageFragment) { IPackageFragment fragment = (IPackageFragment) lifelineOrGroup; IPackageFragmentRoot root = (IPackageFragmentRoot) fragment.getParent(); int lastDot = fragment.getElementName().lastIndexOf('.'); if (lastDot > 0) { String name = fragment.getElementName().substring(0, lastDot); return root.getPackageFragment(name) != null; } } return false; }
From source file:ccw.ClojureCore.java
License:Open Source License
/** * Tries to open a clojure file in an editor * @return an editor input if the file has been found, or null *///from w ww .j a va 2 s. c om private static IEditorInput findEditorInput(IPackageFragmentRoot packageFragmentRoot, String searchedPackage, String searchedFileName) throws JavaModelException { // Find in package fragment IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(searchedPackage); IEditorInput editorInput = findEditorInput(packageFragmentRoot, packageFragment, searchedPackage, searchedFileName); if (editorInput != null) { return editorInput; } return findEditorInputInSourceAttachment(packageFragmentRoot, searchedPackage, searchedFileName); }
From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java
License:Open Source License
public static ICompilationUnit findCompilationUnit(IProject project, String fullyQualifiedName) { IPackageFragment fragment;//from ww w. j a va 2s . c o m ICompilationUnit compilationUnit; IPackageFragmentRoot[] fragmentRoots; try { fragmentRoots = getPackageFragmentRoots(project); } catch (JavaModelException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } catch (CoreException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } // split fullyQualifiedName into package and simple name // int lastDotIndex= fullyQualifiedName.lastIndexOf('.'); String packageName; String compilationUnitName; Pattern pattern = Pattern.compile("(?:(.*)\\.)?([^.]+\\.[^.]+)"); Matcher matcher = pattern.matcher(fullyQualifiedName); if (matcher.matches()) { packageName = matcher.group(1); if (packageName == null) { packageName = ""; // default package } compilationUnitName = matcher.group(2); } else { return null; } /* if (lastDotIndex != -1) { packageName= fullyQualifiedName.substring(0, lastDotIndex); compilationUnitName= fullyQualifiedName.substring(lastDotIndex + 1, fullyQualifiedName.length()); } else { // no dot found ==> try to retrieve class in default package packageName= ""; compilationUnitName= fullyQualifiedName; } */ for (IPackageFragmentRoot root : fragmentRoots) { try { // only take into account source roots if (root.getKind() != IPackageFragmentRoot.K_SOURCE) { continue; } } catch (JavaModelException e) { log.warn("Could not get kind of packageFragmentRoot " + project.getName(), e); continue; } fragment = root.getPackageFragment(packageName); if (fragment == null) { continue; } /* log.info("Trying package fragment: " + fragment.getPath()); */ compilationUnit = fragment.getCompilationUnit(compilationUnitName); if (compilationUnit != null && compilationUnit.exists()) { return compilationUnit; } } return null; }
From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java
License:Open Source License
public static ICompilationUnit findCompilationUnitInClassPackages(IProject project, Iterable<String> classPackages, String packageSuffix, String resourceName) { IPackageFragment fragment;// w ww .ja va2s . c om ICompilationUnit compilationUnit; IPackageFragmentRoot[] fragmentRoots; try { fragmentRoots = getPackageFragmentRoots(project); } catch (JavaModelException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } catch (CoreException e) { log.warn("Could not get packageFragmentRoots of project " + project.getName(), e); return null; } if (classPackages == null) { // add empty string as classPackage if none supplied classPackages = Arrays.asList(new String[] { "" }); } // loop through class package names for (String packageName : classPackages) { for (IPackageFragmentRoot root : fragmentRoots) { try { // only take into account source roots if (root.getKind() != IPackageFragmentRoot.K_SOURCE) { continue; } } catch (JavaModelException e) { log.warn("Could not get kind of packageFragmentRoot " + project.getName(), e); continue; } fragment = root.getPackageFragment(packageName + packageSuffix); if (fragment == null) { continue; } /* log.info("Trying package fragment: " + fragment.getPath()); */ compilationUnit = fragment.getCompilationUnit(resourceName); if (compilationUnit != null && compilationUnit.exists()) { return compilationUnit; } } } return null; }
From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.java
License:Open Source License
private void createNewClass(String fqcn) { int pos = fqcn.lastIndexOf('.'); String packageName = pos < 0 ? "" : fqcn.substring(0, pos); //$NON-NLS-1$ String className = pos <= 0 || pos >= fqcn.length() ? "" : fqcn.substring(pos + 1); //$NON-NLS-1$ // create the wizard page for the class creation, and configure it NewClassWizardPage page = new NewClassWizardPage(); // set the parent class page.setSuperClass(SdkConstants.CLASS_VIEW, true /* canBeModified */); // get the source folders as java elements. IPackageFragmentRoot[] roots = getPackageFragmentRoots(mEditorDelegate.getEditor().getProject(), false /*includeContainers*/, true /*skipGenFolder*/); IPackageFragmentRoot currentRoot = null; IPackageFragment currentFragment = null; int packageMatchCount = -1; for (IPackageFragmentRoot root : roots) { // Get the java element for the package. // This method is said to always return a IPackageFragment even if the // underlying folder doesn't exist... IPackageFragment fragment = root.getPackageFragment(packageName); if (fragment != null && fragment.exists()) { // we have a perfect match! we use it. currentRoot = root;//from w w w . j a v a 2s .com currentFragment = fragment; packageMatchCount = -1; break; } else { // we don't have a match. we look for the fragment with the best match // (ie the closest parent package we can find) try { IJavaElement[] children; children = root.getChildren(); for (IJavaElement child : children) { if (child instanceof IPackageFragment) { fragment = (IPackageFragment) child; if (packageName.startsWith(fragment.getElementName())) { // its a match. get the number of segments String[] segments = fragment.getElementName().split("\\."); //$NON-NLS-1$ if (segments.length > packageMatchCount) { packageMatchCount = segments.length; currentFragment = fragment; currentRoot = root; } } } } } catch (JavaModelException e) { // Couldn't get the children: we just ignore this package root. } } } ArrayList<IPackageFragment> createdFragments = null; if (currentRoot != null) { // if we have a perfect match, we set it and we're done. if (packageMatchCount == -1) { page.setPackageFragmentRoot(currentRoot, true /* canBeModified*/); page.setPackageFragment(currentFragment, true /* canBeModified */); } else { // we have a partial match. // create the package. We have to start with the first segment so that we // know what to delete in case of a cancel. try { createdFragments = new ArrayList<IPackageFragment>(); int totalCount = packageName.split("\\.").length; //$NON-NLS-1$ int count = 0; int index = -1; // skip the matching packages while (count < packageMatchCount) { index = packageName.indexOf('.', index + 1); count++; } // create the rest of the segments, except for the last one as indexOf will // return -1; while (count < totalCount - 1) { index = packageName.indexOf('.', index + 1); count++; createdFragments.add(currentRoot.createPackageFragment(packageName.substring(0, index), true /* force*/, new NullProgressMonitor())); } // create the last package createdFragments.add(currentRoot.createPackageFragment(packageName, true /* force*/, new NullProgressMonitor())); // set the root and fragment in the Wizard page page.setPackageFragmentRoot(currentRoot, true /* canBeModified*/); page.setPackageFragment(createdFragments.get(createdFragments.size() - 1), true /* canBeModified */); } catch (JavaModelException e) { // If we can't create the packages, there's a problem. // We revert to the default package for (IPackageFragmentRoot root : roots) { // Get the java element for the package. // This method is said to always return a IPackageFragment even if the // underlying folder doesn't exist... IPackageFragment fragment = root.getPackageFragment(packageName); if (fragment != null && fragment.exists()) { page.setPackageFragmentRoot(root, true /* canBeModified*/); page.setPackageFragment(fragment, true /* canBeModified */); break; } } } } } else if (roots.length > 0) { // if we haven't found a valid fragment, we set the root to the first source folder. page.setPackageFragmentRoot(roots[0], true /* canBeModified*/); } // if we have a starting class name we use it if (className != null) { page.setTypeName(className, true /* canBeModified*/); } // create the action that will open it the wizard. OpenNewClassWizardAction action = new OpenNewClassWizardAction(); action.setConfiguredWizardPage(page); action.run(); IJavaElement element = action.getCreatedElement(); if (element == null) { // lets delete the packages we created just for this. // we need to start with the leaf and go up if (createdFragments != null) { try { for (int i = createdFragments.size() - 1; i >= 0; i--) { createdFragments.get(i).delete(true /* force*/, new NullProgressMonitor()); } } catch (JavaModelException e) { e.printStackTrace(); } } } }