OSDN Git Service

9923bd4f03df57bf6e9e6c8435fd73ee52903f7f
[neighbornote/NeighborNote.git] / src / com / swabunga / spell / swing / autospell / AutoSpellEditorKit.java
1 /*\r
2 Jazzy - a Java library for Spell Checking\r
3 Copyright (C) 2001 Mindaugas Idzelis\r
4 Full text of license can be found in LICENSE.txt\r
5 \r
6 This library is free software; you can redistribute it and/or\r
7 modify it under the terms of the GNU Lesser General Public\r
8 License as published by the Free Software Foundation; either\r
9 version 2.1 of the License, or (at your option) any later version.\r
10 \r
11 This library is distributed in the hope that it will be useful,\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
14 Lesser General Public License for more details.\r
15 \r
16 You should have received a copy of the GNU Lesser General Public\r
17 License along with this library; if not, write to the Free Software\r
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
19 */\r
20 /*\r
21  * Created on 2004-apr-23\r
22  *\r
23  * To change the template for this generated file go to\r
24  * Window - Preferences - Java - Code Generation - Code and Comments\r
25  */\r
26 package com.swabunga.spell.swing.autospell;\r
27 \r
28 import java.awt.event.ActionEvent;\r
29 import java.io.IOException;\r
30 import java.io.InputStream;\r
31 import java.io.OutputStream;\r
32 import java.io.Reader;\r
33 import java.io.Writer;\r
34 \r
35 import javax.swing.AbstractAction;\r
36 import javax.swing.Action;\r
37 import javax.swing.JEditorPane;\r
38 import javax.swing.JTextPane;\r
39 import javax.swing.text.BadLocationException;\r
40 import javax.swing.text.Caret;\r
41 import javax.swing.text.Document;\r
42 import javax.swing.text.Element;\r
43 import javax.swing.text.MutableAttributeSet;\r
44 import javax.swing.text.SimpleAttributeSet;\r
45 import javax.swing.text.StyledDocument;\r
46 import javax.swing.text.StyledEditorKit;\r
47 import javax.swing.text.ViewFactory;\r
48 \r
49 /**\r
50  * This editorkit just forwards all method calls to the original EditorKit\r
51  * for all method but getAction where it also adds a "MarkAsMisspelled" action \r
52  * and getViewFactory where we return our own ViewFactory (Based on the original).\r
53  * \r
54  * @author Robert Gustavsson (robert@lindesign.se)\r
55  *\r
56  */\r
57 @SuppressWarnings("serial")\r
58 public class AutoSpellEditorKit extends StyledEditorKit implements AutoSpellConstants{\r
59 \r
60         private StyledEditorKit editorKit=null;\r
61         private JEditorPane             pane=null;\r
62         \r
63         public AutoSpellEditorKit(StyledEditorKit editorKit){\r
64                 this.editorKit=editorKit;\r
65         }\r
66         \r
67         public StyledEditorKit getStyledEditorKit(){\r
68                 return editorKit;\r
69         }\r
70         \r
71         @Override\r
72         public Object clone(){\r
73                 return new AutoSpellEditorKit(editorKit);\r
74         }\r
75         \r
76         @Override\r
77         public void     deinstall(JEditorPane c){\r
78                 editorKit.deinstall(c);\r
79                 pane=null;\r
80         }\r
81         \r
82         @Override\r
83         public Element getCharacterAttributeRun(){\r
84                 return editorKit.getCharacterAttributeRun();\r
85         }\r
86         \r
87         @Override\r
88         public MutableAttributeSet getInputAttributes(){\r
89                 return editorKit.getInputAttributes();\r
90         }\r
91         \r
92         @Override\r
93         public void     install(JEditorPane c){\r
94                 editorKit.install(c);\r
95                 pane=c;\r
96         }\r
97         \r
98         /* (non-Javadoc)\r
99          * @see javax.swing.text.EditorKit#getContentType()\r
100          */\r
101         @Override\r
102         public String getContentType() {\r
103                 return editorKit.getContentType();\r
104         }\r
105 \r
106         /* (non-Javadoc)\r
107          * @see javax.swing.text.EditorKit#getActions()\r
108          */\r
109         @Override\r
110         public Action[] getActions() {\r
111                 Action[]        actions=new Action[editorKit.getActions().length+1];\r
112                 \r
113                 for(int i=0;i<editorKit.getActions().length;i++){\r
114                         actions[i]=editorKit.getActions()[i];\r
115                 }\r
116                 actions[actions.length-1]=new SpellCheckAction();\r
117                 return actions;\r
118         }\r
119 \r
120         /* (non-Javadoc)\r
121          * @see javax.swing.text.EditorKit#createCaret()\r
122          */\r
123         @Override\r
124         public Caret createCaret() {\r
125                 return editorKit.createCaret();\r
126         }\r
127 \r
128         /* (non-Javadoc)\r
129          * @see javax.swing.text.EditorKit#createDefaultDocument()\r
130          */\r
131         @Override\r
132         public Document createDefaultDocument() {\r
133                 return editorKit.createDefaultDocument();\r
134         }\r
135 \r
136         /* (non-Javadoc)\r
137          * @see javax.swing.text.EditorKit#getViewFactory()\r
138          */\r
139         @Override\r
140         public ViewFactory getViewFactory() {\r
141                 return new AutoSpellViewFactory(editorKit.getViewFactory());\r
142         }\r
143 \r
144         /* (non-Javadoc)\r
145          * @see javax.swing.text.EditorKit#read(java.io.InputStream, javax.swing.text.Document, int)\r
146          */\r
147         @Override\r
148         public void read(InputStream in, Document doc, int pos)\r
149                 throws IOException, BadLocationException {\r
150                 \r
151                 editorKit.read(in, doc, pos);\r
152         }\r
153 \r
154         /* (non-Javadoc)\r
155          * @see javax.swing.text.EditorKit#write(java.io.OutputStream, javax.swing.text.Document, int, int)\r
156          */\r
157         @Override\r
158         public void write(OutputStream out, Document doc, int pos, int len)\r
159                 throws IOException, BadLocationException {\r
160                 \r
161                 editorKit.write(out, doc, pos, len);\r
162         }\r
163 \r
164         /* (non-Javadoc)\r
165          * @see javax.swing.text.EditorKit#read(java.io.Reader, javax.swing.text.Document, int)\r
166          */\r
167         @Override\r
168         public void read(Reader in, Document doc, int pos)\r
169                 throws IOException, BadLocationException {\r
170                 \r
171                 editorKit.read(in, doc, pos);\r
172         }\r
173 \r
174         /* (non-Javadoc)\r
175          * @see javax.swing.text.EditorKit#write(java.io.Writer, javax.swing.text.Document, int, int)\r
176          */\r
177         @Override\r
178         public void write(Writer out, Document doc, int pos, int len)\r
179                 throws IOException, BadLocationException {\r
180                 \r
181                 editorKit.write(out, doc, pos, len);\r
182         }\r
183 \r
184         // INNER CLASSES\r
185         // ------------------------------------------------------------------\r
186         private class SpellCheckAction extends AbstractAction{\r
187         \r
188                 public SpellCheckAction(){\r
189                         super("Mark as misspelled");\r
190                 }\r
191                 \r
192                 public void actionPerformed(ActionEvent evt){\r
193                         SimpleAttributeSet attr;\r
194                         int pos=pane.getCaretPosition();\r
195                         if(pos<0)\r
196                                 return;\r
197                         attr=new SimpleAttributeSet(((StyledDocument)pane.getDocument()).getCharacterElement(pos).getAttributes());\r
198                         attr.addAttribute(wordMisspelled, wordMisspelledTrue);\r
199                         ((JTextPane)pane).setCharacterAttributes(attr,false);\r
200                 }\r
201         }\r
202 }\r