Assembla home | Assembla project page
 

Changeset 821:b14dbd44fad1

Show
Ignore:
Timestamp:
08/09/08 16:01:06 (4 months ago)
Author:
Bastian Doetsch <bastian.doetsch@gmx.de>
branch:
default
Message:

"Support spell checking in commit dialog. Eclipse editor preferences are used for configuration.

This was *hard*. Integrating into spell check is hardly documented and the preference constants must be searched in plug-in configurations..."

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/com/vectrace/MercurialEclipse/dialogs/CommitDialog.java

    r767 r821  
    1919import org.eclipse.compare.ResourceNode; 
    2020import org.eclipse.core.resources.IResource; 
     21import org.eclipse.core.runtime.Platform; 
     22import org.eclipse.core.runtime.content.IContentType; 
     23import org.eclipse.core.runtime.content.IContentTypeManager; 
    2124import org.eclipse.jface.dialogs.TrayDialog; 
     25import org.eclipse.jface.text.Document; 
     26import org.eclipse.jface.text.IDocument; 
     27import org.eclipse.jface.text.ITextListener; 
     28import org.eclipse.jface.text.TextEvent; 
     29import org.eclipse.jface.text.source.AnnotationModel; 
     30import org.eclipse.jface.text.source.ISourceViewer; 
     31import org.eclipse.jface.text.source.SourceViewer; 
    2232import org.eclipse.jface.viewers.ArrayContentProvider; 
    2333import org.eclipse.jface.viewers.CheckboxTableViewer; 
     
    4555import org.eclipse.swt.widgets.Table; 
    4656import org.eclipse.swt.widgets.TableColumn; 
    47 import org.eclipse.swt.widgets.Text; 
     57import org.eclipse.ui.PlatformUI; 
     58import org.eclipse.ui.editors.text.EditorsUI; 
     59import org.eclipse.ui.texteditor.AnnotationPreference; 
     60import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess; 
     61import org.eclipse.ui.texteditor.SourceViewerDecorationSupport; 
     62import org.eclipse.ui.texteditor.spelling.SpellingAnnotation; 
     63import org.eclipse.ui.texteditor.spelling.SpellingContext; 
     64import org.eclipse.ui.texteditor.spelling.SpellingService; 
    4865 
    4966import com.vectrace.MercurialEclipse.TableColumnSorter; 
     
    5168import com.vectrace.MercurialEclipse.model.HgRoot; 
    5269import com.vectrace.MercurialEclipse.team.IStorageMercurialRevision; 
     70import com.vectrace.MercurialEclipse.ui.TextSpellingProblemCollector; 
    5371import com.vectrace.MercurialEclipse.utils.CompareUtils; 
    5472 
     
    86104    private String defaultCommitMessage = "(no commit message)"; 
    87105 
    88     private Text commitTextBox; 
     106    private ISourceViewer commitTextBox; 
    89107    private Label commitTextLabel; 
    90108    private Label commitFilesLabel; 
     
    104122    private File[] filesToRemove; 
    105123    private List<IResource> resourcesToRemove; 
     124    private IDocument commitTextDocument; 
     125    private SourceViewerDecorationSupport decorationSupport; 
    106126 
    107127    /** 
     
    110130    public CommitDialog(Shell shell, HgRoot root, IResource[] inResources) { 
    111131        super(shell); 
    112         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.TITLE);         
     132        setShellStyle(getShellStyle() | SWT.RESIZE | SWT.TITLE); 
    113133        this.root = root; 
    114134        this.inResources = inResources; 
     
    116136        this.committableFilesFilter = new CommittableFilesFilter(); 
    117137        this.selectableFiles = true; 
     138        this.commitTextDocument = new Document(); 
    118139    } 
    119140 
     
    152173        commitTextLabel = new Label(container, SWT.NONE); 
    153174        commitTextLabel.setText("Commit comments"); 
     175 
     176        // commitTextBox = new Text(container, SWT.V_SCROLL | SWT.MULTI 
     177        // | SWT.BORDER | SWT.WRAP); 
     178 
     179        commitTextBox = new SourceViewer(container, null, SWT.V_SCROLL 
     180                | SWT.MULTI | SWT.BORDER | SWT.WRAP); 
     181        commitTextBox.setEditable(true); 
     182 
     183        // set up spell-check annotations 
    154184         
    155         commitTextBox = new Text(container, SWT.V_SCROLL | SWT.MULTI 
    156                 | SWT.BORDER | SWT.WRAP); 
     185        AnnotationModel annotationModel = new AnnotationModel(); 
     186        commitTextBox.setDocument(commitTextDocument, annotationModel);         
     187         
     188        decorationSupport = new SourceViewerDecorationSupport(commitTextBox, 
     189                null, new DefaultMarkerAnnotationAccess(), EditorsUI 
     190                        .getSharedTextColors()); 
     191                 
     192        AnnotationPreference pref = new AnnotationPreference(); 
     193        pref.setAnnotationType(SpellingAnnotation.TYPE); 
     194        pref.setColorPreferenceKey("spellingIndicationColor"); 
     195        pref.setHighlightPreferenceKey("spellingIndicationHighlighting");                 
     196        pref.setTextPreferenceKey("spellingIndication"); 
     197         
     198        decorationSupport.setAnnotationPreference(pref); 
     199        decorationSupport.install(EditorsUI.getPreferenceStore());                        
     200 
     201        ITextListener textListener = new ITextListener() { 
     202 
     203            private SpellingService spellService; 
     204            private SpellingContext spellContext; 
     205            private TextSpellingProblemCollector collector; 
     206 
     207            public void textChanged(TextEvent event) { 
     208                // reset foreground color 
     209                commitTextBox.setTextColor(PlatformUI.getWorkbench() 
     210                        .getDisplay().getSystemColor( 
     211                                SWT.COLOR_WIDGET_FOREGROUND), 0, 
     212                        commitTextDocument.getLength(), false); 
     213 
     214                // connect to spell service if necessary 
     215                if (spellService == null) { 
     216                    spellService = EditorsUI.getSpellingService(); 
     217                } 
     218 
     219                if (spellContext == null) { 
     220                    spellContext = new SpellingContext(); 
     221                    IContentType contentType = Platform.getContentTypeManager() 
     222                            .getContentType(IContentTypeManager.CT_TEXT); 
     223                    spellContext.setContentType(contentType); 
     224                } 
     225 
     226                if (collector == null) { 
     227                    collector = new TextSpellingProblemCollector(commitTextBox); 
     228                } 
     229 
     230                // check and highlight errors 
     231                spellService.check(commitTextDocument, spellContext, collector, 
     232                        null); 
     233            } 
     234        }; 
     235        commitTextBox.addTextListener(textListener); 
    157236 
    158237        commitFilesLabel = new Label(container, SWT.NONE); 
     
    161240        commitFilesList = createFilesList(container, selectableFiles); 
    162241 
    163          
    164          
    165242        final FormData fd_commitTextLabel = new FormData(); 
    166243        fd_commitTextLabel.top = new FormAttachment(0, 20); 
     
    170247 
    171248        final FormData fd_commitTextBox = new FormData(); 
    172         fd_commitTextBox.top = new FormAttachment(commitTextLabel, 3, SWT.BOTTOM); 
     249        fd_commitTextBox.top = new FormAttachment(commitTextLabel, 3, 
     250                SWT.BOTTOM); 
    173251        fd_commitTextBox.left = new FormAttachment(0, 9); 
    174252        fd_commitTextBox.bottom = new FormAttachment(0, 200); 
    175253        fd_commitTextBox.right = new FormAttachment(100, -9); 
    176         commitTextBox.setLayoutData(fd_commitTextBox); 
     254        commitTextBox.getTextWidget().setLayoutData(fd_commitTextBox); 
    177255 
    178256        final FormData fd_commitFilesLabel = new FormData(); 
    179         fd_commitFilesLabel.top = new FormAttachment(commitTextBox, 3); 
     257        fd_commitFilesLabel.top = new FormAttachment(commitTextBox 
     258                .getTextWidget(), 3); 
    180259        fd_commitFilesLabel.left = new FormAttachment(0, 9); 
    181260        fd_commitFilesLabel.right = new FormAttachment(100, -9); 
    182261        commitFilesLabel.setLayoutData(fd_commitFilesLabel); 
    183262 
    184          
    185263        Table table = commitFilesList.getTable(); 
    186264        final FormData fd_table = new FormData(); 
     
    195273            selectAllButton = new Button(container, SWT.CHECK); 
    196274            selectAllButton.setText("Select/unselect all"); 
    197          
     275 
    198276            showUntrackedFilesButton = new Button(container, SWT.CHECK); 
    199277            showUntrackedFilesButton.setText("Show added/removed files"); 
     
    202280 
    203281            final FormData fd_selectAllButton = new FormData(); 
    204             fd_selectAllButton.bottom = new FormAttachment(showUntrackedFilesButton); 
     282            fd_selectAllButton.bottom = new FormAttachment( 
     283                    showUntrackedFilesButton); 
    205284            fd_selectAllButton.left = new FormAttachment(0, 9); 
    206285            fd_selectAllButton.right = new FormAttachment(100, -9); 
     
    218297 
    219298    private void makeActions() { 
    220         commitTextBox.setCapture(true); 
     299        // commitTextBox.setCapture(true); 
    221300        commitFilesList.addDoubleClickListener(new IDoubleClickListener() { 
    222301            public void doubleClick(DoubleClickEvent event) { 
     
    236315                                    .getResource())); 
    237316 
    238                     CompareUtils.openCompareDialog(leftNode, rightNode, false);                                         
     317                    CompareUtils.openCompareDialog(leftNode, rightNode, false); 
    239318                } 
    240319            } 
     
    291370 
    292371    private void setupDefaultCommitMessage() { 
    293         commitTextBox.setText(defaultCommitMessage); 
    294         commitTextBox.setSelection(0, defaultCommitMessage.length());         
     372        commitTextDocument.set(defaultCommitMessage); 
     373        commitTextBox.setSelectedRange(0, defaultCommitMessage.length()); 
    295374    } 
    296375 
     
    341420        List<CommitResource> tracked = new ArrayList<CommitResource>(); 
    342421        for (CommitResource commitResource : commitResources) { 
    343             if ( commitResource.getStatus() != CommitDialog.FILE_UNTRACKED) { 
     422            if (commitResource.getStatus() != CommitDialog.FILE_UNTRACKED) { 
    344423                tracked.add(commitResource); 
    345424            } 
     
    398477        return list.toArray(new File[0]); 
    399478    } 
    400      
     479 
    401480    private File[] getToRemoveList(Object[] objs) { 
    402481        ArrayList<File> list = new ArrayList<File>(); 
     
    432511        return list; 
    433512    } 
    434      
     513 
    435514    private List<IResource> getToRemoveResourceList(Object[] objs) { 
    436515        ArrayList<IResource> list = new ArrayList<IResource>(); 
     
    459538        resourcesToAdd = getToAddResourceList(commitFilesList 
    460539                .getCheckedElements()); 
    461          
     540 
    462541        filesToCommit = convertToFiles(commitFilesList.getCheckedElements()); 
    463542        resourcesToCommit = convertToResource(commitFilesList 
     
    465544 
    466545        filesToRemove = getToRemoveList(commitFilesList.getCheckedElements()); 
    467         resourcesToRemove = getToRemoveResourceList(commitFilesList.getCheckedElements()); 
    468         commitMessage = commitTextBox.getText(); 
     546        resourcesToRemove = getToRemoveResourceList(commitFilesList 
     547                .getCheckedElements()); 
     548        commitMessage = commitTextDocument.get(); 
    469549 
    470550        super.okPressed();