Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08a053a3c7bd8519b9e09f7cdea14b509e2fb3f2 (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
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (c) 2007, 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.team.internal.ui.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.team.internal.ui.*;
import org.eclipse.team.ui.TeamOperation;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.IWorkingSetManager;

public class ImportProjectSetOperation extends TeamOperation {
	
	private String psfFileContents;
	private String urlString;
	private String psfFile;
	private IWorkingSet[] workingSets;
	
	
	/**
	 * Operation for importing a Team Project Set stored in a String
	 * 
	 * @param context
	 *            a runnable context,
	 *            <code>null</null> for running in background
	 * @param psfFileContents
	 *            the psf file content to load
	 * @param urlString
	 *            url or path to file loaded into <code>psfFileContents</code>
	 * @param workingSets
	 *            an array of working sets where imported project should be
	 *            added. If a working set doesn't exist it will be created. The
	 *            array cannot be <code>null</code>, pass an empty array if you
	 *            don't want to add projects to any working set.
	 */
	public ImportProjectSetOperation(IRunnableContext context,
			String psfFileContents, String urlString, IWorkingSet[] workingSets) {
		super(context);
		this.psfFileContents = psfFileContents;
		this.workingSets = workingSets;
		this.urlString = urlString;
	}

	/**
	 * Operation for importing a Team Project Set file
	 * 
	 * @param context
	 *            a runnable context
	 * @param psfFile
	 *            a psf file name
	 * @param workingSets
	 *            an array of working sets where imported project should be
	 *            added. If a working set doesn't exist it will be created. The
	 *            array cannot be <code>null</code>, pass an empty array if you
	 *            don't want to add projects to any working set.
	 */
	public ImportProjectSetOperation(IRunnableContext context, String psfFile,
			IWorkingSet[] workingSets) {
		super(context);
		this.psfFile = psfFile;
		this.workingSets = workingSets;
	}
	
	private void runForStringContent(IProgressMonitor monitor) throws InvocationTargetException{
		IProject[] newProjects = ProjectSetImporter.importProjectSetFromString(
				psfFileContents, urlString, getShell(), monitor);
		createWorkingSet(workingSets, newProjects);
	}
	
	private void runForFile(IProgressMonitor monitor) throws InvocationTargetException{
		PsfFilenameStore.getInstance().remember(psfFile);
		IProject[] newProjects = ProjectSetImporter.importProjectSet(psfFile,
				getShell(), monitor);
		createWorkingSet(workingSets, newProjects);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void run(IProgressMonitor monitor)
			throws InvocationTargetException, InterruptedException{
		if (psfFileContents != null) {
			runForStringContent(monitor);
		} else {
			runForFile(monitor);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.team.ui.TeamOperation#canRunAsJob()
	 */
	@Override
	protected boolean canRunAsJob() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.team.ui.TeamOperation#getJobName()
	 */
	@Override
	protected String getJobName() {
		return TeamUIMessages.ImportProjectSetMainPage_jobName;
	}
	
	private void createWorkingSet(IWorkingSet[] workingSets, IProject[] projects) {
		IWorkingSetManager manager = TeamUIPlugin.getPlugin().getWorkbench().getWorkingSetManager();
		String workingSetName;
		for (int i = 0; i < workingSets.length; i++) {
			workingSetName = workingSets[i].getName();
			IWorkingSet oldSet = manager.getWorkingSet(workingSetName);
			if (oldSet == null) {
				IWorkingSet newSet = manager.createWorkingSet(workingSetName, projects);
				manager.addWorkingSet(newSet);
			} else {
				//don't overwrite the old elements
				IAdaptable[] tempElements = oldSet.getElements();
				IAdaptable[] adaptedProjects = oldSet.adaptElements(projects);
				IAdaptable[] finalElementList = new IAdaptable[tempElements.length + adaptedProjects.length];
				System.arraycopy(tempElements, 0, finalElementList, 0, tempElements.length);
				System.arraycopy(adaptedProjects, 0,finalElementList, tempElements.length, adaptedProjects.length);
				oldSet.setElements(finalElementList);
			}	
		}
	}
}

Back to the top