KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > ProjectSetImporter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui;
12
13 import java.io.*;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.*;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.team.core.*;
24 import org.eclipse.team.internal.core.TeamPlugin;
25 import org.eclipse.ui.*;
26
27 public class ProjectSetImporter {
28     
29     public static IProject[] importProjectSet(String JavaDoc filename, Shell shell, IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
30         InputStreamReader reader = null;
31         try {
32             reader = new InputStreamReader(new FileInputStream(filename), "UTF-8"); //$NON-NLS-1$
33

34             XMLMemento xmlMemento = XMLMemento.createReadRoot(reader);
35             String JavaDoc version = xmlMemento.getString("version"); //$NON-NLS-1$
36

37             List newProjects = new ArrayList();
38             if (version.equals("1.0")){ //$NON-NLS-1$
39
IProjectSetSerializer serializer = Team.getProjectSetSerializer("versionOneSerializer"); //$NON-NLS-1$
40
if (serializer != null) {
41                     IProject[] projects = serializer.addToWorkspace(new String JavaDoc[0], filename, shell, monitor);
42                     if (projects != null)
43                         newProjects.addAll(Arrays.asList(projects));
44                 }
45             } else {
46                 UIProjectSetSerializationContext context = new UIProjectSetSerializationContext(shell, filename);
47                 List errors = new ArrayList();
48                 ArrayList referenceStrings = new ArrayList();
49                 IMemento[] providers = xmlMemento.getChildren("provider"); //$NON-NLS-1$
50
for (int i = 0; i < providers.length; i++) {
51                     IMemento[] projects = providers[i].getChildren("project"); //$NON-NLS-1$
52
for (int j = 0; j < projects.length; j++) {
53                         referenceStrings.add(projects[j].getString("reference")); //$NON-NLS-1$
54
}
55                     try {
56                         String JavaDoc id = providers[i].getString("id"); //$NON-NLS-1$
57
TeamCapabilityHelper.getInstance().processRepositoryId(id,
58                                 PlatformUI.getWorkbench().getActivitySupport());
59                         RepositoryProviderType providerType = RepositoryProviderType.getProviderType(id);
60                         if (providerType == null) {
61                             // The provider type is absent. Perhaps there is another provider that can import this type
62
providerType = TeamPlugin.getAliasType(id);
63                         }
64                         if (providerType == null) {
65                             throw new TeamException(new Status(IStatus.ERROR, TeamUIPlugin.ID, 0, NLS.bind(TeamUIMessages.ProjectSetImportWizard_0, new String JavaDoc[] { id }), null));
66                         }
67                         ProjectSetCapability serializer = providerType.getProjectSetCapability();
68                         ProjectSetCapability.ensureBackwardsCompatible(providerType, serializer);
69                         if (serializer != null) {
70                             IProject[] allProjects = serializer.addToWorkspace((String JavaDoc[])referenceStrings.toArray(new String JavaDoc[referenceStrings.size()]), context, monitor);
71                             if (allProjects != null)
72                                 newProjects.addAll(Arrays.asList(allProjects));
73                         }
74                     } catch (TeamException e) {
75                         errors.add(e);
76                     }
77                 }
78                 if (!errors.isEmpty()) {
79                     if (errors.size() == 1) {
80                         throw (TeamException)errors.get(0);
81                     } else {
82                         TeamException[] exceptions = (TeamException[]) errors.toArray(new TeamException[errors.size()]);
83                         IStatus[] status = new IStatus[exceptions.length];
84                         for (int i = 0; i < exceptions.length; i++) {
85                             status[i] = exceptions[i].getStatus();
86                         }
87                         throw new TeamException(new MultiStatus(TeamUIPlugin.ID, 0, status, TeamUIMessages.ProjectSetImportWizard_1, null));
88                     }
89                 }
90                 
91                 //try working sets
92
IMemento[] sets = xmlMemento.getChildren("workingSets"); //$NON-NLS-1$
93
IWorkingSetManager wsManager = TeamUIPlugin.getPlugin().getWorkbench().getWorkingSetManager();
94                 
95                 for (int i = 0; i < sets.length; i++) {
96                     IWorkingSet newWs = wsManager.createWorkingSet(sets[i]);
97                     if (newWs != null) {
98                         IWorkingSet oldWs = wsManager.getWorkingSet(newWs
99                                 .getName());
100                         if (oldWs == null) {
101                             wsManager.addWorkingSet(newWs);
102                         } else {
103                             // a working set with the same name has been found
104
String JavaDoc title = TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_title;
105                             String JavaDoc msg = NLS
106                                     .bind(
107                                             TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_message,
108                                             newWs.getName());
109                             String JavaDoc[] buttons = new String JavaDoc[] {
110                                     TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_replace,
111                                     TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_merge,
112                                     TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_skip,
113                                     IDialogConstants.CANCEL_LABEL };
114
115                             final MessageDialog dialog = new MessageDialog(
116                                     shell, title, null, msg,
117                                     MessageDialog.QUESTION, buttons, 0);
118                             
119                             shell.getDisplay().syncExec(new Runnable JavaDoc() {
120                                 public void run() {
121                                      dialog.open();
122                                 }
123                             });
124                             
125                             switch (dialog.getReturnCode()) {
126                             case 0: // overwrite
127
if (oldWs != null)
128                                     wsManager.removeWorkingSet(oldWs);
129                                 wsManager.addWorkingSet(newWs);
130                                 break;
131                             case 1: // combine
132
IAdaptable[] oldElements = oldWs.getElements();
133                                 IAdaptable[] newElements = newWs.getElements();
134                                 
135                                 Set combinedElements = new HashSet();
136                                 combinedElements.addAll(Arrays.asList(oldElements));
137                                 combinedElements.addAll(Arrays.asList(newElements));
138                                 
139                                 oldWs.setElements((IAdaptable[]) combinedElements.toArray(new IAdaptable[0]));
140                                 break;
141                             case 2: // skip
142
break;
143                             case 3: // cancel
144
default:
145                                 throw new OperationCanceledException();
146                             }
147                         }
148                     }
149                 }
150                 
151             }
152             
153             return (IProject[]) newProjects.toArray(new IProject[newProjects.size()]);
154         } catch (IOException e) {
155             throw new InvocationTargetException JavaDoc(e);
156         } catch (TeamException e) {
157             throw new InvocationTargetException JavaDoc(e);
158         } catch (WorkbenchException e) {
159             throw new InvocationTargetException JavaDoc(e);
160         } finally {
161             if (reader != null) {
162                 try {
163                     reader.close();
164                 } catch (IOException e) {
165                     throw new InvocationTargetException JavaDoc(e);
166                 }
167             }
168         }
169     }
170     
171 }
172
Popular Tags