1 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 44 public class CommitCommentArea extends DialogArea { 45 46 private class TextBox implements ModifyListener, TraverseListener, FocusListener, Observer { 47 48 private final StyledText fTextField; private final String fMessage; 50 51 private String fText; 52 53 public TextBox(Composite composite, String message, String 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 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 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 arg) { 141 if (arg instanceof String ) { 142 setText((String )arg); } 144 } 145 146 public String getText() { 147 return fText; 148 } 149 150 private void setText(String 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 fMessage; 166 private final String [] fComments; 167 private String [] fCommentTemplates; 168 private final Combo fCombo; 169 170 171 public ComboBox(Composite composite, String message, String [] options, 172 String [] 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 populateList(); 184 185 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 + ": " + 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 String 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 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 [] templates) { 245 fCommentTemplates = templates; 246 populateList(); 247 } 248 } 249 250 private static final String EMPTY_MESSAGE= CVSUIMessages.CommitCommentArea_0; 251 private static final String COMBO_MESSAGE= CVSUIMessages.CommitCommentArea_1; 252 private static final String CONFIGURE_TEMPLATES_MESSAGE= CVSUIMessages.CommitCommentArea_5; 253 254 public static final String OK_REQUESTED = "OkRequested"; public static final String COMMENT_MODIFIED = "CommentModified"; 257 private TextBox fTextBox; 258 private ComboBox fComboBox; 259 260 private IProject fMainProject; 261 private String fProposedComment; 262 private Composite fComposite; 263 264 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 [] comments = CVSUIPlugin.getPlugin().getRepositoryManager().getPreviousComments(); 277 final String [] 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\">" + CONFIGURE_TEMPLATES_MESSAGE + "</a>"); 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", new String [] { "org.eclipse.team.cvs.ui.CommentTemplatesPreferences" }, null).open(); 302 fComboBox.setCommentTemplates( 303 CVSUIPlugin.getPlugin().getRepositoryManager().getCommentTemplates()); 304 } 305 306 public String getComment(boolean save) { 307 final String comment= fTextBox.getText(); 308 if (comment == null) 309 return ""; 311 final String stripped= strip(comment); 312 if (save && comment.length() > 0) 313 CVSUIPlugin.getPlugin().getRepositoryManager().addComment(comment); 314 315 return stripped; 316 } 317 318 public String getCommentWithPrompt(Shell shell) { 319 final String comment= getComment(false); 320 if (comment.length() == 0) { 321 final IPreferenceStore store= CVSUIPlugin.getPlugin().getPreferenceStore(); 322 final String 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 title= CVSUIMessages.CommitCommentArea_2; 330 final String message= CVSUIMessages.CommitCommentArea_3; 331 final String 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 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 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 property, Object oldValue, Object newValue) { 382 super.firePropertyChangeChange(property, oldValue, newValue); 383 } 384 385 private String getInitialComment() { 386 if (fProposedComment != null) 387 return fProposedComment; 388 try { 389 return getCommitTemplate(); 390 } catch (CVSException e) { 391 CVSUIPlugin.log(e); 392 return ""; } 394 } 395 396 private String strip(String comment) { 397 try { 399 final String 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 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 getCommitTemplate() throws CVSException { 418 CVSTeamProvider provider = getProvider(); 419 if (provider == null) 420 return ""; final String template = provider.getCommitTemplate(); 422 return template != null ? template : ""; } 424 } 425 | Popular Tags |