Lines 79-84
Link Here
|
79 |
import javax.swing.text.Document; |
79 |
import javax.swing.text.Document; |
80 |
import javax.swing.text.EditorKit; |
80 |
import javax.swing.text.EditorKit; |
81 |
import javax.swing.text.StyledDocument; |
81 |
import javax.swing.text.StyledDocument; |
|
|
82 |
import org.netbeans.api.actions.Openable; |
82 |
import org.netbeans.api.annotations.common.NullAllowed; |
83 |
import org.netbeans.api.annotations.common.NullAllowed; |
83 |
import org.netbeans.api.queries.FileEncodingQuery; |
84 |
import org.netbeans.api.queries.FileEncodingQuery; |
84 |
import org.netbeans.modules.openide.loaders.DataObjectAccessor; |
85 |
import org.netbeans.modules.openide.loaders.DataObjectAccessor; |
Lines 86-91
Link Here
|
86 |
import org.openide.DialogDisplayer; |
87 |
import org.openide.DialogDisplayer; |
87 |
import org.openide.NotifyDescriptor; |
88 |
import org.openide.NotifyDescriptor; |
88 |
import org.openide.cookies.EditorCookie; |
89 |
import org.openide.cookies.EditorCookie; |
|
|
90 |
import org.openide.cookies.LineCookie; |
89 |
import org.openide.cookies.OpenCookie; |
91 |
import org.openide.cookies.OpenCookie; |
90 |
import org.openide.filesystems.FileAttributeEvent; |
92 |
import org.openide.filesystems.FileAttributeEvent; |
91 |
import org.openide.filesystems.FileChangeAdapter; |
93 |
import org.openide.filesystems.FileChangeAdapter; |
Lines 121-126
Link Here
|
121 |
* Support for associating an editor and a Swing {@link Document} to a data object. |
123 |
* Support for associating an editor and a Swing {@link Document} to a data object. |
122 |
* @author Jaroslav Tulach |
124 |
* @author Jaroslav Tulach |
123 |
*/ |
125 |
*/ |
|
|
126 |
@NbBundle.Messages("TXT_Question=Question") |
124 |
public class DataEditorSupport extends CloneableEditorSupport { |
127 |
public class DataEditorSupport extends CloneableEditorSupport { |
125 |
/** error manager for CloneableEditorSupport logging and error reporting */ |
128 |
/** error manager for CloneableEditorSupport logging and error reporting */ |
126 |
static final Logger ERR = Logger.getLogger("org.openide.text.DataEditorSupport"); // NOI18N |
129 |
static final Logger ERR = Logger.getLogger("org.openide.text.DataEditorSupport"); // NOI18N |
Lines 593-598
Link Here
|
593 |
ERR.finest("openDocument - charset removed"); |
596 |
ERR.finest("openDocument - charset removed"); |
594 |
} |
597 |
} |
595 |
} |
598 |
} |
|
|
599 |
|
600 |
/** |
601 |
* Get the document associated with a file. |
602 |
* |
603 |
* <p>Method will throw {@link org.openide.util.UserQuestionException} |
604 |
* exception if file size is too big. This exception is caught and its |
605 |
* method {@link org.openide.util.UserQuestionException#confirmed} is used |
606 |
* for confirmation. |
607 |
* |
608 |
* |
609 |
* @param dobj the DataObject |
610 |
* @return {@link javax.swing.text.StyledDocument} or null |
611 |
* @since org.openide.loaders 7.34 |
612 |
*/ |
613 |
public static StyledDocument getStyledDocument(DataObject dobj) { |
614 |
try { |
615 |
EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class); |
616 |
if (ec != null) { |
617 |
StyledDocument doc = null; |
618 |
try { |
619 |
doc = ec.openDocument(); |
620 |
} catch (UserQuestionException uqe) { |
621 |
final Object value = DialogDisplayer.getDefault().notify( |
622 |
new NotifyDescriptor.Confirmation(uqe.getLocalizedMessage(), |
623 |
"#TXT_Question", |
624 |
NotifyDescriptor.YES_NO_OPTION)); |
625 |
if (value != NotifyDescriptor.YES_OPTION) { |
626 |
return null; |
627 |
} |
628 |
uqe.confirmed(); |
629 |
doc = ec.openDocument(); |
630 |
} |
631 |
return doc; |
632 |
} |
633 |
} catch (IOException ioe) { |
634 |
Logger.getLogger(DataEditorSupport.class.getName()).log(Level.WARNING, null, ioe); |
635 |
} |
636 |
return null; |
637 |
} |
638 |
|
639 |
/** |
640 |
* Open the document associated with a file in the Editor window in a |
641 |
* position specified by the offset while controlling open and visibility behavior. |
642 |
* |
643 |
* @param dobj the DataObject |
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(DataObject dobj, int offset, Line.ShowOpenType openType, Line.ShowVisibilityType visibilityType) { |
659 |
assert dobj != null; |
660 |
LineCookie lc = dobj.getLookup().lookup(LineCookie.class); |
661 |
if ((lc != null) && (offset != -1)) { |
662 |
StyledDocument doc = getStyledDocument(dobj); |
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 |
l.show(openType, visibilityType, column); |
677 |
return true; |
678 |
} |
679 |
} |
680 |
} |
681 |
|
682 |
Openable oc = dobj.getLookup().lookup(Openable.class); |
683 |
|
684 |
if (oc != null) { |
685 |
oc.open(); |
686 |
return true; |
687 |
} |
688 |
return false; |
689 |
} |
690 |
|
691 |
/** |
692 |
* Open the document associated with a file in the Editor window in a |
693 |
* position specified by the line and column while controlling open and visibility behavior. |
694 |
* |
695 |
* @param dobj the DataObject |
696 |
* @param line the line the document should be opened (starting at 0) |
697 |
* @param column the column which should be selected (starting at 0), value |
698 |
* -1 does not change previously selected column |
699 |
* @param openType control open behavior, {@link org.openide.text.Line.ShowOpenType#OPEN} |
700 |
* should be used |
701 |
* @param visibilityType control visibility behavior, {@link org.openide.text.Line.ShowVisibilityType#FOCUS} |
702 |
* should be used |
703 |
* @return true if the Document is opened - false otherwise |
704 |
* @see #getStyledDocument |
705 |
* <code>UserQuestionException handling</code> |
706 |
* @see org.openide.text.Line.ShowOpenType |
707 |
* <code>ShowOpenType</code> |
708 |
* @see org.openide.text.Line.ShowVisibilityType |
709 |
* <code>ShowVisibilityType</code> |
710 |
* @since org.openide.loaders 7.34 |
711 |
*/ |
712 |
public static boolean openDocument(DataObject dobj, int line, int column, Line.ShowOpenType openType, Line.ShowVisibilityType visibilityType) { |
713 |
assert dobj != null; |
714 |
LineCookie lc = dobj.getLookup().lookup(LineCookie.class); |
715 |
if ((lc != null) && (line >= 0) && (column >= -1)) { |
716 |
StyledDocument doc = getStyledDocument(dobj); |
717 |
|
718 |
if (doc != null) { |
719 |
Line l = null; |
720 |
try { |
721 |
l = lc.getLineSet().getCurrent(line); |
722 |
} catch (IndexOutOfBoundsException e) { // try to open at least the file (line no. is too high?) |
723 |
l = lc.getLineSet().getCurrent(0); |
724 |
} |
725 |
|
726 |
if (l != null) { |
727 |
l.show(openType, visibilityType, column); |
728 |
return true; |
729 |
} |
730 |
} |
731 |
} |
732 |
|
733 |
Openable oc = dobj.getLookup().lookup(Openable.class); |
734 |
|
735 |
if (oc != null) { |
736 |
oc.open(); |
737 |
return true; |
738 |
} |
739 |
return false; |
740 |
} |
596 |
|
741 |
|
597 |
/** Saves document. Overrides superclass method, adds checking |
742 |
/** Saves document. Overrides superclass method, adds checking |
598 |
* for read-only property of saving file and warns user in that case. */ |
743 |
* for read-only property of saving file and warns user in that case. */ |