KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > wizards > ProjectSetExportWizard


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.wizards;
12
13 import java.io.*;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.*;
16
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
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)); //$NON-NLS-1$
45
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)); //$NON-NLS-1$
49
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 JavaDoc {
57                     String JavaDoc filename = locationPage.getFileName();
58                     Path path = new Path(filename);
59                     if (path.getFileExtension() == null) {
60                         filename = filename + ".psf"; //$NON-NLS-1$
61
}
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); //
67
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); //
74
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); //
80
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                     // Hash the projects by provider
91
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 JavaDoc 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 JavaDoc o1, Object JavaDoc 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 file was written to the workspace, perform the validateEdit
117
if (!locationPage.isSaveToFileSystem())
118                             locationPage.validateEditWorkspaceFile(getShell());
119                         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); //$NON-NLS-1$
120

121                         //
122
XMLMemento xmlMemento = getXMLMementoRoot();
123                         Iterator it = map.keySet().iterator();
124                         monitor.beginTask(null, 1000 * map.keySet().size());
125                         while (it.hasNext()) {
126                             String JavaDoc id = (String JavaDoc)it.next();
127                             IMemento memento = xmlMemento.createChild("provider"); //$NON-NLS-1$
128
memento.putString("id", id); //$NON-NLS-1$
129
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 JavaDoc[] references = serializer.asReference(projectArray, context, new SubProgressMonitor(monitor, 990));
136                                 for (int i = 0; i < references.length; i++) {
137                                     IMemento proj = memento.createChild("project"); //$NON-NLS-1$
138
proj.putString("reference", references[i]); //$NON-NLS-1$
139
}
140                             }
141                         }
142                         if (workingSets != null){
143                             for (int i = 0; i < workingSets.length; i++) {
144                                 IMemento memento =xmlMemento.createChild("workingSets"); //$NON-NLS-1$
145
workingSets[i].saveState(memento);
146                             }
147                         }
148                         xmlMemento.save(writer);
149                         result[0] = true;
150                     } catch (IOException e) {
151                         throw new InvocationTargetException JavaDoc(e);
152                     } catch (TeamException e) {
153                         throw new InvocationTargetException JavaDoc(e);
154                     } finally {
155                         if (writer != null) {
156                             try {
157                                 writer.close();
158                             } catch (IOException e) {
159                                 throw new InvocationTargetException JavaDoc(e);
160                             }
161                         }
162                     }
163                     
164                     // if file was written to the workspace, refresh it
165
if (!locationPage.isSaveToFileSystem())
166                         try {
167                             locationPage.refreshWorkspaceFile(monitor);
168                         } catch (CoreException e) {
169                             //throw away
170
}
171                         
172                     // notify provider types of the project set write
173
for (Iterator iter = map.keySet().iterator();iter.hasNext();) {
174                         String JavaDoc id = (String JavaDoc) 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 JavaDoc document;
189                     try {
190                         document = DocumentBuilderFactory.newInstance()
191                                 .newDocumentBuilder().newDocument();
192                         Element JavaDoc element = document.createElement("psf"); //$NON-NLS-1$
193
element.setAttribute("version", "2.0"); //$NON-NLS-1$ //$NON-NLS-2$
194
document.appendChild(element);
195                         return new XMLMemento(document, element);
196                     } catch (ParserConfigurationException JavaDoc e) {
197                         throw new Error JavaDoc(e.getMessage());
198                     }
199                 }
200
201             });
202         } catch (InterruptedException JavaDoc e) {
203             return true;
204         } catch (InvocationTargetException JavaDoc e) {
205             Throwable JavaDoc 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 JavaDoc) {
211                 throw (RuntimeException JavaDoc)target;
212             }
213             if (target instanceof Error JavaDoc) {
214                 throw (Error JavaDoc)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