1 11 package org.eclipse.team.internal.ui.wizards; 12 13 import java.io.*; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.*; 16 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 import javax.xml.parsers.ParserConfigurationException ; 19 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.jface.operation.IRunnableWithProgress; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.wizard.Wizard; 27 import org.eclipse.team.core.*; 28 import org.eclipse.team.internal.ui.*; 29 import org.eclipse.ui.*; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 33 public class ProjectSetExportWizard extends Wizard implements IExportWizard { 34 ExportProjectSetMainPage mainPage; 35 ExportProjectSetLocationPage locationPage; 36 IStructuredSelection selection; 37 38 public ProjectSetExportWizard() { 39 setNeedsProgressMonitor(true); 40 setWindowTitle(TeamUIMessages.ProjectSetExportWizard_Project_Set_1); 41 } 42 43 public void addPages() { 44 mainPage = new ExportProjectSetMainPage("projectSetMainPage", TeamUIMessages.ProjectSetExportWizard_Export_a_Project_Set_3, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_PROJECTSET_EXPORT_BANNER)); IProject[] projects = (IProject[])selection.toList().toArray(new IProject[0]); 46 addPage(mainPage); 47 mainPage.setSelectedProjects(projects); 48 locationPage = new ExportProjectSetLocationPage("projectSetLocationPage", TeamUIMessages.ProjectSetExportWizard_Export_a_Project_Set_3, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_PROJECTSET_EXPORT_BANNER)); addPage(locationPage); 50 51 } 52 public boolean performFinish() { 53 final boolean[] result = new boolean[] {false}; 54 try { 55 getContainer().run(false, false, new IRunnableWithProgress() { 56 public void run(IProgressMonitor monitor) throws InvocationTargetException { 57 String filename = locationPage.getFileName(); 58 Path path = new Path(filename); 59 if (path.getFileExtension() == null) { 60 filename = filename + ".psf"; } 62 PsfFilenameStore.remember(filename); 63 File file = new File(filename); 64 File parentFile = file.getParentFile(); 65 if (parentFile != null && !parentFile.exists()) { 66 boolean r = MessageDialog.openQuestion(getShell(), TeamUIMessages.ProjectSetExportWizard_Question_4, TeamUIMessages.ProjectSetExportWizard_Target_directory_does_not_exist__Would_you_like_to_create_it__5); if (!r) { 68 result[0] = false; 69 return; 70 } 71 r = parentFile.mkdirs(); 72 if (!r) { 73 MessageDialog.openError(getShell(), TeamUIMessages.ProjectSetExportWizard_Export_Problems_6, TeamUIMessages.ProjectSetExportWizard_An_error_occurred_creating_the_target_directory_7); result[0] = false; 75 return; 76 } 77 } 78 if (file.exists() && file.isFile()) { 79 boolean r = MessageDialog.openQuestion(getShell(), TeamUIMessages.ProjectSetExportWizard_Question_8, TeamUIMessages.ProjectSetExportWizard_Target_already_exists__Would_you_like_to_overwrite_it__9); if (!r) { 81 result[0] = false; 82 return; 83 } 84 } 85 86 IWorkingSet[] workingSets = null; 87 if (mainPage.exportWorkingSets.getSelection()){ 88 workingSets = mainPage.getSelectedWorkingSets(); 89 } 90 IProject[] projects = mainPage.getSelectedProjects(); 92 Map map = new HashMap(); 93 for (int i = 0; i < projects.length; i++) { 94 IProject project = projects[i]; 95 RepositoryProvider provider = RepositoryProvider.getProvider(project); 96 if (provider != null) { 97 String id = provider.getID(); 98 Set list = (Set)map.get(id); 99 if (list == null) { 100 list = new TreeSet(new Comparator() { 101 public int compare(Object o1, Object o2) { 102 return ((IProject) o1).getName().toLowerCase().compareTo(((IProject) o2).getName().toLowerCase()); 103 } 104 }); 105 map.put(id, list); 106 } 107 list.add(project); 108 } 109 } 110 111 112 UIProjectSetSerializationContext context = new UIProjectSetSerializationContext(getShell(), filename); 113 114 BufferedWriter writer = null; 115 try { 116 if (!locationPage.isSaveToFileSystem()) 118 locationPage.validateEditWorkspaceFile(getShell()); 119 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); 121 XMLMemento xmlMemento = getXMLMementoRoot(); 123 Iterator it = map.keySet().iterator(); 124 monitor.beginTask(null, 1000 * map.keySet().size()); 125 while (it.hasNext()) { 126 String id = (String )it.next(); 127 IMemento memento = xmlMemento.createChild("provider"); memento.putString("id", id); Set list = (Set)map.get(id); 130 IProject[] projectArray = (IProject[])list.toArray(new IProject[list.size()]); 131 RepositoryProviderType providerType = RepositoryProviderType.getProviderType(id); 132 ProjectSetCapability serializer = providerType.getProjectSetCapability(); 133 ProjectSetCapability.ensureBackwardsCompatible(providerType, serializer); 134 if (serializer != null) { 135 String [] references = serializer.asReference(projectArray, context, new SubProgressMonitor(monitor, 990)); 136 for (int i = 0; i < references.length; i++) { 137 IMemento proj = memento.createChild("project"); proj.putString("reference", references[i]); } 140 } 141 } 142 if (workingSets != null){ 143 for (int i = 0; i < workingSets.length; i++) { 144 IMemento memento =xmlMemento.createChild("workingSets"); workingSets[i].saveState(memento); 146 } 147 } 148 xmlMemento.save(writer); 149 result[0] = true; 150 } catch (IOException e) { 151 throw new InvocationTargetException (e); 152 } catch (TeamException e) { 153 throw new InvocationTargetException (e); 154 } finally { 155 if (writer != null) { 156 try { 157 writer.close(); 158 } catch (IOException e) { 159 throw new InvocationTargetException (e); 160 } 161 } 162 } 163 164 if (!locationPage.isSaveToFileSystem()) 166 try { 167 locationPage.refreshWorkspaceFile(monitor); 168 } catch (CoreException e) { 169 } 171 172 for (Iterator iter = map.keySet().iterator();iter.hasNext();) { 174 String id = (String ) iter.next(); 175 RepositoryProviderType type = RepositoryProviderType.getProviderType(id); 176 if (type != null) { 177 ProjectSetCapability capability = type.getProjectSetCapability(); 178 if (capability != null) { 179 capability.projectSetCreated(file, context, new SubProgressMonitor(monitor, 10)); 180 } 181 } 182 } 183 184 monitor.done(); 185 } 186 187 private XMLMemento getXMLMementoRoot() { 188 Document document; 189 try { 190 document = DocumentBuilderFactory.newInstance() 191 .newDocumentBuilder().newDocument(); 192 Element element = document.createElement("psf"); element.setAttribute("version", "2.0"); document.appendChild(element); 195 return new XMLMemento(document, element); 196 } catch (ParserConfigurationException e) { 197 throw new Error (e.getMessage()); 198 } 199 } 200 201 }); 202 } catch (InterruptedException e) { 203 return true; 204 } catch (InvocationTargetException e) { 205 Throwable target = e.getTargetException(); 206 if (target instanceof TeamException) { 207 ErrorDialog.openError(getShell(), null, null, ((TeamException)target).getStatus()); 208 return false; 209 } 210 if (target instanceof RuntimeException ) { 211 throw (RuntimeException )target; 212 } 213 if (target instanceof Error ) { 214 throw (Error )target; 215 } 216 } 217 return result[0]; 218 } 219 220 public void init(IWorkbench workbench, IStructuredSelection selection) { 221 this.selection = selection; 222 } 223 } 224 | Popular Tags |