~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/spelling/WordCorrectionProposal.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 *     Sergey Prigogin (Google)
11
 
 *******************************************************************************/
12
 
 
13
 
package org.eclipse.cdt.internal.ui.text.spelling;
14
 
 
15
 
import org.eclipse.jface.text.BadLocationException;
16
 
import org.eclipse.jface.text.IDocument;
17
 
import org.eclipse.jface.text.contentassist.IContextInformation;
18
 
import org.eclipse.swt.graphics.Image;
19
 
import org.eclipse.swt.graphics.Point;
20
 
 
21
 
import org.eclipse.cdt.ui.text.ICCompletionProposal;
22
 
import org.eclipse.cdt.ui.text.IInvocationContext;
23
 
 
24
 
import org.eclipse.cdt.internal.ui.CPluginImages;
25
 
import org.eclipse.cdt.internal.ui.text.IHtmlTagConstants;
26
 
 
27
 
/**
28
 
 * Proposal to correct the incorrectly spelled word.
29
 
 */
30
 
public class WordCorrectionProposal implements ICCompletionProposal {
31
 
        /** The invocation context */
32
 
        private final IInvocationContext fContext;
33
 
 
34
 
        /** The length in the document */
35
 
        private final int fLength;
36
 
 
37
 
        /** The line where to apply the correction */
38
 
        private final String fLine;
39
 
 
40
 
        /** The offset in the document */
41
 
        private final int fOffset;
42
 
 
43
 
        /** The relevance of this proposal */
44
 
        private final int fRelevance;
45
 
 
46
 
        /** The word to complete */
47
 
        private final String fWord;
48
 
 
49
 
        /**
50
 
         * Creates a new word correction proposal.
51
 
         *
52
 
         * @param word the corrected word
53
 
         * @param arguments the problem arguments associated with the spelling problem
54
 
         * @param offset the offset in the document where to apply the proposal
55
 
         * @param length the length in the document to apply the proposal
56
 
         * @param context the invocation context for this proposal
57
 
         * @param relevance the relevance of this proposal
58
 
         */
59
 
        public WordCorrectionProposal(final String word, final String[] arguments, final int offset,
60
 
                        final int length, final IInvocationContext context, final int relevance) {
61
 
                fWord= Character.isUpperCase(arguments[0].charAt(0)) ?
62
 
                                Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;
63
 
 
64
 
                fOffset= offset;
65
 
                fLength= length;
66
 
                fContext= context;
67
 
                fRelevance= relevance;
68
 
 
69
 
                final StringBuffer buffer= new StringBuffer(80);
70
 
 
71
 
                buffer.append("...<br>"); //$NON-NLS-1$
72
 
                buffer.append(getHtmlRepresentation(arguments[1]));
73
 
                buffer.append("<b>"); //$NON-NLS-1$
74
 
                buffer.append(getHtmlRepresentation(fWord));
75
 
                buffer.append("</b>"); //$NON-NLS-1$
76
 
                buffer.append(getHtmlRepresentation(arguments[2]));
77
 
                buffer.append("<br>..."); //$NON-NLS-1$
78
 
 
79
 
                fLine= buffer.toString();
80
 
        }
81
 
 
82
 
        /*
83
 
         * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
84
 
         */
85
 
        public final void apply(final IDocument document) {
86
 
                try {
87
 
                        document.replace(fOffset, fLength, fWord);
88
 
                } catch (BadLocationException exception) {
89
 
                        // Do nothing
90
 
                }
91
 
        }
92
 
 
93
 
        /*
94
 
         * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
95
 
         */
96
 
        public String getAdditionalProposalInfo() {
97
 
                return fLine;
98
 
        }
99
 
 
100
 
        /*
101
 
         * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
102
 
         */
103
 
        public final IContextInformation getContextInformation() {
104
 
                return null;
105
 
        }
106
 
 
107
 
        /*
108
 
         * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
109
 
         */
110
 
        public String getDisplayString() {
111
 
                return Messages.bind(Messages.Spelling_correct_label, fWord);
112
 
        }
113
 
 
114
 
        /*
115
 
         * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
116
 
         */
117
 
        public Image getImage() {
118
 
                return CPluginImages.get(CPluginImages.IMG_CORRECTION_RENAME);
119
 
        }
120
 
 
121
 
        /*
122
 
         * @see org.eclipse.cdt.ui.text.java.IJavaCompletionProposal#getRelevance()
123
 
         */
124
 
        public final int getRelevance() {
125
 
                return fRelevance;
126
 
        }
127
 
 
128
 
        /*
129
 
         * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
130
 
         */
131
 
        public final Point getSelection(final IDocument document) {
132
 
                int offset= fContext.getSelectionOffset();
133
 
                int length= fContext.getSelectionLength();
134
 
 
135
 
                final int delta= fWord.length() - fLength;
136
 
                if (offset <= fOffset && offset + length >= fOffset) {
137
 
                        length += delta;
138
 
                } else if (offset > fOffset && offset + length > fOffset + fLength) {
139
 
                        offset += delta;
140
 
                        length -= delta;
141
 
                } else {
142
 
                        length += delta;
143
 
                }
144
 
 
145
 
                return new Point(offset, length);
146
 
        }
147
 
        
148
 
        /**
149
 
         * Returns the html representation of the specified string.
150
 
         *
151
 
         * @param string The string to return the html representation for
152
 
         * @return The html representation for the string
153
 
         */
154
 
        public static String getHtmlRepresentation(final String string) {
155
 
                final int length= string.length();
156
 
                final StringBuffer buffer= new StringBuffer(string);
157
 
 
158
 
                for (int offset= length - 1; offset >= 0; offset--) {
159
 
                        for (int index= 0; index < IHtmlTagConstants.HTML_ENTITY_CHARACTERS.length; index++) {
160
 
                                if (string.charAt(offset) == IHtmlTagConstants.HTML_ENTITY_CHARACTERS[index]) {
161
 
                                        buffer.replace(offset, offset + 1, String.valueOf(IHtmlTagConstants.HTML_ENTITY_CODES[index]));
162
 
                                        break;
163
 
                                }
164
 
                        }
165
 
                }
166
 
                return buffer.toString();
167
 
        }
168
 
 
169
 
        public String getIdString() {
170
 
                return fWord;
171
 
        }
172
 
}