KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids - bug 57208
11  * Maik Schreiber - bug 102461
12  * Eugene Kuleshov (eu@md.pp.ru) - Bug 112742 [Wizards] Add spell check to commit dialog
13  * Brock Janiczak <brockj@tpg.com.au> - Bug 179183 Use spelling support from JFace in CVS commit dialog
14  *******************************************************************************/

15 package org.eclipse.team.internal.ccvs.ui;
16
17 import java.util.*;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.jface.dialogs.*;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.*;
24 import org.eclipse.jface.text.source.*;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.StyledText;
27 import org.eclipse.swt.events.*;
28 import org.eclipse.swt.layout.FillLayout;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.*;
31 import org.eclipse.team.core.RepositoryProvider;
32 import org.eclipse.team.internal.ccvs.core.*;
33 import org.eclipse.team.internal.ccvs.core.util.Util;
34 import org.eclipse.team.internal.ui.SWTUtils;
35 import org.eclipse.team.internal.ui.dialogs.DialogArea;
36 import org.eclipse.ui.dialogs.PreferencesUtil;
37 import org.eclipse.ui.editors.text.EditorsUI;
38 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
39 import org.eclipse.ui.texteditor.*;
40
41 /**
42  * This area provides the widgets for providing the CVS commit comment
43  */

