Java Code Examples for org.eclipse.jdt.internal.corext.util.JavaModelUtil#isPrimary()

The following examples show how to use org.eclipse.jdt.internal.corext.util.JavaModelUtil#isPrimary() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isRenameAvailable(final ICompilationUnit unit) {
	if (unit == null) {
		return false;
	}
	if (!unit.exists()) {
		return false;
	}
	if (!JavaModelUtil.isPrimary(unit)) {
		return false;
	}
	if (unit.isReadOnly()) {
		return false;
	}
	return true;
}
 
Example 2
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isRenameAvailable(final ICompilationUnit unit) {
	if (unit == null)
		return false;
	if (!unit.exists())
		return false;
	if (!JavaModelUtil.isPrimary(unit))
		return false;
	if (unit.isReadOnly())
		return false;
	return true;
}
 
Example 3
Source File: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void updateSelectionProvider(IPageSite site) {
	ISelectionProvider provider= fOutlineViewer;
	if (fInput != null) {
		ICompilationUnit cu= (ICompilationUnit)fInput.getAncestor(IJavaElement.COMPILATION_UNIT);
		if (cu != null && !JavaModelUtil.isPrimary(cu))
			provider= new EmptySelectionProvider();
	}
	site.setSelectionProvider(provider);
}
 
Example 4
Source File: CompilationUnitDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected FileInfo createFileInfo(Object element) throws CoreException {
	ICompilationUnit original= null;
	if (element instanceof IFileEditorInput) {
		IFileEditorInput input= (IFileEditorInput) element;
		original= createCompilationUnit(input.getFile());
		if (original == null)
			return null;
	}

	FileInfo info= super.createFileInfo(element);
	if (!(info instanceof CompilationUnitInfo))
		return null;

	if (original == null)
		original= createFakeCompiltationUnit(element, false);
	if (original == null)
		return null;

	CompilationUnitInfo cuInfo= (CompilationUnitInfo) info;
	setUpSynchronization(cuInfo);

	IProblemRequestor requestor= cuInfo.fModel instanceof IProblemRequestor ? (IProblemRequestor) cuInfo.fModel : null;
	if (requestor instanceof IProblemRequestorExtension) {
		IProblemRequestorExtension extension= (IProblemRequestorExtension) requestor;
		extension.setIsActive(false);
		extension.setIsHandlingTemporaryProblems(isHandlingTemporaryProblems());
	}

	IResource resource= original.getResource();
	if (JavaModelUtil.isPrimary(original) && (resource == null || resource.exists()))
		original.becomeWorkingCopy(requestor, getProgressMonitor());
	cuInfo.fCopy= original;

	if (cuInfo.fModel instanceof CompilationUnitAnnotationModel)   {
		CompilationUnitAnnotationModel model= (CompilationUnitAnnotationModel) cuInfo.fModel;
		model.setCompilationUnit(cuInfo.fCopy);
	}

	if (cuInfo.fModel != null)
		cuInfo.fModel.addAnnotationModelListener(fGlobalAnnotationModelListener);

	return cuInfo;
}
 
Example 5
Source File: CompilationUnitDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected DocumentProviderOperation createSaveOperation(final Object element, final IDocument document, final boolean overwrite) throws CoreException {
	final FileInfo info= getFileInfo(element);
	if (info instanceof CompilationUnitInfo) {

		// Delegate handling of non-primary CUs
		ICompilationUnit cu= ((CompilationUnitInfo)info).fCopy;
		if (cu != null && !JavaModelUtil.isPrimary(cu))
			return super.createSaveOperation(element, document, overwrite);

		if (info.fTextFileBuffer.getDocument() != document) {
			// the info exists, but not for the given document
			// -> saveAs was executed with a target that is already open
			// in another editor
			// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=85519
			Status status= new Status(IStatus.WARNING, EditorsUI.PLUGIN_ID, IStatus.ERROR, JavaEditorMessages.CompilationUnitDocumentProvider_saveAsTargetOpenInEditor, null);
			throw new CoreException(status);
		}

		return new DocumentProviderOperation() {
			/*
			 * @see org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
			 */
			@Override
			protected void execute(IProgressMonitor monitor) throws CoreException {
				commitWorkingCopy(monitor, element, (CompilationUnitInfo) info, overwrite);
			}
			/*
			 * @see org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#getSchedulingRule()
			 */
			@Override
			public ISchedulingRule getSchedulingRule() {
				if (info.fElement instanceof IFileEditorInput) {
					IFile file= ((IFileEditorInput) info.fElement).getFile();
					return computeSchedulingRule(file);
				} else
					return null;
			}
		};
	}

	return null;
}
 
