KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CVSProjectPropertiesPage


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;
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.resources.IProject;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.dialogs.*;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.jface.viewers.*;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.*;
32 import org.eclipse.team.core.RepositoryProvider;
33 import org.eclipse.team.core.TeamException;
34 import org.eclipse.team.internal.ccvs.core.*;
35 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
36 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.model.WorkbenchContentProvider;
39 import org.eclipse.ui.model.WorkbenchLabelProvider;
40
41 public class CVSProjectPropertiesPage extends CVSPropertiesPage {
42     IProject project;
43     ICVSRepositoryLocation oldLocation;
44     ICVSRepositoryLocation newLocation = null;
45
46     private static final int TABLE_HEIGHT_HINT = 150;
47     
48     // Widgets
49
Text methodText;
50     Text userText;
51     Text hostText;
52     Text pathText;
53     Text moduleText;
54     Text portText;
55     Text tagText;
56     private Button fetchButton;
57     private Button watchEditButton;
58     
59     IUserInfo info;
60     CVSTeamProvider provider;
61     private boolean fetch;
62     private boolean watchEdit;
63
64     public static boolean isCompatible(ICVSRepositoryLocation location, ICVSRepositoryLocation oldLocation) {
65         if (!location.getHost().equals(oldLocation.getHost())) return false;
66         if (!location.getRootDirectory().equals(oldLocation.getRootDirectory())) return false;
67         if (location.equals(oldLocation)) return false;
68         return true;
69     }
70     
71     private class RepositorySelectionDialog extends Dialog {
72         ICVSRepositoryLocation[] allLocations;
73         ICVSRepositoryLocation[] compatibleLocatons;
74         ICVSRepositoryLocation selectedLocation;
75         
76         TableViewer viewer;
77         Button okButton;
78         boolean showCompatible = true;
79         
80         public RepositorySelectionDialog(Shell shell, ICVSRepositoryLocation oldLocation) {
81             super(shell);
82             initialize(oldLocation);
83         }
84         private void initialize(ICVSRepositoryLocation oldLocation) {
85             allLocations = CVSUIPlugin.getPlugin().getRepositoryManager().getKnownRepositoryLocations();
86             List JavaDoc locations = new ArrayList JavaDoc();
87             for (int i = 0; i < allLocations.length; i++) {
88                 ICVSRepositoryLocation location = allLocations[i];
89                 if (isCompatible(location, oldLocation)) {
90                     locations.add(location);
91                 }
92             }
93             compatibleLocatons = (ICVSRepositoryLocation[]) locations.toArray(new ICVSRepositoryLocation[locations.size()]);
94         }
95         protected void createButtonsForButtonBar(Composite parent) {
96             // create OK and Cancel buttons by default
97
okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
98             okButton.setEnabled(false);
99             createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
100         }
101         protected Control createDialogArea(Composite parent) {
102             parent.getShell().setText(CVSUIMessages.CVSProjectPropertiesPage_Select_a_Repository_1);
103             Composite composite = (Composite) super.createDialogArea(parent);
104         
105             createLabel(composite, CVSUIMessages.CVSProjectPropertiesPage_Select_a_CVS_repository_location_to_share_the_project_with__2, 1);
106             Table table = new Table(composite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
107             GridData data = new GridData(GridData.FILL_HORIZONTAL);
108             data.heightHint = TABLE_HEIGHT_HINT;
109             table.setLayoutData(data);
110             viewer = new TableViewer(table);
111             viewer.setLabelProvider(new WorkbenchLabelProvider());
112             viewer.setContentProvider(new WorkbenchContentProvider() {
113                 public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
114                     if (showCompatible) {
115                         return compatibleLocatons;
116                     } else {
117                         return allLocations;
118                     }
119                 }
120             });
121             viewer.addSelectionChangedListener(new ISelectionChangedListener() {
122                 public void selectionChanged(SelectionChangedEvent event) {
123                     IStructuredSelection selection = (IStructuredSelection)event.getSelection();
124                     if (selection.isEmpty()) {
125                         selectedLocation = null;
126                         okButton.setEnabled(false);
127                     } else {
128                         selectedLocation = (ICVSRepositoryLocation)selection.getFirstElement();
129                         okButton.setEnabled(true);
130                     }
131                 }
132             });
133             viewer.addDoubleClickListener(new IDoubleClickListener() {
134                 public void doubleClick(DoubleClickEvent event) {
135                     okPressed();
136                 }
137             });
138             viewer.setInput(compatibleLocatons);
139             
140             final Button compatibleButton = createCheckBox(composite, CVSUIMessages.CVSProjectPropertiesPage_31);
141             compatibleButton.setSelection(showCompatible);
142             compatibleButton.addSelectionListener(new SelectionAdapter() {
143                 public void widgetSelected(SelectionEvent e) {
144                     showCompatible = compatibleButton.getSelection();
145                     viewer.refresh();
146                 }
147             });
148
149             Dialog.applyDialogFont(parent);
150             
151             return composite;
152         }
153         protected void cancelPressed() {
154             selectedLocation = null;
155             super.cancelPressed();
156         }
157         public ICVSRepositoryLocation getLocation() {
158             return selectedLocation;
159         }
160     }
161     
162     /*
163      * @see PreferencesPage#createContents
164      */

165     protected Control createContents(Composite parent) {
166         initialize();
167         
168         Composite composite = new Composite(parent, SWT.NULL);
169         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
170         GridLayout layout = new GridLayout();
171         layout.numColumns = 2;
172         composite.setLayout(layout);
173         
174         Label label = createLabel(composite, CVSUIMessages.CVSProjectPropertiesPage_connectionType, 1);
175         methodText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
176

177         label = createLabel(composite, CVSUIMessages.CVSProjectPropertiesPage_user, 1);
178         userText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
179

180         label = createLabel(composite, CVSUIMessages.CVSRepositoryLocationPropertySource_host, 1);
181         hostText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
182

183         label = createLabel(composite, CVSUIMessages.CVSPropertiesPage_port, 1);
184         portText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
185

186         label = createLabel(composite, CVSUIMessages.CVSRepositoryLocationPropertySource_root, 1);
187         pathText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
188

189         label = createLabel(composite, CVSUIMessages.CVSPropertiesPage_module, 1);
190         moduleText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
191

192         label = createLabel(composite, CVSUIMessages.CVSPropertiesPage_tag, 1);
193         tagText = createReadOnlyText(composite, "", 1); //$NON-NLS-1$
194

195         createLabel(composite, "", 1); //$NON-NLS-1$
196

197         // Should absent directories be fetched on update
198
fetchButton = createCheckBox(composite, CVSUIMessages.CVSProjectPropertiesPage_fetchAbsentDirectoriesOnUpdate);
199         fetchButton.addListener(SWT.Selection, new Listener() {
200             public void handleEvent(Event event) {
201                 fetch = fetchButton.getSelection();
202             }
203         });
204         
205         // Should the project be configured for watch/edit
206
watchEditButton = createCheckBox(composite, CVSUIMessages.CVSProjectPropertiesPage_configureForWatchEdit);
207         watchEditButton.addListener(SWT.Selection, new Listener() {
208             public void handleEvent(Event event) {
209                 watchEdit = watchEditButton.getSelection();
210             }
211         });
212         
213         createLabel(composite, "", 1); //$NON-NLS-1$
214
createLabel(composite, "", 1); //$NON-NLS-1$
215
createLabel(composite, "", 1); //$NON-NLS-1$
216
createLabel(composite, "", 1); //$NON-NLS-1$
217

218         label = new Label(composite, SWT.WRAP);
219         label.setText(CVSUIMessages.CVSProjectPropertiesPage_You_can_change_the_sharing_of_this_project_to_another_repository_location__However__this_is_only_possible_if_the_new_location_is___compatible____on_the_same_host_with_the_same_repository_path___1);
220         GridData data = new GridData(GridData.FILL_HORIZONTAL);
221         data.widthHint = 200;
222         data.horizontalSpan = 2;
223         label.setLayoutData(data);
224         
225         Button changeButton = new Button(composite, SWT.PUSH);
226         changeButton.setText(CVSUIMessages.CVSProjectPropertiesPage_Change_Sharing_5);
227         changeButton.addListener(SWT.Selection, new Listener() {
228             public void handleEvent(Event e) {
229                 RepositorySelectionDialog dialog = new RepositorySelectionDialog(getShell(), oldLocation);
230                 dialog.open();
231                 ICVSRepositoryLocation location = dialog.getLocation();
232                 if (location == null) return;
233                 newLocation = location;
234                 initializeValues(newLocation);
235             }
236         });
237         
238         initializeValues(oldLocation);
239         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.PROJECT_PROPERTY_PAGE);
240         Dialog.applyDialogFont(parent);
241         return composite;
242     }
243     /**
244      * Utility method that creates a label instance
245      * and sets the default layout data.
246      *
247      * @param parent the parent for the new label
248      * @param text the text for the new label
249      * @return the new label
250      */

