<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2006 Subclipse project 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:
 *     Subclipse project committers - initial API and implementation
 ******************************************************************************/
package org.tigris.subversion.subclipse.ui.dialogs;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.tigris.subversion.subclipse.core.ISVNLocalResource;
import org.tigris.subversion.subclipse.core.ISVNRemoteFolder;
import org.tigris.subversion.subclipse.core.ISVNRemoteResource;
import org.tigris.subversion.subclipse.core.ISVNRepositoryLocation;
import org.tigris.subversion.subclipse.core.SVNProviderPlugin;
import org.tigris.subversion.subclipse.core.history.Alias;
import org.tigris.subversion.subclipse.core.history.AliasManager;
import org.tigris.subversion.subclipse.core.history.Branches;
import org.tigris.subversion.subclipse.core.history.Tags;
import org.tigris.subversion.subclipse.core.repo.ISVNListener;
import org.tigris.subversion.subclipse.core.resources.RepositoryRootFolder;
import org.tigris.subversion.subclipse.core.resources.SVNWorkspaceRoot;
import org.tigris.subversion.subclipse.ui.IHelpContextIds;
import org.tigris.subversion.subclipse.ui.ISVNUIConstants;
import org.tigris.subversion.subclipse.ui.Policy;
import org.tigris.subversion.subclipse.ui.SVNUIPlugin;
import org.tigris.subversion.subclipse.ui.actions.CreateRemoteFolderAction;
import org.tigris.subversion.subclipse.ui.actions.DeleteRemoteResourceAction;
import org.tigris.subversion.subclipse.ui.repository.RepositoryFilters;
import org.tigris.subversion.subclipse.ui.repository.model.AllRootsElement;
import org.tigris.subversion.subclipse.ui.repository.model.RemoteContentProvider;

public class ChooseUrlDialog extends TrayDialog {
    private static final int LIST_HEIGHT_HINT = 250;
    private static final int LIST_WIDTH_HINT = 450;

    private TreeViewer treeViewer;
    private Action refreshAction;
    private Action newFolderAction;
    private Action deleteFolderAction;

    private String url;
    private String name;
    private String[] urls;
    private String[] names;
    private IResource resource;
    private String message;
    private boolean multipleSelect = false;
    private ISVNRepositoryLocation repositoryLocation;
    private boolean foldersOnly = false;
    private boolean includeBranchesAndTags = true;
    
    private IDialogSettings settings;
    private RemoteContentProvider contentProvider;
    
    private static boolean needsRefresh = true;
    
    static {
    	ISVNListener repositoryListener = new ISVNListener() {
			public void remoteResourceCopied(ISVNRemoteResource source, ISVNRemoteFolder destination) {
				needsRefresh = true;
			}
			public void remoteResourceCreated(ISVNRemoteFolder parent, String resourceName) {
				needsRefresh = true;
			}
			public void remoteResourceDeleted(ISVNRemoteResource resource) {
				needsRefresh = true;
			}
			public void remoteResourceMoved(ISVNRemoteResource resource, ISVNRemoteFolder destinationFolder, String destinationResourceName) {
				needsRefresh = true;
			}
			public void repositoryAdded(ISVNRepositoryLocation root) {
				needsRefresh = true;
			}
			public void repositoryModified(ISVNRepositoryLocation root) {
				needsRefresh = true;
			}
			public void repositoryRemoved(ISVNRepositoryLocation root) {
				needsRefresh = true;
			}  		
    	};
    	SVNProviderPlugin.getPlugin().getRepositoryResourcesManager().addRepositoryListener(repositoryListener);
    }

    public ChooseUrlDialog(Shell parentShell, IResource resource) {
        super(parentShell);
		int shellStyle = getShellStyle();
		setShellStyle(shellStyle | SWT.RESIZE);
        this.resource = resource;
        refreshAction = new Action(Policy.bind("ChooseUrlDialog.refresh"), SVNUIPlugin.getPlugin().getImageDescriptor(ISVNUIConstants.IMG_REFRESH_ENABLED)) { //$NON-NLS-1$
            public void run() {
                refreshViewer(true);
            }
        };
        newFolderAction = new Action(Policy.bind("NewRemoteFolderWizard.title")) { //$NON-NLS-1$
            public void run() {
                CreateRemoteFolderAction createAction = new CreateRemoteFolderAction();
                createAction.selectionChanged(null, treeViewer.getSelection());
                createAction.run(null);
                refreshViewer(true);
            }
        };
        deleteFolderAction = new Action(Policy.bind("ChooseUrlDialog.delete")) { //$NON-NLS-1$
            public void run() {
                DeleteRemoteResourceAction deleteAction = new DeleteRemoteResourceAction();
                deleteAction.selectionChanged(null, treeViewer.getSelection());
                deleteAction.run(null);
                refreshViewer(true);
            }
        };
        settings = SVNUIPlugin.getPlugin().getDialogSettings();
        if (needsRefresh) refreshRepositoriesFolders();
    }

