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