OSDN Git Service

Cleanup compiler warning messages and alter backup & restore to handle new database...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / SpellCheck.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.dialog;\r
21 \r
22 import java.util.List;\r
23 \r
24 import com.swabunga.spell.engine.Word;\r
25 import com.swabunga.spell.event.SpellChecker;\r
26 import com.trolltech.qt.core.Qt.AlignmentFlag;\r
27 import com.trolltech.qt.gui.QDialog;\r
28 import com.trolltech.qt.gui.QGridLayout;\r
29 import com.trolltech.qt.gui.QIcon;\r
30 import com.trolltech.qt.gui.QLabel;\r
31 import com.trolltech.qt.gui.QLineEdit;\r
32 import com.trolltech.qt.gui.QListWidget;\r
33 import com.trolltech.qt.gui.QPushButton;\r
34 \r
35 public class SpellCheck extends QDialog {\r
36 \r
37         private boolean         replacePressed;\r
38         private boolean         cancelPressed;\r
39         private final QLabel    currentWord;\r
40         private final QLineEdit replacementWord;\r
41         private String misspelledWord;\r
42         private final QPushButton replace;\r
43         private final QPushButton ignore;\r
44         private final QPushButton ignoreAll;\r
45         private final QPushButton addToDictionary;\r
46         private final QListWidget suggestions;\r
47         private final SpellChecker checker;\r
48         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
49         \r
50         \r
51         // Constructor\r
52         public SpellCheck(SpellChecker checker) {\r
53                 setWindowIcon(new QIcon(iconPath+"spellCheck.png"));\r
54                 replacePressed = false;\r
55                 cancelPressed = false;\r
56                 this.checker = checker;\r
57                 setWindowTitle(tr("Spell Check"));\r
58                 QGridLayout grid = new QGridLayout();\r
59                 setLayout(grid);\r
60                 QGridLayout suggestionGrid = new QGridLayout();\r
61                 QGridLayout buttonGrid = new QGridLayout();\r
62                 \r
63                 currentWord = new QLabel(misspelledWord);\r
64                 replacementWord = new QLineEdit();\r
65                 suggestions = new QListWidget();\r
66                 \r
67                 replacementWord.textChanged.connect(this, "validateInput()");\r
68                 suggestions.itemSelectionChanged.connect(this, "replacementChosen()");\r
69                 \r
70                 suggestionGrid.addWidget(currentWord, 1, 1);\r
71                 suggestionGrid.addWidget(new QLabel(tr("Suggestion")), 2,1);\r
72                 suggestionGrid.addWidget(replacementWord, 3, 1);\r
73                 suggestionGrid.addWidget(suggestions,4,1);\r
74                 suggestionGrid.setContentsMargins(10, 10,  -10, -10);\r
75                 grid.addLayout(suggestionGrid,1,1);\r
76                 \r
77                 replace = new QPushButton(tr("Replace"));\r
78                 ignore = new QPushButton(tr("Ignore"));\r
79                 ignoreAll = new QPushButton(tr("Ignore All"));\r
80                 addToDictionary = new QPushButton(tr("Add To Dictionary"));\r
81                 replace.clicked.connect(this, "replaceButtonPressed()");\r
82                 ignore.clicked.connect(this, "ignoreButtonPressed()");\r
83                 ignoreAll.clicked.connect(this, "ignoreAllButtonPressed()");\r
84                 addToDictionary.clicked.connect(this, "addToDictionaryButtonPressed()");\r
85                 QPushButton cancel = new QPushButton(tr("Cancel"));\r
86                 cancel.clicked.connect(this, "cancelButtonPressed()");\r
87                 suggestionGrid.addWidget(replace, 1, 2);\r
88                 suggestionGrid.addWidget(ignore, 2, 2);\r
89                 suggestionGrid.addWidget(ignoreAll,3,2);\r
90                 suggestionGrid.addWidget(addToDictionary,4,2);\r
91                 suggestionGrid.setAlignment(addToDictionary, AlignmentFlag.AlignTop);\r
92                 buttonGrid.addWidget(new QLabel(), 1,1);\r
93                 buttonGrid.addWidget(cancel, 1,2);\r
94                 buttonGrid.addWidget(new QLabel(), 1,3);\r
95                 buttonGrid.setColumnStretch(1, 10);\r
96                 buttonGrid.setColumnStretch(3, 10);\r
97                 grid.addLayout(buttonGrid,2,1);\r
98         }\r
99         \r
100         // The OK button was pressed\r
101         @SuppressWarnings("unused")\r
102         private void replaceButtonPressed() {\r
103                 replacePressed = true;\r
104                 cancelPressed = false;\r
105                 close();\r
106         }\r
107         \r
108         // The CANCEL button was pressed\r
109         @SuppressWarnings("unused")\r
110         private void cancelButtonPressed() {\r
111                 replacePressed = false;\r
112                 cancelPressed = true;\r
113                 close();\r
114         }\r
115         \r
116         // The ignore button was pressed\r
117         @SuppressWarnings("unused")\r
118         private void ignoreButtonPressed() {\r
119                 replacePressed = false;\r
120                 cancelPressed = false;\r
121                 close();\r
122         }\r
123         \r
124         // The ignore button was pressed\r
125         @SuppressWarnings("unused")\r
126         private void ignoreAllButtonPressed() {\r
127                 checker.ignoreAll(misspelledWord);\r
128                 close();\r
129         }\r
130         \r
131         // Get the userid from the field\r
132         public String getReplacementWord() {\r
133                 return replacementWord.text();\r
134         }\r
135         \r
136         // Set the current misspelled word\r
137         public void setWord(String w) {\r
138                 misspelledWord = w;\r
139                 currentWord.setText(tr("Word: ") +misspelledWord);\r
140         }\r
141         \r
142         // Check if the OK button was pressed\r
143         public boolean replacePressed() {\r
144                 return replacePressed;\r
145         }\r
146         \r
147         // Check if the OK button was pressed\r
148         public boolean cancelPressed() {\r
149                 return cancelPressed;\r
150         }\r
151         \r
152         // Validate user input\r
153         public void validateInput() {\r
154                 replace.setEnabled(true);\r
155                 suggestions.clear();\r
156                 if (replacementWord.text().trim().equals("")) {\r
157                         replace.setEnabled(false);\r
158                         return;\r
159                 }\r
160                 \r
161                 @SuppressWarnings("unchecked")\r
162                 List<Word> values = checker.getSuggestions(replacementWord.text(), 10);\r
163                 for (int i=0; i<values.size(); i++) {\r
164                         suggestions.addItem(values.get(i).toString());\r
165                 }\r
166         }\r
167         \r
168         @SuppressWarnings("unused")\r
169         private void replacementChosen() {\r
170                 String sel = suggestions.currentItem().text();\r
171                 replacementWord.setText(sel);\r
172         }\r
173         \r
174         //Add an item to the dictionary\r
175         @SuppressWarnings("unused")\r
176         private void addToDictionaryButtonPressed() {\r
177                 checker.addToDictionary(misspelledWord);\r
178                 this.close();\r
179         }\r
180         \r
181         // Add a suggestion\r
182         public void addSuggestion(String word) {\r
183                 suggestions.addItem(word);\r
184         }\r
185         \r
186         // Set the current suggestion\r
187         public void setCurrentSuggestion(String word) {\r
188                 replacementWord.setText(word);\r
189         }\r
190         \r
191         // Empty out the list\r
192         public void clearSuggestions() {\r
193                 suggestions.clear();\r
194         }\r
195         \r
196         public void setSelectedSuggestion(int index) {\r
197                 if (index < suggestions.count())\r
198                         suggestions.setCurrentRow(index);\r
199         }\r
200 }\r