1 11 package org.eclipse.team.internal.ui; 12 13 import java.io.*; 14 import java.lang.reflect.InvocationTargetException ; 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 filename, Shell shell, IProgressMonitor monitor) throws InvocationTargetException { 30 InputStreamReader reader = null; 31 try { 32 reader = new InputStreamReader(new FileInputStream(filename), "UTF-8"); 34 XMLMemento xmlMemento = XMLMemento.createReadRoot(reader); 35 String version = xmlMemento.getString("version"); 37 List newProjects = new ArrayList(); 38 if (version.equals("1.0")){ IProjectSetSerializer serializer = Team.getProjectSetSerializer("versionOneSerializer"); if (serializer != null) { 41 IProject[] projects = serializer.addToWorkspace(new String [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"); for (int i = 0; i < providers.length; i++) { 51 IMemento[] projects = providers[i].getChildren("project"); for (int j = 0; j < projects.length; j++) { 53 referenceStrings.add(projects[j].getString("reference")); } 55 try { 56 String id = providers[i].getString("id"); TeamCapabilityHelper.getInstance().processRepositoryId(id, 58 PlatformUI.getWorkbench().getActivitySupport()); 59 RepositoryProviderType providerType = RepositoryProviderType.getProviderType(id); 60 if (providerType == null) { 61 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 [] { id }), null)); 66 } 67 ProjectSetCapability serializer = providerType.getProjectSetCapability(); 68 ProjectSetCapability.ensureBackwardsCompatible(providerType, serializer); 69 if (serializer != null) { 70 IProject[] allProjects = serializer.addToWorkspace((String [])referenceStrings.toArray(new String [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 IMemento[] sets = xmlMemento.getChildren("workingSets"); 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 String title = TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_title; 105 String msg = NLS 106 .bind( 107 TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_message, 108 newWs.getName()); 109 String [] buttons = new String [] { 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 () { 120 public void run() { 121 dialog.open(); 122 } 123 }); 124 125 switch (dialog.getReturnCode()) { 126 case 0: if (oldWs != null) 128 wsManager.removeWorkingSet(oldWs); 129 wsManager.addWorkingSet(newWs); 130 break; 131 case 1: 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: break; 143 case 3: 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 (e); 156 } catch (TeamException e) { 157 throw new InvocationTargetException (e); 158 } catch (WorkbenchException e) { 159 throw new InvocationTargetException (e); 160 } finally { 161 if (reader != null) { 162 try { 163 reader.close(); 164 } catch (IOException e) { 165 throw new InvocationTargetException (e); 166 } 167 } 168 } 169 } 170 171 } 172 | Popular Tags |