KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Philippe Ombredanne - bug 84808
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.wizards;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.*;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.widgets.*;
29 import org.eclipse.team.core.RepositoryProvider;
30 import org.eclipse.team.internal.ccvs.core.*;
31 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
32 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
33 import org.eclipse.team.internal.ccvs.ui.*;
34 import org.eclipse.ui.PlatformUI;
35
36 /**
37  * This page allows the user to select the target parent container for
38  * the folders being checked out.
39  */

40 public class CheckoutAsProjectSelectionPage extends CVSWizardPage {
41     
42     public static final String JavaDoc NAME = "CheckoutAsProjectSelectionPage"; //$NON-NLS-1$
43

44     private TreeViewer tree;
45     private Text nameField;
46     private Combo filterList;
47     
48     private IResource selection;
49     private ICVSRemoteFolder[] remoteFolders;
50     private String JavaDoc folderName;
51     private int filter;
52
53     /**
54      * Constructor for CheckoutIntoProjectSelectionPage.
55      * @param pageName
56      * @param title
57      * @param titleImage
58      */

59     public CheckoutAsProjectSelectionPage(ImageDescriptor titleImage, ICVSRemoteFolder[] remoteFolders) {
60         super(NAME, CVSUIMessages.CheckoutAsProjectSelectionPage_title, titleImage, CVSUIMessages.CheckoutAsProjectSelectionPage_description); //
61
this.remoteFolders = remoteFolders;
62     }
63
64     /**
65      * @return
66      */

67     private boolean isSingleFolder() {
68         return remoteFolders.length == 1;
69     }
70     
71     /*
72      * For the single folder case, return the name of the folder
73      */

74     private String JavaDoc getInputFolderName() {
75         return getPreferredFolderName(remoteFolders[0]);
76     }
77     
78     private String JavaDoc getRepository() throws CVSException {
79         return remoteFolders[0].getFolderSyncInfo().getRoot();
80     }
81     
82     /**
83      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
84      */

85     public void createControl(Composite parent) {
86         Composite composite= createComposite(parent, 2, false);
87         setControl(composite);
88         
89         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.CHECKOUT_PROJECT_SELECTION_PAGE);
90         
91         if (isSingleFolder()) {
92             createLabel(composite, CVSUIMessages.CheckoutAsProjectSelectionPage_name);
93             nameField = createTextField(composite);
94             nameField.addListener(SWT.Modify, new Listener() {
95                 public void handleEvent(Event event) {
96                     folderName = nameField.getText();
97                     updateWidgetEnablements();
98                 }
99             });
100         }
101         
102         createWrappingLabel(composite, CVSUIMessages.CheckoutAsProjectSelectionPage_treeLabel, 0, 2);
103         
104         tree = createResourceSelectionTree(composite, IResource.PROJECT | IResource.FOLDER, 2 /* horizontal span */);
105         tree.addSelectionChangedListener(new ISelectionChangedListener() {
106             public void selectionChanged(SelectionChangedEvent event) {
107                 handleResourceSelection(event);
108             }
109         });
110
111         Composite filterComposite = createComposite(composite, 2, false);
112         GridData data = new GridData();
113         data.verticalAlignment = GridData.FILL;
114         data.horizontalAlignment = GridData.FILL;
115         data.horizontalSpan = 2;
116         filterComposite.setLayoutData(data);
117         createLabel(filterComposite, CVSUIMessages.CheckoutAsProjectSelectionPage_showLabel);
118         filterList = createCombo(filterComposite);
119         filterList.addSelectionListener(new SelectionAdapter() {
120             public void widgetSelected(SelectionEvent e) {
121                 handleFilterSelection();
122             }
123         });
124         
125         createWrappingLabel(composite, "", 0, 2); //$NON-NLS-1$
126

127         initializeValues();
128         updateWidgetEnablements();
129         tree.getControl().setFocus();
130         Dialog.applyDialogFont(parent);
131     }
132
133     /**
134      * Method initializeValues.
135      */

