Lines 48-61
Link Here
|
48 |
import org.netbeans.api.editor.mimelookup.MimePath; |
48 |
import org.netbeans.api.editor.mimelookup.MimePath; |
49 |
|
49 |
|
50 |
/** |
50 |
/** |
51 |
* An interceptor which is called when user deletes text from a document. You should |
51 |
* An interceptor which is called when user use camel case action. You should |
52 |
* implement this interface if you want to hook in the keyboard input |
52 |
* implement this interface if you want to hook in the keyboard input |
53 |
* processing done in the editor infrastructure that would normally result in removing text |
53 |
* processing done in the editor infrastructure that would normally result in removing word |
54 |
* from a document. Typically, implementations of this interface will be called |
54 |
* from a document, selecting word or moving to next word. Typically, implementations of this interface will be called |
55 |
* when processing <code>KeyEvent</code>s that reach the default editor actions bound to |
55 |
* when processing <code>KeyEvent</code>s that reach the default editor actions bound to |
56 |
* <code>VK_DELETE</code> and <code>VK_BACK_SPACE</code> events. |
56 |
* CTRL+DEL, CTRL+BACKSPACE, CTRL+RIGHT_ARROW, CTRL+LEFT_ARROW, CTRL+SHIFT+RIGHT_ARROW, CTRL+SHIFT+LEFT_ARROW events. |
57 |
* |
57 |
* |
58 |
* <p><b>Registration</b>: <code>DeletedTextInterceptor</code>s can be plugged in the editor infrastructure |
58 |
* <p>When you set nextWordOffset, the implementation deletes/jumps according to this value. If value is not set or set to -1, |
|
|
59 |
* the default implementation of camel case action is performed. |
60 |
* |
61 |
* <p><b>Registration</b>: <code>CamelCaseInterceptor</code>s can be plugged in the editor infrastructure |
59 |
* by implementing the {@link Factory} interface and registering it in <code>MimeLookup</code> |
62 |
* by implementing the {@link Factory} interface and registering it in <code>MimeLookup</code> |
60 |
* under the appropriate mimetype (ie. <code>MimePath</code>). |
63 |
* under the appropriate mimetype (ie. <code>MimePath</code>). |
61 |
* |
64 |
* |
Lines 66-72
Link Here
|
66 |
* the interceptors are guaranteed to be called in AWT thread only, which means that |
69 |
* the interceptors are guaranteed to be called in AWT thread only, which means that |
67 |
* they should not need any internal synchronization model. |
70 |
* they should not need any internal synchronization model. |
68 |
* |
71 |
* |
69 |
* <p><b>Processing rules</b>: If there are multiple instances of <code>DeletedTextInterceptor</code> registered |
72 |
* <p><b>Processing rules</b>: If there are multiple instances of <code>CamelCaseInterceptor</code> registered |
70 |
* for the same mime type the infrastructure will queue them up in their registration |
73 |
* for the same mime type the infrastructure will queue them up in their registration |
71 |
* order and when processing an event it will call them all until the processing is done |
74 |
* order and when processing an event it will call them all until the processing is done |
72 |
* or terminated. |
75 |
* or terminated. |
Lines 78-122
Link Here
|
78 |
* all the queued interceptors before moving on to the next stage and calling next |
81 |
* all the queued interceptors before moving on to the next stage and calling next |
79 |
* method. |
82 |
* method. |
80 |
* |
83 |
* |
81 |
* <ul> |
|
|
82 |
* <li>{@link #beforeRemove(Context)} - It's called before any text is removed |
83 |
* from a document. No document lock is held when this method is called. The method |
84 |
* is not allowed to modify the document (and it's not supposed to do any tricks to |
85 |
* break this rule). An interceptor can stop further processing of the event by returning |
86 |
* <code>true</code> from this method. If it does so, no other interceptors' |
87 |
* <code>beforeRemove</code> method will be called and the processing will be terminated |
88 |
* without removing any text. |
89 |
* |
90 |
* <li>{@link #remove(Context)} - This method is called during the text |
91 |
* removal stage immediately after the text was removed from a document. At this |
92 |
* time the document is already write locked and the interceptors can modify it |
93 |
* if they need to. |
94 |
* |
95 |
* <li>{@link #afterRemove(Context)} - This is the last method in the processing |
96 |
* chain and it will be called when the text has already been removed from the document. |
97 |
* Similarly as in <code>beforeRemove</code> the document is not locked when |
98 |
* this method is called. |
99 |
* |
100 |
* <li>{@link #cancelled(Context)} - This is an additional method that will be called |
101 |
* when the processing is terminated in the before-removal stage (ie. by an interceptor |
102 |
* returning <code>true</code> from its <code>beforeRemove</code> method). |
103 |
* The infrastructure will only call this method on interceptors that have already |
104 |
* had their <code>beforeRemove</code> method called, but not on those that |
105 |
* have not yet been called at all. |
106 |
* </ul> |
107 |
* |
108 |
* <p><b>Errors recovery</b>: If an exception is thrown from any of the methods |
84 |
* <p><b>Errors recovery</b>: If an exception is thrown from any of the methods |
109 |
* when calling an interceptor the infrastructure will catch it and log it, |
85 |
* when calling an interceptor the infrastructure will catch it and log it, |
110 |
* but it will not stop further processing. The infrastructure may blacklist the offending |
86 |
* but it will not stop further processing. The infrastructure may blacklist the offending |
111 |
* interceptor and exclude it from processing future events. |
87 |
* interceptor and exclude it from processing future events. |
112 |
* |
88 |
* |
113 |
* @author Vita Stejskal |
89 |
* @since 1.79 |
114 |
* @since 1.31 |
|
|
115 |
*/ |
90 |
*/ |
116 |
public interface DeletedTextInterceptor { |
91 |
public interface CamelCaseInterceptor { |
117 |
|
92 |
|
118 |
/** |
93 |
/** |
119 |
* This method is called before any text is removed from a document. The context object |
94 |
* This method is called before any text is removed from a document or before caret move. The context object |
120 |
* passed to the method provides access to the document and its editor pane. The interceptors |
95 |
* passed to the method provides access to the document and its editor pane. The interceptors |
121 |
* are not allowed to modify the document in this method. |
96 |
* are not allowed to modify the document in this method. |
122 |
* |
97 |
* |
Lines 138-147
Link Here
|
138 |
* @throws BadLocationException Since the document is not locked prior calling this |
113 |
* @throws BadLocationException Since the document is not locked prior calling this |
139 |
* method the processing may fail when working with stale context data. |
114 |
* method the processing may fail when working with stale context data. |
140 |
*/ |
115 |
*/ |
141 |
boolean beforeRemove(Context context) throws BadLocationException; |
116 |
boolean beforeChange(MutableContext context) throws BadLocationException; |
142 |
|
117 |
|
143 |
/** |
118 |
/** |
144 |
* This method is called immediately after the text is removed from a document. |
119 |
* This method is called immediately after the text is removed from a document or caret is moved. |
145 |
* Implementors can modify the document as they need. The document and all |
120 |
* Implementors can modify the document as they need. The document and all |
146 |
* the other useful information is accessible from the <code>Context</code> object |
121 |
* the other useful information is accessible from the <code>Context</code> object |
147 |
* passed in this method. The document is write-locked. |
122 |
* passed in this method. The document is write-locked. |
Lines 154-163
Link Here
|
154 |
* |
129 |
* |
155 |
* @throws BadLocationException If the processing fails. |
130 |
* @throws BadLocationException If the processing fails. |
156 |
*/ |
131 |
*/ |
157 |
void remove(Context context) throws BadLocationException; |
132 |
void change(MutableContext context) throws BadLocationException; |
158 |
|
133 |
|
159 |
/** |
134 |
/** |
160 |
* This method is called after text is removed from a document and its editor's |
135 |
* This method is called after text is removed from a document and/or its editor's |
161 |
* caret is adjusted. |
136 |
* caret is adjusted. |
162 |
* |
137 |
* |
163 |
* <p><b>Locking</b>: When this method is called the document is not locked |
138 |
* <p><b>Locking</b>: When this method is called the document is not locked |
Lines 170-193
Link Here
|
170 |
* @throws BadLocationException Since the document is not locked prior calling this |
145 |
* @throws BadLocationException Since the document is not locked prior calling this |
171 |
* method the processing may fail when working with stale context data. |
146 |
* method the processing may fail when working with stale context data. |
172 |
*/ |
147 |
*/ |
173 |
void afterRemove(Context context) throws BadLocationException; |
148 |
void afterChange(MutableContext context) throws BadLocationException; |
174 |
|
149 |
|
175 |
/** |
150 |
/** |
176 |
* This method is called when the normal processing is terminated by some |
151 |
* This method is called when the normal processing is terminated by some |
177 |
* interceptor's <code>beforeRemove</code> method. Please note that this |
152 |
* interceptor's <code>beforeChange</code> method. Please note that this |
178 |
* method will not be called if the <code>beforeRemove</code> method was not |
153 |
* method will not be called if the <code>beforeChange</code> method was not |
179 |
* called. |
154 |
* called. |
180 |
* |
155 |
* |
181 |
* @param context The context object used for calling the <code>beforeRemove</code> |
156 |
* @param context The context object used for calling the <code>beforeChange</code> |
182 |
* method. |
157 |
* method. |
183 |
*/ |
158 |
*/ |
184 |
void cancelled(Context context); |
159 |
void cancelled(MutableContext context); |
185 |
|
160 |
|
186 |
/** |
161 |
/** |
187 |
* The context class providing information about the edited document, its |
162 |
* The context class providing information about the edited document, its |
188 |
* editor pane and the offset where the delete key event occurred. |
163 |
* editor pane and the offset where the key event occurred. |
189 |
*/ |
164 |
*/ |
190 |
public static final class Context { |
165 |
public static final class MutableContext { |
191 |
|
166 |
|
192 |
/** |
167 |
/** |
193 |
* Gets the editor component where the currently processed key typed event |
168 |
* Gets the editor component where the currently processed key typed event |
Lines 210-236
Link Here
|
210 |
} |
185 |
} |
211 |
|
186 |
|
212 |
/** |
187 |
/** |
213 |
* Gets the removal offset. This is the offset in the document where |
188 |
* Gets the offset. This is the offset in the document where |
214 |
* a user performed the delete action (ie. where the currently processed <code>KeyEvent</code> |
189 |
* a user performed the camel case action (ie. where the currently processed <code>KeyEvent</code> |
215 |
* happened). This is also the offset with text, which will be removed. |
190 |
* happened). This is also the offset with text, which will be removed or jumped. |
216 |
* |
191 |
* |
217 |
* @return The offset in the edited document. |
192 |
* @return The offset in the edited document. |
218 |
*/ |
193 |
*/ |
219 |
public int getOffset() { |
194 |
public int getOffset() { |
220 |
return offset; |
195 |
return offset; |
221 |
} |
196 |
} |
222 |
|
|
|
223 |
// XXX: since this is always one (character) it make no sense to have it |
224 |
// public int getLength() { |
225 |
// return lenght; |
226 |
// } |
227 |
|
197 |
|
228 |
/** |
198 |
/** |
229 |
* Determines the type of the character removal action performed by a user. The two |
199 |
* Determines the type of the direction performed by a user. The two |
230 |
* possible actions are called differently on different platforms, |
200 |
* possible actions are called differently on different platforms, |
231 |
* but they are always defined by the position of a character, which they are |
201 |
* but they are always defined by the position of a character, which they are |
232 |
* applied to. The <i>backspace</i> action deletes a character on the left hand |
202 |
* applied to. The <i>backspace/left arrow</i> action deletes/jumps a character on the left hand |
233 |
* side of a caret, while the <i>delete</i> action deletes a character on |
203 |
* side of a caret, while the <i>delete/right arrow</i> action deletes/jumps a character on |
234 |
* the right hand side of the caret. |
204 |
* the right hand side of the caret. |
235 |
* |
205 |
* |
236 |
* <p>In other words one delete action removes characters backwards moving |
206 |
* <p>In other words one delete action removes characters backwards moving |
Lines 242-261
Link Here
|
242 |
* backspace action. <code>false</code> if the handled action is the |
212 |
* backspace action. <code>false</code> if the handled action is the |
243 |
* delete action. |
213 |
* delete action. |
244 |
*/ |
214 |
*/ |
245 |
public boolean isBackwardDelete() { |
215 |
public boolean isBackward() { |
246 |
return backwardDelete; |
216 |
return backward; |
247 |
} |
217 |
} |
248 |
|
218 |
|
249 |
/** |
219 |
/** |
250 |
* Gets the text being removed. In <code>beforeRemove</code> method this |
220 |
* Sets the new offset after remove or jump. |
251 |
* text is still present in the document, while in the other methods this |
221 |
* |
252 |
* text has already been removed from the document. Nevertheless this method |
222 |
* @param nextWordOffset == -1 if default implementation should process otherwise nextWordOffset is used to jump/delete |
253 |
* always returns a copy of the text. |
|
|
254 |
* |
255 |
* @return The text being removed by the currently processed key typed event. |
256 |
*/ |
223 |
*/ |
257 |
public String getText() { |
224 |
public void setNextWordOffset(int nextWordOffset) { |
258 |
return removedText; |
225 |
this.nextWordOffset = nextWordOffset; |
259 |
} |
226 |
} |
260 |
|
227 |
|
261 |
// ------------------------------------------------------------------- |
228 |
// ------------------------------------------------------------------- |
Lines 265-297
Link Here
|
265 |
private final JTextComponent component; |
232 |
private final JTextComponent component; |
266 |
private final Document document; |
233 |
private final Document document; |
267 |
private final int offset; |
234 |
private final int offset; |
268 |
private final boolean backwardDelete; |
235 |
private final boolean backward; |
269 |
private final String removedText; |
236 |
private int nextWordOffset = -1; |
270 |
|
237 |
|
271 |
/* package */ Context(JTextComponent component, int offset, String removedText, boolean backwardDelete) { |
238 |
/* package */ MutableContext(JTextComponent component, int offset, boolean backwardDelete) { |
272 |
this.component = component; |
239 |
this.component = component; |
273 |
this.document = component.getDocument(); |
240 |
this.document = component.getDocument(); |
274 |
this.offset = offset; |
241 |
this.offset = offset; |
275 |
this.backwardDelete = backwardDelete; |
242 |
this.backward = backwardDelete; |
276 |
this.removedText = removedText; |
|
|
277 |
} |
243 |
} |
|
|
244 |
|
245 |
Object[] getData() { |
246 |
return nextWordOffset != -1 ? new Object[] {nextWordOffset} : null; |
247 |
} |
248 |
|
249 |
|
278 |
|
250 |
|
279 |
} // End of Context class |
251 |
} // End of Context class |
280 |
|
252 |
|
281 |
/** |
253 |
/** |
282 |
* The factory interface for registering <code>DeletedTextInterceptor</code>s |
254 |
* The factory interface for registering <code>CamelCaseInterceptor</code>s |
283 |
* in <code>MimeLookup</code>. An example registration in an XML layer shown |
255 |
* in <code>MimeLookup</code>. An example registration with java annotations. |
284 |
* below registers <code>Factory</code> implementation under <code>text/x-something</code> |
256 |
* |
285 |
* mime type in <code>MimeLookup</code>. |
|
|
286 |
* |
257 |
* |
287 |
* <pre> |
258 |
* <pre> |
288 |
* <folder name="Editors"> |
259 |
* @MimeRegistrations({ |
289 |
* <folder name="text"> |
260 |
* @MimeRegistration(mimeType = "x-something", service = CamelCaseInterceptor.Factory.class)}) |
290 |
* <folder name="x-something"> |
|
|
291 |
* <file name="org-some-module-DTIFactory.instance" /> |
292 |
* </folder> |
293 |
* </folder> |
294 |
* </folder> |
295 |
* </pre> |
261 |
* </pre> |
296 |
*/ |
262 |
*/ |
297 |
public interface Factory { |
263 |
public interface Factory { |
Lines 306-311
Link Here
|
306 |
* |
272 |
* |
307 |
* @return The new interceptor. |
273 |
* @return The new interceptor. |
308 |
*/ |
274 |
*/ |
309 |
DeletedTextInterceptor createDeletedTextInterceptor(MimePath mimePath); |
275 |
CamelCaseInterceptor createCamelCaseInterceptor(MimePath mimePath); |
310 |
} // End of Factory interface |
276 |
} // End of Factory interface |
311 |
} |
277 |
} |