Lines 45-57
Link Here
|
45 |
|
45 |
|
46 |
import java.awt.Color; |
46 |
import java.awt.Color; |
47 |
import java.awt.Component; |
47 |
import java.awt.Component; |
|
|
48 |
import java.io.IOException; |
49 |
import java.util.logging.Level; |
50 |
import java.util.logging.Logger; |
48 |
|
51 |
|
49 |
|
52 |
|
50 |
import javax.swing.JEditorPane; |
53 |
import javax.swing.JEditorPane; |
51 |
import javax.swing.JToolBar; |
54 |
import javax.swing.JToolBar; |
52 |
import javax.swing.SwingUtilities; |
55 |
import javax.swing.SwingUtilities; |
53 |
import javax.swing.text.*; |
56 |
import javax.swing.text.*; |
|
|
57 |
import org.netbeans.api.actions.Openable; |
58 |
import org.openide.DialogDisplayer; |
59 |
import org.openide.NotifyDescriptor; |
54 |
import org.openide.cookies.EditorCookie; |
60 |
import org.openide.cookies.EditorCookie; |
|
|
61 |
import org.openide.cookies.LineCookie; |
62 |
import org.openide.util.Lookup; |
63 |
import org.openide.util.Mutex; |
64 |
import org.openide.util.NbBundle; |
65 |
import org.openide.util.UserQuestionException; |
55 |
|
66 |
|
56 |
|
67 |
|
57 |
/** Dummy class holding utility methods for working with NetBeans document conventions. |
68 |
/** Dummy class holding utility methods for working with NetBeans document conventions. |
Lines 518-523
Link Here
|
518 |
} |
529 |
} |
519 |
((Annotatable) doc).removeAnnotation(annotation); |
530 |
((Annotatable) doc).removeAnnotation(annotation); |
520 |
} |
531 |
} |
|
|
532 |
|
533 |
/** |
534 |
* Get the document associated with a file. |
535 |
* |
536 |
* <p>Method will throw {@link org.openide.util.UserQuestionException} |
537 |
* exception if file size is too big. This exception is caught and its |
538 |
* method {@link org.openide.util.UserQuestionException#confirmed} is used |
539 |
* for confirmation. |
540 |
* |
541 |
* |
542 |
* @param provider for example a {@link DataObject} |
543 |
* @return {@link javax.swing.text.StyledDocument} or null |
544 |
* @since org.openide.text 6.45 |
545 |
*/ |
546 |
@NbBundle.Messages("TXT_Question=Question") |
547 |
public static StyledDocument getDocument(Lookup.Provider provider) { |
548 |
try { |
549 |
EditorCookie ec = provider.getLookup().lookup(EditorCookie.class); |
550 |
if (ec != null) { |
551 |
StyledDocument doc = null; |
552 |
try { |
553 |
doc = ec.openDocument(); |
554 |
} catch (UserQuestionException uqe) { |
555 |
final Object value = DialogDisplayer.getDefault().notify( |
556 |
new NotifyDescriptor.Confirmation(uqe.getLocalizedMessage(), |
557 |
Bundle.TXT_Question(), |
558 |
NotifyDescriptor.YES_NO_OPTION)); |
559 |
if (value != NotifyDescriptor.YES_OPTION) { |
560 |
return null; |
561 |
} |
562 |
uqe.confirmed(); |
563 |
doc = ec.openDocument(); |
564 |
} |
565 |
return doc; |
566 |
} |
567 |
} catch (IOException ioe) { |
568 |
Logger.getLogger(NbDocument.class.getName()).log(Level.WARNING, null, ioe); |
569 |
} |
570 |
return null; |
571 |
} |
572 |
|
573 |
/** |
574 |
* Open the document associated with a file in the Editor window in a |
575 |
* position specified by the offset while controlling open and visibility behavior. |
576 |
* This method can be called from any thread but blocks until the document is opened. |
577 |
* |
578 |
* @param provider for example a {@link DataObject} |
579 |
* @param offset the position the document should be opened (starting at 0) |
580 |
* @param openType control open behavior, {@link org.openide.text.Line.ShowOpenType#OPEN} |
581 |
* would typically be used |
582 |
* @param visibilityType control visibility behavior, {@link org.openide.text.Line.ShowVisibilityType#FOCUS} |
583 |
* would typically be used |
584 |
* @return true if the Document is opened - false otherwise |
585 |
* @see #getDocument |
586 |
* <code>UserQuestionException handling</code> |
587 |
* @since org.openide.text 6.45 |
588 |
*/ |
589 |
public static boolean openDocument(Lookup.Provider provider, int offset, Line.ShowOpenType openType, Line.ShowVisibilityType visibilityType) { |
590 |
assert provider != null; |
591 |
LineCookie lc = provider.getLookup().lookup(LineCookie.class); |
592 |
if ((lc != null) && (offset != -1)) { |
593 |
StyledDocument doc = getDocument(provider); |
594 |
|
595 |
if (doc != null) { |
596 |
int line = NbDocument.findLineNumber(doc, offset); |
597 |
int column = NbDocument.findLineColumn(doc, offset); |
598 |
|
599 |
Line l = null; |
600 |
try { |
601 |
l = lc.getLineSet().getCurrent(line); |
602 |
} catch (IndexOutOfBoundsException e) { // try to open at least the file (line no. is too high?) |
603 |
l = lc.getLineSet().getCurrent(0); |
604 |
} |
605 |
|
606 |
if (l != null) { |
607 |
doShow(l, column, openType, visibilityType); |
608 |
return true; |
609 |
} |
610 |
} |
611 |
} |
612 |
|
613 |
Openable oc = provider.getLookup().lookup(Openable.class); |
614 |
|
615 |
if (oc != null) { |
616 |
doOpen(oc); |
617 |
return true; |
618 |
} |
619 |
return false; |
620 |
} |
621 |
|
622 |
/** |
623 |
* Open the document associated with a file in the Editor window in a |
624 |
* position specified by the line and column while controlling open and visibility behavior. |
625 |
* This method can be called from any thread but blocks until the document is opened. |
626 |
* |
627 |
* @param provider for example a {@link DataObject} |
628 |
* @param line the line the document should be opened (starting at 0) |
629 |
* @param column the column which should be selected (starting at 0), value |
630 |
* -1 does not change previously selected column |
631 |
* @param openType control open behavior, {@link org.openide.text.Line.ShowOpenType#OPEN} |
632 |
* would typically be used |
633 |
* @param visibilityType control visibility behavior, {@link org.openide.text.Line.ShowVisibilityType#FOCUS} |
634 |
* would typically be used |
635 |
* @return true if the Document is opened - false otherwise |
636 |
* @see #getDocument |
637 |
* <code>UserQuestionException handling</code> |
638 |
* @since org.openide.text 6.45 |
639 |
*/ |
640 |
public static boolean openDocument(Lookup.Provider provider, int line, int column, Line.ShowOpenType openType, Line.ShowVisibilityType visibilityType) { |
641 |
assert provider != null; |
642 |
LineCookie lc = provider.getLookup().lookup(LineCookie.class); |
643 |
if ((lc != null) && (line >= 0) && (column >= -1)) { |
644 |
StyledDocument doc = getDocument(provider); |
645 |
|
646 |
if (doc != null) { |
647 |
Line l = null; |
648 |
try { |
649 |
l = lc.getLineSet().getCurrent(line); |
650 |
} catch (IndexOutOfBoundsException e) { // try to open at least the file (line no. is too high?) |
651 |
l = lc.getLineSet().getCurrent(0); |
652 |
} |
653 |
|
654 |
if (l != null) { |
655 |
doShow(l, column, openType, visibilityType); |
656 |
return true; |
657 |
} |
658 |
} |
659 |
} |
660 |
|
661 |
final Openable oc = provider.getLookup().lookup(Openable.class); |
662 |
|
663 |
if (oc != null) { |
664 |
doOpen(oc); |
665 |
return true; |
666 |
} |
667 |
|
668 |
return false; |
669 |
} |
670 |
|
671 |
private static void doShow(final Line l, final int column, final Line.ShowOpenType openType, final Line.ShowVisibilityType visibilityType) { |
672 |
Mutex.EVENT.writeAccess(new Runnable() { |
673 |
|
674 |
@Override |
675 |
public void run() { |
676 |
l.show(openType, visibilityType, column); |
677 |
} |
678 |
}); |
679 |
} |
680 |
|
681 |
private static void doOpen(final Openable oc) { |
682 |
Mutex.EVENT.writeAccess(new Runnable() { |
683 |
|
684 |
@Override |
685 |
public void run() { |
686 |
oc.open(); |
687 |
} |
688 |
}); |
689 |
} |
521 |
|
690 |
|
522 |
/** Specialized version of document that knows how to lock the document |
691 |
/** Specialized version of document that knows how to lock the document |
523 |
* for complex modifications. |
692 |
* for complex modifications. |