Skip to main content
summaryrefslogtreecommitdiffstats
blob: eb47d1628949ea4832e96a85c10bae551106a410 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.releng.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.team.core.ProjectSetCapability;
import org.eclipse.team.core.RepositoryProviderType;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.ui.actions.CVSAction;
import org.eclipse.team.internal.ui.UIProjectSetSerializationContext;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;

import org.eclipse.jface.action.IAction;

import org.eclipse.ui.actions.WorkspaceModifyOperation;


public class LoadMap extends CVSAction {

	/**
	 * @see org.eclipse.team.internal.ccvs.ui.actions.CVSAction#execute(org.eclipse.jface.action.IAction)
	 */
	@Override
	protected void execute(IAction action) throws InvocationTargetException, InterruptedException {
		run(new WorkspaceModifyOperation(null) {
			@Override
			public void execute(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
				try {
					IResource[] resources = getSelectedResources();
					String[] referenceStrings = getReferenceStrings(resources);
					RepositoryProviderType type = RepositoryProviderType.getProviderType(CVSProviderPlugin.getTypeId());
					ProjectSetCapability c = type.getProjectSetCapability();
					c.addToWorkspace(referenceStrings, new UIProjectSetSerializationContext(getShell(), null), monitor);
				} catch (TeamException e) {
					throw new InvocationTargetException(e);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}

		}, true, PROGRESS_DIALOG);
	}

	/**
	 * Get the project set reference strings from the provider map files
	 * 
	 * @param mapFiles
	 * @return
	 * @throws CoreException
	 */
	protected String[] getReferenceStrings(IResource[] mapFiles) throws CoreException {
		List allStrings = new ArrayList();
		for (int i = 0; i < mapFiles.length; i++) {
			IResource resource = mapFiles[i];
			String[] referenceStrings = readReferenceStrings((IFile)resource);
			allStrings.addAll(Arrays.asList(referenceStrings));
		}
		return (String[]) allStrings.toArray(new String[allStrings.size()]);
	}
	
	/**
	 * Method readReferenceStrings.
	 * @param file
	 * @return String[]
	 */

	protected static String[] readReferenceStrings(IFile file) throws CoreException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents()));
		try {
			try {
				String line = reader.readLine();
				List references = new ArrayList();
				while (line != null) {
					String referenceString = new MapEntry(line).getReferenceString();
					if (referenceString != null)  {
						references.add(referenceString);
					}
					line = reader.readLine();
				}
				return (String[]) references.toArray(new String[references.size()]);
			} finally {
				reader.close();
			}
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, RelEngPlugin.ID, 0, "An I/O error occured", e));
		}
	}


	/**
	 * @see org.eclipse.team.internal.ui.actions.TeamAction#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		IResource[] resources = getSelectedResources();
		if (resources.length == 0) return false;
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			if (resource.getType() != IResource.FILE)
				return false;
			if (!MapFile.isMapFile((IFile)resource))
				return false;
		}
		return true;
	}

}

Back to the top