1 19 20 package code_folding; 21 22 import java.awt.event.KeyEvent ; 23 import javax.swing.text.AbstractDocument ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.JTextComponent ; 26 import org.netbeans.jellytools.Bundle; 27 import org.netbeans.jellytools.EditorOperator; 28 import org.netbeans.jellytools.EditorWindowOperator; 29 import lib.EditorTestCase; 30 import org.netbeans.api.editor.fold.Fold; 31 import org.netbeans.api.editor.fold.FoldHierarchy; 32 import org.netbeans.api.editor.fold.FoldUtilities; 33 import org.netbeans.jemmy.operators.JEditorPaneOperator; 34 import org.netbeans.jemmy.operators.JTextComponentOperator; 35 36 43 public class CodeFoldingTest extends EditorTestCase { 44 45 private final int COLLAPSE_EXPAND_DELAY = 200; 46 47 48 public CodeFoldingTest(String testMethodName) { 49 super(testMethodName); 50 } 51 52 protected static void appendSpaces(StringBuffer sb, int spaces) { 53 while (--spaces >= 0) { 54 sb.append(' '); 55 } 56 } 57 58 protected static String foldToStringChildren(Fold fold, int indent) { 59 indent += 4; 60 StringBuffer sb = new StringBuffer (); 61 sb.append(fold); 62 sb.append('\n'); 63 int foldCount = fold.getFoldCount(); 64 for (int i = 0; i < foldCount; i++) { 65 appendSpaces(sb, indent); 66 sb.append('['); 67 sb.append(i); 68 sb.append("]: "); sb.append(foldToStringChildren(fold.getFold(i), indent)); 70 } 71 72 return sb.toString(); 73 } 74 75 76 public static String foldHierarchyToString(JTextComponent target){ 77 String ret = ""; 78 AbstractDocument adoc = (AbstractDocument )target.getDocument(); 79 80 FoldHierarchy hierarchy = FoldHierarchy.get(target); 82 adoc.readLock(); 83 try { 84 hierarchy.lock(); 85 try { 86 Fold root = hierarchy.getRootFold(); 87 ret = (root == null) ? "root is null" : foldToStringChildren(root, 0); } finally { 89 hierarchy.unlock(); 90 } 91 } finally { 92 adoc.readUnlock(); 93 } 94 return ret; 95 } 96 97 protected void waitForFolding(JTextComponent target, int maxMiliSeconds){ 98 int time = (int) maxMiliSeconds / 100; 100 101 AbstractDocument adoc = (AbstractDocument )target.getDocument(); 102 103 FoldHierarchy hierarchy = FoldHierarchy.get(target); 105 int foldCount = 0; 106 while (foldCount==0 && time > 0) { 107 108 adoc.readLock(); 109 try { 110 hierarchy.lock(); 111 try { 112 foldCount = hierarchy.getRootFold().getFoldCount(); 113 } finally { 114 hierarchy.unlock(); 115 } 116 } finally { 117 adoc.readUnlock(); 118 } 119 120 try { 121 Thread.currentThread().sleep(100); 122 } catch (InterruptedException ex) { 123 time=0; 124 } 125 time--; 126 127 } 128 129 } 130 131 private ValueResolver getResolver(final JTextComponent target){ 132 133 ValueResolver foldValueResolver = new ValueResolver(){ 134 public Object getValue(){ 135 FoldHierarchy hierarchy = FoldHierarchy.get(target); 136 int dot = target.getCaret().getDot(); 137 hierarchy.lock(); 138 try{ 139 try{ 140 int rowStart = javax.swing.text.Utilities.getRowStart(target, dot); 141 int rowEnd = javax.swing.text.Utilities.getRowEnd(target, dot); 142 Fold fold = getLineFold(hierarchy, dot, rowStart, rowEnd); 143 if (fold!=null){ 144 return Boolean.valueOf(fold.isCollapsed()); 145 }else{ 146 return null; 147 } 148 }catch(BadLocationException ble){ 149 ble.printStackTrace(); 150 } 151 }finally { 152 hierarchy.unlock(); 153 } 154 return null; 155 } 156 }; 157 158 return foldValueResolver; 159 } 160 161 protected void collapseFoldAtCaretPosition(EditorOperator editor, int line, int column){ 162 editor.setCaretPosition(line, column); 164 165 JEditorPaneOperator txtOper = editor.txtEditorPane(); 167 txtOper.pushKey(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK); 168 169 JTextComponentOperator text = new JTextComponentOperator(editor); 170 JTextComponent target = (JTextComponent )text.getSource(); 171 172 waitMaxMilisForValue(500, getResolver(target), Boolean.TRUE); 174 175 } 176 177 protected void expandFoldAtCaretPosition(EditorOperator editor, int line, int column){ 178 editor.setCaretPosition(line, column); 180 181 JEditorPaneOperator txtOper = editor.txtEditorPane(); 183 txtOper.pushKey(KeyEvent.VK_ADD, KeyEvent.CTRL_DOWN_MASK); 184 185 JTextComponentOperator text = new JTextComponentOperator(editor); 186 JTextComponent target = (JTextComponent )text.getSource(); 187 188 waitMaxMilisForValue(500, getResolver(target), Boolean.FALSE); 190 } 191 192 protected void collapseAllFolds(EditorOperator editor){ 193 JEditorPaneOperator txtOper = editor.txtEditorPane(); 195 txtOper.pushKey(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK); 196 197 try { 199 Thread.currentThread().sleep(COLLAPSE_EXPAND_DELAY); 200 } catch (InterruptedException ex) { 201 } 202 } 203 204 protected void expandAllFolds(EditorOperator editor){ 205 JEditorPaneOperator txtOper = editor.txtEditorPane(); 207 txtOper.pushKey(KeyEvent.VK_ADD, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK); 208 209 try { 211 Thread.currentThread().sleep(COLLAPSE_EXPAND_DELAY); 212 } catch (InterruptedException ex) { 213 } 214 } 215 216 223 private static Fold getLineFold(FoldHierarchy hierarchy, int dot, int lineStart, int lineEnd){ 224 Fold caretOffsetFold = FoldUtilities.findOffsetFold(hierarchy, dot); 225 226 Fold fold = FoldUtilities.findNearestFold(hierarchy, lineStart); 228 229 while (fold!=null && 230 (fold.getEndOffset()<=dot || (!fold.isCollapsed() && fold.getFoldCount() > 0 && fold.getStartOffset()+1<dot) 234 ) 235 ){ 236 237 Fold nextFold = FoldUtilities.findNearestFold(hierarchy, 239 (fold.getFoldCount()>0) ? fold.getStartOffset()+1 : fold.getEndOffset()); 240 if (nextFold!=null && nextFold.getStartOffset()<lineEnd){ 241 if (nextFold == fold) return fold; 242 fold = nextFold; 243 }else{ 244 break; 245 } 246 } 247 248 249 if (fold == null || fold.getStartOffset()>lineEnd) { 251 252 if (caretOffsetFold == null){ 257 caretOffsetFold = FoldUtilities.findOffsetFold(hierarchy, lineStart); 258 } 259 260 return caretOffsetFold; 261 } 262 263 if (caretOffsetFold == null) return fold; 265 266 if (caretOffsetFold.isCollapsed()) return caretOffsetFold; 268 269 if ( caretOffsetFold.getEndOffset()>fold.getEndOffset() && 274 fold.getEndOffset()>dot){ 275 return fold; 276 } 277 278 if (fold.getStartOffset()>caretOffsetFold.getEndOffset()) return caretOffsetFold; 282 283 if (fold.getEndOffset()<dot) return caretOffsetFold; 287 288 return fold; 289 } 290 291 } 292 | Popular Tags |