nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
nsandonato | 20e2c04 | 2010-02-26 22:18:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2009, 2010 IBM Corporation and others. |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.wst.xml.ui.internal.projection; |
| 13 | |
| 14 | import org.eclipse.jface.text.Position; |
| 15 | import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
| 16 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
| 17 | import org.eclipse.wst.sse.ui.internal.projection.AbstractStructuredFoldingStrategy; |
nsandonato | 550035b | 2009-10-21 21:37:11 +0000 | [diff] [blame] | 18 | import org.eclipse.wst.xml.core.internal.document.CommentImpl; |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; |
| 20 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * A folding strategy for XML type structured documents. |
| 25 | * See AbstractStructuredFoldingStrategy for more details. |
| 26 | */ |
| 27 | public class XMLFoldingStrategy extends AbstractStructuredFoldingStrategy { |
| 28 | |
| 29 | /** |
| 30 | * Create an instance of the folding strategy. |
| 31 | * Be sure to set the viewer and document after creation. |
| 32 | */ |
| 33 | public XMLFoldingStrategy() { |
| 34 | super(); |
| 35 | } |
| 36 | |
nsandonato | 550035b | 2009-10-21 21:37:11 +0000 | [diff] [blame] | 37 | /** |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 38 | * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) |
| 39 | */ |
| 40 | protected Position calcNewFoldPosition(IndexedRegion indexedRegion) { |
| 41 | Position retPos = null; |
| 42 | |
nsandonato | 550035b | 2009-10-21 21:37:11 +0000 | [diff] [blame] | 43 | //only want to fold regions of the valid type and with a valid range |
nsandonato | 20e2c04 | 2010-02-26 22:18:54 +0000 | [diff] [blame] | 44 | if(indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) { |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 45 | IDOMNode node = (IDOMNode)indexedRegion; |
| 46 | IStructuredDocumentRegion startRegion = node.getStartStructuredDocumentRegion(); |
| 47 | IStructuredDocumentRegion endRegion = node.getEndStructuredDocumentRegion(); |
| 48 | |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 49 | //if the node has an endRegion (end tag) then folding region is |
| 50 | // between the start and end tag |
nsandonato | 550035b | 2009-10-21 21:37:11 +0000 | [diff] [blame] | 51 | //else if the region is a comment |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 52 | //else if the region is only an open tag or an open/close tag then don't fold it |
| 53 | if(startRegion != null && endRegion != null) { |
nsandonato | 550035b | 2009-10-21 21:37:11 +0000 | [diff] [blame] | 54 | retPos = new XMLElementFoldingPosition(startRegion, endRegion); |
| 55 | } else if(startRegion != null && indexedRegion instanceof CommentImpl) { |
| 56 | retPos = new XMLCommentFoldingPosition(startRegion); |
| 57 | } |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return retPos; |
| 61 | } |
| 62 | |
nsandonato | 550035b | 2009-10-21 21:37:11 +0000 | [diff] [blame] | 63 | /** |
nsandonato | 9592423 | 2009-08-26 20:50:40 +0000 | [diff] [blame] | 64 | * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#indexedRegionValidType(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) |
| 65 | */ |
| 66 | protected boolean indexedRegionValidType(IndexedRegion indexedRegion) { |
| 67 | return (indexedRegion instanceof IDOMNode) && !(indexedRegion instanceof IDOMText); |
| 68 | } |
| 69 | } |