/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.internal.ui.javaeditor;


import org.eclipse.core.runtime.CoreException;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace;

import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
import org.eclipse.ui.texteditor.MarkerAnnotation;

import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.JavaCore;


/**
 *
 */
public class ClassFileMarkerAnnotationModel extends AbstractMarkerAnnotationModel implements IResourceChangeListener {

	protected IClassFile fClassFile;
	protected IWorkspace fWorkspace;
	protected IResource fMarkerResource;
	protected boolean fChangesApplied;


	public ClassFileMarkerAnnotationModel(IResource markerResource) {
		super();
		fMarkerResource= markerResource;
		fWorkspace= fMarkerResource.getWorkspace();
	}

	public void setClassFile(IClassFile classFile) {
		fClassFile= classFile;
	}

	/**
	 * @see AbstractMarkerAnnotationModel#isAcceptable
	 */
	@Override
	protected boolean isAcceptable(IMarker marker) {
		try {
			return JavaCore.isReferencedBy(fClassFile, marker);
		} catch (CoreException x) {
			handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_isAcceptable);
			return false;
		}
	}

	protected boolean isAffected(IMarkerDelta markerDelta) {
		try {
			return JavaCore.isReferencedBy(fClassFile, markerDelta);
		} catch (CoreException x) {
			handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_isAffected);
			return false;
		}
	}

	/**
	 * @see AbstractMarkerAnnotationModel#createMarkerAnnotation(IMarker)
	 */
	@Override
	protected MarkerAnnotation createMarkerAnnotation(IMarker marker) {
		return new JavaMarkerAnnotation(marker);
	}

	/**
	 * @see AbstractMarkerAnnotationModel#listenToMarkerChanges(boolean)
	 */
	@Override
	protected void listenToMarkerChanges(boolean listen) {
		if (listen)
			fWorkspace.addResourceChangeListener(this);
		else
			fWorkspace.removeResourceChangeListener(this);
	}

	/**
	 * @see AbstractMarkerAnnotationModel#deleteMarkers(IMarker[])
	 */
	@Override
	protected void deleteMarkers(IMarker[] markers) throws CoreException {
		// empty as class files are read only
	}

	/**
	 * @see AbstractMarkerAnnotationModel#retrieveMarkers()
	 */
	@Override
	protected IMarker[] retrieveMarkers() throws CoreException {
		if (fMarkerResource != null) {
			IMarker[] workspaceMarkers= fWorkspace.getRoot().findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO);
			IMarker[] resourceMarkers= fMarkerResource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO);
			int workspaceMarkersLength= workspaceMarkers.length;
			if (workspaceMarkersLength == 0)
				return resourceMarkers;

			int resourceMarkersLength= resourceMarkers.length;
			if (resourceMarkersLength == 0)
				return workspaceMarkers;

			IMarker[] result= new IMarker[resourceMarkersLength + workspaceMarkersLength];
			System.arraycopy(resourceMarkers, 0, result, 0, resourceMarkersLength);
			System.arraycopy(workspaceMarkers, 0, result, resourceMarkersLength, workspaceMarkersLength);
			return result;
		}
		return null;
	}

	private void checkDeltas(IMarkerDelta[] markerDeltas) throws CoreException {
		for (IMarkerDelta markerDelta : markerDeltas) {
			if (isAffected(markerDelta)) {
				IMarker marker= markerDelta.getMarker();
				switch (markerDelta.getKind()) {
					case IResourceDelta.ADDED :
						addMarkerAnnotation(marker);
						fChangesApplied= true;
						break;
					case IResourceDelta.REMOVED :
						removeMarkerAnnotation(marker);
						fChangesApplied= true;
						break;
					case IResourceDelta.CHANGED:
						modifyMarkerAnnotation(marker);
						fChangesApplied= true;
						break;
				}
			}
		}
	}

	/**
	 * @see IResourceChangeListener#resourceChanged
	 */
	@Override
	public void resourceChanged(IResourceChangeEvent e) {
		try {
			IMarkerDelta[] deltas= e.findMarkerDeltas(null, true);
			if (deltas != null) {
				fChangesApplied= false;
				checkDeltas(deltas);
				if (fChangesApplied)
					fireModelChanged();
			}
		} catch (CoreException x) {
			handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_resourceChanged);
		}
	}
}

