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.plugin;
11:
12: import java.util.HashMap;
13: import java.util.Map;
14: import java.util.Set;
15:
16: import org.eclipse.jface.text.BadLocationException;
17: import org.eclipse.jface.text.IDocument;
18: import org.eclipse.jface.text.Position;
19: import org.eclipse.pde.core.plugin.IExtensions;
20: import org.eclipse.pde.core.plugin.IPluginExtension;
21: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
22: import org.eclipse.pde.internal.core.text.IEditingModel;
23: import org.eclipse.pde.internal.core.text.plugin.PluginModel;
24: import org.eclipse.pde.internal.ui.editor.AbstractFoldingStructureProvider;
25: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
26:
27: public class PluginFoldingStructureProvider extends
28: AbstractFoldingStructureProvider {
29:
30: private Map fPositionToElement = new HashMap();
31:
32: public PluginFoldingStructureProvider(PDESourcePage editor,
33: IEditingModel model) {
34: super (editor, model);
35: }
36:
37: public void addFoldingRegions(Set currentRegions,
38: IEditingModel model) throws BadLocationException {
39: IExtensions extensions = ((PluginModel) model).getExtensions();
40: IPluginExtension[] pluginExtensions = extensions
41: .getExtensions();
42:
43: addFoldingRegions(currentRegions, pluginExtensions, model
44: .getDocument());
45: }
46:
47: private void addFoldingRegions(Set regions,
48: IPluginExtension[] nodes, IDocument document)
49: throws BadLocationException {
50: for (int i = 0; i < nodes.length; i++) {
51: IDocumentElementNode element = (IDocumentElementNode) 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: private void addFoldingRegions(Set regions,
72: IDocumentElementNode[] nodes, IDocument document)
73: throws BadLocationException {
74: for (int i = 0; i < nodes.length; i++) {
75: IDocumentElementNode element = nodes[i];
76: int startLine = document.getLineOfOffset(element
77: .getOffset());
78: int endLine = document.getLineOfOffset(element.getOffset()
79: + element.getLength());
80: if (startLine < endLine) {
81: int start = document.getLineOffset(startLine);
82: int end = document.getLineOffset(endLine)
83: + document.getLineLength(endLine);
84: Position position = new Position(start, end - start);
85: regions.add(position);
86: fPositionToElement.put(position, element);
87: }
88: IDocumentElementNode[] children = element.getChildNodes();
89: if (children != null) {
90: addFoldingRegions(regions, children, document);
91: }
92: }
93: }
94:
95: }
|