44 public class CommitCommentArea extends DialogArea {
45
46     private class TextBox implements ModifyListener, TraverseListener, FocusListener, Observer {
47         
48         private final StyledText fTextField; // updated only by modify events
49
private final String JavaDoc fMessage;
50         
51         private String JavaDoc fText;
52         
53         public TextBox(Composite composite, String JavaDoc message, String JavaDoc initialText) {
54             
55             fMessage= message;
56             fText= initialText;
57             
58             AnnotationModel annotationModel = new AnnotationModel();
59             IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();
60
61             Composite cc = new Composite(composite, SWT.BORDER);
62             cc.setLayout(new FillLayout());
63             cc.setLayoutData(new GridData(GridData.FILL_BOTH));
64             
65             SourceViewer sourceViewer = new SourceViewer(cc, null, null, true, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
66             sourceViewer.getTextWidget().setIndent(2);
67             
68             final SourceViewerDecorationSupport support = new SourceViewerDecorationSupport(sourceViewer, null, annotationAccess, EditorsUI.getSharedTextColors());
69             Iterator e= new MarkerAnnotationPreferences().getAnnotationPreferences().iterator();
70             while (e.hasNext())
71                 support.setAnnotationPreference((AnnotationPreference) e.next());
72             
73             support.install(EditorsUI.getPreferenceStore());
74             
75             sourceViewer.getTextWidget().addDisposeListener(new DisposeListener() {
76             
77                 public void widgetDisposed(DisposeEvent e) {
78                     support.uninstall();
79                 }
80             
81             });
82             
83             Document document = new Document(initialText);
84
85             // NOTE: Configuration must be applied before the document is set in order for
86
// Hyperlink coloring to work. (Presenter needs document object up front)
87
sourceViewer.configure(new TextSourceViewerConfiguration(EditorsUI.getPreferenceStore()));
88             sourceViewer.setDocument(document, annotationModel);
89             fTextField = sourceViewer.getTextWidget();
90             
91             fTextField.addTraverseListener(this);
92             fTextField.addModifyListener(this);
93             fTextField.addFocusListener(this);
94         }
95
96         public void modifyText(ModifyEvent e) {
97             final String JavaDoc old = fText;
98             fText = fTextField.getText();
99             firePropertyChangeChange(COMMENT_MODIFIED, old, fText);
100         }
101         
102         public void keyTraversed(TraverseEvent e) {
103             if (e.detail == SWT.TRAVERSE_RETURN && (e.stateMask & SWT.CTRL) != 0) {
104                 e.doit = false;
105                 firePropertyChangeChange(OK_REQUESTED, null, null);
106             }
107         }
108         
109         public void focusGained(FocusEvent e) {
110
111             if (fText.length() > 0)
112                 return;
113             
114             fTextField.removeModifyListener(this);
115             try {
116                 fTextField.setText(fText);
117             } finally {
118                 fTextField.addModifyListener(this);
119             }
120         }
121         
122         public void focusLost(FocusEvent e) {
123             
124             if (fText.length() > 0)
125                 return;
126             
127             fTextField.removeModifyListener(this);
128             try {
129                 fTextField.setText(fMessage);
130                 fTextField.selectAll();
131             } finally {
132                 fTextField.addModifyListener(this);
133             }
134         }
135         
136         public void setEnabled(boolean enabled) {
137             fTextField.setEnabled(enabled);
138         }
139         
140         public void update(Observable o, Object JavaDoc arg) {
141             if (arg instanceof String JavaDoc) {
142                 setText((String JavaDoc)arg); // triggers a modify event
143
}
144         }
145         
146         public String JavaDoc getText() {
147             return fText;
148         }
149         
150         private void setText(String JavaDoc text) {
151             if (text.length() == 0) {
152                 fTextField.setText(fMessage);
153                 fTextField.selectAll();
154             } else
155                 fTextField.setText(text);
156         }
157
158         public void setFocus() {
159             fTextField.setFocus();
160         }
161     }
162     
163     private static class ComboBox extends Observable implements SelectionListener, FocusListener {
164         
165         private final String JavaDoc fMessage;
166         private final String JavaDoc [] fComments;
167         private String JavaDoc[] fCommentTemplates;
168         private final Combo fCombo;
169         
170         
171         public ComboBox(Composite composite, String JavaDoc message, String JavaDoc [] options,
172                 String JavaDoc[] commentTemplates) {
173             
174             fMessage= message;
175             fComments= options;
176             fCommentTemplates = commentTemplates;
177             
178             fCombo = new Combo(composite, SWT.READ_ONLY);
179             fCombo.setLayoutData(SWTUtils.createHFillGridData());
180             fCombo.setVisibleItemCount(20);
181             
182             // populate the previous comment list
183
populateList();
184             
185             // We don't want to have an initial selection
186
// (see bug 32078: http://bugs.eclipse.org/bugs/show_bug.cgi?id=32078)
187
fCombo.addFocusListener(this);
188             fCombo.addSelectionListener(this);
189         }
190
191         private void populateList() {
192             fCombo.removeAll();
193             
194             fCombo.add(fMessage);
195             for (int i = 0; i < fCommentTemplates.length; i++) {
196                 fCombo.add(CVSUIMessages.CommitCommentArea_6 + ": " + //$NON-NLS-1$
197
Util.flattenText(fCommentTemplates[i]));
198             }
199             for (int i = 0; i < fComments.length; i++) {
200                 fCombo.add(Util.flattenText(fComments[i]));
201             }
202             fCombo.setText(fMessage);
203         }
204         
205         public void widgetSelected(SelectionEvent e) {
206             int index = fCombo.getSelectionIndex();
207             if (index > 0) {
208                 index--;
209                 setChanged();
210                 
211                 // map from combo box index to array index
212
String JavaDoc message;
213                 if (index < fCommentTemplates.length) {
214                     message = fCommentTemplates[index];
215                 } else {
216                     message = fComments[index - fCommentTemplates.length];
217                 }
218                 notifyObservers(message);
219             }
220         }
221         
222         public void widgetDefaultSelected(SelectionEvent e) {
223         }
224         
225         public void focusGained(FocusEvent e) {
226         }
227         
228         /* (non-Javadoc)
229          * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
230          */

231         public void focusLost(FocusEvent e) {
232             fCombo.removeSelectionListener(this);
233             try {
234                 fCombo.setText(fMessage);
235             } finally {
236                 fCombo.addSelectionListener(this);
237             }
238         }
239         
240         public void setEnabled(boolean enabled) {
241             fCombo.setEnabled(enabled);
242         }
243         
244         void setCommentTemplates(String JavaDoc[] templates) {
245             fCommentTemplates = templates;
246             populateList();
247         }
248     }
249     
250     private static final String JavaDoc EMPTY_MESSAGE= CVSUIMessages.CommitCommentArea_0;
251     private static final String JavaDoc COMBO_MESSAGE= CVSUIMessages.CommitCommentArea_1;
252     private static final String JavaDoc CONFIGURE_TEMPLATES_MESSAGE= CVSUIMessages.CommitCommentArea_5;
253     
254     public static final String JavaDoc OK_REQUESTED = "OkRequested";//$NON-NLS-1$
255
public static final String JavaDoc COMMENT_MODIFIED = "CommentModified";//$NON-NLS-1$
256

257     private TextBox fTextBox;
258     private ComboBox fComboBox;
259     
260     private IProject fMainProject;
261     private String JavaDoc fProposedComment;
262     private Composite fComposite;
263     
264     /**
265      * @see org.eclipse.team.internal.ccvs.ui.DialogArea#createArea(org.eclipse.swt.widgets.Composite)
266      */

267     public void createArea(Composite parent) {
268         Dialog.applyDialogFont(parent);
269         initializeDialogUnits(parent);
270         
271         fComposite = createGrabbingComposite(parent, 1);
272         initializeDialogUnits(fComposite);
273         
274         fTextBox= new TextBox(fComposite, EMPTY_MESSAGE, getInitialComment());
275         
276         final String JavaDoc [] comments = CVSUIPlugin.getPlugin().getRepositoryManager().getPreviousComments();
277         final String JavaDoc[] commentTemplates = CVSUIPlugin.getPlugin().getRepositoryManager().getCommentTemplates();
278         fComboBox= new ComboBox(fComposite, COMBO_MESSAGE, comments, commentTemplates);
279         
280         Link templatesPrefsLink = new Link(fComposite, 0);
281         templatesPrefsLink.setText("<a HREF=\"configureTemplates\">" + //$NON-NLS-1$
282
CONFIGURE_TEMPLATES_MESSAGE + "</a>"); //$NON-NLS-1$
283
templatesPrefsLink.addSelectionListener(new SelectionListener() {
284             public void widgetDefaultSelected(SelectionEvent e) {
285                 openCommentTemplatesPreferencePage();
286             }
287         
288             public void widgetSelected(SelectionEvent e) {
289                 openCommentTemplatesPreferencePage();
290             }
291         });
292         
293         fComboBox.addObserver(fTextBox);
294     }
295     
296     void openCommentTemplatesPreferencePage() {
297         PreferencesUtil.createPreferenceDialogOn(
298                 null,
299                 "org.eclipse.team.cvs.ui.CommentTemplatesPreferences", //$NON-NLS-1$
300
new String JavaDoc[] { "org.eclipse.team.cvs.ui.CommentTemplatesPreferences" }, //$NON-NLS-1$
301
null).open();
302         fComboBox.setCommentTemplates(
303                 CVSUIPlugin.getPlugin().getRepositoryManager().getCommentTemplates());
304     }
305
306     public String JavaDoc getComment(boolean save) {
307         final String JavaDoc comment= fTextBox.getText();
308         if (comment == null)
309             return ""; //$NON-NLS-1$
310

311         final String JavaDoc stripped= strip(comment);
312         if (save && comment.length() > 0)
313             CVSUIPlugin.getPlugin().getRepositoryManager().addComment(comment);
314
315         return stripped;
316     }
317     
318     public String JavaDoc getCommentWithPrompt(Shell shell) {
319         final String JavaDoc comment= getComment(false);
320         if (comment.length() == 0) {
321             final IPreferenceStore store= CVSUIPlugin.getPlugin().getPreferenceStore();
322             final String JavaDoc value= store.getString(ICVSUIConstants.PREF_ALLOW_EMPTY_COMMIT_COMMENTS);
323             
324             if (MessageDialogWithToggle.NEVER.equals(value))
325                 return null;
326             
327             if (MessageDialogWithToggle.PROMPT.equals(value)) {
328                 
329                 final String JavaDoc title= CVSUIMessages.CommitCommentArea_2;
330                 final String JavaDoc message= CVSUIMessages.CommitCommentArea_3;
331                 final String JavaDoc toggleMessage= CVSUIMessages.CommitCommentArea_4;
332                 
333                 final MessageDialogWithToggle dialog= MessageDialogWithToggle.openYesNoQuestion(shell, title, message, toggleMessage, false, store, ICVSUIConstants.PREF_ALLOW_EMPTY_COMMIT_COMMENTS);
334                 if (dialog.getReturnCode() != IDialogConstants.YES_ID) {
335                     fTextBox.setFocus();
336                     return null;
337                 }
338             }
339         }
340         return getComment(true);
341     }
342
343     
344     public void setProject(IProject iProject) {
345         this.fMainProject = iProject;
346     }
347     
348     public void setFocus() {
349         if (fTextBox != null) {
350             fTextBox.setFocus();
351         }
352     }
353     
354     public void setProposedComment(String JavaDoc proposedComment) {
355         if (proposedComment == null || proposedComment.length() == 0) {
356             this.fProposedComment = null;
357         } else {
358             this.fProposedComment = proposedComment;
359         }
360     }
361     
362     public boolean hasCommitTemplate() {
363         try {
364             String JavaDoc commitTemplate = getCommitTemplate();
365             return commitTemplate != null && commitTemplate.length() > 0;
366         } catch (CVSException e) {
367             CVSUIPlugin.log(e);
368             return false;
369         }
370     }
371     
372     public void setEnabled(boolean enabled) {
373         fTextBox.setEnabled(enabled);
374         fComboBox.setEnabled(enabled);
375     }
376     
377     public Composite getComposite() {
378         return fComposite;
379     }
380     
381     protected void firePropertyChangeChange(String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
382         super.firePropertyChangeChange(property, oldValue, newValue);
383     }
384     
385     private String JavaDoc getInitialComment() {
386         if (fProposedComment != null)
387             return fProposedComment;
388         try {
389             return getCommitTemplate();
390         } catch (CVSException e) {
391             CVSUIPlugin.log(e);
392             return ""; //$NON-NLS-1$
393
}
394     }
395
396     private String JavaDoc strip(String JavaDoc comment) {
397         // strip template from the comment entered
398
try {
399             final String JavaDoc commitTemplate = getCommitTemplate();
400             if (comment.startsWith(commitTemplate)) {
401                 return comment.substring(commitTemplate.length());
402             } else if (comment.endsWith(commitTemplate)) {
403                 return comment.substring(0, comment.length() - commitTemplate.length());
404             }
405         } catch (CVSException e) {
406             // we couldn't get the commit template. Log the error and continue
407
CVSUIPlugin.log(e);
408         }
409         return comment;
410     }
411
412     private CVSTeamProvider getProvider() {
413         if (fMainProject == null) return null;
414         return (CVSTeamProvider) RepositoryProvider.getProvider(fMainProject, CVSProviderPlugin.getTypeId());
415     }
416
417     private String JavaDoc getCommitTemplate() throws CVSException {
418         CVSTeamProvider provider = getProvider();
419         if (provider == null)
420             return ""; //$NON-NLS-1$
421
final String JavaDoc template = provider.getCommitTemplate();
422         return template != null ? template : ""; //$NON-NLS-1$
423
}
424 }
425
Popular Tags