/*******************************************************************************
 * Copyright (c) 2008, 2018 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.rpm.ui.editor.markers;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog;

public abstract class SpecfileMarkerHandler {

	IDocument document;
	IFile file;

	public SpecfileMarkerHandler(IFile file, IDocument document) {
		this.file = file;
		this.document = document;
	}

	abstract String getMarkerID();

	protected Integer getCharOffset(int lineNumber, int columnNumber) {
		try {
			return document.getLineOffset(lineNumber) + columnNumber;
		} catch (BadLocationException e) {
			SpecfileLog.logError(e);
			return null;
		}
	}

	public void removeExistingMarkers() {
		if (file == null || !file.exists()) {
			return;
		}

		try {
			file.deleteMarkers(getMarkerID(), true, IResource.DEPTH_ZERO);
		} catch (CoreException e1) {
			SpecfileLog.logError(e1);
		}
	}

	public void setFile(IFile file) {
		this.file = file;
	}

	public void setDocument(IDocument document) {
		this.document = document;
	}
}
