/*******************************************************************************
 * Copyright (c) 2012 Mathias Kinzler <mathias.kinzler@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.ui.internal.dialogs;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.core.op.RenameBranchOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.ValidationUtils;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Allows to rename a single branch
 */
public class BranchRenameDialog extends TitleAreaDialog {
	private final Repository repository;

	private final Ref branchToRename;

	private Text name;

	/**
	 * @param parentShell
	 * @param repository
	 * @param branchToRename
	 *            the branch; name must start with {@link Constants#R_HEADS} or
	 *            {@link Constants#R_REMOTES}
	 */
	public BranchRenameDialog(Shell parentShell, Repository repository,
			Ref branchToRename) {
		super(parentShell);
		this.repository = repository;
		this.branchToRename = branchToRename;
		setHelpAvailable(false);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(main);
		GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5)
				.applyTo(main);
		new Label(main, SWT.NONE)
				.setText(UIText.BranchRenameDialog_NewNameLabel);
		name = new Text(main, SWT.BORDER);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(name);
		return main;
	}

	@Override
	public void create() {
		super.create();
		setTitle(UIText.BranchRenameDialog_Title);
		String oldName = branchToRename.getName();
		String prefix;
		if (oldName.startsWith(Constants.R_HEADS))
			prefix = Constants.R_HEADS;
		else if (oldName.startsWith(Constants.R_REMOTES))
			prefix = Constants.R_REMOTES;
		else
			prefix = null;
		String shortName = null;
		if (prefix != null) {
			shortName = Repository.shortenRefName(branchToRename
					.getName());
			setMessage(NLS.bind(UIText.BranchRenameDialog_Message, shortName));
			final IInputValidator inputValidator = ValidationUtils
					.getRefNameInputValidator(repository, prefix, true);
			name.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					String error = inputValidator.isValid(name.getText());
					setErrorMessage(error);
					getButton(OK).setEnabled(error == null);
				}
			});
		} else
			setErrorMessage(NLS.bind(
					UIText.BranchRenameDialog_WrongPrefixErrorMessage, oldName));

		if (shortName != null) {
			name.setText(shortName);
			name.setSelection(0, shortName.length());
		}

		getButton(OK).setEnabled(false);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(UIText.BranchRenameDialog_WindowTitle);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == OK)
			try {
				String newName = name.getText();
				new RenameBranchOperation(repository, branchToRename, newName)
						.execute(null);
			} catch (CoreException e) {
				Activator.handleError(
						UIText.BranchRenameDialog_RenameExceptionMessage, e,
						true);
				return;
			}
		super.buttonPressed(buttonId);
	}
}

@@ -3044,16 +3044,226 @@
 ame));%0A%09
+%09%7D else%0A%09%09%09setErrorMessage(NLS.bind(%0A%09%09%09%09%09UIText.BranchRenameDialog_WrongPrefixErrorMessage, oldName));%0A%0A%09%09if (shortName != null) %7B%0A%09%09%09name.setText(shortName);%0A%09%09%09name.setSelection(0, shortName.length());%0A%09%09%7D%0A%0A
 %09%09final 
@@ -3311,17 +3311,16 @@
 onUtils%0A
-%09
 %09%09%09%09.get
@@ -3366,25 +3366,24 @@
 x, true);%0A%09%09
-%09
 name.addModi
@@ -3419,17 +3419,16 @@
 () %7B%0A%09%09%09
-%09
 public v
@@ -3459,17 +3459,16 @@
 nt e) %7B%0A
-%09
 %09%09%09%09Stri
@@ -3522,17 +3522,16 @@
 ));%0A%09%09%09%09
-%09
 setError
@@ -3546,17 +3546,16 @@
 error);%0A
-%09
 %09%09%09%09getB
@@ -3598,227 +3598,15 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D);
-%0A%09%09%7D else%0A%09%09%09setErrorMessage(NLS.bind(%0A%09%09%09%09%09UIText.BranchRenameDialog_WrongPrefixErrorMessage, oldName));%0A%0A%09%09if (shortName != null) %7B%0A%09%09%09name.setText(shortName);%0A%09%09%09name.setSelection(0, shortName.length());%0A%09%09%7D
 %0A%0A%09%09

Rename branch dialog should not show an error when freshly opened