1 19 20 package org.netbeans.modules.editor.fold; 21 22 import javax.swing.event.DocumentEvent ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Document ; 25 import org.netbeans.spi.editor.fold.FoldManager; 26 import org.netbeans.api.editor.fold.Fold; 27 import org.netbeans.api.editor.fold.FoldHierarchy; 28 import org.netbeans.spi.editor.fold.FoldOperation; 29 import org.netbeans.api.editor.fold.FoldType; 30 31 32 55 56 public final class FoldOperationImpl { 57 58 private static final boolean debug 59 = Boolean.getBoolean("netbeans.debug.editor.fold"); 60 61 65 private static final boolean debugStack 66 = Boolean.getBoolean("netbeans.debug.editor.fold.stack"); 67 68 69 private FoldOperation operation; 70 71 private FoldHierarchyExecution execution; 72 73 private FoldManager manager; 74 75 private int priority; 76 77 private boolean released; 78 79 public FoldOperationImpl(FoldHierarchyExecution execution, 80 FoldManager manager, int priority) { 81 this.execution = execution; 82 this.manager = manager; 83 this.priority = priority; 84 85 this.operation = SpiPackageAccessor.get().createFoldOperation(this); 86 if (manager != null) { manager.init(getOperation()); 88 } 89 } 90 91 public FoldOperation getOperation() { 92 return operation; 93 } 94 95 public void initFolds(FoldHierarchyTransactionImpl transaction) { 96 manager.initFolds(transaction.getTransaction()); 97 } 98 99 public FoldHierarchy getHierarchy() { 100 return execution.getHierarchy(); 101 } 102 103 public FoldManager getManager() { 104 return manager; 105 } 106 107 public int getPriority() { 108 return priority; 109 } 110 111 public Document getDocument() { 112 return execution.getComponent().getDocument(); 113 } 114 115 public Fold createFold(FoldType type, String description, boolean collapsed, 116 int startOffset, int endOffset, int startGuardedLength, int endGuardedLength, 117 Object extraInfo) 118 throws BadLocationException { 119 if (debug) { 120 System.err.println("Creating fold: type=" + type + ", description='" + description + "', collapsed=" + collapsed + ", startOffset=" + startOffset + ", endOffset=" + endOffset + ", startGuardedLength=" + startGuardedLength + ", endGuardedLength=" + endGuardedLength + ", extraInfo=" + extraInfo ); 127 128 if (debugStack) { 129 Thread.dumpStack(); 130 } 131 } 132 133 return getAccessor().createFold(this, 134 type, description, collapsed, 135 getDocument(), startOffset, endOffset, 136 startGuardedLength, endGuardedLength, 137 extraInfo 138 ); 139 } 140 141 public Object getExtraInfo(Fold fold) { 142 checkFoldOperation(fold); 143 return getAccessor().foldGetExtraInfo(fold); 144 } 145 146 public boolean isStartDamaged(Fold fold) { 147 checkFoldOperation(fold); 148 return getAccessor().foldIsStartDamaged(fold); 149 } 150 151 public boolean isEndDamaged(Fold fold) { 152 checkFoldOperation(fold); 153 return getAccessor().foldIsEndDamaged(fold); 154 } 155 156 public FoldHierarchyTransactionImpl openTransaction() { 157 return execution.openTransaction(); 158 } 159 160 public boolean addToHierarchy(Fold fold, FoldHierarchyTransactionImpl transaction) { 161 checkFoldOperation(fold); 162 return execution.add(fold, transaction); 163 } 164 165 public void removeFromHierarchy(Fold fold, FoldHierarchyTransactionImpl transaction) { 166 checkFoldOperation(fold); 167 execution.remove(fold, transaction); 168 } 169 170 public boolean isAddedOrBlocked(Fold fold) { 171 checkFoldOperation(fold); 172 return execution.isAddedOrBlocked(fold); 173 } 174 175 public boolean isBlocked(Fold fold) { 176 checkFoldOperation(fold); 177 return execution.isBlocked(fold); 178 } 179 180 public void setEndOffset(Fold fold, int endOffset, FoldHierarchyTransactionImpl transaction) 181 throws BadLocationException { 182 checkFoldOperation(fold); 183 int origEndOffset = fold.getEndOffset(); 184 ApiPackageAccessor api = getAccessor(); 185 api.foldSetEndOffset(fold, getDocument(), endOffset); 186 api.foldStateChangeEndOffsetChanged(transaction.getFoldStateChange(fold), origEndOffset); 187 } 188 189 public void insertUpdate(DocumentEvent evt, FoldHierarchyTransactionImpl transaction) { 190 manager.insertUpdate(evt, transaction.getTransaction()); 191 } 192 193 public void removeUpdate(DocumentEvent evt, FoldHierarchyTransactionImpl transaction) { 194 manager.removeUpdate(evt, transaction.getTransaction()); 195 } 196 197 public void changedUpdate(DocumentEvent evt, FoldHierarchyTransactionImpl transaction) { 198 manager.changedUpdate(evt, transaction.getTransaction()); 199 } 200 201 public void release() { 202 released = true; 203 manager.release(); 204 } 205 206 public boolean isReleased() { 207 return released; 208 } 209 210 private void checkFoldOperation(Fold fold) { 211 FoldOperationImpl foldOperation = getAccessor().foldGetOperation(fold); 212 if (foldOperation != this) { 213 throw new IllegalStateException ( 214 "Attempt to use the fold " + fold + " with invalid fold operation " + foldOperation + " instead of " + this ); 218 } 219 } 220 221 private static ApiPackageAccessor getAccessor() { 222 return ApiPackageAccessor.get(); 223 } 224 225 } 226 | Popular Tags |