KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > ManifestEditorOpener


1 /*******************************************************************************
2  * Copyright (c) 2005 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.search;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.FindReplaceDocumentAdapter;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.Region;
18 import org.eclipse.pde.core.plugin.IFragment;
19 import org.eclipse.pde.core.plugin.IPlugin;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginImport;
23 import org.eclipse.pde.core.plugin.IPluginObject;
24 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
25 import org.eclipse.search.ui.text.Match;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.PartInitException;
28
29
30 public class ManifestEditorOpener {
31
32     public static IEditorPart open(Match match, boolean activate) throws PartInitException {
33         IEditorPart editorPart = null;
34         editorPart = ManifestEditor.open(match.getElement(), true);
35         if (editorPart != null && editorPart instanceof ManifestEditor) {
36             ManifestEditor editor = (ManifestEditor)editorPart;
37             IDocument doc = editor.getDocument(match);
38             if (doc != null) {
39                 Match exact = findExactMatch(doc, match);
40                 editor.openToSourcePage(match.getElement(), exact.getOffset(), exact.getLength());
41             }
42         }
43         return editorPart;
44     }
45     
46     public static Match findExactMatch(IDocument document, Match match) {
47         if (match.getOffset() == -1 && match.getBaseUnit() == Match.UNIT_LINE)
48             return new Match(match.getElement(), Match.UNIT_CHARACTER, 0,0);
49         IPluginObject element = (IPluginObject)match.getElement();
50         String JavaDoc name = null;
51         String JavaDoc value = null;
52         if (element instanceof IPluginExtension) {
53             name = "point"; //$NON-NLS-1$
54
value = ((IPluginExtension)element).getPoint();
55         } else if (element instanceof IPluginExtensionPoint) {
56             name = "id"; //$NON-NLS-1$
57
value = ((IPluginExtensionPoint)element).getId();
58         } else if (element instanceof IPluginImport) {
59             name = "plugin"; //$NON-NLS-1$
60
value = ((IPluginImport)element).getId();
61         } else if (element instanceof IPlugin) {
62             name = "id"; //$NON-NLS-1$
63
value = ((IPlugin)element).getId();
64         } else if (element instanceof IFragment) {
65             name = "id"; //$NON-NLS-1$
66
value = ((IFragment)element).getId();
67         }
68         IRegion region = getAttributeRegion(document, name, value, match.getOffset());
69         if (region != null) {
70             return new Match(element, Match.UNIT_CHARACTER, region.getOffset(), region.getLength());
71         }
72         return match;
73     }
74     
75     private static IRegion getAttributeRegion(IDocument document, String JavaDoc name, String JavaDoc value, int line) {
76         try {
77             int offset = document.getLineOffset(line) + document.getLineLength(line);
78             FindReplaceDocumentAdapter findReplaceAdapter = new FindReplaceDocumentAdapter(document);
79             IRegion nameRegion = findReplaceAdapter.find(offset, name+"\\s*=\\s*\""+value, false, false, false, true); //$NON-NLS-1$
80
if (nameRegion != null) {
81                 if (document.get(nameRegion.getOffset() + nameRegion.getLength() - value.length(), value.length()).equals(value))
82                     return new Region(nameRegion.getOffset() + nameRegion.getLength() - value.length(), value.length());
83             }
84         } catch (BadLocationException e) {
85         }
86         return null;
87     }
88
89
90 }
91
Popular Tags