alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.spi.editor.fold;

import javax.swing.text.BadLocationException;
import org.netbeans.api.editor.fold.Fold;
import org.netbeans.api.editor.fold.FoldHierarchy;
import org.netbeans.api.editor.fold.FoldType;
import org.netbeans.modules.editor.fold.FoldHierarchyTransactionImpl;
import org.netbeans.modules.editor.fold.FoldOperationImpl;
import org.netbeans.modules.editor.fold.SpiPackageAccessor;


/**
 * Fold operation represents services
 * provided to an individual fold manager.
 * 
* Each manager has its own dedicated instance * of fold operation. * *

* There are three main services - creation of a new fold * and adding or removing it from the hierarchy. *
* Adding and removing of the folds requires a valid transaction * that can be obtained by {@link #openTransaction()}. * * @author Miloslav Metelka * @version 1.00 */ public final class FoldOperation { private static boolean spiPackageAccessorRegistered; static { ensureSpiAccessorRegistered(); } private static void ensureSpiAccessorRegistered() { if (!spiPackageAccessorRegistered) { spiPackageAccessorRegistered = true; SpiPackageAccessor.register(new SpiPackageAccessorImpl()); } } private final FoldOperationImpl impl; private FoldOperation(FoldOperationImpl impl) { this.impl = impl; } /** * Create new fold instance and add it to the hierarchy. *
* The fold will either become part of the hierarchy directly or it will * become blocked by another fold already present in the hierarchy. *
* Once the blocking fold gets removed this fold will be phyiscally added * to the hierarchy automatically. * *

* The fold is logically bound to the fold manager that uses this fold operation. *
* The fold can only be removed by this fold operation. * * @param type type of the fold to be assigned to the fold. * @param description textual description of the fold that will be displayed * once the fold becomes collapsed. * @param collapsed whether the fold should initially be collapsed or expanded. * @param startOffset starting offset of the fold. The fold creates swing position * for the offset. * @param endOffset ending offset of the fold. The fold creates swing position * for the offset. * @param startGuardedLength >=0 initial guarded area of the fold (starting at the start offset). * If the guarded area is modified the fold will be removed automatically. * @param endGuardedLength >=0 ending guarded area of the fold (ending at the end offset). * If the guarded area is modified the fold will be removed automatically. * @param extraInfo arbitrary extra information specific for the fold being created. * It's not touched or used by the folding infrastructure in any way. * null can be passed if there is no extra information. *
* The extra info of the existing fold can be obtained by * {@link #getExtraInfo(org.netbeans.api.editor.fold.Fold)}. * * @return new fold instance that was added to the hierarchy. */ public Fold addToHierarchy(FoldType type, String description, boolean collapsed, int startOffset, int endOffset, int startGuardedLength, int endGuardedLength, Object extraInfo, FoldHierarchyTransaction transaction) throws BadLocationException { Fold fold = impl.createFold(type, description, collapsed, startOffset, endOffset, startGuardedLength, endGuardedLength, extraInfo ); impl.addToHierarchy(fold, transaction.getImpl()); return fold; } /** * This static method can be used to check whether the bounds * of the fold that is planned to be added are valid. *
* The conditions are:

     *  startOffset < endOffset
     * 
* *
     *  startGuardedLength >= 0
     * 
* *
     *  endGuardedLength >= 0
     * 
* *
     *  startOffset + startGuardedLength <= endOffset - endGuardedLength
     * 
* * @return true if the bounds are OK or false otherwise. */ public static boolean isBoundsValid(int startOffset, int endOffset, int startGuardedLength, int endGuardedLength) { return (startOffset < endOffset) && (startGuardedLength >= 0) && (endGuardedLength >= 0) && ((startOffset + startGuardedLength) <= (endOffset -endGuardedLength)); } /** * Remove the fold that is either present in the hierarchy or blocked * by another fold. * * @param fold fold to be removed * @param transaction non-null transaction under which the fold should be removed. */ public void removeFromHierarchy(Fold fold, FoldHierarchyTransaction transaction) { impl.removeFromHierarchy(fold, transaction.getImpl()); } /** * Return extra info object passed to fold at time of its creation. * * @return extra information object specific for the fold * or null if there was no extra info. */ public Object getExtraInfo(Fold fold) { return impl.getExtraInfo(fold); } /** * Check whether the starting guarded area of the fold * is damaged by a document modification. * * @param fold fold to check. The fold must be managed by this fold operation. * @return true if the starting area of the fold was damaged by the modification * or false otherwise. */ public boolean isStartDamaged(Fold fold) { return impl.isStartDamaged(fold); } /** * Check whether the ending guarded area of the fold * is damaged by a document modification. * * @param fold fold to check. The fold must be managed by this fold operation. * @return true if the ending area of the fold was damaged by the modification * or false otherwise. */ public boolean isEndDamaged(Fold fold) { return impl.isEndDamaged(fold); } /** * Open a new transaction over the fold hierarchy. *
* Note: Always use the following pattern: *
     *     FoldHierarchyTransaction transaction = operation.openTransaction();
     *     try {
     *         ...
     *     } finally {
     *         transaction.commit();
     *     }
     * 
* * @return opened transaction for further use. */ public FoldHierarchyTransaction openTransaction() { return impl.openTransaction().getTransaction(); } /** * Check whether the fold is currently present in the hierarchy or blocked. * * @return true if the fold is currently present in the hierarchy or blocked * or false otherwise. */ public boolean isAddedOrBlocked(Fold fold) { return impl.isAddedOrBlocked(fold); } /** * Is the given fold blocked by another fold? */ public boolean isBlocked(Fold fold) { return impl.isBlocked(fold); } /** * Get the hierarchy for which this fold operations works. */ public FoldHierarchy getHierarchy() { return impl.getHierarchy(); } public boolean isReleased() { return impl.isReleased(); } private static final class SpiPackageAccessorImpl extends SpiPackageAccessor { public FoldHierarchyTransaction createFoldHierarchyTransaction( FoldHierarchyTransactionImpl impl) { return new FoldHierarchyTransaction(impl); } public FoldHierarchyTransactionImpl getImpl(FoldHierarchyTransaction transaction) { return transaction.getImpl(); } public FoldOperation createFoldOperation(FoldOperationImpl impl) { return new FoldOperation(impl); } } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.