251     protected Label createLabel(Composite parent, String JavaDoc text, int span) {
252         Label label = new Label(parent, SWT.LEFT);
253         label.setText(text);
254         GridData data = new GridData();
255         data.horizontalSpan = span;
256         data.horizontalAlignment = GridData.FILL;
257         label.setLayoutData(data);
258         return label;
259     }
260     /**
261      * Utility method that creates a text instance
262      * and sets the default layout data.
263      *
264      * @param parent the parent for the new label
265      * @param text the text for the new text
266      * @return the new text
267      */

268     protected Text createReadOnlyText(Composite parent, String JavaDoc text, int span) {
269         Text txt = new Text(parent, SWT.LEFT | SWT.READ_ONLY);
270         txt.setText(text);
271         GridData data = new GridData();
272         data.horizontalSpan = span;
273         data.horizontalAlignment = GridData.FILL;
274         txt.setLayoutData(data);
275         return txt;
276     }
277     /**
278      * Creates a new checkbox instance and sets the default layout data.
279      *
280      * @param group the composite in which to create the checkbox
281      * @param label the string to set into the checkbox
282      * @return the new checkbox
283      */

284     protected Button createCheckBox(Composite group, String JavaDoc label) {
285         Button button = new Button(group, SWT.CHECK | SWT.LEFT);
286         button.setText(label);
287         GridData data = new GridData();
288         data.horizontalSpan = 2;
289         button.setLayoutData(data);
290         return button;
291     }
292     /**
293      * Initializes the page
294      */

