KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.team.internal.ccvs.ui.wizards;
13
14 import org.eclipse.compare.structuremergeviewer.IDiffElement;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.jface.dialogs.*;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.jface.viewers.TreeViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.wizard.*;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.team.core.synchronize.SyncInfo;
31 import org.eclipse.team.core.synchronize.SyncInfoSet;
32 import org.eclipse.team.internal.ccvs.ui.*;
33 import org.eclipse.team.internal.ccvs.ui.IHelpContextIds;
34 import org.eclipse.team.internal.core.subscribers.ActiveChangeSet;
35 import org.eclipse.team.internal.core.subscribers.ChangeSet;
36 import org.eclipse.team.internal.ui.*;
37 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
38 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
39 import org.eclipse.team.ui.synchronize.*;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.part.PageBook;
42
43 /**
44  * This wizard page shows a preview of the commit operation and allows entering
45  * a commit comment.
46  */

47 public class CommitWizardCommitPage extends WizardPage implements IPropertyChangeListener {
48     
49     private final CommitCommentArea fCommentArea;
50     
51     private ISynchronizePageConfiguration fConfiguration;
52     
53     protected final CommitWizard fWizard;
54     
55     private ParticipantPagePane fPagePane;
56     private PageBook bottomChild;
57     
58     public CommitWizardCommitPage(IResource [] resources, CommitWizard wizard) {
59         
60         super(CVSUIMessages.CommitWizardCommitPage_0);
61         setTitle(CVSUIMessages.CommitWizardCommitPage_0);
62         setDescription(CVSUIMessages.CommitWizardCommitPage_2);
63         
64         fWizard= wizard;
65         fCommentArea= new CommitCommentArea();
66         fCommentArea.setProposedComment(getProposedComment(resources));
67         if (resources.length > 0)
68             fCommentArea.setProject(resources[0].getProject());
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
73      */

74     public void createControl(Composite parent) {
75         initializeDialogUnits(parent);
76         Dialog.applyDialogFont(parent);
77         final PixelConverter converter= new PixelConverter(parent);
78         
79         final Composite composite= new Composite(parent, SWT.NONE);
80         composite.setLayout(SWTUtils.createGridLayout(1, converter, SWTUtils.MARGINS_DEFAULT));
81         // set F1 help
82
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.COMMIT_COMMENT_PAGE);
83         
84         createCommentArea(composite, converter);
85         createChangesArea(composite, converter);
86         
87         //fSashForm.setWeights(weights);
88
Dialog.applyDialogFont(parent);
89         setControl(composite);
90         
91         fCommentArea.setFocus();
92         
93         validatePage(false);
94     }
95     
96     private void createCommentArea(Composite parent, PixelConverter converter) {
97         
98         fCommentArea.createArea(parent);
99         fCommentArea.getComposite().setLayoutData(SWTUtils.createGridData(SWT.DEFAULT, SWT.DEFAULT, SWT.FILL, SWT.FILL, true, true));
100         fCommentArea.addPropertyChangeListener(this);
101         
102         createPlaceholder(parent);
103     }
104     
105     private void createChangesArea(Composite parent, PixelConverter converter) {
106         
107         CommitWizardParticipant participant= fWizard.getParticipant();
108         int size = participant.getSyncInfoSet().size();
109         if (size > getFileDisplayThreshold()) {
110             // Create a page book to allow eventual inclusion of changes
111
bottomChild = new PageBook(parent, SWT.NONE);
112             bottomChild.setLayoutData(SWTUtils.createGridData(SWT.DEFAULT, SWT.DEFAULT, SWT.FILL, SWT.FILL, true, false));
113             // Create composite for showing the reason for not showing the changes and a button to show them
114
Composite changeDesc = new Composite(bottomChild, SWT.NONE);
115             changeDesc.setLayout(SWTUtils.createGridLayout(1, converter, SWTUtils.MARGINS_NONE));
116             SWTUtils.createLabel(changeDesc, NLS.bind(CVSUIMessages.CommitWizardCommitPage_1, new String JavaDoc[] { Integer.toString(size), Integer.toString(getFileDisplayThreshold()) }));
117             Button showChanges = new Button(changeDesc, SWT.PUSH);
118             showChanges.setText(CVSUIMessages.CommitWizardCommitPage_5);
119             showChanges.addSelectionListener(new SelectionAdapter() {
120                 public void widgetSelected(SelectionEvent e) {
121                     showChangesPane();
122                 }
123             });
124             showChanges.setLayoutData(new GridData());
125             bottomChild.showPage(changeDesc);
126         } else {
127             final Composite composite= new Composite(parent, SWT.NONE);
128             composite.setLayout(SWTUtils.createGridLayout(1, converter, SWTUtils.MARGINS_NONE));
129             composite.setLayoutData(SWTUtils.createGridData(SWT.DEFAULT, SWT.DEFAULT, SWT.FILL, SWT.FILL, true, true));
130             
131             createPlaceholder(composite);
132             
133             Control c = createChangesPage(composite, participant);
134             c.setLayoutData(SWTUtils.createHVFillGridData());
135         }
136     }
137
138     protected void showChangesPane() {
139         Control c = createChangesPage(bottomChild, fWizard.getParticipant());
140         bottomChild.setLayoutData(SWTUtils.createGridData(SWT.DEFAULT, SWT.DEFAULT, SWT.FILL, SWT.FILL, true, true));
141         bottomChild.showPage(c);
142         Dialog.applyDialogFont(getControl());
143         ((Composite)getControl()).layout();
144     }
145
146     private Control createChangesPage(final Composite composite, CommitWizardParticipant participant) {
147         fConfiguration= participant.createPageConfiguration();
148         fPagePane= new ParticipantPagePane(getShell(), true /* modal */, fConfiguration, participant);
149         Control control = fPagePane.createPartControl(composite);
150         return control;
151     }
152
153     private int getFileDisplayThreshold() {
154         return CVSUIPlugin.getPlugin().getPreferenceStore().getInt(ICVSUIConstants.PREF_COMMIT_FILES_DISPLAY_THRESHOLD);
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
159      */

160     public void dispose() {
161         super.dispose();
162         // Disposing of the page pane will dispose of the page and the configuration
163
if (fPagePane != null)
164             fPagePane.dispose();
165     }
166     
167     private void createPlaceholder(final Composite composite) {
168         final Composite placeholder= new Composite(composite, SWT.NONE);
169         placeholder.setLayoutData(new GridData(SWT.DEFAULT, convertHorizontalDLUsToPixels(IDialogConstants.VERTICAL_SPACING) /3));
170     }
171     
172     public String JavaDoc getComment(Shell shell) {
173         return fCommentArea.getCommentWithPrompt(shell);
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
178      */

179     public boolean isPageComplete() {
180         /* if empty comment is not allowed (see bug 114678) */
181         final IPreferenceStore store = CVSUIPlugin.getPlugin()
182                 .getPreferenceStore();
183         final String JavaDoc allowEmptyComment = store
184                 .getString(ICVSUIConstants.PREF_ALLOW_EMPTY_COMMIT_COMMENTS);
185         if (allowEmptyComment.equals(MessageDialogWithToggle.NEVER)) {
186             /* but is empty */
187             final String JavaDoc comment = fCommentArea.getComment(false);
188             if (comment.equals("")) { //$NON-NLS-1$
189
return false; // then the page is not complete
190
}
191         }
192         return super.isPageComplete();
193     }
194     
195     /*
196      * (non-Javadoc)
197      *
198      * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
199      */

200     public void setVisible(boolean visible) {
201         super.setVisible(visible);
202         expand();
203         fCommentArea.setFocus();
204     }
205     
206     protected void expand() {
207         if (fConfiguration != null) {
208             final Viewer viewer= fConfiguration.getPage().getViewer();
209             if (viewer instanceof TreeViewer) {
210                 try {
211                     viewer.getControl().setRedraw(false);
212                     ((TreeViewer)viewer).expandAll();
213                 } finally {
214                     viewer.getControl().setRedraw(true);
215                 }
216             }
217         }
218     }
219     
220     /*
221      * Expand the sync elements and update the page enablement
222      */

223     protected void updateForModelChange() {
224         Control control = getControl();
225         if (control == null || control.isDisposed()) return;
226         expand();
227         updateEnablements();
228     }
229     
230     public void updateEnablements() {
231         if (fConfiguration != null) {
232             SyncInfoSet set = fConfiguration.getSyncInfoSet();
233             if (set.hasConflicts()) {
234                 setErrorMessage(CVSUIMessages.CommitWizardCommitPage_4);
235                 setPageComplete(false);
236                 return;
237             }
238             if (set.isEmpty()) {
239                 // No need for a message as it should be obvious that there are no resources to commit
240
setErrorMessage(null);
241                 setPageComplete(false);
242                 return;
243             }
244         }
245         setErrorMessage(null);
246         setPageComplete(true);
247     }
248
249     boolean validatePage(boolean setMessage) {
250         if (fCommentArea != null && fCommentArea.getComment(false).length() == 0) {
251             final IPreferenceStore store= CVSUIPlugin.getPlugin().getPreferenceStore();
252             final String JavaDoc value= store.getString(ICVSUIConstants.PREF_ALLOW_EMPTY_COMMIT_COMMENTS);
253             if (MessageDialogWithToggle.NEVER.equals(value)) {
254                 setPageComplete(false);
255                 if (setMessage)
256                     setErrorMessage(CVSUIMessages.CommitWizardCommitPage_3);
257                 return false;
258             }
259         }
260         setPageComplete(true);
261         setErrorMessage(null);
262         return true;
263     }
264     
265     public void setFocus() {
266         fCommentArea.setFocus();
267         validatePage(true);
268     }
269     
270     protected IWizardContainer getContainer() {
271         return super.getContainer();
272     }
273     
274     public SyncInfoSet getInfosToCommit() {
275
276         final SyncInfoSet infos= new SyncInfoSet();
277         if (fConfiguration == null) {
278             return fWizard.getParticipant().getSyncInfoSet();
279         }
280         
281         final IDiffElement root = (ISynchronizeModelElement)fConfiguration.getProperty(SynchronizePageConfiguration.P_MODEL);
282         final IDiffElement [] elements= Utils.getDiffNodes(new IDiffElement [] { root });
283         
284         for (int i = 0; i < elements.length; i++) {
285             if (elements[i] instanceof SyncInfoModelElement) {
286                 SyncInfo syncInfo = ((SyncInfoModelElement)elements[i]).getSyncInfo();
287                 int direction = syncInfo.getKind() & SyncInfo.DIRECTION_MASK;
288                 if (direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING)
289                     infos.add(syncInfo);
290             }
291         }
292         return infos;
293     }
294     
295     /* (non-Javadoc)
296      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
297      */

298     public void propertyChange(PropertyChangeEvent event) {
299         
300         if (event.getProperty().equals(CommitCommentArea.OK_REQUESTED)) {
301             final IWizardContainer container= getContainer();
302             if (container instanceof WizardDialog) {
303                 final WizardDialog dialog= (WizardDialog)container;
304                 if (getWizard().canFinish()) {
305                     try {
306                         getWizard().performFinish();
307                     } finally {
308                         dialog.close();
309                     }
310                 }
311             }
312         }
313         if (event.getProperty().equals(CommitCommentArea.COMMENT_MODIFIED)) {
314             validatePage(true);
315         }
316     }
317     
318     /*
319      * Get a proposed comment by looking at the active change sets
320      */

321     private String JavaDoc getProposedComment(IResource[] resourcesToCommit) {
322         StringBuffer JavaDoc comment = new StringBuffer JavaDoc();
323         ChangeSet[] sets = CVSUIPlugin.getPlugin().getChangeSetManager().getSets();
324         int numMatchedSets = 0;
325         for (int i = 0; i < sets.length; i++) {
326             ChangeSet set = sets[i];
327             if (isUserSet(set) && containsOne(set, resourcesToCommit)) {
328                 if(numMatchedSets > 0) comment.append(System.getProperty("line.separator")); //$NON-NLS-1$
329
comment.append(set.getComment());
330                 numMatchedSets++;
331             }
332         }
333         return comment.toString();
334     }
335     
336     private boolean isUserSet(ChangeSet set) {
337         if (set instanceof ActiveChangeSet) {
338             ActiveChangeSet acs = (ActiveChangeSet) set;
339             return acs.isUserCreated();
340         }
341         return false;
342     }
343
344     private boolean containsOne(ChangeSet set, IResource[] resourcesToCommit) {
345         for (int j = 0; j < resourcesToCommit.length; j++) {
346             IResource resource = resourcesToCommit[j];
347             if (set.contains(resource)) {
348                 return true;
349             }
350             if (set instanceof ActiveChangeSet) {
351                 ActiveChangeSet acs = (ActiveChangeSet) set;
352                 if (acs.getDiffTree().members(resource).length > 0)
353                     return true;
354             }
355         }
356         return false;
357    }
358     
359 }
360
361
Popular Tags