/*******************************************************************************
 * 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.compare;

import org.eclipse.team.ui.TeamUI;
import org.eclipse.team.ui.history.HistoryPageCompareEditorInput;

import org.eclipse.core.resources.IFile;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.PlatformUI;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareUI;

import org.eclipse.jdt.core.IMember;

import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;


/**
 * Provides "Replace from local history" for Java elements.
 */
class JavaCompareWithEditionActionImpl extends JavaHistoryActionImpl {

	private static boolean USE_MODAL_COMPARE = false;

	JavaCompareWithEditionActionImpl() {
		super(false);
	}

	@Override
	public void run(ISelection selection) {

		IMember input= getEditionElement(selection);
		if (input == null) {
			MessageDialog.openInformation(getShell(), CompareMessages.CompareWithHistory_title, CompareMessages.CompareWithHistory_invalidSelectionMessage);
			return;
		}

		JavaElementHistoryPageSource pageSource = JavaElementHistoryPageSource.getInstance();
		final IFile file= pageSource.getFile(input);
		if (file == null) {
			MessageDialog.openError(getShell(), CompareMessages.CompareWithHistory_title, CompareMessages.CompareWithHistory_internalErrorMessage);
			return;
		}

		if (USE_MODAL_COMPARE) {
			CompareConfiguration cc = new CompareConfiguration();
			cc.setLeftEditable(false);
			cc.setRightEditable(false);
			HistoryPageCompareEditorInput ci = new HistoryPageCompareEditorInput(cc, pageSource, input);
			ci.setTitle(CompareMessages.JavaCompareWithEditionActionImpl_0);
			ci.setHelpContextId(IJavaHelpContextIds.COMPARE_ELEMENT_WITH_HISTORY_DIALOG);
			CompareUI.openCompareDialog(ci);
		} else {
			TeamUI.showHistoryFor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, pageSource);
		}
	}
}