295     private void initialize() {
296         // Get the project that is the source of this property page
297
project = null;
298         IAdaptable element = getElement();
299         if (element instanceof IProject) {
300             project = (IProject)element;
301         } else {
302             Object JavaDoc adapter = element.getAdapter(IProject.class);
303             if (adapter instanceof IProject) {
304                 project = (IProject)adapter;
305             }
306         }
307         // Do some pre-checks to ensure we're in a good state
308
provider = (CVSTeamProvider)RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId());
309         if (provider == null) return;
310         CVSWorkspaceRoot cvsRoot = provider.getCVSWorkspaceRoot();
311         try {
312             oldLocation = cvsRoot.getRemoteLocation();
313             fetch = provider.getFetchAbsentDirectories();
314             watchEdit = provider.isWatchEditEnabled();
315         } catch (TeamException e) {
316             handle(e);
317         }
318     }
319     /**
320      * Set the initial values of the widgets
321      */

322     private void initializeValues(ICVSRepositoryLocation location) {
323         if (provider == null) return;
324         CVSWorkspaceRoot cvsRoot = provider.getCVSWorkspaceRoot();
325         ICVSFolder folder = cvsRoot.getLocalRoot();
326         
327         try {
328             if (!folder.isCVSFolder()) return;
329             methodText.setText(location.getMethod().getName());
330             info = location.getUserInfo(true);
331             userText.setText(info.getUsername());
332             hostText.setText(location.getHost());
333             int port = location.getPort();
334             if (port == ICVSRepositoryLocation.USE_DEFAULT_PORT) {
335                 portText.setText(CVSUIMessages.CVSPropertiesPage_defaultPort);
336             } else {
337                 portText.setText("" + port); //$NON-NLS-1$
338
}
339             pathText.setText(location.getRootDirectory());
340             FolderSyncInfo syncInfo = folder.getFolderSyncInfo();
341             if (syncInfo == null) return;
342             String JavaDoc label = syncInfo.getRepository();
343             if (label.equals(FolderSyncInfo.VIRTUAL_DIRECTORY)) {
344                 label = NLS.bind(CVSUIMessages.CVSPropertiesPage_virtualModule, new String JavaDoc[] { label });
345             }
346             moduleText.setText(label);
347             fetchButton.setSelection(fetch);
348             watchEditButton.setSelection(watchEdit);
349         } catch (TeamException e) {
350             handle(e);
351         }
352         
353         initializeTag();
354     }
355     
356     private void initializeTag() {
357         provider = (CVSTeamProvider)RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId());
358         if (provider == null) return;
359         try {
360             ICVSFolder local = CVSWorkspaceRoot.getCVSFolderFor(project);
361             CVSTag tag = local.getFolderSyncInfo().getTag();
362             
363             tagText.setText(getTagLabel(tag));
364
365         } catch (TeamException e) {
366             handle(e);
367         }
368     }
369     /*
370      * @see PreferencesPage#performOk
371      */

