001: /*******************************************************************************
002: * Copyright (c) 2006 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;
011:
012: import org.eclipse.jface.action.IAction;
013: import org.eclipse.jface.action.IMenuManager;
014: import org.eclipse.jface.preference.IPreferenceStore;
015: import org.eclipse.jface.text.reconciler.IReconciler;
016: import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
017: import org.eclipse.jface.text.source.ISourceViewer;
018: import org.eclipse.jface.text.source.IVerticalRuler;
019: import org.eclipse.jface.text.source.projection.IProjectionListener;
020: import org.eclipse.jface.text.source.projection.ProjectionSupport;
021: import org.eclipse.jface.text.source.projection.ProjectionViewer;
022: import org.eclipse.jface.util.PropertyChangeEvent;
023: import org.eclipse.pde.core.IBaseModel;
024: import org.eclipse.pde.internal.core.text.IEditingModel;
025: import org.eclipse.pde.internal.ui.IPreferenceConstants;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.editor.actions.PDEActionConstants;
028: import org.eclipse.pde.internal.ui.editor.text.ChangeAwareSourceViewerConfiguration;
029: import org.eclipse.pde.internal.ui.editor.text.ColorManager;
030: import org.eclipse.pde.internal.ui.editor.text.IColorManager;
031: import org.eclipse.pde.internal.ui.editor.text.ReconcilingStrategy;
032: import org.eclipse.swt.widgets.Composite;
033:
034: public abstract class PDEProjectionSourcePage extends PDESourcePage
035: implements IProjectionListener {
036:
037: private ProjectionSupport fProjectionSupport;
038: private IFoldingStructureProvider fFoldingStructureProvider;
039: private IColorManager fColorManager;
040: private ChangeAwareSourceViewerConfiguration fConfiguration;
041:
042: public PDEProjectionSourcePage(PDEFormEditor editor, String id,
043: String title) {
044: super (editor, id, title);
045: fColorManager = ColorManager.getDefault();
046: fConfiguration = SourceViewerConfigurationFactory
047: .createSourceViewerConfiguration(this , fColorManager);
048: if (fConfiguration != null)
049: setSourceViewerConfiguration(fConfiguration);
050: }
051:
052: public void createPartControl(Composite parent) {
053: super .createPartControl(parent);
054:
055: ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
056: createFoldingSupport(projectionViewer);
057:
058: if (isFoldingEnabled()) {
059: projectionViewer.doOperation(ProjectionViewer.TOGGLE);
060: }
061: }
062:
063: protected ISourceViewer createSourceViewer(Composite parent,
064: IVerticalRuler ruler, int styles) {
065: ISourceViewer viewer = new PDEProjectionViewer(parent, ruler,
066: getOverviewRuler(), isOverviewRulerVisible(), styles,
067: isQuickOutlineEnabled());
068: getSourceViewerDecorationSupport(viewer);
069: return viewer;
070: }
071:
072: /**
073: * @return
074: */
075: public abstract boolean isQuickOutlineEnabled();
076:
077: public void dispose() {
078: ((ProjectionViewer) getSourceViewer())
079: .removeProjectionListener(this );
080: if (fProjectionSupport != null) {
081: fProjectionSupport.dispose();
082: fProjectionSupport = null;
083: }
084: fColorManager.dispose();
085: if (fConfiguration != null)
086: fConfiguration.dispose();
087: super .dispose();
088: }
089:
090: private void createFoldingSupport(ProjectionViewer projectionViewer) {
091: fProjectionSupport = new ProjectionSupport(projectionViewer,
092: getAnnotationAccess(), getSharedColors());
093:
094: fProjectionSupport.install();
095: ((ProjectionViewer) getSourceViewer())
096: .addProjectionListener(this );
097:
098: }
099:
100: public void projectionEnabled() {
101: IBaseModel model = getInputContext().getModel();
102: if (model instanceof IEditingModel) {
103: fFoldingStructureProvider = FoldingStructureProviderFactory
104: .createProvider(this , (IEditingModel) model);
105: if (fFoldingStructureProvider != null) {
106: fFoldingStructureProvider.initialize();
107: IReconciler rec = getSourceViewerConfiguration()
108: .getReconciler(getSourceViewer());
109: IReconcilingStrategy startegy = rec
110: .getReconcilingStrategy(new String());
111: if (startegy instanceof ReconcilingStrategy) {
112: ((ReconcilingStrategy) startegy)
113: .addParticipant(fFoldingStructureProvider);
114: }
115: }
116: }
117: }
118:
119: public void projectionDisabled() {
120: fFoldingStructureProvider = null;
121: }
122:
123: private boolean isFoldingEnabled() {
124: IPreferenceStore store = PDEPlugin.getDefault()
125: .getPreferenceStore();
126: return store
127: .getBoolean(IPreferenceConstants.EDITOR_FOLDING_ENABLED);
128: }
129:
130: protected boolean affectsTextPresentation(PropertyChangeEvent event) {
131: if (fConfiguration == null)
132: return false;
133: return fConfiguration.affectsTextPresentation(event)
134: || super .affectsTextPresentation(event);
135: }
136:
137: protected void handlePreferenceStoreChanged(
138: PropertyChangeEvent event) {
139: try {
140: if (fConfiguration != null) {
141: ISourceViewer sourceViewer = getSourceViewer();
142: if (sourceViewer != null)
143: fConfiguration.adaptToPreferenceChange(event);
144: }
145: } finally {
146: super .handlePreferenceStoreChanged(event);
147: }
148: }
149:
150: public Object getAdapter(Class key) {
151: if (fProjectionSupport != null) {
152: Object adapter = fProjectionSupport.getAdapter(
153: getSourceViewer(), key);
154: if (adapter != null) {
155: return adapter;
156: }
157: }
158: return super .getAdapter(key);
159: }
160:
161: /* (non-Javadoc)
162: * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
163: */
164: protected void editorContextMenuAboutToShow(IMenuManager menu) {
165: // Add the quick outline menu entry to the context menu
166: addQuickOutlineMenuEntry(menu);
167: // Add the rest
168: super .editorContextMenuAboutToShow(menu);
169: }
170:
171: /**
172: * @param menu
173: */
174: private void addQuickOutlineMenuEntry(IMenuManager menu) {
175: // Only add the action if the source page supports it
176: if (isQuickOutlineEnabled() == false) {
177: return;
178: }
179: // Get the appropriate quick outline action associated with the active
180: // source page
181: IAction quickOutlineAction = getAction(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE);
182: // Ensure it is defined
183: if (quickOutlineAction == null) {
184: return;
185: }
186: // Insert the quick outline action after the "Show In" menu contributed
187: menu.add(quickOutlineAction);
188: }
189:
190: }
|