KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > AddWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ccvs.ui.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.resources.*;
22 import org.eclipse.core.resources.mapping.ResourceTraversal;
23 import org.eclipse.core.runtime.*;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.team.core.IFileContentManager;
27 import org.eclipse.team.core.Team;
28 import org.eclipse.team.internal.ccvs.core.CVSException;
29 import org.eclipse.team.internal.ccvs.core.ICVSFile;
30 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
31 import org.eclipse.team.internal.ccvs.ui.*;
32 import org.eclipse.team.internal.ccvs.ui.operations.AddOperation;
33 import org.eclipse.ui.PlatformUI;
34
35 public class AddWizard extends ResizableWizard {
36
37     private final AddOperation op;
38     private final IFile[] unknowns;
39     private CommitWizardFileTypePage fFileTypePage;
40
41     public static void run(Shell shell, final AddOperation op) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
42         // Prompt if there are files of unknown type being added
43
PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
44             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
45                     InterruptedException JavaDoc {
46                 try {
47                     op.buildScope(monitor);
48                 } catch (CVSException e) {
49                     throw new InvocationTargetException JavaDoc(e);
50                 }
51             }
52         });
53         
54         IFile[] unknowns = getUnaddedWithUnknownFileType(op.getTraversals());
55         if (unknowns.length == 0) {
56             op.run();
57         } else {
58             AddWizard wizard = new AddWizard(op, unknowns);
59             ResizableWizard.open(shell, wizard);
60         }
61     }
62     
63     private static IFile[] getUnaddedWithUnknownFileType(final ResourceTraversal[] traversals) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
64         final List JavaDoc unadded = new ArrayList JavaDoc();
65         PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
66             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
67                     InterruptedException JavaDoc {
68                 final IFileContentManager manager= Team.getFileContentManager();
69                 for (int i = 0; i < traversals.length; i++) {
70                     ResourceTraversal traversal = traversals[i];
71                     IResource[] resources = traversal.getResources();
72                     for (int j = 0; j < resources.length; j++) {
73                         final IResource resource = resources[j];
74                         try {
75                             resource.accept(new IResourceVisitor() {
76                                 public boolean visit(IResource resource) throws CoreException {
77                                     if (resource.getType() == IResource.FILE) {
78                                         ICVSFile file = CVSWorkspaceRoot.getCVSFileFor((IFile)resource);
79                                         if (!file.isManaged()) {
80                                             if (!file.isIgnored() || file.equals(resource)) {
81                                                 final String JavaDoc extension= ((IFile)resource).getFileExtension();
82                                                 if (manager.getType((IFile)resource) == Team.UNKNOWN) {
83                                                     if (extension != null && !manager.isKnownExtension(extension)) {
84                                                         unadded.add(resource);
85                                                     } else {
86                                                         final String JavaDoc name= file.getName();
87                                                         if (extension == null && name != null && !manager.isKnownFilename(name))
88                                                             unadded.add(resource);
89                                                     }
90                                                 }
91                                             }
92                                         }
93                                     }
94                                     return true;
95                                 }
96                             }, traversal.getDepth(), false);
97                         } catch (CoreException e) {
98                             throw new InvocationTargetException JavaDoc(e);
99                         }
100                     }
101                 }
102             }
103         
104         });
105         return (IFile[]) unadded.toArray(new IFile[unadded.size()]);
106     }
107     
108     public AddWizard(AddOperation op, IFile[] unknowns) {
109         super("AddWizard", CVSUIPlugin.getPlugin().getDialogSettings()); //$NON-NLS-1$
110
this.op = op;
111         this.unknowns = unknowns;
112         setWindowTitle(CVSUIMessages.AddWizard_0);
113         setDefaultPageImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_NEW_LOCATION));
114     }
115     
116     public void addPages() {
117         
118         final Collection JavaDoc names= new HashSet JavaDoc();
119         final Collection JavaDoc extensions= new HashSet JavaDoc();
120         getUnknownNamesAndExtension(unknowns, names, extensions);
121         
122         if (names.size() + extensions.size() > 0) {
123             fFileTypePage= new CommitWizardFileTypePage(extensions, names);
124             addPage(fFileTypePage);
125         }
126         
127         super.addPages();
128     }
129     
130     private static void getUnknownNamesAndExtension(IFile[] files, Collection JavaDoc names, Collection JavaDoc extensions) {
131         
132         final IFileContentManager manager= Team.getFileContentManager();
133         
134         for (int i = 0; i < files.length; i++) {
135             IFile file = files[i];
136             
137             final String JavaDoc extension= file.getFileExtension();
138             if (extension != null && !manager.isKnownExtension(extension)) {
139                 extensions.add(extension);
140             }
141             
142             final String JavaDoc name= file.getName();
143             if (extension == null && name != null && !manager.isKnownFilename(name))
144                 names.add(name);
145         }
146     }
147     
148     public boolean performFinish() {
149         final Map JavaDoc extensionsToSave= new HashMap JavaDoc();
150         final Map JavaDoc extensionsNotToSave= new HashMap JavaDoc();
151         
152         fFileTypePage.getModesForExtensions(extensionsToSave, extensionsNotToSave);
153         CommitWizardFileTypePage.saveExtensionMappings(extensionsToSave);
154         op.addModesForExtensions(extensionsNotToSave);
155         
156         final Map JavaDoc namesToSave= new HashMap JavaDoc();
157         final Map JavaDoc namesNotToSave= new HashMap JavaDoc();
158         
159         fFileTypePage.getModesForNames(namesToSave, namesNotToSave);
160         CommitWizardFileTypePage.saveNameMappings(namesToSave);
161         op.addModesForNames(namesNotToSave);
162         
163         try {
164             op.run();
165         } catch (InvocationTargetException JavaDoc e) {
166             return false;
167         } catch (InterruptedException JavaDoc e) {
168             return false;
169         }
170         
171         return super.performFinish();
172     }
173     
174 }
175
Popular Tags