KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > PluginFoldingStructureProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
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 package org.eclipse.pde.internal.ui.editor.plugin;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
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.core.plugin.IExtensions;
21 import org.eclipse.pde.core.plugin.IPluginExtension;
22 import org.eclipse.pde.internal.core.text.IDocumentNode;
23 import org.eclipse.pde.internal.core.text.IEditingModel;
24 import org.eclipse.pde.internal.core.text.plugin.PluginModel;
25 import org.eclipse.pde.internal.ui.editor.AbstractFoldingStructureProvider;
26 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
27
28 public class PluginFoldingStructureProvider extends AbstractFoldingStructureProvider {
29
30     private Map JavaDoc fPositionToElement = new HashMap JavaDoc();
31
32     public PluginFoldingStructureProvider(PDESourcePage editor, IEditingModel model) {
33         super(editor, model);
34     }
35
36     public void addFoldingRegions(Set JavaDoc currentRegions, IEditingModel model)
37     throws BadLocationException {
38         IExtensions extensions = ((PluginModel) model).getExtensions();
39         IPluginExtension[] pluginExtensions = extensions.getExtensions();
40
41
42         addFoldingRegions(currentRegions, pluginExtensions, model.getDocument());
43     }
44
45     private void addFoldingRegions(Set JavaDoc regions, IPluginExtension[] nodes, IDocument document) throws BadLocationException {
46         for(int i = 0; i < nodes.length; i++) {
47             IDocumentNode element = (IDocumentNode) nodes[i];
48             int startLine = document.getLineOfOffset(element.getOffset());
49             int endLine = document.getLineOfOffset(element.getOffset() + element.getLength());
50             if (startLine < endLine) {
51                 int start = document.getLineOffset(startLine);
52                 int end = document.getLineOffset(endLine) + document.getLineLength(endLine);
53                 Position position = new Position(start, end - start);
54                 regions.add(position);
55                 fPositionToElement.put(position, element);
56             }
57             IDocumentNode[] children = element.getChildNodes();
58             if (children != null) {
59                 addFoldingRegions(regions, children, document);
60             }
61         }
62     }
63
64     private void addFoldingRegions(Set JavaDoc regions, IDocumentNode[] nodes, IDocument document) throws BadLocationException {
65         for(int i = 0; i < nodes.length; i++) {
66             IDocumentNode element = nodes[i];
67             int startLine = document.getLineOfOffset(element.getOffset());
68             int endLine = document.getLineOfOffset(element.getOffset() + element.getLength());
69             if (startLine < endLine) {
70                 int start = document.getLineOffset(startLine);
71                 int end = document.getLineOffset(endLine) + document.getLineLength(endLine);
72                 Position position = new Position(start, end - start);
73                 regions.add(position);
74                 fPositionToElement.put(position, element);
75             }
76             IDocumentNode[] children = element.getChildNodes();
77             if (children != null) {
78                 addFoldingRegions(regions, children, document);
79             }
80         }
81     }
82
83 }
84
Popular Tags