1 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 47 public class AddWordProposal implements IJavaCompletionProposal { 48 49 private static final String PREF_KEY_DO_NOT_ASK= "do_not_ask_to_install_user_dictionary"; 51 52 private final IInvocationContext fContext; 53 54 55 private final String fWord; 56 57 58 66 public AddWordProposal(final String word, final IInvocationContext context) { 67 fContext= context; 68 fWord= word; 69 } 70 71 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 [] preferencePageIds= new String [] { "org.eclipse.ui.editors.preferencePages.Spelling" }; 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 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 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 153 public String getAdditionalProposalInfo() { 154 return Messages.format(JavaUIMessages.Spelling_add_info, new String [] { WordCorrectionProposal.getHtmlRepresentation(fWord)}); 155 } 156 157 160 public final IContextInformation getContextInformation() { 161 return null; 162 } 163 164 167 public String getDisplayString() { 168 return Messages.format(JavaUIMessages.Spelling_add_label, new String [] { fWord }); 169 } 170 171 174 public Image getImage() { 175 return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD); 176 } 177 178 181 public int getRelevance() { 182 return Integer.MIN_VALUE; 183 } 184 185 188 public final Point getSelection(final IDocument document) { 189 return new Point(fContext.getSelectionOffset(), fContext.getSelectionLength()); 190 } 191 } 192 | Popular Tags |