/*******************************************************************************
 * Copyright (c) 2008 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.pde.internal.ua.ui.editor.ctxhelp;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
import org.eclipse.pde.internal.core.text.IDocumentElementNode;
import org.eclipse.pde.internal.core.text.IDocumentRange;
import org.eclipse.pde.internal.ua.core.ctxhelp.text.CtxHelpModel;
import org.eclipse.pde.internal.ua.core.ctxhelp.text.CtxHelpObject;
import org.eclipse.pde.internal.ua.ui.PDEUserAssistanceUIPlugin;
import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
import org.eclipse.pde.internal.ui.editor.XMLSourcePage;

/**
 * Source page for the context help editor.  Displays the
 * xml source.
 * @since 3.4
 * @see CtxHelpEditor
 */
public class CtxHelpSourcePage extends XMLSourcePage {

	public CtxHelpSourcePage(PDEFormEditor editor, String id, String title) {
		super(editor, id, title);
	}

	@Override
	public boolean isQuickOutlineEnabled() {
		return true;
	}

	@Override
	public ViewerComparator createOutlineComparator() {
		return null;
	}

	@Override
	public ITreeContentProvider createOutlineContentProvider() {
		return new CtxHelpContentProvider();
	}

	@Override
	public ILabelProvider createOutlineLabelProvider() {
		return PDEUserAssistanceUIPlugin.getDefault().getLabelProvider();
	}

	@Override
	protected void setPartName(String partName) {
		super.setPartName(CtxHelpMessages.CtxHelpSourcePage_name);
	}

	@Override
	protected boolean isSelectionListener() {
		return true;
	}

	@Override
	public void updateSelection(Object object) {
		if ((object instanceof IDocumentElementNode) && !((IDocumentElementNode) object).isErrorNode()) {
			setSelectedObject(object);
			setHighlightRange((IDocumentElementNode) object, true);
			setSelectedRange((IDocumentElementNode) object, false);
		}
	}

	@Override
	protected IDocumentRange findRange() {
		if (getSelection() instanceof IDocumentElementNode) {
			return (IDocumentElementNode) getSelection();
		}

		return null;
	}

	@Override
	public IDocumentRange getRangeElement(int offset, boolean searchChildren) {
		CtxHelpObject root = ((CtxHelpModel) getInputContext().getModel()).getCtxHelpRoot();
		return findNode(root, offset, searchChildren);
	}

	@Override
	protected void synchronizeOutlinePage(int offset) {
		IDocumentRange rangeElement = getRangeElement(offset, true);
		updateHighlightRange(rangeElement);
		// TODO: MP: TEO: LOW: Generalize for parent - search children = true and handle attributes
		if (rangeElement instanceof IDocumentAttributeNode) {
			rangeElement = ((IDocumentAttributeNode) rangeElement).getEnclosingElement();
		}
		updateOutlinePageSelection(rangeElement);
	}

	@Override
	protected void initializeEditor() {
		super.initializeEditor();
		// TODO Fix help context
		//		setHelpContextId(IHelpContextIds.TOC_EDITOR);
	}
}
