Mercurial > projects > dwt-addons
comparison dwtx/jface/text/templates/TemplateProposal.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | c4fb132a086c |
comparison
equal
deleted
inserted
replaced
128:8df1d4193877 | 129:eb30df5ca28b |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.templates.TemplateProposal; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 | |
18 import dwt.graphics.Image; | |
19 import dwt.graphics.Point; | |
20 import dwt.widgets.Shell; | |
21 import dwtx.core.runtime.Assert; | |
22 import dwtx.jface.dialogs.MessageDialog; | |
23 import dwtx.jface.text.BadLocationException; | |
24 import dwtx.jface.text.BadPositionCategoryException; | |
25 import dwtx.jface.text.DocumentEvent; | |
26 import dwtx.jface.text.IDocument; | |
27 import dwtx.jface.text.IInformationControlCreator; | |
28 import dwtx.jface.text.IRegion; | |
29 import dwtx.jface.text.ITextViewer; | |
30 import dwtx.jface.text.Position; | |
31 import dwtx.jface.text.Region; | |
32 import dwtx.jface.text.contentassist.ICompletionProposal; | |
33 import dwtx.jface.text.contentassist.ICompletionProposalExtension; | |
34 import dwtx.jface.text.contentassist.ICompletionProposalExtension2; | |
35 import dwtx.jface.text.contentassist.ICompletionProposalExtension3; | |
36 import dwtx.jface.text.contentassist.IContextInformation; | |
37 import dwtx.jface.text.link.ILinkedModeListener; | |
38 import dwtx.jface.text.link.LinkedModeModel; | |
39 import dwtx.jface.text.link.LinkedModeUI; | |
40 import dwtx.jface.text.link.LinkedPosition; | |
41 import dwtx.jface.text.link.LinkedPositionGroup; | |
42 import dwtx.jface.text.link.ProposalPosition; | |
43 | |
44 | |
45 /** | |
46 * A template completion proposal. | |
47 * <p> | |
48 * Clients may subclass.</p> | |
49 * | |
50 * @since 3.0 | |
51 */ | |
52 public class TemplateProposal : ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3 { | |
53 | |
54 private final Template fTemplate; | |
55 private final TemplateContext fContext; | |
56 private final Image fImage; | |
57 private final IRegion fRegion; | |
58 private int fRelevance; | |
59 | |
60 private IRegion fSelectedRegion; // initialized by apply() | |
61 private String fDisplayString; | |
62 private InclusivePositionUpdater fUpdater; | |
63 private IInformationControlCreator fInformationControlCreator; | |
64 | |
65 /** | |
66 * Creates a template proposal with a template and its context. | |
67 * | |
68 * @param template the template | |
69 * @param context the context in which the template was requested. | |
70 * @param region the region this proposal is applied to | |
71 * @param image the icon of the proposal. | |
72 */ | |
73 public TemplateProposal(Template template, TemplateContext context, IRegion region, Image image) { | |
74 this(template, context, region, image, 0); | |
75 } | |
76 | |
77 /** | |
78 * Creates a template proposal with a template and its context. | |
79 * | |
80 * @param template the template | |
81 * @param context the context in which the template was requested. | |
82 * @param image the icon of the proposal. | |
83 * @param region the region this proposal is applied to | |
84 * @param relevance the relevance of the proposal | |
85 */ | |
86 public TemplateProposal(Template template, TemplateContext context, IRegion region, Image image, int relevance) { | |
87 Assert.isNotNull(template); | |
88 Assert.isNotNull(context); | |
89 Assert.isNotNull(region); | |
90 | |
91 fTemplate= template; | |
92 fContext= context; | |
93 fImage= image; | |
94 fRegion= region; | |
95 | |
96 fDisplayString= null; | |
97 | |
98 fRelevance= relevance; | |
99 } | |
100 | |
101 /** | |
102 * Sets the information control creator for this completion proposal. | |
103 * | |
104 * @param informationControlCreator the information control creator | |
105 * @since 3.1 | |
106 */ | |
107 public final void setInformationControlCreator(IInformationControlCreator informationControlCreator) { | |
108 fInformationControlCreator= informationControlCreator; | |
109 } | |
110 | |
111 /** | |
112 * Returns the template of this proposal. | |
113 * | |
114 * @return the template of this proposal | |
115 * @since 3.1 | |
116 */ | |
117 protected final Template getTemplate() { | |
118 return fTemplate; | |
119 } | |
120 | |
121 /** | |
122 * Returns the context in which the template was requested. | |
123 * | |
124 * @return the context in which the template was requested | |
125 * @since 3.1 | |
126 */ | |
127 protected final TemplateContext getContext() { | |
128 return fContext; | |
129 } | |
130 | |
131 /* | |
132 * @see ICompletionProposal#apply(IDocument) | |
133 */ | |
134 public final void apply(IDocument document) { | |
135 // not called anymore | |
136 } | |
137 | |
138 /** | |
139 * Inserts the template offered by this proposal into the viewer's document | |
140 * and sets up a <code>LinkedModeUI</code> on the viewer to edit any of | |
141 * the template's unresolved variables. | |
142 * | |
143 * @param viewer {@inheritDoc} | |
144 * @param trigger {@inheritDoc} | |
145 * @param stateMask {@inheritDoc} | |
146 * @param offset {@inheritDoc} | |
147 */ | |
148 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { | |
149 | |
150 IDocument document= viewer.getDocument(); | |
151 try { | |
152 fContext.setReadOnly(false); | |
153 int start; | |
154 TemplateBuffer templateBuffer; | |
155 { | |
156 int oldReplaceOffset= getReplaceOffset(); | |
157 try { | |
158 // this may already modify the document (e.g. add imports) | |
159 templateBuffer= fContext.evaluate(fTemplate); | |
160 } catch (TemplateException e1) { | |
161 fSelectedRegion= fRegion; | |
162 return; | |
163 } | |
164 | |
165 start= getReplaceOffset(); | |
166 int shift= start - oldReplaceOffset; | |
167 int end= Math.max(getReplaceEndOffset(), offset + shift); | |
168 | |
169 // insert template string | |
170 String templateString= templateBuffer.getString(); | |
171 document.replace(start, end - start, templateString); | |
172 } | |
173 | |
174 // translate positions | |
175 LinkedModeModel model= new LinkedModeModel(); | |
176 TemplateVariable[] variables= templateBuffer.getVariables(); | |
177 bool hasPositions= false; | |
178 for (int i= 0; i !is variables.length; i++) { | |
179 TemplateVariable variable= variables[i]; | |
180 | |
181 if (variable.isUnambiguous()) | |
182 continue; | |
183 | |
184 LinkedPositionGroup group= new LinkedPositionGroup(); | |
185 | |
186 int[] offsets= variable.getOffsets(); | |
187 int length= variable.getLength(); | |
188 | |
189 LinkedPosition first; | |
190 { | |
191 String[] values= variable.getValues(); | |
192 ICompletionProposal[] proposals= new ICompletionProposal[values.length]; | |
193 for (int j= 0; j < values.length; j++) { | |
194 ensurePositionCategoryInstalled(document, model); | |
195 Position pos= new Position(offsets[0] + start, length); | |
196 document.addPosition(getCategory(), pos); | |
197 proposals[j]= new PositionBasedCompletionProposal(values[j], pos, length); | |
198 } | |
199 | |
200 if (proposals.length > 1) | |
201 first= new ProposalPosition(document, offsets[0] + start, length, proposals); | |
202 else | |
203 first= new LinkedPosition(document, offsets[0] + start, length); | |
204 } | |
205 | |
206 for (int j= 0; j !is offsets.length; j++) | |
207 if (j is 0) | |
208 group.addPosition(first); | |
209 else | |
210 group.addPosition(new LinkedPosition(document, offsets[j] + start, length)); | |
211 | |
212 model.addGroup(group); | |
213 hasPositions= true; | |
214 } | |
215 | |
216 if (hasPositions) { | |
217 model.forceInstall(); | |
218 LinkedModeUI ui= new LinkedModeUI(model, viewer); | |
219 ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE); | |
220 ui.enter(); | |
221 | |
222 fSelectedRegion= ui.getSelectedRegion(); | |
223 } else { | |
224 ensurePositionCategoryRemoved(document); | |
225 fSelectedRegion= new Region(getCaretOffset(templateBuffer) + start, 0); | |
226 } | |
227 | |
228 } catch (BadLocationException e) { | |
229 openErrorDialog(viewer.getTextWidget().getShell(), e); | |
230 ensurePositionCategoryRemoved(document); | |
231 fSelectedRegion= fRegion; | |
232 } catch (BadPositionCategoryException e) { | |
233 openErrorDialog(viewer.getTextWidget().getShell(), e); | |
234 fSelectedRegion= fRegion; | |
235 } | |
236 | |
237 } | |
238 | |
239 private void ensurePositionCategoryInstalled(final IDocument document, LinkedModeModel model) { | |
240 if (!document.containsPositionCategory(getCategory())) { | |
241 document.addPositionCategory(getCategory()); | |
242 fUpdater= new InclusivePositionUpdater(getCategory()); | |
243 document.addPositionUpdater(fUpdater); | |
244 | |
245 model.addLinkingListener(new ILinkedModeListener() { | |
246 | |
247 /* | |
248 * @see dwtx.jface.text.link.ILinkedModeListener#left(dwtx.jface.text.link.LinkedModeModel, int) | |
249 */ | |
250 public void left(LinkedModeModel environment, int flags) { | |
251 ensurePositionCategoryRemoved(document); | |
252 } | |
253 | |
254 public void suspend(LinkedModeModel environment) {} | |
255 public void resume(LinkedModeModel environment, int flags) {} | |
256 }); | |
257 } | |
258 } | |
259 | |
260 private void ensurePositionCategoryRemoved(IDocument document) { | |
261 if (document.containsPositionCategory(getCategory())) { | |
262 try { | |
263 document.removePositionCategory(getCategory()); | |
264 } catch (BadPositionCategoryException e) { | |
265 // ignore | |
266 } | |
267 document.removePositionUpdater(fUpdater); | |
268 } | |
269 } | |
270 | |
271 private String getCategory() { | |
272 return "TemplateProposalCategory_" + toString(); //$NON-NLS-1$ | |
273 } | |
274 | |
275 private int getCaretOffset(TemplateBuffer buffer) { | |
276 | |
277 TemplateVariable[] variables= buffer.getVariables(); | |
278 for (int i= 0; i !is variables.length; i++) { | |
279 TemplateVariable variable= variables[i]; | |
280 if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME)) | |
281 return variable.getOffsets()[0]; | |
282 } | |
283 | |
284 return buffer.getString().length(); | |
285 } | |
286 | |
287 /** | |
288 * Returns the offset of the range in the document that will be replaced by | |
289 * applying this template. | |
290 * | |
291 * @return the offset of the range in the document that will be replaced by | |
292 * applying this template | |
293 * @since 3.1 | |
294 */ | |
295 protected final int getReplaceOffset() { | |
296 int start; | |
297 if (fContext instanceof DocumentTemplateContext) { | |
298 DocumentTemplateContext docContext = (DocumentTemplateContext)fContext; | |
299 start= docContext.getStart(); | |
300 } else { | |
301 start= fRegion.getOffset(); | |
302 } | |
303 return start; | |
304 } | |
305 | |
306 /** | |
307 * Returns the end offset of the range in the document that will be replaced | |
308 * by applying this template. | |
309 * | |
310 * @return the end offset of the range in the document that will be replaced | |
311 * by applying this template | |
312 * @since 3.1 | |
313 */ | |
314 protected final int getReplaceEndOffset() { | |
315 int end; | |
316 if (fContext instanceof DocumentTemplateContext) { | |
317 DocumentTemplateContext docContext = (DocumentTemplateContext)fContext; | |
318 end= docContext.getEnd(); | |
319 } else { | |
320 end= fRegion.getOffset() + fRegion.getLength(); | |
321 } | |
322 return end; | |
323 } | |
324 | |
325 /* | |
326 * @see ICompletionProposal#getSelection(IDocument) | |
327 */ | |
328 public Point getSelection(IDocument document) { | |
329 return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); | |
330 } | |
331 | |
332 /* | |
333 * @see ICompletionProposal#getAdditionalProposalInfo() | |
334 */ | |
335 public String getAdditionalProposalInfo() { | |
336 try { | |
337 fContext.setReadOnly(true); | |
338 TemplateBuffer templateBuffer; | |
339 try { | |
340 templateBuffer= fContext.evaluate(fTemplate); | |
341 } catch (TemplateException e) { | |
342 return null; | |
343 } | |
344 | |
345 return templateBuffer.getString(); | |
346 | |
347 } catch (BadLocationException e) { | |
348 return null; | |
349 } | |
350 } | |
351 | |
352 /* | |
353 * @see ICompletionProposal#getDisplayString() | |
354 */ | |
355 public String getDisplayString() { | |
356 if (fDisplayString is null) { | |
357 String[] arguments= new String[] { fTemplate.getName(), fTemplate.getDescription() }; | |
358 fDisplayString= JFaceTextTemplateMessages.getFormattedString("TemplateProposal.displayString", arguments); //$NON-NLS-1$ | |
359 } | |
360 return fDisplayString; | |
361 } | |
362 | |
363 /* | |
364 * @see ICompletionProposal#getImage() | |
365 */ | |
366 public Image getImage() { | |
367 return fImage; | |
368 } | |
369 | |
370 /* | |
371 * @see ICompletionProposal#getContextInformation() | |
372 */ | |
373 public IContextInformation getContextInformation() { | |
374 return null; | |
375 } | |
376 | |
377 private void openErrorDialog(Shell shell, Exception e) { | |
378 MessageDialog.openError(shell, JFaceTextTemplateMessages.getString("TemplateProposal.errorDialog.title"), e.getMessage()); //$NON-NLS-1$ | |
379 } | |
380 | |
381 /** | |
382 * Returns the relevance. | |
383 * | |
384 * @return the relevance | |
385 */ | |
386 public int getRelevance() { | |
387 return fRelevance; | |
388 } | |
389 | |
390 /* | |
391 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension3#getInformationControlCreator() | |
392 */ | |
393 public IInformationControlCreator getInformationControlCreator() { | |
394 return fInformationControlCreator; | |
395 } | |
396 | |
397 /* | |
398 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#selected(dwtx.jface.text.ITextViewer, bool) | |
399 */ | |
400 public void selected(ITextViewer viewer, bool smartToggle) { | |
401 } | |
402 | |
403 /* | |
404 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#unselected(dwtx.jface.text.ITextViewer) | |
405 */ | |
406 public void unselected(ITextViewer viewer) { | |
407 } | |
408 | |
409 /* | |
410 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#validate(dwtx.jface.text.IDocument, int, dwtx.jface.text.DocumentEvent) | |
411 */ | |
412 public bool validate(IDocument document, int offset, DocumentEvent event) { | |
413 try { | |
414 int replaceOffset= getReplaceOffset(); | |
415 if (offset >= replaceOffset) { | |
416 String content= document.get(replaceOffset, offset - replaceOffset); | |
417 return fTemplate.getName().toLowerCase().startsWith(content.toLowerCase()); | |
418 } | |
419 } catch (BadLocationException e) { | |
420 // concurrent modification - ignore | |
421 } | |
422 return false; | |
423 } | |
424 | |
425 /* | |
426 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionText(dwtx.jface.text.IDocument, int) | |
427 */ | |
428 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { | |
429 return fTemplate.getName(); | |
430 } | |
431 | |
432 /* | |
433 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionStart(dwtx.jface.text.IDocument, int) | |
434 */ | |
435 public int getPrefixCompletionStart(IDocument document, int completionOffset) { | |
436 return getReplaceOffset(); | |
437 } | |
438 | |
439 /* | |
440 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#apply(dwtx.jface.text.IDocument, char, int) | |
441 */ | |
442 public void apply(IDocument document, char trigger, int offset) { | |
443 // not called any longer | |
444 } | |
445 | |
446 /* | |
447 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#isValidFor(dwtx.jface.text.IDocument, int) | |
448 */ | |
449 public bool isValidFor(IDocument document, int offset) { | |
450 // not called any longer | |
451 return false; | |
452 } | |
453 | |
454 /* | |
455 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters() | |
456 */ | |
457 public char[] getTriggerCharacters() { | |
458 // no triggers | |
459 return new char[0]; | |
460 } | |
461 | |
462 /* | |
463 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition() | |
464 */ | |
465 public int getContextInformationPosition() { | |
466 return fRegion.getOffset(); | |
467 } | |
468 } |