136     private void initializeValues() {
137         if (isSingleFolder()) {
138             nameField.setText(getInputFolderName());
139         }
140         tree.setInput(ResourcesPlugin.getWorkspace().getRoot());
141         filter = 0;
142         updateTreeContents(filter);
143         filterList.add(CVSUIMessages.CheckoutAsProjectSelectionPage_showAll);
144         filterList.add(CVSUIMessages.CheckoutAsProjectSelectionPage_showUnshared);
145         filterList.add(CVSUIMessages.CheckoutAsProjectSelectionPage_showSameRepo);
146         filterList.select(filter);
147     }
148
149     private void handleResourceSelection(SelectionChangedEvent event) {
150         ISelection sel = event.getSelection();
151         if (sel.isEmpty()) {
152             this.selection = null;
153         } else if (sel instanceof IStructuredSelection) {
154             this.selection = (IResource)((IStructuredSelection)sel).getFirstElement();
155         }
156         updateWidgetEnablements();
157     }
158     
159     /**
160      * Method updateWidgetEnablement.
161      */

162     private void updateWidgetEnablements() {
163         if (isSingleFolder() && !Path.EMPTY.isValidSegment(folderName)) {
164             setPageComplete(false);
165             setErrorMessage(NLS.bind(CVSUIMessages.CheckoutAsProjectSelectionPage_invalidFolderName, new String JavaDoc[] { folderName }));
166             return;
167         }
168         boolean complete = selection != null && selection.getType() != IResource.FILE;
169         setErrorMessage(null);
170         setPageComplete(complete);
171     }
172     
173     /**
174      * Returns the selection.
175      * @return IResource
176      */

177     public IResource getSelection() {
178         return selection;
179     }
180
181     private void updateTreeContents(int selected) {
182         try {
183             if (selected == 0) {
184                 tree.setInput(new AdaptableResourceList(getProjects(getRepository(), true)));
185             } else if (selected == 1) {
186                 tree.setInput(new AdaptableResourceList(getProjects(null, true)));
187             } else if (selected == 2) {
188                 tree.setInput(new AdaptableResourceList(getProjects(getRepository(), false)));
189             }
190         } catch (CVSException e) {
191             CVSUIPlugin.log(e);
192         }
193     }
194             
195     /**
196      * Method getValidTargetProjects returns the set of projects that match the provided criteria.
197      * @return IResource
198      */

199     private IProject[] getProjects(String JavaDoc root, boolean unshared) throws CVSException {
200         List JavaDoc validTargets = new ArrayList JavaDoc();
201         try {
202             IResource[] projects = ResourcesPlugin.getWorkspace().getRoot().members();
203             for (int i = 0; i < projects.length; i++) {
204                 IResource resource = projects[i];
205                 if (resource instanceof IProject) {
206                     IProject project = (IProject) resource;
207                     if (project.isAccessible()) {
208                         RepositoryProvider provider = RepositoryProvider.getProvider(project);
209                         if (provider == null && unshared) {
210                             validTargets.add(project);
211                         } else if (provider != null && provider.getID().equals(CVSProviderPlugin.getTypeId())) {
212                             ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor(project);
213                             FolderSyncInfo info = cvsFolder.getFolderSyncInfo();
214                             if (root != null && info != null && root.equals(info.getRoot())) {
215                                 validTargets.add(project);
216                             }
217                         }
218                     }
219                 }
220             }
221         } catch (CoreException e) {
222             throw CVSException.wrapException(e);
223         }
224         return (IProject[]) validTargets.toArray(new IProject[validTargets.size()]);
225     }
226     
227     public IContainer getLocalFolder() {
228         if (Path.EMPTY.isValidSegment(folderName)) {
229             return ((IContainer)getSelection()).getFolder(new Path(null, folderName));
230         } else {
231             return null;
232         }
233     }
234     
235     public IContainer getParentFolder() {
236         return ((IContainer)getSelection());
237     }
238     
239     private void handleFilterSelection() {
240         filter = filterList.getSelectionIndex();
241         updateTreeContents(filter);
242     }
243
244 }
245
Popular Tags