1 /*******************************************************************************
2 * Copyright (c) 2000, 2009 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.text.spelling;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
25 import org.eclipse.ui.dialogs.PreferencesUtil;
27 import org.eclipse.ui.texteditor.spelling.SpellingProblem;
29 import org.eclipse.jdt.internal.corext.util.Messages;
31 import org.eclipse.jdt.ui.PreferenceConstants;
32 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.JavaPluginImages;
36 import org.eclipse.jdt.internal.ui.JavaUIMessages;
37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
38 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
41 * Proposal to add the unknown word to the dictionaries.
45 public class AddWordProposal implements IJavaCompletionProposal {
47 private static final String PREF_KEY_DO_NOT_ASK= "do_not_ask_to_install_user_dictionary"; //$NON-NLS-1$
49 /** The invocation context */
50 private final IQuickAssistInvocationContext fContext;
52 /** The word to add */
53 private final String fWord;
57 * Creates a new add word proposal
62 * The invocation context
64 public AddWordProposal(final String word, final IQuickAssistInvocationContext context) {
70 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
72 public final void apply(final IDocument document) {
74 final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
75 final ISpellChecker checker= engine.getSpellChecker();
80 if (!checker.acceptsWords()) {
82 if (fContext != null && fContext.getSourceViewer() != null)
83 shell= fContext.getSourceViewer().getTextWidget().getShell();
85 shell= JavaPlugin.getActiveWorkbenchShell();
87 if (!canAskToConfigure() || !askUserToConfigureUserDictionary(shell))
90 String[] preferencePageIds= new String[] { "org.eclipse.ui.editors.preferencePages.Spelling" }; //$NON-NLS-1$
91 PreferencesUtil.createPreferenceDialogOn(shell, preferencePageIds[0], preferencePageIds, null).open();
94 if (checker.acceptsWords()) {
95 checker.addWord(fWord);
96 if (fContext != null && fContext.getSourceViewer() != null)
97 SpellingProblem.removeAll(fContext.getSourceViewer(), fWord);
102 * Asks the user whether he wants to configure a user dictionary.
104 * @param shell the shell
105 * @return <code>true</code> if the user wants to configure the user dictionary
108 private boolean askUserToConfigureUserDictionary(Shell shell) {
109 MessageDialogWithToggle toggleDialog= MessageDialogWithToggle.openYesNoQuestion(
111 JavaUIMessages.Spelling_add_askToConfigure_title,
112 JavaUIMessages.Spelling_add_askToConfigure_question,
113 JavaUIMessages.Spelling_add_askToConfigure_ignoreMessage,
118 PreferenceConstants.getPreferenceStore().setValue(PREF_KEY_DO_NOT_ASK, toggleDialog.getToggleState());
120 return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;
124 * Tells whether this proposal can ask to
125 * configure a user dictionary.
127 * @return <code>true</code> if it can ask the user
129 static boolean canAskToConfigure() {
130 return !PreferenceConstants.getPreferenceStore().getBoolean(PREF_KEY_DO_NOT_ASK);
134 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
136 public String getAdditionalProposalInfo() {
137 return Messages.format(JavaUIMessages.Spelling_add_info, new String[] { WordCorrectionProposal.getHtmlRepresentation(fWord)});
141 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
143 public final IContextInformation getContextInformation() {
148 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
150 public String getDisplayString() {
151 return Messages.format(JavaUIMessages.Spelling_add_label, new String[] { fWord });
155 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
157 public Image getImage() {
158 return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
162 * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
164 public int getRelevance() {
165 return Integer.MIN_VALUE;
169 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
171 public final Point getSelection(final IDocument document) {
172 return new Point(fContext.getOffset(), fContext.getLength());