KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > RefreshRemoteProjectWizard


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.ccvs.ui.repo;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jface.dialogs.*;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.jface.wizard.Wizard;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.team.core.TeamException;
28 import org.eclipse.team.internal.ccvs.core.*;
29 import org.eclipse.team.internal.ccvs.ui.*;
30 import org.eclipse.team.internal.ccvs.ui.Policy;
31
32 /**
33  * Wizard for refreshing the tags for a CVS repository location
34  */

35 public class RefreshRemoteProjectWizard extends Wizard {
36     
37     // The initial size of this wizard.
38
private final static int INITIAL_WIDTH = 300;
39     private final static int INITIAL_HEIGHT = 350;
40     
41     private ICVSRepositoryLocation root;
42     private ICVSRemoteResource[] rootFolders;
43     private RefreshRemoteProjectSelectionPage projectSelectionPage;
44     private IDialogSettings settings;
45     
46     public static boolean execute(Shell shell, final ICVSRepositoryLocation root) {
47         final ICVSRemoteResource[][] rootFolders = new ICVSRemoteResource[1][0];
48         rootFolders[0] = null;
49         try {
50             new ProgressMonitorDialog(shell).run(true, true, new IRunnableWithProgress() {
51                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
52                     try {
53                         rootFolders[0] = CVSUIPlugin.getPlugin().getRepositoryManager().getFoldersForTag(root, CVSTag.DEFAULT, monitor);
54                     } catch (CVSException e) {
55                         throw new InvocationTargetException JavaDoc(e);
56                     }
57                 }
58             });
59         } catch (InvocationTargetException JavaDoc e) {
60             CVSUIPlugin.openError(shell, null, null, e);
61             return false;
62         } catch (InterruptedException JavaDoc e) {
63             return false;
64         }
65         RefreshRemoteProjectWizard wizard = new RefreshRemoteProjectWizard(root, rootFolders[0]);
66         WizardDialog dialog = new WizardDialog(shell, wizard);
67         /**
68          * This is the only place where a size hint > 0 is required. The wizard
69          * page should in general have hints of 0 (and grab excessive space).
70          */

71         dialog.setMinimumPageSize(INITIAL_WIDTH, INITIAL_HEIGHT);
72         return (dialog.open() == Window.OK);
73     }
74     
75     public RefreshRemoteProjectWizard(ICVSRepositoryLocation root, ICVSRemoteResource[] rootFolders) {
76         this.root = root;
77         this.rootFolders = rootFolders;
78         IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
79         this.settings = workbenchSettings.getSection("RefreshRemoteProjectWizard");//$NON-NLS-1$
80
if (settings == null) {
81             this.settings = workbenchSettings.addNewSection("RefreshRemoteProjectWizard");//$NON-NLS-1$
82
}
83         setWindowTitle(CVSUIMessages.RefreshRemoteProjectWizard_title);
84     }
85     
86     /**
87      * @see org.eclipse.jface.wizard.IWizard#addPages()
88      */

89     public void addPages() {
90         setNeedsProgressMonitor(true);
91         ImageDescriptor substImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_NEW_LOCATION);
92         projectSelectionPage = new RefreshRemoteProjectSelectionPage(
93             "ProjectSelectionPage", //$NON-NLS-1$
94
CVSUIMessages.RefreshRemoteProjectSelectionPage_pageTitle,
95             substImage,
96             CVSUIMessages.RefreshRemoteProjectSelectionPage_pageDescription,
97             settings, root, rootFolders);
98         addPage(projectSelectionPage);
99     }
100     
101     /**
102      * @see org.eclipse.jface.wizard.Wizard#performFinish()
103      */

104     public boolean performFinish() {
105         final ICVSRemoteResource[] selectedFolders = projectSelectionPage.getSelectedRemoteProject();
106         try {
107             getContainer().run(true, true, new IRunnableWithProgress() {
108                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
109                     final RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager();
110                     // Run in the manager to avoid multiple repo view updates
111
manager.run(new IRunnableWithProgress() {
112                         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,InterruptedException JavaDoc {
113                             monitor.beginTask(null, 100);
114                             ICVSRemoteResource[] failedFolders = internalRefresh(manager, selectedFolders, false /* recurse */, Policy.subMonitorFor(monitor, 80));
115                             if (failedFolders.length > 0) {
116                                 // Go deep any any failed folders.
117
if (promptForDeepRefresh(failedFolders))
118                                     internalRefresh(manager, failedFolders, true /* recurse */, Policy.subMonitorFor(monitor, 20));
119                             }
120                             monitor.done();
121                         }
122                     }, monitor);
123                 }
124             });
125             return true;
126         } catch (InvocationTargetException JavaDoc e) {
127             CVSUIPlugin.openError(getShell(), null, null ,e);
128         } catch (InterruptedException JavaDoc e) {
129         }
130         return false;
131     }
132
133     /*
134      * Refresh the tags of the given resources and return those for which no tags were found.
135      */

136     private ICVSRemoteResource[] internalRefresh(final RepositoryManager manager, final ICVSRemoteResource[] selectedFolders, final boolean recurse, IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
137         List JavaDoc failedFolders = new ArrayList JavaDoc();
138         monitor.beginTask(null, 100 * selectedFolders.length);
139             for (int i = 0; i < selectedFolders.length; i++) {
140                 try {
141                     ICVSRemoteResource resource = selectedFolders[i];
142                     if (resource instanceof ICVSFolder) {
143                         CVSTag[] tags = manager.refreshDefinedTags((ICVSFolder)resource, recurse, true /* notify */, Policy.subMonitorFor(monitor, 100));
144                         if (tags.length == 0) {
145                             failedFolders.add(resource);
146                         }
147                     }
148                 } catch (TeamException e) {
149                     CVSUIPlugin.log(IStatus.ERROR, NLS.bind("An error occurred while fetching the tags for {0}", selectedFolders[i].getName()), e); //$NON-NLS-1$
150
}
151             }
152             return (ICVSRemoteResource[]) failedFolders.toArray(new ICVSRemoteResource[failedFolders.size()]);
153     }
154
155     private boolean promptForDeepRefresh(final ICVSRemoteResource[] folders) {
156         final boolean[] prompt = new boolean[] { false };
157         getShell().getDisplay().syncExec(new Runnable JavaDoc() {
158             public void run() {
159                 MessageDialog dialog = new MessageDialog(getShell(), CVSUIMessages.RefreshRemoteProjectWizard_0, null,
160                         getNoTagsMessage(folders),
161                         MessageDialog.INFORMATION,
162                         new String JavaDoc[] {
163                             CVSUIMessages.RefreshRemoteProjectWizard_1,
164                             CVSUIMessages.RefreshRemoteProjectWizard_2
165                         }, 1);
166                 int code = dialog.open();
167                 if (code == 0) {
168                     prompt[0] = true;
169                 }
170
171             }
172         });
173         return prompt[0];
174     }
175
176     private String JavaDoc getNoTagsMessage(ICVSRemoteResource[] folders) {
177         if (folders.length == 1) {
178             return NLS.bind(CVSUIMessages.RefreshRemoteProjectWizard_3, new String JavaDoc[] { folders[0].getRepositoryRelativePath() });
179         }
180         return NLS.bind(CVSUIMessages.RefreshRemoteProjectWizard_4, new String JavaDoc[] { Integer.toString(folders.length) });
181     }
182 }
183
Popular Tags