001: /*******************************************************************************
002: * Copyright (c) 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: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.cheatsheet.simple;
011:
012: import org.eclipse.jface.action.IMenuManager;
013: import org.eclipse.jface.action.Separator;
014: import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
015: import org.eclipse.jface.viewers.ILabelProvider;
016: import org.eclipse.jface.viewers.ITreeContentProvider;
017: import org.eclipse.jface.viewers.ViewerComparator;
018: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS;
019: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
020: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
021: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
022: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSPerformWhen;
023: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject;
024: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
025: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
026: import org.eclipse.pde.internal.core.text.IDocumentRange;
027: import org.eclipse.pde.internal.core.text.IDocumentTextNode;
028: import org.eclipse.pde.internal.core.text.cheatsheet.simple.SimpleCSModel;
029: import org.eclipse.pde.internal.ui.IHelpContextIds;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEUIMessages;
032: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
033: import org.eclipse.pde.internal.ui.editor.XMLSourcePage;
034: import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions.SimpleCSPreviewAction;
035:
036: /**
037: * SimpleCSSourcePage
038: *
039: */
040: public class SimpleCSSourcePage extends XMLSourcePage {
041:
042: /**
043: * @param editor
044: * @param id
045: * @param title
046: */
047: public SimpleCSSourcePage(PDEFormEditor editor, String id,
048: String title) {
049: super (editor, id, title);
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.pde.internal.ui.editor.PDEProjectionSourcePage#isQuickOutlineEnabled()
054: */
055: public boolean isQuickOutlineEnabled() {
056: return true;
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#createOutlineComparator()
061: */
062: public ViewerComparator createOutlineComparator() {
063: return null;
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#createOutlineContentProvider()
068: */
069: public ITreeContentProvider createOutlineContentProvider() {
070: return new SimpleCSContentProvider();
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#createOutlineLabelProvider()
075: */
076: public ILabelProvider createOutlineLabelProvider() {
077: return PDEPlugin.getDefault().getLabelProvider();
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#isSelectionListener()
082: */
083: protected boolean isSelectionListener() {
084: return true;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#updateSelection(java.lang.Object)
089: */
090: public void updateSelection(Object object) {
091: if ((object instanceof IDocumentElementNode)
092: && (((IDocumentElementNode) object).isErrorNode() == false)) {
093: fSelection = object;
094: setHighlightRange((IDocumentElementNode) object, true);
095: setSelectedRange((IDocumentElementNode) object, false);
096: }
097: }
098:
099: /* (non-Javadoc)
100: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#findRange()
101: */
102: protected IDocumentRange findRange() {
103: if (fSelection instanceof IDocumentElementNode) {
104: return (IDocumentElementNode) fSelection;
105: }
106: return null;
107: }
108:
109: /* (non-Javadoc)
110: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#getRangeElement(int, boolean)
111: */
112: public IDocumentRange getRangeElement(int offset,
113: boolean searchChildren) {
114: IDocumentElementNode rootNode = ((SimpleCSModel) getInputContext()
115: .getModel()).getSimpleCS();
116: return findNode(rootNode, offset, searchChildren);
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#synchronizeOutlinePage(int)
121: */
122: protected void synchronizeOutlinePage(int offset) {
123: IDocumentRange range = getRangeElement(offset, true);
124: updateHighlightRange(range);
125: range = adaptRange(range);
126: updateOutlinePageSelection(range);
127: }
128:
129: /**
130: * @param range
131: * @return
132: */
133: public IDocumentRange adaptRange(IDocumentRange range) {
134: // Adapt the range to node that is viewable in the outline view
135: if (range instanceof IDocumentAttributeNode) {
136: // Attribute
137: return adaptRange(((IDocumentAttributeNode) range)
138: .getEnclosingElement());
139: } else if (range instanceof IDocumentTextNode) {
140: // Content
141: return adaptRange(((IDocumentTextNode) range)
142: .getEnclosingElement());
143: } else if (range instanceof IDocumentElementNode) {
144: // Element
145: if (range instanceof ISimpleCS) {
146: return range;
147: } else if (range instanceof ISimpleCSItem) {
148: return range;
149: } else if (range instanceof ISimpleCSSubItemObject) {
150: return range;
151: } else if (range instanceof ISimpleCSIntro) {
152: return range;
153: } else if (range instanceof ISimpleCSPerformWhen) {
154: return range;
155: } else {
156: return adaptRange(((IDocumentElementNode) range)
157: .getParentNode());
158: }
159: }
160: return null;
161: }
162:
163: /* (non-Javadoc)
164: * @see org.eclipse.pde.internal.ui.editor.PDEProjectionSourcePage#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
165: */
166: protected void editorContextMenuAboutToShow(IMenuManager menu) {
167: // Get the editor
168: PDEFormEditor editor = (PDEFormEditor) getEditor();
169: // Get the form editor contributor
170: SimpleCSEditorContributor contributor = (SimpleCSEditorContributor) editor
171: .getContributor();
172: // Get the model
173: // TODO: MP: SimpleCS: Preview does not show unsaved changes made to source page,
174: // check if fixed after implementing text edit operations
175: ISimpleCSModel model = (ISimpleCSModel) editor
176: .getAggregateModel();
177: // Get the preview action
178: SimpleCSPreviewAction previewAction = contributor
179: .getPreviewAction();
180: // Set the cheat sheet object
181: previewAction.setDataModelObject(model.getSimpleCS());
182: // Set the editor input
183: previewAction.setEditorInput(editor.getEditorInput());
184: // Add the preview action to the context menu
185: menu.add(previewAction);
186: menu.add(new Separator());
187: super .editorContextMenuAboutToShow(menu);
188: }
189:
190: /* (non-Javadoc)
191: * @see org.eclipse.ui.part.EditorPart#setPartName(java.lang.String)
192: */
193: protected void setPartName(String partName) {
194: super .setPartName(PDEUIMessages.EditorSourcePage_name);
195: }
196:
197: /* (non-Javadoc)
198: * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
199: */
200: protected void initializeEditor() {
201: super .initializeEditor();
202: setHelpContextId(IHelpContextIds.SIMPLE_CS_EDITOR);
203: }
204:
205: /* (non-Javadoc)
206: * @see org.eclipse.pde.internal.ui.editor.PDEProjectionSourcePage#getAdapter(java.lang.Class)
207: */
208: public Object getAdapter(Class adapter) {
209: if (IHyperlinkDetector.class.equals(adapter)) {
210: return new SimpleCSHyperlinkDetector(this);
211: }
212: return super.getAdapter(adapter);
213: }
214:
215: }
|