KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > BuildSourcePage


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.build;
12 import org.eclipse.jface.preference.IPreferenceStore;
13 import org.eclipse.jface.text.ITextSelection;
14 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
15 import org.eclipse.jface.util.PropertyChangeEvent;
16 import org.eclipse.jface.viewers.ILabelProvider;
17 import org.eclipse.jface.viewers.ITreeContentProvider;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.pde.core.build.IBuild;
20 import org.eclipse.pde.core.build.IBuildEntry;
21 import org.eclipse.pde.core.build.IBuildModel;
22 import org.eclipse.pde.internal.core.text.IDocumentKey;
23 import org.eclipse.pde.internal.core.text.IDocumentRange;
24 import org.eclipse.pde.internal.core.text.build.BuildEntry;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEPluginImages;
27 import org.eclipse.pde.internal.ui.editor.KeyValueSourcePage;
28 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
29 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
30 import org.eclipse.swt.graphics.Image;
31
32 public class BuildSourcePage extends KeyValueSourcePage {
33     class BuildOutlineContentProvider extends DefaultContentProvider
34             implements
35                 ITreeContentProvider {
36         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
37             return new Object JavaDoc[0];
38         }
39         public boolean hasChildren(Object JavaDoc parent) {
40             return false;
41         }
42         public Object JavaDoc getParent(Object JavaDoc child) {
43             if (child instanceof IBuildEntry)
44                 return ((IBuildEntry) child).getModel();
45             return null;
46         }
47         public Object JavaDoc[] getElements(Object JavaDoc parent) {
48             if (parent instanceof IBuildModel) {
49                 IBuildModel model = (IBuildModel) parent;
50                 IBuild build = model.getBuild();
51                 return build.getBuildEntries();
52             }
53             return new Object JavaDoc[0];
54         }
55     }
56     class BuildLabelProvider extends LabelProvider {
57         public String JavaDoc getText(Object JavaDoc obj) {
58             if (obj instanceof IBuildEntry) {
59                 return ((IBuildEntry) obj).getName();
60             }
61             return super.getText(obj);
62         }
63         public Image getImage(Object JavaDoc obj) {
64             if (obj instanceof IBuildEntry)
65                 return PDEPlugin.getDefault().getLabelProvider().get(
66                     PDEPluginImages.DESC_BUILD_VAR_OBJ);
67             return null;
68         }
69     }
70
71     public BuildSourcePage(PDEFormEditor editor, String JavaDoc id, String JavaDoc title) {
72         super(editor, id, title);
73     }
74     
75     public void setPreferenceStore(IPreferenceStore store) {
76         super.setPreferenceStore(store);
77     }
78     
79     public ILabelProvider createOutlineLabelProvider() {
80         return new BuildLabelProvider();
81     }
82     
83     public ITreeContentProvider createOutlineContentProvider() {
84         return new BuildOutlineContentProvider();
85     }
86     protected IDocumentRange getRangeElement(ITextSelection selection) {
87         if (selection.isEmpty())
88             return null;
89         IBuildModel model = (IBuildModel) getInputContext().getModel();
90         return findBuildNode(model.getBuild().getBuildEntries(), selection.getOffset());
91     }
92
93     private BuildEntry findBuildNode(IBuildEntry[] nodes, int offset) {
94         for (int i = 0; i < nodes.length; i++) {
95             BuildEntry node = (BuildEntry) nodes[i];
96             if (offset >= node.getOffset()
97                     && offset < node.getOffset() + node.getLength()) {
98                 return node;
99             }
100         }
101         return null;
102     }
103     
104     protected String JavaDoc[] collectContextMenuPreferencePages() {
105         String JavaDoc[] ids= super.collectContextMenuPreferencePages();
106         String JavaDoc[] more= new String JavaDoc[ids.length + 1];
107         more[0]= "org.eclipse.jdt.ui.preferences.PropertiesFileEditorPreferencePage"; //$NON-NLS-1$
108
System.arraycopy(ids, 0, more, 1, ids.length);
109         return more;
110     }
111     
112     protected boolean affectsTextPresentation(PropertyChangeEvent event) {
113         return ((BuildSourceViewerConfiguration)getSourceViewerConfiguration()).affectsTextPresentation(event) || super.affectsTextPresentation(event);
114     }
115     
116     public IDocumentRange getRangeElement(int offset, boolean searchChildren) {
117         IBuildModel model = (IBuildModel) getInputContext().getModel();
118         IBuildEntry[] buildEntries = model.getBuild().getBuildEntries();
119
120         for (int i = 0; i < buildEntries.length; i++) {
121             IDocumentKey key = (IDocumentKey) buildEntries[i];
122             if (offset >= key.getOffset() && offset < key.getOffset() + key.getLength())
123                 return key;
124         }
125         return null;
126     }
127     
128     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
129         if (IHyperlinkDetector[].class.equals(adapter))
130             return new IHyperlinkDetector[] { new BuildHyperlinkDetector(this) };
131         return super.getAdapter(adapter);
132     }
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#updateSelection(java.lang.Object)
136      */

137     public void updateSelection(Object JavaDoc object) {
138         if (object instanceof IDocumentKey) {
139             setHighlightRange((IDocumentKey)object);
140         } else {
141             resetHighlightRange();
142         }
143     }
144     
145 }
146
Popular Tags