Example 6
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Object getAdapter(Class required) {

	if (IContentOutlinePage.class.equals(required)) {
		if (fOutlinePage == null && getSourceViewer() != null && isCalledByOutline())
			fOutlinePage= createOutlinePage();
		return fOutlinePage;
	}

	if (IEncodingSupport.class.equals(required))
		return fEncodingSupport;

	if (required == IShowInTargetList.class) {
		return new IShowInTargetList() {
			public String[] getShowInTargetIds() {
				return new String[] { JavaUI.ID_PACKAGES, IPageLayout.ID_OUTLINE, JavaPlugin.ID_RES_NAV };
			}

		};
	}

	if (required == IShowInSource.class) {
		IJavaElement inputJE= getInputJavaElement();
		if (inputJE instanceof ICompilationUnit && !JavaModelUtil.isPrimary((ICompilationUnit) inputJE))
			return null;

		return new IShowInSource() {
			public ShowInContext getShowInContext() {
				return new ShowInContext(null, null) {
					/*
					 * @see org.eclipse.ui.part.ShowInContext#getInput()
					 * @since 3.4
					 */
					@Override
					public Object getInput() {
						if (isBreadcrumbActive())
							return null;

						return getEditorInput();
					}

					/*
					 * @see org.eclipse.ui.part.ShowInContext#getSelection()
					 * @since 3.3
					 */
					@Override
					public ISelection getSelection() {
						if (isBreadcrumbActive())
							return getBreadcrumb().getSelectionProvider().getSelection();

						try {
							IJavaElement je= SelectionConverter.getElementAtOffset(JavaEditor.this);
							if (je != null)
								return new StructuredSelection(je);
							return null;
						} catch (JavaModelException ex) {
							return null;
						}
					}
				};
			}
		};
	}

	if (required == IJavaFoldingStructureProvider.class)
		return fProjectionModelUpdater;

	if (fProjectionSupport != null) {
		Object adapter= fProjectionSupport.getAdapter(getSourceViewer(), required);
		if (adapter != null)
			return adapter;
	}

	if (required == IContextProvider.class) {
		if (isBreadcrumbActive()) {
			return JavaUIHelp.getHelpContextProvider(this, IJavaHelpContextIds.JAVA_EDITOR_BREADCRUMB);
		} else {
			return JavaUIHelp.getHelpContextProvider(this, IJavaHelpContextIds.JAVA_EDITOR);
		}
	}

	return super.getAdapter(required);
}
 
Example 7
Source File: TypeHierarchyLifeCycle.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void processDelta(IJavaElementDelta delta, ArrayList<IType> changedTypes) {
	IJavaElement element= delta.getElement();
	switch (element.getElementType()) {
		case IJavaElement.TYPE:
			processTypeDelta((IType) element, changedTypes);
			processChildrenDelta(delta, changedTypes); // (inner types)
			break;
		case IJavaElement.JAVA_MODEL:
		case IJavaElement.JAVA_PROJECT:
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
		case IJavaElement.PACKAGE_FRAGMENT:
			processChildrenDelta(delta, changedTypes);
			break;
		case IJavaElement.COMPILATION_UNIT:
			ICompilationUnit cu= (ICompilationUnit)element;
			if (!JavaModelUtil.isPrimary(cu)) {
				return;
			}

			if (delta.getKind() == IJavaElementDelta.CHANGED && isPossibleStructuralChange(delta.getFlags())) {
				try {
					if (cu.exists()) {
						IType[] types= cu.getAllTypes();
						for (int i= 0; i < types.length; i++) {
							processTypeDelta(types[i], changedTypes);
						}
					}
				} catch (JavaModelException e) {
					JavaPlugin.log(e);
				}
			} else {
				processChildrenDelta(delta, changedTypes);
			}
			break;
		case IJavaElement.CLASS_FILE:
			if (delta.getKind() == IJavaElementDelta.CHANGED) {
				IType type= ((IClassFile) element).getType();
				processTypeDelta(type, changedTypes);
			} else {
				processChildrenDelta(delta, changedTypes);
			}
			break;
	}
}
 
Example 8
Source File: InterfaceIndicatorLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void processDelta(IJavaElementDelta delta, List<IJavaElement> result) {
	IJavaElement elem= delta.getElement();

	boolean isChanged= delta.getKind() == IJavaElementDelta.CHANGED;
	boolean isRemoved= delta.getKind() == IJavaElementDelta.REMOVED;
	int flags= delta.getFlags();

	switch (elem.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			if (isRemoved || (isChanged &&
					(flags & IJavaElementDelta.F_CLOSED) != 0)) {
				return;
			}
			processChildrenDelta(delta, result);
			return;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			if (isRemoved || (isChanged && (
					(flags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0 ||
					(flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0))) {
				return;
			}
			processChildrenDelta(delta, result);
			return;
		case IJavaElement.PACKAGE_FRAGMENT:
			if (isRemoved)
				return;
			processChildrenDelta(delta, result);
			return;
		case IJavaElement.TYPE:
		case IJavaElement.CLASS_FILE:
			return;
		case IJavaElement.JAVA_MODEL:
			processChildrenDelta(delta, result);
			return;
		case IJavaElement.COMPILATION_UNIT:
			// Not the primary compilation unit. Ignore it
			if (!JavaModelUtil.isPrimary((ICompilationUnit) elem)) {
				return;
			}

			if (isChanged &&  ((flags & IJavaElementDelta.F_CONTENT) != 0 || (flags & IJavaElementDelta.F_FINE_GRAINED) != 0)) {
				if (delta.getAffectedChildren().length == 0)
					return;

				result.add(elem);
			}
			return;
		default:
			// fields, methods, imports ect
			return;
	}
}
 
Example 9
Source File: WorkingCopyManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the working copy remembered for the compilation unit encoded in the
 * given editor input.
 * <p>
 * Note: This method must not be part of the public {@link IWorkingCopyManager} API.
 * </p>
 *
 * @param input the editor input
 * @param primaryOnly if <code>true</code> only primary working copies will be returned
 * @return the working copy of the compilation unit, or <code>null</code> if the
 *   input does not encode an editor input, or if there is no remembered working
 *   copy for this compilation unit
 * @since 3.2
 */
public ICompilationUnit getWorkingCopy(IEditorInput input, boolean primaryOnly) {
	ICompilationUnit unit= fMap == null ? null : fMap.get(input);
	if (unit == null)
		unit= fDocumentProvider.getWorkingCopy(input);
	if (unit != null && (!primaryOnly || JavaModelUtil.isPrimary(unit)))
		return unit;
	return null;
}