use of org.eclipse.jdt.core.IPackageDeclaration in project che by eclipse.
the class CorrectPackageDeclarationProposal method getName.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeCorrectionProposal#getName()
*/
@Override
public String getName() {
ICompilationUnit cu = getCompilationUnit();
IPackageFragment parentPack = (IPackageFragment) cu.getParent();
try {
IPackageDeclaration[] decls = cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName()));
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
} catch (JavaModelException e) {
JavaPlugin.log(e);
}
return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
use of org.eclipse.jdt.core.IPackageDeclaration in project che by eclipse.
the class SearchManager method performFindUsageSearch.
private FindUsagesResponse performFindUsageSearch(IJavaElement element) throws JavaModelException, BadLocationException {
JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
boolean isInsideJRE = factory.isInsideJRE(element);
JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope"));
NewSearchUI.runQueryInForeground(null, query);
ISearchResult result = query.getSearchResult();
JavaSearchResult javaResult = ((JavaSearchResult) result);
FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class);
Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>();
JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult);
for (Object o : javaResult.getElements()) {
IJavaElement javaElement = (IJavaElement) o;
IDocument document = null;
if (javaElement instanceof IMember) {
IMember member = ((IMember) javaElement);
if (member.isBinary()) {
if (member.getClassFile().getSource() != null) {
document = new Document(member.getClassFile().getSource());
}
} else {
document = getDocument(member.getCompilationUnit());
}
} else if (javaElement instanceof IPackageDeclaration) {
ICompilationUnit ancestor = (ICompilationUnit) (javaElement).getAncestor(IJavaElement.COMPILATION_UNIT);
document = getDocument(ancestor);
}
converter.addElementToProjectHierarchy(javaElement);
Match[] matches = javaResult.getMatches(o);
List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>();
for (Match match : matches) {
org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory.newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class);
if (document != null) {
IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset());
int offsetInLine = match.getOffset() - lineInformation.getOffset();
Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine).withLength(match.getLength());
dtoMatch.setMatchInLine(matchInLine);
dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset()));
dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength()));
}
dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset()).withLength(match.getLength()));
matchList.add(dtoMatch);
}
mapMaches.put(javaElement.getHandleIdentifier(), matchList);
}
List<JavaProject> projects = converter.getProjects();
response.setProjects(projects);
response.setMatches(mapMaches);
response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT));
return response;
}
use of org.eclipse.jdt.core.IPackageDeclaration in project bndtools by bndtools.
the class ComponentDecorator method decorate.
@Override
public void decorate(Object element, IDecoration decoration) {
try {
if (element instanceof CompilationUnit) {
CompilationUnit unit = (CompilationUnit) element;
if (!unit.getJavaProject().getProject().hasNature(BndtoolsConstants.NATURE_ID)) {
return;
}
if (!isComponentInImports(unit)) {
return;
}
IPackageDeclaration[] decs = unit.getPackageDeclarations();
if (decs != null && decs.length > 0) {
IPackageDeclaration dec = decs[0];
if (dec != null) {
boolean found = false;
String customText = null;
for (IMarker marker : unit.getResource().findMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE)) {
found = true;
customText = marker.getAttribute(IMarker.MESSAGE).toString();
}
if (found) {
decoration.addOverlay(componentIcon);
if (customText != null) {
if (customText.equals("OSGi Component")) {
decoration.addSuffix(" [Component]");
} else {
decoration.addSuffix(" [" + customText + "]");
}
}
}
}
}
} else if (element instanceof SourceType) {
SourceType type = (SourceType) element;
if (!type.getJavaProject().getProject().hasNature(BndtoolsConstants.NATURE_ID)) {
return;
}
if (!isComponentInImports(type.getCompilationUnit())) {
return;
}
boolean found = false;
String customText = null;
for (IMarker marker : type.getCompilationUnit().getResource().findMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE)) {
found = true;
customText = marker.getAttribute(IMarker.MESSAGE).toString();
}
if (found) {
decoration.addOverlay(componentIcon);
if (customText != null) {
if (customText.equals("OSGi Component")) {
decoration.addSuffix(" [Component]");
} else {
decoration.addSuffix(" [" + customText + "]");
}
}
}
} else if (element instanceof IPackageFragment) {
IPackageFragment frag = (IPackageFragment) element;
if (!frag.getJavaProject().getProject().hasNature(BndtoolsConstants.NATURE_ID)) {
return;
}
IResource resource = (IResource) frag.getAdapter(IResource.class);
if (resource != null && countComponents(resource)) {
decoration.addOverlay(componentIcon);
}
}
} catch (CoreException e) {
logger.logError("Component Decorator error", e);
}
}
use of org.eclipse.jdt.core.IPackageDeclaration in project che by eclipse.
the class ReorgCorrectionsSubProcessor method getWrongPackageDeclNameProposals.
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
boolean isLinked = cu.getResource().isLinked();
// correct package declaration
// bug 38357
int relevance = cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION;
proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));
// move to package
IPackageDeclaration[] packDecls = cu.getPackageDeclarations();
//$NON-NLS-1$
String newPackName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(cu);
IPackageFragment newPack = root.getPackageFragment(newPackName);
ICompilationUnit newCU = newPack.getCompilationUnit(cu.getElementName());
if (!newCU.exists() && !isLinked) {
String label;
if (newPack.isDefaultPackage()) {
label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
} else {
String packageLabel = JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
}
CompositeChange composite = new CompositeChange(label);
composite.add(new CreatePackageChange(newPack));
composite.add(new MoveCompilationUnitChange(cu, newPack));
proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
}
}
use of org.eclipse.jdt.core.IPackageDeclaration in project che by eclipse.
the class CorrectPackageDeclarationProposal method addEdits.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
* .TextBuffer)
*/
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
ICompilationUnit cu = getCompilationUnit();
IPackageFragment parentPack = (IPackageFragment) cu.getParent();
IPackageDeclaration[] decls = cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
for (int i = 0; i < decls.length; i++) {
ISourceRange range = decls[i].getSourceRange();
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
}
return;
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
String lineDelim = StubUtility.getLineDelimiterUsed(cu);
//$NON-NLS-1$
String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
root.addChild(new InsertEdit(0, str));
return;
}
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Aggregations