KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.core.ibundle.IBundle;
21 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
22 import org.eclipse.pde.internal.core.text.IEditingModel;
23 import org.eclipse.pde.internal.core.text.bundle.BundleModel;
24 import org.eclipse.pde.internal.ui.editor.AbstractFoldingStructureProvider;
25 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
26 import org.osgi.framework.Constants;
27
28 public class BundleFoldingStructureProvider extends
29         AbstractFoldingStructureProvider {
30
31     private Map JavaDoc fPositionToElement = new HashMap JavaDoc();
32     
33     public BundleFoldingStructureProvider(PDESourcePage editor, IEditingModel model) {
34         super(editor, model);
35     }
36
37     public void addFoldingRegions(Set JavaDoc currentRegions, IEditingModel model)
38             throws BadLocationException {
39         IBundle bundle = ((BundleModel) model).getBundle();
40
41         IManifestHeader importPackageHeader = bundle.getManifestHeader(Constants.IMPORT_PACKAGE);
42         IManifestHeader exportPackageHeader = bundle.getManifestHeader(Constants.EXPORT_PACKAGE);
43         IManifestHeader requireBundleHeader = bundle.getManifestHeader(Constants.REQUIRE_BUNDLE);
44     
45         try {
46             addFoldingRegions(currentRegions, importPackageHeader, model.getDocument());
47             addFoldingRegions(currentRegions, exportPackageHeader, model.getDocument());
48             addFoldingRegions(currentRegions, requireBundleHeader, model.getDocument());
49         } catch (BadLocationException e) {}
50
51     }
52     
53     private void addFoldingRegions(Set JavaDoc regions, IManifestHeader header, IDocument document) throws BadLocationException {
54         if(header == null) return;
55         int startLine = document.getLineOfOffset(header.getOffset());
56         int endLine = document.getLineOfOffset(header.getOffset() + header.getLength() - 1);
57         if (startLine < endLine) {
58             int start = document.getLineOffset(startLine);
59             int end = document.getLineOffset(endLine) + document.getLineLength(endLine);
60             Position position = new Position(start, end - start);
61             regions.add(position);
62             fPositionToElement.put(position, header);
63         }
64     }
65
66 }
67
Popular Tags