372     public boolean performOk() {
373         final boolean[] changeReadOnly = { false };
374         try {
375             if (fetch != provider.getFetchAbsentDirectories())
376                 provider.setFetchAbsentDirectories(fetch);
377             if (watchEdit != provider.isWatchEditEnabled()) {
378                 provider.setWatchEditEnabled(watchEdit);
379                 changeReadOnly[0] = true;
380             }
381         } catch (CVSException e) {
382             handle(e);
383         }
384         if (newLocation == null && !changeReadOnly[0]) {
385             return true;
386         }
387         try {
388             if (newLocation != null && !isCompatible(newLocation, oldLocation)) {
389                 if (!MessageDialog.openQuestion(getShell(), CVSUIMessages.CVSProjectPropertiesPage_32, CVSUIMessages.CVSProjectPropertiesPage_33)) { //
390
return false;
391                 }
392             }
393             new ProgressMonitorDialog(getShell()).run(true, true, new IRunnableWithProgress() {
394                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
395                     try {
396                         monitor.beginTask(CVSUIMessages.CVSProjectPropertiesPage_progressTaskName,
397                         ((newLocation == null)?0:100) + (changeReadOnly[0]?100:0));
398                         if (newLocation != null)
399                             provider.setRemoteRoot(newLocation, Policy.subMonitorFor(monitor, 100));
400                         if (changeReadOnly[0])
401                             setReadOnly(watchEdit, Policy.infiniteSubMonitorFor(monitor, 100));
402                     } catch (TeamException e) {
403                         throw new InvocationTargetException JavaDoc(e);
404                     }
405                 }
406             });
407             newLocation = null;
408             if (changeReadOnly[0]) {
409                 CVSUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, CVSUIPlugin.P_DECORATORS_CHANGED, null, null));
410             }
411         } catch (InvocationTargetException JavaDoc e) {
412             handle(e);
413         } catch (InterruptedException JavaDoc e) {
414             return false;
415         }
416             
417         return true;
418     }
419     /**
420      * @param watchEdit
421      */

422     protected void setReadOnly(final boolean watchEdit, final IProgressMonitor monitor) throws CVSException {
423         monitor.beginTask(null, 512);
424         String JavaDoc taskName = watchEdit?
425             CVSUIMessages.CVSProjectPropertiesPage_setReadOnly:
426             CVSUIMessages.CVSProjectPropertiesPage_clearReadOnly;
427         monitor.subTask(taskName);
428         ICVSFolder root = CVSWorkspaceRoot.getCVSFolderFor(project);
429         root.accept(new ICVSResourceVisitor() {
430             public void visitFile(ICVSFile file) throws CVSException {
431                 // only change managed, unmodified files
432
if (file.isManaged() && !file.isModified(null))
433                     file.setReadOnly(watchEdit);
434                 monitor.worked(1);
435             }
436
437             public void visitFolder(ICVSFolder folder) throws CVSException {
438                 folder.acceptChildren(this);
439             }
440         });
441         monitor.done();
442     }
443     /**
444      * Shows the given errors to the user.
445      */

446     protected void handle(Throwable JavaDoc t) {
447         CVSUIPlugin.openError(getShell(), null, null, t);
448     }
449 }
450
451
Popular Tags