]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/spelling/AddWordProposal.java
857ce7428fa00d2fb509b7a61ff0b0a8a3098f94
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / spelling / AddWordProposal.java
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
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 import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
24
25 import org.eclipse.ui.dialogs.PreferencesUtil;
26
27 import org.eclipse.ui.texteditor.spelling.SpellingProblem;
28
29 import org.eclipse.jdt.internal.corext.util.Messages;
30
31 import org.eclipse.jdt.ui.PreferenceConstants;
32 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
33
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;
39
40 /**
41  * Proposal to add the unknown word to the dictionaries.
42  *
43  * @since 3.0
44  */
45 public class AddWordProposal implements IJavaCompletionProposal {
46
47         private static final String PREF_KEY_DO_NOT_ASK= "do_not_ask_to_install_user_dictionary"; //$NON-NLS-1$
48
49         /** The invocation context */
50         private final IQuickAssistInvocationContext fContext;
51
52         /** The word to add */
53         private final String fWord;
54
55
56         /**
57          * Creates a new add word proposal
58          *
59          * @param word
60          *                   The word to add
61          * @param context
62          *                   The invocation context
63          */
64         public AddWordProposal(final String word, final IQuickAssistInvocationContext context) {
65                 fContext= context;
66                 fWord= word;
67         }
68
69         /*
70          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
71          */
72         public final void apply(final IDocument document) {
73
74                 final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
75                 final ISpellChecker checker= engine.getSpellChecker();
76
77                 if (checker == null)
78                         return;
79
80                 if (!checker.acceptsWords()) {
81                         final Shell shell;
82                         if (fContext != null && fContext.getSourceViewer() != null)
83                                 shell= fContext.getSourceViewer().getTextWidget().getShell();
84                         else
85                                 shell= JavaPlugin.getActiveWorkbenchShell();
86
87                         if (!canAskToConfigure() || !askUserToConfigureUserDictionary(shell))
88                                 return;
89
90                         String[] preferencePageIds= new String[] { "org.eclipse.ui.editors.preferencePages.Spelling" }; //$NON-NLS-1$
91                         PreferencesUtil.createPreferenceDialogOn(shell, preferencePageIds[0], preferencePageIds, null).open();
92                 }
93
94                 if (checker.acceptsWords()) {
95                         checker.addWord(fWord);
96                         if (fContext != null && fContext.getSourceViewer() != null)
97                                 SpellingProblem.removeAll(fContext.getSourceViewer(), fWord);
98                 }
99         }
100
101         /**
102          * Asks the user whether he wants to configure a user dictionary.
103          * 
104          * @param shell the shell
105          * @return <code>true</code> if the user wants to configure the user dictionary
106          * @since 3.3
107          */
108         private boolean askUserToConfigureUserDictionary(Shell shell) {
109                 MessageDialogWithToggle toggleDialog= MessageDialogWithToggle.openYesNoQuestion(
110                                 shell,
111                                 JavaUIMessages.Spelling_add_askToConfigure_title,
112                                 JavaUIMessages.Spelling_add_askToConfigure_question,
113                                 JavaUIMessages.Spelling_add_askToConfigure_ignoreMessage,
114                                 false,
115                                 null,
116                                 null);
117
118                 PreferenceConstants.getPreferenceStore().setValue(PREF_KEY_DO_NOT_ASK, toggleDialog.getToggleState());
119
120                 return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;
121         }
122
123         /**
124          * Tells whether this proposal can ask to
125          * configure a user dictionary.
126          *
127          * @return <code>true</code> if it can ask the user
128          */
129         static boolean canAskToConfigure() {
130                 return !PreferenceConstants.getPreferenceStore().getBoolean(PREF_KEY_DO_NOT_ASK);
131         }
132
133         /*
134          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
135          */
136         public String getAdditionalProposalInfo() {
137                 return Messages.format(JavaUIMessages.Spelling_add_info, new String[] { WordCorrectionProposal.getHtmlRepresentation(fWord)});
138         }
139
140         /*
141          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
142          */
143         public final IContextInformation getContextInformation() {
144                 return null;
145         }
146
147         /*
148          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
149          */
150         public String getDisplayString() {
151                 return Messages.format(JavaUIMessages.Spelling_add_label, new String[] { fWord });
152         }
153
154         /*
155          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
156          */
157         public Image getImage() {
158                 return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
159         }
160
161         /*
162          * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
163          */
164         public int getRelevance() {
165                 return Integer.MIN_VALUE;
166         }
167
168         /*
169          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
170          */
171         public final Point getSelection(final IDocument document) {
172                 return new Point(fContext.getOffset(), fContext.getLength());
173         }
174 }