Lines 119-131
Link Here
|
119 |
* Support for associating an editor and a Swing {@link Document} to a data object. |
119 |
* Support for associating an editor and a Swing {@link Document} to a data object. |
120 |
* @author Jaroslav Tulach |
120 |
* @author Jaroslav Tulach |
121 |
*/ |
121 |
*/ |
122 |
@NbBundle.Messages("TXT_Question=Question") |
|
|
123 |
public class DataEditorSupport extends CloneableEditorSupport { |
122 |
public class DataEditorSupport extends CloneableEditorSupport { |
124 |
/** error manager for CloneableEditorSupport logging and error reporting */ |
123 |
/** error manager for CloneableEditorSupport logging and error reporting */ |
125 |
static final Logger ERR = Logger.getLogger("org.openide.text.DataEditorSupport"); // NOI18N |
124 |
static final Logger ERR = Logger.getLogger("org.openide.text.DataEditorSupport"); // NOI18N |
126 |
|
|
|
127 |
private static Future<Boolean> openTask; |
128 |
private static ExecutorService executor = null; |
129 |
|
125 |
|
130 |
/** Which data object we are associated with */ |
126 |
/** Which data object we are associated with */ |
131 |
private final DataObject obj; |
127 |
private final DataObject obj; |
Lines 595-794
Link Here
|
595 |
ERR.finest("openDocument - charset removed"); |
591 |
ERR.finest("openDocument - charset removed"); |
596 |
} |
592 |
} |
597 |
} |
593 |
} |
598 |
|
|
|
599 |
/** |
600 |
* Get the document associated with a file. |
601 |
* |
602 |
* <p>Method will throw {@link org.openide.util.UserQuestionException} |
603 |
* exception if file size is too big. This exception is caught and its |
604 |
* method {@link org.openide.util.UserQuestionException#confirmed} is used |
605 |
* for confirmation. |
606 |
* |
607 |
* |
608 |
* @param provider the Lookup.Provider |
609 |
* @return {@link javax.swing.text.StyledDocument} or null |
610 |
* @since org.openide.loaders 7.34 |
611 |
*/ |
612 |
public static StyledDocument getStyledDocument(Lookup.Provider provider) { |
613 |
try { |
614 |
EditorCookie ec = provider.getLookup().lookup(EditorCookie.class); |
615 |
if (ec != null) { |
616 |
StyledDocument doc = null; |
617 |
try { |
618 |
doc = ec.openDocument(); |
619 |
} catch (UserQuestionException uqe) { |
620 |
final Object value = DialogDisplayer.getDefault().notify( |
621 |
new NotifyDescriptor.Confirmation(uqe.getLocalizedMessage(), |
622 |
"#TXT_Question", |
623 |
NotifyDescriptor.YES_NO_OPTION)); |
624 |
if (value != NotifyDescriptor.YES_OPTION) { |
625 |
return null; |
626 |
} |
627 |
uqe.confirmed(); |
628 |
doc = ec.openDocument(); |
629 |
} |
630 |
return doc; |
631 |
} |
632 |
} catch (IOException ioe) { |
633 |
Logger.getLogger(DataEditorSupport.class.getName()).log(Level.WARNING, null, ioe); |
634 |
} |
635 |
return null; |
636 |
} |
637 |
|
638 |
/** |
639 |
* Open the document associated with a file in the Editor window in a |
640 |
* position specified by the offset while controlling open and visibility behavior. |
641 |
* This method can be called from any thread. |
642 |
* |
643 |
* @param provider the Lookup.Provider |
644 |
* @param offset the position the document should be opened (starting at 0) |
645 |
* @param openType control open behavior, {@link org.openide.text.Line.ShowOpenType#OPEN} |
646 |
* should be used |
647 |
* @param visibilityType control visibility behavior, {@link org.openide.text.Line.ShowVisibilityType#FOCUS} |
648 |
* should be used |
649 |
* @return true if the Document is opened - false otherwise |
650 |
* @see #getStyledDocument |
651 |
* <code>UserQuestionException handling</code> |
652 |
* @see org.openide.text.Line.ShowOpenType |
653 |
* <code>ShowOpenType</code> |
654 |
* @see org.openide.text.Line.ShowVisibilityType |
655 |
* <code>ShowVisibilityType</code> |
656 |
* @since org.openide.loaders 7.34 |
657 |
*/ |
658 |
public static boolean openDocument(Lookup.Provider provider, int offset, Line.ShowOpenType openType, Line.ShowVisibilityType visibilityType) { |
659 |
assert provider != null; |
660 |
LineCookie lc = provider.getLookup().lookup(LineCookie.class); |
661 |
if ((lc != null) && (offset != -1)) { |
662 |
StyledDocument doc = getStyledDocument(provider); |
663 |
|
664 |
if (doc != null) { |
665 |
int line = NbDocument.findLineNumber(doc, offset); |
666 |
int column = NbDocument.findLineColumn(doc, offset); |
667 |
|
668 |
Line l = null; |
669 |
try { |
670 |
l = lc.getLineSet().getCurrent(line); |
671 |
} catch (IndexOutOfBoundsException e) { // try to open at least the file (line no. is too high?) |
672 |
l = lc.getLineSet().getCurrent(0); |
673 |
} |
674 |
|
675 |
if (l != null) { |
676 |
return doShow(l, column, openType, visibilityType); |
677 |
} |
678 |
} |
679 |
} |
680 |
|
681 |
Openable oc = provider.getLookup().lookup(Openable.class); |
682 |
|
683 |
if (oc != null) { |
684 |
return doOpen(oc); |
685 |
} |
686 |
return false; |
687 |
} |
688 |
|
689 |
/** |
690 |
* Open the document associated with a file in the Editor window in a |
691 |
* position specified by the line and column while controlling open and visibility behavior. |
692 |
* This method can be called from any thread. |
693 |
* |
694 |
* @param provider the Lookup.Provider |
695 |
* @param line the line the document should be opened (starting at 0) |
696 |
* @param column the column which should be selected (starting at 0), value |
697 |
* -1 does not change previously selected column |
698 |
* @param openType control open behavior, {@link org.openide.text.Line.ShowOpenType#OPEN} |
699 |
* should be used |
700 |
* @param visibilityType control visibility behavior, {@link org.openide.text.Line.ShowVisibilityType#FOCUS} |
701 |
* should be used |
702 |
* @return true if the Document is opened - false otherwise |
703 |
* @see #getStyledDocument |
704 |
* <code>UserQuestionException handling</code> |
705 |
* @see org.openide.text.Line.ShowOpenType |
706 |
* <code>ShowOpenType</code> |
707 |
* @see org.openide.text.Line.ShowVisibilityType |
708 |
* <code>ShowVisibilityType</code> |
709 |
* @since org.openide.loaders 7.34 |
710 |
*/ |
711 |
public static boolean openDocument(Lookup.Provider provider, int line, int column, Line.ShowOpenType openType, Line.ShowVisibilityType visibilityType) { |
712 |
assert provider != null; |
713 |
LineCookie lc = provider.getLookup().lookup(LineCookie.class); |
714 |
if ((lc != null) && (line >= 0) && (column >= -1)) { |
715 |
StyledDocument doc = getStyledDocument(provider); |
716 |
|
717 |
if (doc != null) { |
718 |
Line l = null; |
719 |
try { |
720 |
l = lc.getLineSet().getCurrent(line); |
721 |
} catch (IndexOutOfBoundsException e) { // try to open at least the file (line no. is too high?) |
722 |
l = lc.getLineSet().getCurrent(0); |
723 |
} |
724 |
|
725 |
if (l != null) { |
726 |
return doShow(l, column, openType, visibilityType); |
727 |
} |
728 |
} |
729 |
} |
730 |
|
731 |
final Openable oc = provider.getLookup().lookup(Openable.class); |
732 |
|
733 |
if (oc != null) { |
734 |
return doOpen(oc); |
735 |
} |
736 |
|
737 |
return false; |
738 |
} |
739 |
|
740 |
private static ExecutorService getExecutorService () { |
741 |
if (executor == null || executor.isShutdown ()) { |
742 |
executor = Executors.newSingleThreadExecutor (); |
743 |
} |
744 |
return executor; |
745 |
} |
746 |
|
747 |
private static boolean doShow(final Line l, final int column, final Line.ShowOpenType openType, final Line.ShowVisibilityType visibilityType) { |
748 |
Callable<Boolean> callable = new Callable<Boolean>() { |
749 |
|
750 |
@Override |
751 |
public Boolean call() throws Exception { |
752 |
l.show(openType, visibilityType, column); |
753 |
return true; |
754 |
} |
755 |
}; |
756 |
boolean retval = false; |
757 |
try { |
758 |
openTask = getExecutorService().submit(callable); |
759 |
retval = openTask.get(); |
760 |
} catch (CancellationException ex) { |
761 |
ERR.log(Level.FINE, "DataEditorSupport.openDocument was cancelled ", ex); // NOI18N |
762 |
} catch (InterruptedException ex) { |
763 |
ERR.log(Level.FINE, "DataEditorSupport.openDocument was interupted ", ex); // NOI18N |
764 |
} catch (ExecutionException ex) { |
765 |
ERR.log(Level.FINE, "DataEditorSupport.openDocument exception ", ex); // NOI18N |
766 |
} |
767 |
return retval; |
768 |
} |
769 |
|
770 |
private static boolean doOpen(final Openable oc) { |
771 |
Callable<Boolean> callable = new Callable<Boolean>() { |
772 |
|
773 |
@Override |
774 |
public Boolean call() throws Exception { |
775 |
oc.open(); |
776 |
return true; |
777 |
} |
778 |
}; |
779 |
boolean retval = false; |
780 |
try { |
781 |
openTask = getExecutorService().submit(callable); |
782 |
retval = openTask.get(); |
783 |
} catch (CancellationException ex) { |
784 |
ERR.log(Level.FINE, "DataEditorSupport.openDocument was cancelled ", ex); // NOI18N |
785 |
} catch (InterruptedException ex) { |
786 |
ERR.log(Level.FINE, "DataEditorSupport.openDocument was interupted ", ex); // NOI18N |
787 |
} catch (ExecutionException ex) { |
788 |
ERR.log(Level.FINE, "DataEditorSupport.openDocument exception ", ex); // NOI18N |
789 |
} |
790 |
return retval; |
791 |
} |
792 |
|
594 |
|
793 |
/** Saves document. Overrides superclass method, adds checking |
595 |
/** Saves document. Overrides superclass method, adds checking |
794 |
* for read-only property of saving file and warns user in that case. */ |
596 |
* for read-only property of saving file and warns user in that case. */ |