EADS-3749 encountering issues with the displaying of code in Developer Studio when code folding is enabled
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
27 * If you wish your version of this file to be governed by only the CDDL
28 * or only the GPL Version 2, indicate your decision by adding
29 * "[Contributor] elects to include this software in this distribution
30 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31 * single choice of license, a recipient has the option to distribute
32 * your version of this file under either the CDDL, the GPL Version 2 or
33 * to extend the choice of license to its licensees as provided above.
34 * However, if you add GPL Version 2 code and therefore, elected the GPL
35 * Version 2 license, then the option applies only if the new code is
36 * made subject to such option by the copyright holder.
40 * Portions Copyrighted 2011 Sun Microsystems, Inc.
42 package org.netbeans.modules.plsql.fold;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.Comparator;
47 import java.util.List;
48 import java.util.Observable;
49 import java.util.Observer;
50 import java.util.logging.Level;
51 import java.util.logging.Logger;
52 import javax.swing.event.DocumentEvent;
53 import javax.swing.text.AbstractDocument;
54 import javax.swing.text.BadLocationException;
55 import javax.swing.text.Document;
56 import org.netbeans.api.editor.fold.Fold;
57 import org.netbeans.api.editor.fold.FoldHierarchy;
58 import org.netbeans.api.editor.fold.FoldType;
59 import org.netbeans.editor.BaseDocument;
60 import org.netbeans.modules.plsql.lexer.PlsqlBlock;
61 import org.netbeans.modules.plsql.lexer.PlsqlBlockFactory;
62 import org.netbeans.modules.plsql.lexer.PlsqlBlockType;
63 import org.netbeans.modules.plsqlsupport.options.OptionsUtilities;
64 import org.netbeans.spi.editor.fold.FoldHierarchyTransaction;
65 import org.netbeans.spi.editor.fold.FoldManager;
66 import org.netbeans.spi.editor.fold.FoldOperation;
67 import org.openide.util.Exceptions;
68 import org.openide.util.Lookup;
69 import org.openide.util.RequestProcessor;
75 public class NewPlsqlFoldManager implements FoldManager, Runnable, Observer {
77 private static final Logger LOG = Logger.getLogger(NewPlsqlFoldManager.class.getName());
78 private static final RequestProcessor RP = new RequestProcessor(NewPlsqlFoldManager.class.getName(), 1, false, false);
79 private static final int TASK_DELAY = 100;
80 private final RequestProcessor.Task task = RP.create(this);
81 private FoldOperation operation;
83 // Note: FoldSearchObject need to be in a List, otherwise contains doesn't work. Seems HashSet keeps internal list of hash.
84 private final List<FoldSearchObject> foldSearchObjects = new ArrayList<FoldSearchObject>();
85 private List<Fold> removedFoldList = new ArrayList<Fold>(3);
86 private boolean initial = true;
87 private PlsqlBlockFactory blockFactory;
90 public void init(FoldOperation operation) {
91 this.operation = operation;
92 if (LOG.isLoggable(Level.FINE)) {
93 LOG.log(Level.FINE, "Initialized: {0}", System.identityHashCode(this));
97 private FoldOperation getOperation() {
102 public void initFolds(FoldHierarchyTransaction transaction) {
103 doc = getOperation().getHierarchy().getComponent().getDocument();
104 blockFactory = getBlockFactory();
105 if (blockFactory != null) {
106 blockFactory.addObserver(this);
108 task.schedule(TASK_DELAY);
112 public void insertUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
113 if (LOG.isLoggable(Level.FINER)) {
114 LOG.log(Level.FINER, "insertUpdate: {0}", System.identityHashCode(this));
116 processRemovedFolds(transaction);
120 public void removeUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
121 if (LOG.isLoggable(Level.FINER)) {
122 LOG.log(Level.FINER, "removeUpdate: {0}", System.identityHashCode(this));
124 processRemovedFolds(transaction);
128 public void changedUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
129 if (LOG.isLoggable(Level.FINER)) {
130 LOG.log(Level.FINER, "changeUpdate: {0}", System.identityHashCode(this));
132 processRemovedFolds(transaction);
136 public void removeEmptyNotify(Fold emptyFold) {
137 if (LOG.isLoggable(Level.FINER)) {
138 LOG.log(Level.FINER, "removeEmptyNotify: {0}", System.identityHashCode(this));
140 removeFoldNotify(emptyFold);
144 public void removeDamagedNotify(Fold damagedFold) {
145 if (LOG.isLoggable(Level.FINER)) {
146 LOG.log(Level.FINER, "removeDamagedNotify: {0}", System.identityHashCode(this));
148 removeFoldNotify(damagedFold);
152 public void expandNotify(Fold expandedFold) {
156 public void release() {
157 if (LOG.isLoggable(Level.FINE)) {
158 LOG.log(Level.FINE, "release: {0}", System.identityHashCode(this));
163 public void update(Observable o, Object arg) {
164 if (LOG.isLoggable(Level.FINER)) {
165 LOG.log(Level.FINER, "update: {0}", System.identityHashCode(this));
167 task.schedule(TASK_DELAY);
172 if (operation.isReleased()) {
173 if (LOG.isLoggable(Level.FINE)) {
174 LOG.log(Level.FINE, "Update skipped, already released: {0}", System.identityHashCode(this));
178 ((BaseDocument) doc).readLock();
180 if (blockFactory != null) {
181 FoldHierarchy hierarchy = getOperation().getHierarchy();
185 if (operation.isReleased()) {
186 if (LOG.isLoggable(Level.FINE)) {
187 LOG.log(Level.FINE, "Update skipped, already released: {0}", System.identityHashCode(this));
191 if (LOG.isLoggable(Level.FINE)) {
192 LOG.log(Level.FINE, "Updating: {0}", System.identityHashCode(this));
193 LOG.log(Level.FINE, "blockFactory.getBlockHierarchy().size(): {0}", blockFactory.getBlockHierarchy().size());
194 LOG.log(Level.FINE, "blockFactory.getNewBlocks().size(): {0}", blockFactory.getNewBlocks().size());
195 LOG.log(Level.FINE, "blockFactory.getRemovedBlocks().size(): {0}", blockFactory.getRemovedBlocks().size());
197 FoldHierarchyTransaction transaction = getOperation().openTransaction();
200 final Fold root = hierarchy.getRootFold();
203 List<PlsqlBlock> blocks = blockFactory.getBlockHierarchy();
204 updateFolds(blocks, transaction, null);
205 //Add custom fold blocks
206 updateFolds(blockFactory.getCustomFolds(), transaction, null);
209 final List<FoldSearchObject> collapsedFolds = new ArrayList<FoldSearchObject>();
210 getCollapsedFolds(root, collapsedFolds);
211 final List<PlsqlBlock> oldBlocks = blockFactory.getRemovedBlocks();
213 //Remove non existing blocks
214 if (!oldBlocks.isEmpty()) {
215 final int childCount = root.getFoldCount();
216 for (int i = (childCount - 1); i >= 0; i--) {
217 final Fold child = root.getFold(i);
218 removeWithChildren(child, oldBlocks, transaction);
221 //Add new blocks to the hierarchy
222 List<PlsqlBlock> blocks = blockFactory.getNewBlocks();
223 updateFolds(blocks, transaction, collapsedFolds);
224 //Add custom fold blocks
225 updateFolds(blockFactory.getCustomFolds(), transaction, collapsedFolds);
227 } catch (BadLocationException ex) {
228 Exceptions.printStackTrace(ex);
230 transaction.commit();
237 ((BaseDocument) doc).readUnlock();
241 private void getCollapsedFolds(final Fold parent, final List<FoldSearchObject> foldInfoLst) {
242 if (parent.isCollapsed()) {
243 final FoldSearchObject tempInfo = new FoldSearchObject(parent.getStartOffset(), parent.getEndOffset(), parent.getType());
244 foldInfoLst.add(tempInfo);
246 final int count = parent.getFoldCount();
247 for (int i = 0; i < count; i++) {
248 final Fold temp = parent.getFold(i);
249 getCollapsedFolds(temp, foldInfoLst);
254 * Remove fold with its children
259 * @return true if removed all the children
261 private void removeWithChildren(final Fold fold, final List<PlsqlBlock> blockHier, final FoldHierarchyTransaction transaction) {
263 final int childCount = fold.getFoldCount();
264 for (int i = (childCount - 1); i >= 0; i--) {
265 final Fold child = fold.getFold(i);
266 removeWithChildren(child, blockHier, transaction);
269 //If a custom fold remove
270 if (fold.getType() == PlsqlFoldTypes.CUSTOM || checkExists(fold, blockHier)) {
271 operation.removeFromHierarchy(fold, transaction);
272 foldSearchObjects.remove(new FoldSearchObject(new FoldAdapter(fold)));
277 * Method that will check whether given fold exists in block hier
283 private boolean checkExists(final Fold fold, final List<PlsqlBlock> blockHier) {
284 final Comparator<PlsqlBlock> comparator = new Comparator<PlsqlBlock>() {
286 public int compare(final PlsqlBlock o1, final PlsqlBlock o2) {
287 Integer o1pos, o2pos;
288 if (o1.getStartOffset() > -1 && o2.getStartOffset() > -1) {
289 o1pos = Integer.valueOf(o1.getStartOffset());
290 o2pos = Integer.valueOf(o2.getStartOffset());
292 o1pos = Integer.valueOf(o1.getEndOffset());
293 o2pos = Integer.valueOf(o2.getEndOffset());
295 return o1pos.compareTo(o2pos);
298 return checkExists(fold, blockHier, comparator);
302 * Method that will check whether given fold exists in block hier
309 private boolean checkExists(final Fold fold, final List<PlsqlBlock> blockHier, final Comparator<PlsqlBlock> comparator) {
310 final int size = blockHier.size();
311 Collections.sort(blockHier, comparator);
315 //make sure that the fold isn't before the first block or after the last block in the hierarchy.
316 if (fold.getStartOffset() > blockHier.get(size - 1).getEndOffset()
317 || fold.getEndOffset() < blockHier.get(0).getStartOffset()) {
320 for (int i = 0; i < size; i++) {
321 final PlsqlBlock block = blockHier.get(i);
322 if (block.getStartOffset() <= fold.getStartOffset() && block.getEndOffset() >= fold.getEndOffset()) {
325 if (block.getPreviousStart() <= fold.getStartOffset() && block.getPreviousEnd() >= fold.getEndOffset()) {
328 if ((block.getEndOffset() == fold.getEndOffset() || fold.getEndOffset() == block.getPreviousEnd()
329 || block.getStartOffset() == fold.getStartOffset() || block.getPreviousStart() == fold.getStartOffset())
330 && (getFoldType(block.getType()).equals(fold.getType()))
331 && (getFoldDescription(block).equals(fold.getDescription()))) {
335 if (checkExists(fold, block.getChildBlocks(), comparator)) {
343 private FoldType getFoldType(final PlsqlBlockType blockType) {
346 return PlsqlFoldTypes.VIEW;
348 return PlsqlFoldTypes.TABLECOMMENT;
350 return PlsqlFoldTypes.COLUMNCOMMENT;
352 return PlsqlFoldTypes.COMMENT;
354 return PlsqlFoldTypes.PACKAGE;
356 return PlsqlFoldTypes.PACKAGEBODY;
358 return PlsqlFoldTypes.PROCEDUREIMPL;
360 return PlsqlFoldTypes.FUNCTIONIMPL;
362 return PlsqlFoldTypes.PROCEDUREDEF;
364 return PlsqlFoldTypes.FUNCTIONDEF;
366 return PlsqlFoldTypes.DECLAREEND;
368 return PlsqlFoldTypes.BEGINEND;
370 return PlsqlFoldTypes.TRIGGER;
372 return PlsqlFoldTypes.IF;
374 return PlsqlFoldTypes.CASE;
376 return PlsqlFoldTypes.WHILELOOP;
378 return PlsqlFoldTypes.FORLOOP;
380 return PlsqlFoldTypes.LOOP;
382 return PlsqlFoldTypes.CUSTOM;
384 return PlsqlFoldTypes.STATEMENT;
386 return PlsqlFoldTypes.CURSOR;
388 return PlsqlFoldTypes.JAVASOURCE;
395 * Method that will return the fold description given the Plsql block Used
396 * when changing the descriptions only.
401 private String getFoldDescription(final PlsqlBlock block) {
402 switch (block.getType()) {
404 return block.getPrefix() + "VIEW " + block.getName();
406 return "COMMENT ON TABLE " + block.getName();
408 return "COLUMN COMMENTS ON TABLE " + block.getName();
410 return block.getName();
412 return block.getPrefix() + "PACKAGE " + block.getName();
414 return block.getPrefix() + "PACKAGE BODY " + block.getName();
416 return block.getPrefix() + "PROCEDURE IMPLEMENTATION " + block.getName();
418 return block.getPrefix() + "FUNCTION IMPLEMENTATION " + block.getName();
420 return "PROCEDURE DEFINITION " + block.getName();
422 return "FUNCTION DEFINITION " + block.getName();
424 return "DECLARE BLOCK";
426 return "BEGIN BLOCK";
428 return block.getPrefix() + "TRIGGER " + block.getName();
430 return block.getName();
432 return block.getName();
434 return "WHILE " + block.getName();
436 return "FOR " + block.getName();
440 return block.getName();
442 return block.getPrefix() + block.getName();
444 return "CURSOR " + block.getName();
446 return block.getPrefix() + "JAVA SOURCE";
452 private void removeFoldNotify(Fold removedFold) {
453 removedFoldList.add(removedFold);
456 private void processRemovedFolds(FoldHierarchyTransaction transaction) {
457 for (Fold removedFold : removedFoldList) {
458 operation.removeFromHierarchy(removedFold, transaction);
459 if (LOG.isLoggable(Level.FINE)) {
460 LOG.log(Level.FINE, "Fold={0} removed={1}", new Object[]{removedFold, true});
463 removedFoldList.clear();
466 private Document getDocument() {
468 for (int i = 0; i < 10; i++) {
469 obj = getOperation().getHierarchy().getComponent().getDocument();
470 if (obj instanceof AbstractDocument) {
471 return (Document) obj;
474 Thread.currentThread().sleep(i);
475 } catch (InterruptedException e) {
478 throw new IllegalStateException("[PLSQLFolding] PLSQLFoldManager.getDocument() NOT returned AbstractDocument, but " + obj.getClass() + "!. This is caused by not yet resolved issue #49497."); //NOI18N
481 private void updateFolds(final List<PlsqlBlock> blockHier, final FoldHierarchyTransaction transaction, final List<FoldSearchObject> collapsedFolds) throws BadLocationException {
482 final int count = blockHier.size();
483 Document doc = getDocument();
484 for (int i = 0; i < count; i++) {
485 final PlsqlBlock block = blockHier.get(i);
486 FoldType foldType = null;
487 final PlsqlBlockType type = block.getType();
488 String description = "";
490 if (!(type == PlsqlBlockType.COMMENT && doc.getText(block.getStartOffset(), block.getEndOffset() - block.getStartOffset()).indexOf("\n") == -1)) { // check for single line comments
491 foldType = getFoldType(type);
492 description = getFoldDescription(block);
494 if (doc.getEndPosition().getOffset() >= block.getEndOffset()) {
495 operation.addToHierarchy(foldType, description, isCollapsed(block, foldType, collapsedFolds),
496 block.getStartOffset(), block.getEndOffset(), 0, 0, null, transaction);
497 //check for any child folds and add them also
498 updateFolds(block.getChildBlocks(), transaction, collapsedFolds);
505 * Method that will select and return the corresponding fold to parent from
506 * oldRoot fold hierarchy
512 private boolean isCollapsed(final PlsqlBlock block, final FoldType foldType, final List<FoldSearchObject> foldInfoLst) {
513 if (OptionsUtilities.isPlSqlExpandFolds()) {
516 if (foldInfoLst == null) {
517 return foldType == PlsqlFoldTypes.COMMENT;
519 final int size = foldInfoLst.size();
520 for (int i = 0; i < size; i++) {
521 final FoldSearchObject temp = foldInfoLst.get(i);
523 if ((temp.getFoldType() == foldType)
524 && (temp.getStartOffset() == block.getPreviousStart())
525 && (temp.getEndOffset() == block.getPreviousEnd())) {
534 * Method that will return the relevant block factory
538 private PlsqlBlockFactory getBlockFactory() {
539 final Object obj = doc.getProperty(Document.StreamDescriptionProperty);
540 if (obj instanceof Lookup.Provider) {
541 return ((Lookup.Provider) obj).getLookup().lookup(PlsqlBlockFactory.class);