KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Dan Rubel - initial API and implementation
10  * IBM Corporation - ongoing maintenance
11  *******************************************************************************/

12
13 package org.eclipse.team.internal.ui;
14
15 import java.io.File JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.team.core.ProjectSetSerializationContext;
25 import org.eclipse.team.core.TeamException;
26 import org.eclipse.team.internal.ui.dialogs.IPromptCondition;
27 import org.eclipse.team.internal.ui.dialogs.PromptingDialog;
28
29 /**
30  * The UI based context in which project serialization occurs.
31  * The class may be subclasses to represent different UI based serialization contexts.
32  * It is recommended that all UI based serialization contexts
33  * use this class directly or indirectly as their superclass.
34  *
35  * @since 3.0
36  */

37 public class UIProjectSetSerializationContext extends ProjectSetSerializationContext {
38
39     /**
40      * The parent shell for this UI context
41      */

42     private final Shell shell;
43
44     /**
45      * Construct a new instance
46      *
47      * @param shell The parent shell for this UI context
48      */

49     public UIProjectSetSerializationContext(Shell shell, String JavaDoc filename) {
50         super(filename);
51         Assert.isNotNull(shell);
52         this.shell = shell;
53     }
54
55     /**
56      * Answer the shell associated with this UI context.
57      *
58      * @return the shell (not <code>null</code>)
59      */

60     public Object JavaDoc getShell() {
61         return shell;
62     }
63     
64     /**
65      * Given an array of projects that currently exist in the workspace
66      * prompt the user to determine which of those projects should be overwritten.
67      * <p>
68      * This default implementation prompts the user
69      * to determine which projects should be overwritten.
70      * Subclasses may override this as appropriate.
71      *
72      * @param projects
73      * an array of projects currently existing in the workspace
74      * that are desired to be overwritten.
75      * (not <code>null</code>, contains no <code>null</code>s)
76      * @return
77      * an array of zero or more projects that should be overwritten
78      * or <code>null</code> if the operation is to be canceled
79      *
80      * @see org.eclipse.team.core.ProjectSetSerializationContext#confirmOverwrite(org.eclipse.core.resources.IProject[])
81      */

82     public IProject[] confirmOverwrite(final IProject[] projects) throws TeamException {
83         IPromptCondition prompt = new IPromptCondition() {
84             List JavaDoc resources = Arrays.asList(projects);
85             public boolean needsPrompt(IResource resource) {
86                 if (resource instanceof IProject) {
87                     IProject project = (IProject) resource;
88                     return (project.exists() || getTargetFile(project).exists()) && resources.contains(resource);
89                 }
90                 return false;
91             }
92             public String JavaDoc promptMessage(IResource resource) {
93                 if (resource.exists())
94                     return NLS.bind(TeamUIMessages.UIProjectSetSerializationContext_0, new String JavaDoc[] { resource.getName() });
95                 return NLS.bind(TeamUIMessages.UIProjectSetSerializationContext_2, new String JavaDoc[] { resource.getName(), getTargetFile((IProject)resource).getAbsolutePath() });
96             }
97             public File JavaDoc getTargetFile(IProject project) {
98                 return new File JavaDoc(project.getParent().getLocation().toFile(), project.getName());
99             }
100         };
101         PromptingDialog dialog =
102             new PromptingDialog(
103                 (Shell)getShell(),
104                 projects,
105                 prompt,
106                 TeamUIMessages.UIProjectSetSerializationContext_1);
107         IResource[] resourcesToOverwrite;
108         try {
109             resourcesToOverwrite = dialog.promptForMultiple();
110         } catch (InterruptedException JavaDoc e) {
111             // Return null indicating that the user canceled the operation
112
return null;
113         }
114         IProject[] projectsToOverwrite = new IProject[resourcesToOverwrite.length];
115         System.arraycopy(resourcesToOverwrite, 0, projectsToOverwrite, 0, resourcesToOverwrite.length);
116         return projectsToOverwrite;
117     }
118
119 }
120
Popular Tags