01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.toc;
11:
12: import java.util.HashMap;
13: import java.util.List;
14: import java.util.Map;
15: import java.util.Set;
16:
17: import org.eclipse.jface.text.BadLocationException;
18: import org.eclipse.jface.text.IDocument;
19: import org.eclipse.jface.text.Position;
20: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
21: import org.eclipse.pde.internal.core.text.IEditingModel;
22: import org.eclipse.pde.internal.core.text.toc.TocModel;
23: import org.eclipse.pde.internal.core.text.toc.TocObject;
24: import org.eclipse.pde.internal.ui.editor.AbstractFoldingStructureProvider;
25: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
26:
27: public class TocFoldingStructureProvider extends
28: AbstractFoldingStructureProvider {
29:
30: private Map fPositionToElement = new HashMap();
31:
32: public TocFoldingStructureProvider(PDESourcePage editor,
33: IEditingModel model) {
34: super (editor, model);
35: }
36:
37: public void addFoldingRegions(Set currentRegions,
38: IEditingModel model) throws BadLocationException {
39: TocObject toc = ((TocModel) model).getToc();
40: List childList = toc.getChildren();
41: IDocumentElementNode[] children = (IDocumentElementNode[]) childList
42: .toArray(new IDocumentElementNode[childList.size()]);
43:
44: addFoldingRegions(currentRegions, children, model.getDocument());
45: }
46:
47: private void addFoldingRegions(Set regions,
48: IDocumentElementNode[] nodes, IDocument document)
49: throws BadLocationException {
50: for (int i = 0; i < nodes.length; i++) {
51: IDocumentElementNode element = nodes[i];
52: int startLine = document.getLineOfOffset(element
53: .getOffset());
54: int endLine = document.getLineOfOffset(element.getOffset()
55: + element.getLength());
56: if (startLine < endLine) {
57: int start = document.getLineOffset(startLine);
58: int end = document.getLineOffset(endLine)
59: + document.getLineLength(endLine);
60: Position position = new Position(start, end - start);
61: regions.add(position);
62: fPositionToElement.put(position, element);
63: }
64: IDocumentElementNode[] children = element.getChildNodes();
65: if (children != null) {
66: addFoldingRegions(regions, children, document);
67: }
68: }
69: }
70:
71: }
|