001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brock Janiczak <brockj@tpg.com.au> - bug 198881
011: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.build;
012:
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.jface.text.ITextSelection;
015: import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
016: import org.eclipse.jface.util.PropertyChangeEvent;
017: import org.eclipse.jface.viewers.ILabelProvider;
018: import org.eclipse.jface.viewers.ITreeContentProvider;
019: import org.eclipse.jface.viewers.LabelProvider;
020: import org.eclipse.pde.core.build.IBuild;
021: import org.eclipse.pde.core.build.IBuildEntry;
022: import org.eclipse.pde.core.build.IBuildModel;
023: import org.eclipse.pde.internal.core.text.IDocumentKey;
024: import org.eclipse.pde.internal.core.text.IDocumentRange;
025: import org.eclipse.pde.internal.core.text.build.BuildEntry;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEPluginImages;
028: import org.eclipse.pde.internal.ui.editor.KeyValueSourcePage;
029: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
030: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
031: import org.eclipse.swt.graphics.Image;
032:
033: public class BuildSourcePage extends KeyValueSourcePage {
034: class BuildOutlineContentProvider extends DefaultContentProvider
035: implements ITreeContentProvider {
036: public Object[] getChildren(Object parent) {
037: return new Object[0];
038: }
039:
040: public boolean hasChildren(Object parent) {
041: return false;
042: }
043:
044: public Object getParent(Object child) {
045: if (child instanceof IBuildEntry)
046: return ((IBuildEntry) child).getModel();
047: return null;
048: }
049:
050: public Object[] getElements(Object parent) {
051: if (parent instanceof IBuildModel) {
052: IBuildModel model = (IBuildModel) parent;
053: IBuild build = model.getBuild();
054: return build.getBuildEntries();
055: }
056: return new Object[0];
057: }
058: }
059:
060: class BuildLabelProvider extends LabelProvider {
061: public String getText(Object obj) {
062: if (obj instanceof IBuildEntry) {
063: return ((IBuildEntry) obj).getName();
064: }
065: return super .getText(obj);
066: }
067:
068: public Image getImage(Object obj) {
069: if (obj instanceof IBuildEntry)
070: return PDEPlugin.getDefault().getLabelProvider().get(
071: PDEPluginImages.DESC_BUILD_VAR_OBJ);
072: return null;
073: }
074: }
075:
076: public BuildSourcePage(PDEFormEditor editor, String id, String title) {
077: super (editor, id, title);
078: }
079:
080: public void setPreferenceStore(IPreferenceStore store) {
081: super .setPreferenceStore(store);
082: }
083:
084: public ILabelProvider createOutlineLabelProvider() {
085: return new BuildLabelProvider();
086: }
087:
088: public ITreeContentProvider createOutlineContentProvider() {
089: return new BuildOutlineContentProvider();
090: }
091:
092: protected IDocumentRange getRangeElement(ITextSelection selection) {
093: if (selection.isEmpty())
094: return null;
095: IBuildModel model = (IBuildModel) getInputContext().getModel();
096: return findBuildNode(model.getBuild().getBuildEntries(),
097: selection.getOffset());
098: }
099:
100: private BuildEntry findBuildNode(IBuildEntry[] nodes, int offset) {
101: for (int i = 0; i < nodes.length; i++) {
102: BuildEntry node = (BuildEntry) nodes[i];
103: if (offset >= node.getOffset()
104: && offset < node.getOffset() + node.getLength()) {
105: return node;
106: }
107: }
108: return null;
109: }
110:
111: protected String[] collectContextMenuPreferencePages() {
112: String[] ids = super .collectContextMenuPreferencePages();
113: String[] more = new String[ids.length + 1];
114: more[0] = "org.eclipse.jdt.ui.preferences.PropertiesFileEditorPreferencePage"; //$NON-NLS-1$
115: System.arraycopy(ids, 0, more, 1, ids.length);
116: return more;
117: }
118:
119: protected boolean affectsTextPresentation(PropertyChangeEvent event) {
120: return ((BuildSourceViewerConfiguration) getSourceViewerConfiguration())
121: .affectsTextPresentation(event)
122: || super .affectsTextPresentation(event);
123: }
124:
125: public IDocumentRange getRangeElement(int offset,
126: boolean searchChildren) {
127: IBuildModel model = (IBuildModel) getInputContext().getModel();
128: IBuildEntry[] buildEntries = model.getBuild().getBuildEntries();
129:
130: for (int i = 0; i < buildEntries.length; i++) {
131: IDocumentKey key = (IDocumentKey) buildEntries[i];
132: if (offset >= key.getOffset()
133: && offset < key.getOffset() + key.getLength())
134: return key;
135: }
136: return null;
137: }
138:
139: public Object getAdapter(Class adapter) {
140: if (IHyperlinkDetector.class.equals(adapter))
141: return new BuildHyperlinkDetector(this );
142: return super .getAdapter(adapter);
143: }
144:
145: /* (non-Javadoc)
146: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#updateSelection(java.lang.Object)
147: */
148: public void updateSelection(Object object) {
149: if (object instanceof IDocumentKey) {
150: setHighlightRange((IDocumentKey) object);
151: } else {
152: resetHighlightRange();
153: }
154: }
155:
156: }
|