Lines 49-55
Link Here
|
49 |
import java.awt.Font; |
49 |
import java.awt.Font; |
50 |
import java.awt.Graphics; |
50 |
import java.awt.Graphics; |
51 |
import java.awt.event.KeyEvent; |
51 |
import java.awt.event.KeyEvent; |
|
|
52 |
import java.io.IOException; |
53 |
import java.net.MalformedURLException; |
52 |
import java.net.URL; |
54 |
import java.net.URL; |
|
|
55 |
import java.util.logging.Level; |
56 |
import java.util.logging.Logger; |
53 |
import javax.swing.Action; |
57 |
import javax.swing.Action; |
54 |
import javax.swing.text.*; |
58 |
import javax.swing.text.*; |
55 |
import javax.swing.Icon; |
59 |
import javax.swing.Icon; |
Lines 57-69
Link Here
|
57 |
import org.netbeans.editor.*; |
61 |
import org.netbeans.editor.*; |
58 |
import javax.swing.JLabel; |
62 |
import javax.swing.JLabel; |
59 |
import org.netbeans.api.editor.completion.Completion; |
63 |
import org.netbeans.api.editor.completion.Completion; |
|
|
64 |
import org.netbeans.api.queries.FileEncodingQuery; |
60 |
import org.netbeans.modules.xml.api.model.GrammarResult; |
65 |
import org.netbeans.modules.xml.api.model.GrammarResult; |
|
|
66 |
import org.netbeans.modules.xml.api.model.DescriptionSource; |
61 |
import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext; |
67 |
import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext; |
62 |
import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport; |
68 |
import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport; |
63 |
import org.netbeans.spi.editor.completion.CompletionDocumentation; |
69 |
import org.netbeans.spi.editor.completion.CompletionDocumentation; |
64 |
import org.netbeans.spi.editor.completion.CompletionItem; |
70 |
import org.netbeans.spi.editor.completion.CompletionItem; |
65 |
import org.netbeans.spi.editor.completion.CompletionResultSet; |
71 |
import org.netbeans.spi.editor.completion.CompletionResultSet; |
66 |
import org.netbeans.spi.editor.completion.CompletionTask; |
72 |
import org.netbeans.spi.editor.completion.CompletionTask; |
|
|
73 |
import org.openide.filesystems.FileObject; |
74 |
import org.openide.filesystems.URLMapper; |
75 |
import org.openide.util.Exceptions; |
67 |
|
76 |
|
68 |
/** |
77 |
/** |
69 |
* This class carries result information required by NetBeans Editor module. |
78 |
* This class carries result information required by NetBeans Editor module. |
Lines 72-77
Link Here
|
72 |
* @author Sandeep Randhawa |
81 |
* @author Sandeep Randhawa |
73 |
*/ |
82 |
*/ |
74 |
class XMLResultItem implements CompletionItem { |
83 |
class XMLResultItem implements CompletionItem { |
|
|
84 |
private static final Logger LOG = Logger.getLogger(XMLResultItem.class.getName()); |
75 |
|
85 |
|
76 |
// text to be diplayed to user |
86 |
// text to be diplayed to user |
77 |
public final String displayText; |
87 |
public final String displayText; |
Lines 304-328
Link Here
|
304 |
protected CompletionTask doCreateDocumentationTask(final GrammarResult res) { |
314 |
protected CompletionTask doCreateDocumentationTask(final GrammarResult res) { |
305 |
return new CompletionTask() { |
315 |
return new CompletionTask() { |
306 |
public void query(CompletionResultSet resultSet) { |
316 |
public void query(CompletionResultSet resultSet) { |
307 |
if (res != null && res.getDescription() != null) { |
317 |
CompletionDocumentation cd = create(); |
308 |
resultSet.setDocumentation(new Docum(res.getDescription())); |
318 |
if (cd != null) { |
309 |
|
319 |
resultSet.setDocumentation(cd); |
310 |
} |
320 |
} |
311 |
resultSet.finish(); |
321 |
resultSet.finish(); |
312 |
} |
322 |
} |
313 |
public void refresh(CompletionResultSet resultSet) { |
323 |
public void refresh(CompletionResultSet resultSet) { |
314 |
if (res != null && res.getDescription() != null) { |
324 |
query(resultSet); |
315 |
resultSet.setDocumentation(new Docum(res.getDescription())); |
|
|
316 |
} |
317 |
resultSet.finish(); |
318 |
} |
325 |
} |
319 |
public void cancel() {} |
326 |
public void cancel() {} |
|
|
327 |
|
328 |
|
329 |
private CompletionDocumentation create() { |
330 |
String doc; |
331 |
|
332 |
doc = res.getDescription(); |
333 |
if (!(res instanceof DescriptionSource)) { |
334 |
if (doc == null) { |
335 |
return null; |
336 |
} |
337 |
return new Docum(doc); |
338 |
} else { |
339 |
DescriptionSource ds = (DescriptionSource)res; |
340 |
if (doc == null && ds.getContentURL() == null) { |
341 |
return null; |
342 |
} |
343 |
return new ExtDocum(ds, doc); |
344 |
} |
345 |
} |
320 |
}; |
346 |
}; |
321 |
} |
347 |
} |
|
|
348 |
|
349 |
/** |
350 |
* Extended documentation, based on the {@link DescriptionSource} SPI. |
351 |
*/ |
352 |
private static class ExtDocum extends URLDocum implements CompletionDocumentation { |
353 |
private DescriptionSource src; |
354 |
private String doc; |
355 |
|
356 |
ExtDocum(DescriptionSource src, String doc) { |
357 |
super(src.getContentURL(), src.isExternal()); |
358 |
this.src = src; |
359 |
this.doc = doc; |
360 |
} |
361 |
|
362 |
@Override |
363 |
public String getText() { |
364 |
if (doc == null) { |
365 |
doc = src.getDescription(); |
366 |
if (doc == null) { |
367 |
doc = super.getText(); |
368 |
} |
369 |
} |
370 |
return doc; |
371 |
} |
372 |
|
373 |
@Override |
374 |
public CompletionDocumentation resolveLink(String link) { |
375 |
try { |
376 |
DescriptionSource target = src.resolveLink(link); |
377 |
if (target != null) { |
378 |
return new ExtDocum(target, null); |
379 |
} |
380 |
|
381 |
URL base = src.getContentURL(); |
382 |
if (base == null) { |
383 |
// sorry, cannot resolve. |
384 |
return null; |
385 |
} |
386 |
|
387 |
URL targetURL = new URL(base, link); |
388 |
return new URLDocum(targetURL, src.isExternal()); |
389 |
} catch (MalformedURLException ex) { |
390 |
Exceptions.printStackTrace(ex); |
391 |
return null; |
392 |
} |
393 |
} |
394 |
|
395 |
@Override |
396 |
public Action getGotoSourceAction() { |
397 |
return null; |
398 |
} |
399 |
} |
400 |
|
401 |
/** |
402 |
* Pure URL documentation item. Resolves links, if original URL was able |
403 |
* to open externally, the derived URLs do it as well. |
404 |
*/ |
405 |
private static class URLDocum implements CompletionDocumentation { |
406 |
URL content; |
407 |
boolean external; |
408 |
|
409 |
URLDocum(URL content, boolean external) { |
410 |
this.content = content; |
411 |
this.external = external; |
412 |
} |
413 |
|
414 |
URLDocum(boolean external) { |
415 |
this.external = external; |
416 |
} |
417 |
|
418 |
@Override |
419 |
public Action getGotoSourceAction() { |
420 |
return null; |
421 |
} |
422 |
|
423 |
@Override |
424 |
public String getText() { |
425 |
FileObject f = URLMapper.findFileObject(content); |
426 |
if (f != null) { |
427 |
try { |
428 |
return new String(f.asBytes(), FileEncodingQuery.getEncoding(f)); |
429 |
} catch (IOException x) { |
430 |
LOG.log(Level.INFO, "Could not load " + content, x); |
431 |
} |
432 |
} |
433 |
return null; |
434 |
} |
435 |
|
436 |
@Override |
437 |
public URL getURL() { |
438 |
return external ? content : null; |
439 |
} |
440 |
|
441 |
@Override |
442 |
public CompletionDocumentation resolveLink(String link) { |
443 |
if (content == null) { |
444 |
return null; |
445 |
} |
446 |
try { |
447 |
return new URLDocum(new URL(content, link), external); |
448 |
} catch (MalformedURLException ex) { |
449 |
Exceptions.printStackTrace(ex); |
450 |
return null; |
451 |
} |
452 |
} |
453 |
|
454 |
} |
322 |
|
455 |
|
323 |
private static class Docum implements CompletionDocumentation { |
456 |
private static class Docum implements CompletionDocumentation { |
324 |
private String doc; |
457 |
private String doc; |
325 |
|
458 |
|
326 |
private Docum(String doc) { |
459 |
private Docum(String doc) { |
327 |
this.doc = doc; |
460 |
this.doc = doc; |
328 |
} |
461 |
} |