KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > spelling > AddWordProposal


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.jdt.internal.ui.text.spelling;
13
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.widgets.Shell;
17
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
20
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.dialogs.PreferencesUtil;
27 import org.eclipse.ui.texteditor.ITextEditor;
28 import org.eclipse.ui.texteditor.spelling.SpellingProblem;
29
30 import org.eclipse.jdt.internal.corext.util.Messages;
31
32 import org.eclipse.jdt.ui.PreferenceConstants;
33 import org.eclipse.jdt.ui.text.java.IInvocationContext;
34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
35
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.JavaPluginImages;
38 import org.eclipse.jdt.internal.ui.JavaUIMessages;
39 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
40 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
41
42 /**
43  * Proposal to add the unknown word to the dictionaries.
44  *
45  * @since 3.0
46  */

47 public class AddWordProposal implements IJavaCompletionProposal {
48
49     private static final String JavaDoc PREF_KEY_DO_NOT_ASK= "do_not_ask_to_install_user_dictionary"; //$NON-NLS-1$
50

51     /** The invocation context */
52     private final IInvocationContext fContext;
53
54     /** The word to add */
55     private final String JavaDoc fWord;
56
57
58     /**
59      * Creates a new add word proposal
60      *
61      * @param word
62      * The word to add
63      * @param context
64      * The invocation context
65      */

66     public AddWordProposal(final String JavaDoc word, final IInvocationContext context) {
67         fContext= context;
68         fWord= word;
69     }
70
71     /*
72      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
73      */

74     public final void apply(final IDocument document) {
75
76         final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
77         final ISpellChecker checker= engine.getSpellChecker();
78
79         if (checker == null)
80             return;
81         
82         final ITextEditor editor= getEditor();
83         
84         if (!checker.acceptsWords()) {
85             final Shell shell;
86             if (editor != null)
87                 shell= editor.getEditorSite().getShell();
88             else
89                 shell= JavaPlugin.getActiveWorkbenchShell();
90             
91             if (!canAskToConfigure() || !askUserToConfigureUserDictionary(shell))
92                 return;
93             
94             String JavaDoc[] preferencePageIds= new String JavaDoc[] { "org.eclipse.ui.editors.preferencePages.Spelling" }; //$NON-NLS-1$
95
PreferencesUtil.createPreferenceDialogOn(shell, preferencePageIds[0], preferencePageIds, null).open();
96         }
97         
98         if (checker.acceptsWords()) {
99             checker.addWord(fWord);
100             SpellingProblem.removeAllInActiveEditor(editor, fWord);
101         }
102     }
103     
104     /**
105      * Asks the user whether he wants to configure
106      * a user dictionary.
107      *
108      * @param shell
109      * @return <code>true</code> if the user wants to configure the user dictionary
110      * @since 3.3
111      */

112     private boolean askUserToConfigureUserDictionary(Shell shell) {
113         MessageDialogWithToggle toggleDialog= MessageDialogWithToggle.openYesNoQuestion(
114                 shell,
115                 JavaUIMessages.Spelling_add_askToConfigure_title,
116                 JavaUIMessages.Spelling_add_askToConfigure_question,
117                 JavaUIMessages.Spelling_add_askToConfigure_ignoreMessage,
118                 false,
119                 null,
120                 null);
121         
122         PreferenceConstants.getPreferenceStore().setValue(PREF_KEY_DO_NOT_ASK, toggleDialog.getToggleState());
123         
124         return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;
125     }
126
127     /**
128      * Tells whether this proposal can ask to
129      * configure a user dictionary.
130      *
131      * @return <code>true</code> if it can ask the user
132      */

133     static boolean canAskToConfigure() {
134         return !PreferenceConstants.getPreferenceStore().getBoolean(PREF_KEY_DO_NOT_ASK);
135     }
136
137     private ITextEditor getEditor() {
138         IWorkbenchPage activePage= JavaPlugin.getActivePage();
139         if (activePage == null)
140             return null;
141     
142         IEditorPart editor= activePage.getActiveEditor();
143         if (activePage.getActivePart() != editor || !(editor instanceof ITextEditor))
144             return null;
145         
146         return (ITextEditor)editor;
147         
148     }
149
150     /*
151      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
152      */

153     public String JavaDoc getAdditionalProposalInfo() {
154         return Messages.format(JavaUIMessages.Spelling_add_info, new String JavaDoc[] { WordCorrectionProposal.getHtmlRepresentation(fWord)});
155     }
156
157     /*
158      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
159      */

160     public final IContextInformation getContextInformation() {
161         return null;
162     }
163
164     /*
165      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
166      */

167     public String JavaDoc getDisplayString() {
168         return Messages.format(JavaUIMessages.Spelling_add_label, new String JavaDoc[] { fWord });
169     }
170
171     /*
172      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
173      */

174     public Image getImage() {
175         return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
176     }
177
178     /*
179      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
180      */

181     public int getRelevance() {
182         return Integer.MIN_VALUE;
183     }
184
185     /*
186      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
187      */

188     public final Point getSelection(final IDocument document) {
189         return new Point(fContext.getSelectionOffset(), fContext.getSelectionLength());
190     }
191 }
192
Popular Tags