KickJava   Java API By Example, From Geeks To Geeks.

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


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.wizards;
12
13 import org.eclipse.compare.CompareConfiguration;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.AbstractTreeViewer;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.wizard.IWizardPage;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.widgets.*;
27 import org.eclipse.team.core.diff.*;
28 import org.eclipse.team.core.mapping.IResourceDiffTree;
29 import org.eclipse.team.internal.ccvs.ui.*;
30 import org.eclipse.team.internal.ccvs.ui.IHelpContextIds;
31 import org.eclipse.team.internal.ccvs.ui.mappings.ModelSynchronizeWizard;
32 import org.eclipse.team.internal.ui.*;
33 import org.eclipse.team.ui.synchronize.*;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.part.PageBook;
36
37 /**
38  * Page that displays the compare input for sharing
39  */

40 public class SharingWizardSyncPage extends CVSWizardPage implements IDiffChangeListener {
41     
42     // Constant keys used to store last size for this page
43
private static final String JavaDoc PAGE_HEIGHT = "SyncPageHeight"; //$NON-NLS-1$
44
private static final String JavaDoc PAGE_WIDTH = "SyncPageWidth"; //$NON-NLS-1$
45

46     private ParticipantPageSaveablePart input;
47     private ISynchronizePageConfiguration configuration;
48     private IProject project;
49     
50     PageBook pageBook;
51     private Control syncPage;
52     private Control noChangesPage;
53     
54     private int width;
55     private int height;
56     private SharingWizardPageActionGroup sharingWizardPageActionGroup;
57     private Button fCheckbox;
58     
59     public SharingWizardSyncPage(String JavaDoc pageName, String JavaDoc title, ImageDescriptor titleImage, String JavaDoc description) {
60         super(pageName, title, titleImage, description);
61     }
62
63     public void setProject(IProject project) {
64         this.project = project;
65     }
66     
67     /* (non-Javadoc)
68      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
69      */

70     public void createControl(Composite parent) {
71         
72         final PixelConverter converter= SWTUtils.createDialogPixelConverter(parent);
73         
74         final Composite composite = new Composite(parent, SWT.NONE);
75         composite.setLayout(SWTUtils.createGridLayout(1, converter, SWTUtils.MARGINS_DEFAULT));
76         setControl(composite);
77         
78         pageBook = new PageBook(composite, SWT.NONE);
79         pageBook.setLayoutData(SWTUtils.createHVFillGridData());
80         
81         syncPage = createSyncPage(pageBook);
82         
83         noChangesPage = createNoChangesPage(pageBook);
84         noChangesPage.setLayoutData(SWTUtils.createHVFillGridData());
85         
86         updatePage();
87         
88         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.SHARING_SYNC_PAGE);
89         Dialog.applyDialogFont(parent);
90     }
91     
92     private IResourceDiffTree getDiffTree() {
93         if (configuration == null)
94             return null;
95         return getParticipant().getContext().getDiffTree();
96     }
97     
98     private Control createSyncPage(PageBook pageBook) {
99         Composite composite = createComposite(pageBook, 1, false);
100         input = createCompareInput();
101         input.createPartControl(composite);
102         getDiffTree().addDiffChangeListener(this);
103         
104         fCheckbox= new Button(composite, SWT.CHECK);
105         fCheckbox.setLayoutData(SWTUtils.createHFillGridData());
106         fCheckbox.setText(CVSUIMessages.SharingWizardSyncPage_12);
107         fCheckbox.setSelection(true);
108         
109         return composite;
110     }
111     
112     private Control createNoChangesPage(PageBook pageBook) {
113         Composite composite = createComposite(pageBook, 1, false);
114         createWrappingLabel(composite, NLS.bind(CVSUIMessages.SharingWizardSyncPage_3, new String JavaDoc[] { project.getName() }), 0);
115         return composite;
116     }
117
118     /* private */ void showErrors(final IStatus[] status) {
119         if (status.length == 0) return;
120         getShell().getDisplay().syncExec(new Runnable JavaDoc() {
121         
122             public void run() {
123                 String JavaDoc title = CVSUIMessages.SharingWizardSyncPage_8;
124                 if (status.length == 1) {
125                     IStatus s = status[0];
126                     if (s.getException() instanceof CoreException) {
127                         s = ((CoreException)s.getException()).getStatus();
128                     }
129                     ErrorDialog.openError(getShell(), title, null, s);
130                 } else {
131                     MultiStatus multi = new MultiStatus(CVSUIPlugin.ID, 0, status, CVSUIMessages.SharingWizardSyncPage_9, null);
132                     ErrorDialog.openError(getShell(), title, null, multi);
133                 }
134             }
135         });
136     }
137     
138     private ParticipantPageSaveablePart createCompareInput() {
139         ISynchronizeParticipant participant = createParticipant();
140         configuration = participant.createPageConfiguration();
141         configuration.setProperty(ISynchronizePageConfiguration.P_TOOLBAR_MENU, new String JavaDoc[] {ISynchronizePageConfiguration.NAVIGATE_GROUP, SharingWizardPageActionGroup.ACTION_GROUP});
142         sharingWizardPageActionGroup = new SharingWizardPageActionGroup();
143         configuration.addActionContribution(sharingWizardPageActionGroup);
144         configuration.setRunnableContext(getContainer());
145         
146         CompareConfiguration cc = new CompareConfiguration();
147         cc.setLeftEditable(false);
148         cc.setRightEditable(false);
149         ParticipantPageSaveablePart part = new ParticipantPageSaveablePart(getShell(), cc, configuration, participant);
150         part.setShowContentPanes(false);
151         return part;
152     }
153
154     private ISynchronizeParticipant createParticipant() {
155         return ModelSynchronizeWizard.createWorkspaceParticipant(Utils.getResourceMappings(new IProject[] { project }), getShell());
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
160      */

161     public void dispose() {
162         if (input != null) {
163             input.getParticipant().dispose();
164             input.dispose();
165         }
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.jface.wizard.WizardPage#setPreviousPage(org.eclipse.jface.wizard.IWizardPage)
170      */

171     public void setPreviousPage(IWizardPage page) {
172         // There's no going back from this page
173
super.setPreviousPage(null);
174     }
175
176     private void updatePage() {
177         Display.getDefault().syncExec(new Runnable JavaDoc() {
178             public void run() {
179                 if (pageBook.isDisposed()) return;
180                 if (getDiffTree().isEmpty()) {
181                     pageBook.showPage(noChangesPage);
182                 } else {
183                     pageBook.showPage(syncPage);
184                 }
185             }
186         });
187     }
188
189     public ModelSynchronizeParticipant getParticipant() {
190         return (ModelSynchronizeParticipant)configuration.getParticipant();
191     }
192     
193     /* (non-Javadoc)
194      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
195      */

196     public void setVisible(boolean visible) {
197         super.setVisible(visible);
198         if (syncPage.isVisible()) {
199             initializeSize();
200             getShell().setSize(Math.max(width, 300), Math.max(height, 300));
201             if(input != null) {
202                 Viewer viewer = input.getPageConfiguration().getPage().getViewer();
203                 if(viewer instanceof AbstractTreeViewer && !viewer.getControl().isDisposed()) {
204                     ((AbstractTreeViewer)viewer).expandToLevel(2);
205                 }
206             }
207         }
208     }
209     
210     private void initializeSize() {
211         IDialogSettings settings = getDialogSettings();
212         if (settings != null) {
213             try {
214                 width = settings.getInt(PAGE_WIDTH);
215                 height = settings.getInt(PAGE_HEIGHT);
216             } catch (NumberFormatException JavaDoc e) {
217                 // Ignore and go on;
218
}
219         }
220         if (width == 0) width = 640;
221         if (height == 0) height = 480;
222     }
223
224     /**
225      * Save the size of the page so it can be opened with the same size next time
226      */

227     public void saveSettings() {
228         IDialogSettings settings = getDialogSettings();
229         if (settings != null) {
230             Point size = getShell().getSize();
231             settings.put(PAGE_WIDTH, size.x);
232             settings.put(PAGE_HEIGHT, size.y);
233         }
234     }
235     
236     public boolean commitChanges() {
237         return fCheckbox != null ? fCheckbox.getSelection() && hasOutgoingChanges() : false;
238     }
239     
240     private boolean hasOutgoingChanges() {
241         IResourceDiffTree tree = getDiffTree();
242         return tree != null && tree.hasMatchingDiffs(ResourcesPlugin.getWorkspace().getRoot().getFullPath(), new FastDiffFilter() {
243             public boolean select(IDiff diff) {
244                 if (diff instanceof IThreeWayDiff) {
245                     IThreeWayDiff twd = (IThreeWayDiff) diff;
246                     return twd.getDirection() == IThreeWayDiff.OUTGOING || twd.getDirection() == IThreeWayDiff.CONFLICTING;
247                 }
248                 return false;
249             }
250         });
251     }
252
253     /**
254      * @return Returns the project.
255      */

256     public IProject getProject() {
257         return project;
258     }
259
260     public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
261         showErrors(event.getErrors());
262         updatePage();
263     }
264
265     public void propertyChanged(IDiffTree tree, int property, IPath[] paths) {
266         // Ignore
267
}
268 }
269
Popular Tags