<commit_before>/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     <NAME> (<EMAIL>) - modified for Subversion 
 *******************************************************************************/
package org.tigris.subversion.subclipse.ui.repository;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.ui.actions.SelectionListenerAction;
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.SVNTeamProvider;
import org.tigris.subversion.subclipse.ui.Policy;
import org.tigris.subversion.subclipse.ui.SVNUIPlugin;
import org.tigris.subversion.subclipse.ui.actions.SVNAction;
import org.tigris.subversion.subclipse.ui.util.DetailsDialogWithProjects;


/**
 * RemoveRootAction removes a repository
 */
public class RemoveRootAction extends SelectionListenerAction {
	private IStructuredSelection selection;
	private Shell shell;
	
	public RemoveRootAction(Shell shell) {
		super(Policy.bind("RemoteRootAction.label")); //$NON-NLS-1$
		this.shell = shell;
	}
	
	/**
	 * Returns the selected remote files
	 */
	protected ISVNRepositoryLocation[] getSelectedRemoteRoots() {
		ArrayList resources = null;
		if (selection!=null && !selection.isEmpty()) {
			resources = new ArrayList();
			Iterator elements = selection.iterator();
			while (elements.hasNext()) {
				Object next = SVNAction.getAdapter(elements.next(), ISVNRepositoryLocation.class);
				if (next instanceof ISVNRepositoryLocation) {
					resources.add(next);
				}
			}
		}
		if (resources != null && !resources.isEmpty()) {
			ISVNRepositoryLocation[] result = new ISVNRepositoryLocation[resources.size()];
			resources.toArray(result);
			return result;
		}
		return new ISVNRepositoryLocation[0];
	}
	
	protected String getErrorTitle() {
		return Policy.bind("RemoveRootAction.removeRoot_3"); //$NON-NLS-1$
	}

	public void run() {
		ISVNRepositoryLocation[] roots = getSelectedRemoteRoots();
		if (roots.length == 0) return;
		SVNProviderPlugin provider = SVNProviderPlugin.getPlugin();
		for (int i = 0; i < roots.length; i++) {
			try {	
				// Check if any projects are shared with the repository
				IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
				final ArrayList shared = new ArrayList();
				for (int j = 0; j < projects.length; j++) {
					RepositoryProvider teamProvider = RepositoryProvider.getProvider(projects[j], SVNProviderPlugin.getTypeId());
					if (teamProvider!=null) {
						SVNTeamProvider svnProvider = (SVNTeamProvider)teamProvider;
						if (svnProvider.getSVNWorkspaceRoot().getRepository().equals(roots[i])) {
							shared.add(projects[j]);
						}
					}
				}
			
				// This will notify the RepositoryManager of the removal<commit_msg>When discarding a location we get a list of all projects being managed by Subclipse.  We then call getRepository() on each project so that we can  verify that none of the projects are attached to the one being deleted.  <commit_after>/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     <NAME> (<EMAIL>) - modified for Subversion 
 *******************************************************************************/
package org.tigris.subversion.subclipse.ui.repository;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.ui.actions.SelectionListenerAction;
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.SVNTeamProvider;
import org.tigris.subversion.subclipse.ui.Policy;
import org.tigris.subversion.subclipse.ui.SVNUIPlugin;
import org.tigris.subversion.subclipse.ui.actions.SVNAction;
import org.tigris.subversion.subclipse.ui.util.DetailsDialogWithProjects;


/**
 * RemoveRootAction removes a repository
 */
public class RemoveRootAction extends SelectionListenerAction {
	private IStructuredSelection selection;
	private Shell shell;
	
	public RemoveRootAction(Shell shell) {
		super(Policy.bind("RemoteRootAction.label")); //$NON-NLS-1$
		this.shell = shell;
	}
	
	/**
	 * Returns the selected remote files
	 */
	protected ISVNRepositoryLocation[] getSelectedRemoteRoots() {
		ArrayList resources = null;
		if (selection!=null && !selection.isEmpty()) {
			resources = new ArrayList();
			Iterator elements = selection.iterator();
			while (elements.hasNext()) {
				Object next = SVNAction.getAdapter(elements.next(), ISVNRepositoryLocation.class);
				if (next instanceof ISVNRepositoryLocation) {
					resources.add(next);
				}
			}
		}
		if (resources != null && !resources.isEmpty()) {
			ISVNRepositoryLocation[] result = new ISVNRepositoryLocation[resources.size()];
			resources.toArray(result);
			return result;
		}
		return new ISVNRepositoryLocation[0];
	}
	
	protected String getErrorTitle() {
		return Policy.bind("RemoveRootAction.removeRoot_3"); //$NON-NLS-1$
	}

	public void run() {
		ISVNRepositoryLocation[] roots = getSelectedRemoteRoots();
		if (roots.length == 0) return;
		SVNProviderPlugin provider = SVNProviderPlugin.getPlugin();
		for (int i = 0; i < roots.length; i++) {
			try {	
				// Check if any projects are shared with the repository
				IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
				final ArrayList shared = new ArrayList();
				for (int j = 0; j < projects.length; j++) {
					RepositoryProvider teamProvider = RepositoryProvider.getProvider(projects[j], SVNProviderPlugin.getTypeId());
					if (teamProvider!=null) {
					    try {
							SVNTeamProvider svnProvider = (SVNTeamProvider)teamProvider;
							if (svnProvider.getSVNWorkspaceRoot().getRepository().equals(roots[i])) {
								shared.add(projects[j]);
							}
					    } catch(Exception e) {
					        // Don't let any exception prevent from
					        // continuing
					    }
					}
				}
			
				// This will notify the RepositoryManager of the removal
{'fraction_non_alphanumeric': 0.14589216081753395, 'fraction_numerical': 0.005109587199139438, 'mean_word_length': 4.550746268656717, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '4765480', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2039, 'n_words': 498}