/*******************************************************************************
 * Copyright (c) 2005-2009 Bastian Doetsch and others.
 * 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
 *
 * Contributors:
 * bastian	implementation
 *******************************************************************************/
package com.vectrace.MercurialEclipse.ui;

import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.dialogs.PropertyPage;

import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
import com.vectrace.MercurialEclipse.storage.HgRepositoryLocation;
import com.vectrace.MercurialEclipse.storage.HgRepositoryLocationManager;
import com.vectrace.MercurialEclipse.team.MercurialUtilities;

/**
 * @author bastian
 * 
 */
public class HgProjectPropertyPage extends PropertyPage {
    private IProject project;
    private Group reposGroup;

    /**
     * 
     */
    public HgProjectPropertyPage() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse
     * .swt.widgets.Composite)
     */
    @Override
    protected Control createContents(Composite parent) {
        this.project = (IProject) super.getElement();
        
        // create gui elements
        Composite comp = SWTWidgetHelper.createComposite(parent, 1);

        if (!MercurialUtilities.hgIsTeamProviderFor(project, false)) {
            setMessage("This project doesn't use MercurialEclipse as Team provider.");
            return comp;
        }
        
        
        reposGroup = SWTWidgetHelper.createGroup(comp,
                "Repository paths:", 1,
                        GridData.FILL_HORIZONTAL);
        
        // each repository gets a label with its logical name and a combo for
        // setting it within MercurialEclipse

        HgRepositoryLocationManager mgr = MercurialEclipsePlugin
                .getRepoManager();
        Set<HgRepositoryLocation> repos = mgr
                .getAllProjectRepoLocations(project);
        for (HgRepositoryLocation repo : repos) {
            Composite repoComposite = SWTWidgetHelper.createComposite(
                    reposGroup, 2);
            SWTWidgetHelper.createLabel(repoComposite,
                    repo.getLogicalName() == null ? "Unnamed" : repo
                            .getLogicalName());
            Combo combo = SWTWidgetHelper.createEditableCombo(repoComposite);
            combo.add(repo.getLocation());
            combo.select(0);
        }
        return comp;
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.PreferencePage#performOk()
     */
    @Override
    public boolean performOk() {
        if (!MercurialUtilities.hgIsTeamProviderFor(project, false)) {
            return super.performOk();
        }
        IPreferenceStore store = MercurialEclipsePlugin.getDefault().getPreferenceStore();
        
        Control[] composites = reposGroup.getChildren();
        
        for (Control control : composites) {
            Composite comp = (Composite) control;
            Control[] controls = comp.getChildren();
            store.putValue("repository." + controls[0].getData(),
                    ((Combo) controls[1]).getText());
        }
        return super.performOk();
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.PreferencePage#performApply()
     */
    @Override
    protected void performApply() {
        this.performOk();
    }
}

@@ -636,41 +636,175 @@
 pse.
-jface.preference.IPreferenceStore
+core.runtime.CoreException;%0Aimport org.eclipse.jface.preference.IPreferenceStore;%0Aimport org.eclipse.swt.events.MouseEvent;%0Aimport org.eclipse.swt.events.MouseListener
 ;%0Aim
@@ -841,16 +841,55 @@
 idData;%0A
+import org.eclipse.swt.widgets.Button;%0A
 import o
@@ -2465,16 +2465,22 @@
         
+final 
 HgReposi
@@ -2676,16 +2676,22 @@
    for (
+final 
 HgReposi
@@ -2822,17 +2822,17 @@
 sGroup, 
-2
+3
 );%0A     
@@ -2994,24 +2994,24 @@
 calName());%0A
-
             
@@ -3076,16 +3076,821 @@
 osite);%0A
+            Button defaultButton = SWTWidgetHelper.createPushButton(%0A                    repoComposite,%0A                    %22Set as default%22, 1);%0A            defaultButton.addMouseListener(new MouseListener() %7B%0A%0A                public void mouseUp(MouseEvent e) %7B%0A                    try %7B%0A                        mgr.setDefaultProjectRepository(project, repo);%0A                    %7D catch (CoreException e1) %7B%0A                        MercurialEclipsePlugin.logError(e1);%0A                        setErrorMessage(e1.getLocalizedMessage());%0A                    %7D%0A                %7D%0A%0A                public void mouseDown(MouseEvent e) %7B%0A                    // TODO Auto-generated method stub%0A%0A                %7D%0A%0A                public void mouseDoubleClick(MouseEvent e) %7B%0A%0A                %7D%0A            %7D);%0A
         

Add button for setting default repository to project property page.