	protected Control createDialogArea(Composite parent) {
		getShell().setText(Policy.bind("ChooseUrlDialog.title")); //$NON-NLS-1$
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		if (message != null) {
			Label messageLabel = new Label(composite, SWT.NONE);
			messageLabel.setText(message);
		}

		if (multipleSelect) treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
		else treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        contentProvider = new RemoteContentProvider();
        contentProvider.setIncludeBranchesAndTags(includeBranchesAndTags);
        contentProvider.setResource(resource);
        treeViewer.setContentProvider(contentProvider);
        if( foldersOnly )
        	treeViewer.addFilter(RepositoryFilters.FOLDERS_ONLY);

        //        treeViewer.setLabelProvider(new WorkbenchLabelProvider());
        treeViewer.setLabelProvider(new RemoteLabelProvider());
        if (repositoryLocation == null) {
	        if (resource == null) treeViewer.setInput(new AllRootsElement());
	        else {
	            ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);
	            ISVNRepositoryLocation repository = svnResource.getRepository();

	            if (!repository.getUrl().toString().equals(repository.getRepositoryRoot().toString())) {
	            	RepositoryRootFolder rootFolder = new RepositoryRootFolder(repository, repository.getRepositoryRoot(), repository.getRootFolder().getRevision());
	            	contentProvider.setRootFolder(rootFolder);
	            }
	            
	            if (repository == null) treeViewer.setInput(new AllRootsElement());
	            else treeViewer.setInput(svnResource.getRepository());
	        }
        } else treeViewer.setInput(repositoryLocation);

		GridData data = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
		data.heightHint = LIST_HEIGHT_HINT;
		data.widthHint = LIST_WIDTH_HINT;
		treeViewer.getControl().setLayoutData(data);
		
