1 19 20 package org.netbeans.editor; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Element ; 26 import javax.swing.text.JTextComponent ; 27 import javax.swing.text.Position ; 28 import javax.swing.text.View ; 29 import javax.swing.text.ViewFactory ; 30 import org.netbeans.api.editor.fold.Fold; 31 import org.netbeans.editor.Utilities; 32 import org.netbeans.lib.editor.view.GapMultiLineView; 33 34 40 41 class FoldMultiLineView extends GapMultiLineView { 42 43 48 private List foldAndEndLineElemList; 49 50 FoldMultiLineView(Element firstLineElement, List foldAndEndLineElemList) { 51 super(firstLineElement); 52 this.foldAndEndLineElemList = foldAndEndLineElemList; 53 54 int foldAndEndLineElemListSize = foldAndEndLineElemList.size(); 55 58 setLastLineElement((Element )foldAndEndLineElemList.get( 59 foldAndEndLineElemList.size() - 1)); 60 } 61 62 private JTextComponent getTextComponent() { 63 return (JTextComponent )getContainer(); 64 } 65 66 protected boolean useCustomReloadChildren() { 67 return true; 68 } 69 70 protected void reloadChildren(int index, int removeLength, int startOffset, int endOffset) { 71 74 index = 0; 76 removeLength = getViewCount(); 77 78 Element lineElem = getElement(); View [] added = null; 80 ViewFactory f = getViewFactory(); 81 if (f != null) { 82 int lineElemEndOffset = lineElem.getEndOffset(); 83 int lastViewEndOffset = lineElem.getStartOffset(); 86 87 int cnt = foldAndEndLineElemList.size(); 88 List childViews = new ArrayList (); 89 for (int i = 0; i < cnt; i++) { 90 Fold fold = (Fold)foldAndEndLineElemList.get(i); 91 int foldStartOffset = fold.getStartOffset(); 92 int foldEndOffset = fold.getEndOffset(); 93 94 BaseDocument doc = (BaseDocument) lineElem.getDocument(); 95 try { 96 if (foldStartOffset > lastViewEndOffset) { View lineView = f.create(lineElem); View intraFrag = lineView.createFragment(lastViewEndOffset, foldStartOffset); 99 childViews.add(intraFrag); 100 lastViewEndOffset = foldStartOffset; 101 } 102 103 Position viewStartPos = doc.createPosition(foldStartOffset); 105 Position viewEndPos = doc.createPosition(foldEndOffset); 106 CollapsedView collapsedView = new CollapsedView(lineElem, 107 viewStartPos, viewEndPos, fold.getDescription()); 108 childViews.add(collapsedView); 109 lastViewEndOffset = foldEndOffset; 110 111 } catch (BadLocationException e) { 112 throw new IllegalStateException ("Failed to create view boundary positions"); } 114 115 i++; 117 lineElem = (Element )foldAndEndLineElemList.get(i); 118 lineElemEndOffset = lineElem.getEndOffset(); 119 } 120 121 if (lastViewEndOffset < lineElemEndOffset) { View lineView = f.create(lineElem); 125 View endingFrag = lineView.createFragment(lastViewEndOffset, lineElemEndOffset); 126 childViews.add(endingFrag); 127 } 129 130 added = new View [childViews.size()]; 131 childViews.toArray(added); 132 } 133 134 135 replace(index, removeLength, added); 136 } 137 138 } 139 | Popular Tags |