        // when F5 is pressed, refresh
        treeViewer.getControl().addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent event) {
                if (event.keyCode == SWT.F5) {
                    refreshAction.run();
                }
            }
        });		

        treeViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent e) {<commit_msg>Fix compare with Branch/Tag repository browse button to open on external repository rather than project repository when an external resource is being compared. Issue #: 1099<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2006 Subclipse project 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:
 *     Subclipse project committers - initial API and implementation
 ******************************************************************************/
package org.tigris.subversion.subclipse.ui.dialogs;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.tigris.subversion.subclipse.core.ISVNLocalResource;
import org.tigris.subversion.subclipse.core.ISVNRemoteFolder;
import org.tigris.subversion.subclipse.core.ISVNRemoteResource;
import org.tigris.subversion.subclipse.core.ISVNRepositoryLocation;
import org.tigris.subversion.subclipse.core.SVNException;
import org.tigris.subversion.subclipse.core.SVNProviderPlugin;
import org.tigris.subversion.subclipse.core.history.Alias;
import org.tigris.subversion.subclipse.core.history.AliasManager;
import org.tigris.subversion.subclipse.core.history.Branches;
import org.tigris.subversion.subclipse.core.history.Tags;
import org.tigris.subversion.subclipse.core.repo.ISVNListener;
import org.tigris.subversion.subclipse.core.resources.LocalResourceStatus;
import org.tigris.subversion.subclipse.core.resources.RepositoryRootFolder;
import org.tigris.subversion.subclipse.core.resources.SVNWorkspaceRoot;
import org.tigris.subversion.subclipse.ui.IHelpContextIds;
import org.tigris.subversion.subclipse.ui.ISVNUIConstants;
import org.tigris.subversion.subclipse.ui.Policy;
import org.tigris.subversion.subclipse.ui.SVNUIPlugin;
import org.tigris.subversion.subclipse.ui.actions.CreateRemoteFolderAction;
import org.tigris.subversion.subclipse.ui.actions.DeleteRemoteResourceAction;
import org.tigris.subversion.subclipse.ui.repository.RepositoryFilters;
import org.tigris.subversion.subclipse.ui.repository.model.AllRootsElement;
import org.tigris.subversion.subclipse.ui.repository.model.RemoteContentProvider;

public class ChooseUrlDialog extends TrayDialog {
    private static final int LIST_HEIGHT_HINT = 250;
    private static final int LIST_WIDTH_HINT = 450;

    private TreeViewer treeViewer;
    private Action refreshAction;
    private Action newFolderAction;
    private Action deleteFolderAction;

    private String url;
    private String name;
    private String[] urls;
    private String[] names;
    private IResource resource;
    private String message;
    private boolean multipleSelect = false;
    private ISVNRepositoryLocation repositoryLocation;
    private boolean foldersOnly = false;
    private boolean includeBranchesAndTags = true;
    
    private IDialogSettings settings;
    private RemoteContentProvider contentProvider;
    
    private static boolean needsRefresh = true;
    
    static {
    	ISVNListener repositoryListener = new ISVNListener() {
			public void remoteResourceCopied(ISVNRemoteResource source, ISVNRemoteFolder destination) {
				needsRefresh = true;
			}
			public void remoteResourceCreated(ISVNRemoteFolder parent, String resourceName) {
				needsRefresh = true;
			}
			public void remoteResourceDeleted(ISVNRemoteResource resource) {
				needsRefresh = true;
			}
			public void remoteResourceMoved(ISVNRemoteResource resource, ISVNRemoteFolder destinationFolder, String destinationResourceName) {
				needsRefresh = true;
			}
			public void repositoryAdded(ISVNRepositoryLocation root) {
				needsRefresh = true;
			}
			public void repositoryModified(ISVNRepositoryLocation root) {
				needsRefresh = true;
			}
			public void repositoryRemoved(ISVNRepositoryLocation root) {
				needsRefresh = true;
			}  		
    	};
    	SVNProviderPlugin.getPlugin().getRepositoryResourcesManager().addRepositoryListener(repositoryListener);
    }

    public ChooseUrlDialog(Shell parentShell, IResource resource) {
        super(parentShell);
		int shellStyle = getShellStyle();
		setShellStyle(shellStyle | SWT.RESIZE);
        this.resource = resource;
        refreshAction = new Action(Policy.bind("ChooseUrlDialog.refresh"), SVNUIPlugin.getPlugin().getImageDescriptor(ISVNUIConstants.IMG_REFRESH_ENABLED)) { //$NON-NLS-1$
            public void run() {
                refreshViewer(true);
            }
        };
        newFolderAction = new Action(Policy.bind("NewRemoteFolderWizard.title")) { //$NON-NLS-1$
            public void run() {
                CreateRemoteFolderAction createAction = new CreateRemoteFolderAction();
                createAction.selectionChanged(null, treeViewer.getSelection());
                createAction.run(null);
                refreshViewer(true);
            }
        };
        deleteFolderAction = new Action(Policy.bind("ChooseUrlDialog.delete")) { //$NON-NLS-1$
            public void run() {
                DeleteRemoteResourceAction deleteAction = new DeleteRemoteResourceAction();
                deleteAction.selectionChanged(null, treeViewer.getSelection());
                deleteAction.run(null);
                refreshViewer(true);
            }
        };
        settings = SVNUIPlugin.getPlugin().getDialogSettings();
        if (needsRefresh) refreshRepositoriesFolders();
    }

	protected Control createDialogArea(Composite parent) {
		getShell().setText(Policy.bind("ChooseUrlDialog.title")); //$NON-NLS-1$
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		if (message != null) {
			Label messageLabel = new Label(composite, SWT.NONE);
			messageLabel.setText(message);
		}

		if (multipleSelect) treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
		else treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        contentProvider = new RemoteContentProvider();
        contentProvider.setIncludeBranchesAndTags(includeBranchesAndTags);
        contentProvider.setResource(resource);
        treeViewer.setContentProvider(contentProvider);
        if( foldersOnly )
        	treeViewer.addFilter(RepositoryFilters.FOLDERS_ONLY);

        //        treeViewer.setLabelProvider(new WorkbenchLabelProvider());
        treeViewer.setLabelProvider(new RemoteLabelProvider());
        if (repositoryLocation == null) {
	        if (resource == null) treeViewer.setInput(new AllRootsElement());
	        else {
	        	ISVNRepositoryLocation repository = null;
	            ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);
	            try {
	            	LocalResourceStatus status = svnResource.getStatus();
	            	if (status != null) {
	            		repository = svnResource.getStatus().getRepository();
	            	}
				} catch (SVNException e1) {}
				if (repository == null) {
					repository = svnResource.getRepository();
				}

	            if (!repository.getUrl().toString().equals(repository.getRepositoryRoot().toString())) {
	            	RepositoryRootFolder rootFolder = new RepositoryRootFolder(repository, repository.getRepositoryRoot(), repository.getRootFolder().getRevision());
	            	contentProvider.setRootFolder(rootFolder);
	            }
	            
	            if (repository == null) treeViewer.setInput(new AllRootsElement());
	            else treeViewer.setInput(repository);
	        }
        } else treeViewer.setInput(repositoryLocation);

		GridData data = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
		data.heightHint = LIST_HEIGHT_HINT;
		data.widthHint = LIST_WIDTH_HINT;
		treeViewer.getControl().setLayoutData(data);
		
        // when F5 is pressed, refresh
        treeViewer.getControl().addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent event) {
                if (event.keyCode == SWT.F5) {
                    refreshAction.run();
                }
            }
        });		

        treeViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent e) {
{'fraction_non_alphanumeric': 0.10756172839506173, 'fraction_numerical': 0.002726337448559671, 'mean_word_length': 3.997686375321337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5330947', 'n_tokens_mistral': 5724, 'n_tokens_neox': 5367, 'n_words': 985}