001: /*******************************************************************************
002: * Copyright (c) 2005, 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.text;
011:
012: import org.eclipse.jface.preference.IPreferenceStore;
013: import org.eclipse.jface.text.AbstractInformationControlManager;
014: import org.eclipse.jface.text.DefaultInformationControl;
015: import org.eclipse.jface.text.IInformationControl;
016: import org.eclipse.jface.text.IInformationControlCreator;
017: import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
018: import org.eclipse.jface.text.information.IInformationPresenter;
019: import org.eclipse.jface.text.information.IInformationProvider;
020: import org.eclipse.jface.text.information.InformationPresenter;
021: import org.eclipse.jface.text.reconciler.IReconciler;
022: import org.eclipse.jface.text.reconciler.MonoReconciler;
023: import org.eclipse.jface.text.source.ISourceViewer;
024: import org.eclipse.jface.util.PropertyChangeEvent;
025: import org.eclipse.pde.core.IBaseModel;
026: import org.eclipse.pde.internal.core.text.IReconcilingParticipant;
027: import org.eclipse.pde.internal.ui.PDEPlugin;
028: import org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage;
029: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
030: import org.eclipse.pde.internal.ui.editor.actions.PDEActionConstants;
031: import org.eclipse.pde.internal.ui.editor.contentassist.display.HTMLTextPresenter;
032: import org.eclipse.pde.internal.ui.editor.outline.QuickOutlinePopupDialog;
033: import org.eclipse.swt.SWT;
034: import org.eclipse.swt.widgets.Shell;
035: import org.eclipse.ui.editors.text.EditorsUI;
036: import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
037: import org.eclipse.ui.texteditor.ChainedPreferenceStore;
038:
039: public abstract class ChangeAwareSourceViewerConfiguration extends
040: TextSourceViewerConfiguration {
041:
042: protected PDESourcePage fSourcePage;
043: protected IColorManager fColorManager;
044: private MonoReconciler fReconciler;
045: private InformationPresenter fInfoPresenter;
046: private InformationPresenter fOutlinePresenter;
047:
048: /**
049: * @param page
050: * @param manager - an IColorManager, clients must dispose this themselves.
051: * @param store
052: */
053: public ChangeAwareSourceViewerConfiguration(PDESourcePage page,
054: IColorManager manager, IPreferenceStore store) {
055: super (store);
056: fColorManager = manager;
057: fSourcePage = page;
058: }
059:
060: public ChangeAwareSourceViewerConfiguration(PDESourcePage page,
061: IColorManager manager) {
062: this (page, manager, new ChainedPreferenceStore(
063: new IPreferenceStore[] {
064: PDEPlugin.getDefault().getPreferenceStore(),
065: EditorsUI.getPreferenceStore() // general text editor store
066: }));
067: }
068:
069: public IReconciler getReconciler(ISourceViewer sourceViewer) {
070: if (fSourcePage != null && fReconciler == null) {
071: IBaseModel model = fSourcePage.getInputContext().getModel();
072: if (model instanceof IReconcilingParticipant) {
073: ReconcilingStrategy strategy = new ReconcilingStrategy();
074: strategy
075: .addParticipant((IReconcilingParticipant) model);
076: ISortableContentOutlinePage outline = fSourcePage
077: .getContentOutline();
078: if (outline instanceof IReconcilingParticipant)
079: strategy
080: .addParticipant((IReconcilingParticipant) outline);
081: fReconciler = new MonoReconciler(strategy, false);
082: fReconciler.setDelay(500);
083: }
084: }
085: return fReconciler;
086: }
087:
088: public IInformationPresenter getInformationPresenter(
089: ISourceViewer sourceViewer) {
090: if (fSourcePage == null)
091: return null;
092: if (fInfoPresenter == null
093: && getInfoImplementationType() != SourceInformationProvider.F_NO_IMP) {
094: IInformationControlCreator icc = getInformationControlCreator(false);
095: fInfoPresenter = new InformationPresenter(icc);
096: fInfoPresenter
097: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
098:
099: // Register information provider
100: IInformationProvider provider = new SourceInformationProvider(
101: fSourcePage, icc, getInfoImplementationType());
102: String[] contentTypes = getConfiguredContentTypes(sourceViewer);
103: for (int i = 0; i < contentTypes.length; i++)
104: fInfoPresenter.setInformationProvider(provider,
105: contentTypes[i]);
106:
107: fInfoPresenter.setSizeConstraints(60, 10, true, true);
108: }
109: return fInfoPresenter;
110: }
111:
112: /**
113: * @param sourceViewer
114: * @return
115: */
116: public IInformationPresenter getOutlinePresenter(
117: ISourceViewer sourceViewer) {
118: // Ensure the source page is defined
119: if (fSourcePage == null) {
120: return null;
121: }
122: // Reuse the old outline presenter
123: if (fOutlinePresenter != null) {
124: return fOutlinePresenter;
125: }
126: // Define a new outline presenter
127: fOutlinePresenter = new InformationPresenter(
128: getOutlinePresenterControlCreator(sourceViewer,
129: PDEActionConstants.COMMAND_ID_QUICK_OUTLINE));
130: fOutlinePresenter
131: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
132: fOutlinePresenter
133: .setAnchor(AbstractInformationControlManager.ANCHOR_GLOBAL);
134: // Define a new outline provider
135: IInformationProvider provider = new PDESourceInfoProvider(
136: fSourcePage);
137: // Set the provider on all defined content types
138: String[] contentTypes = getConfiguredContentTypes(sourceViewer);
139: for (int i = 0; i < contentTypes.length; i++) {
140: fOutlinePresenter.setInformationProvider(provider,
141: contentTypes[i]);
142: }
143: // Set the presenter size constraints
144: fOutlinePresenter.setSizeConstraints(50, 20, true, false);
145:
146: return fOutlinePresenter;
147: }
148:
149: /**
150: * Returns the outline presenter control creator. The creator is a
151: * factory creating outline presenter controls for the given source viewer.
152: *
153: * @param sourceViewer the source viewer to be configured by this configuration
154: * @param commandId the ID of the command that opens this control
155: * @return an information control creator
156: */
157: private IInformationControlCreator getOutlinePresenterControlCreator(
158: ISourceViewer sourceViewer, final String commandId) {
159: return new IInformationControlCreator() {
160: public IInformationControl createInformationControl(
161: Shell parent) {
162: int shellStyle = SWT.RESIZE;
163: QuickOutlinePopupDialog dialog = new QuickOutlinePopupDialog(
164: parent, shellStyle, fSourcePage, fSourcePage);
165: return dialog;
166: }
167: };
168: }
169:
170: protected int getInfoImplementationType() {
171: return SourceInformationProvider.F_NO_IMP;
172: }
173:
174: protected IInformationControlCreator getInformationControlCreator(
175: final boolean cutDown) {
176: return new IInformationControlCreator() {
177: public IInformationControl createInformationControl(
178: Shell parent) {
179: int style = cutDown ? SWT.RESIZE : (SWT.V_SCROLL
180: | SWT.H_SCROLL | SWT.RESIZE);
181: return new DefaultInformationControl(parent, style,
182: new HTMLTextPresenter(cutDown));
183: }
184: };
185: }
186:
187: public IHyperlinkDetector[] getHyperlinkDetectors(
188: ISourceViewer sourceViewer) {
189: if (fSourcePage != null)
190: return new IHyperlinkDetector[] { (IHyperlinkDetector) fSourcePage
191: .getAdapter(IHyperlinkDetector.class) };
192: return super .getHyperlinkDetectors(sourceViewer);
193: }
194:
195: public abstract boolean affectsTextPresentation(
196: PropertyChangeEvent event);
197:
198: public abstract boolean affectsColorPresentation(
199: PropertyChangeEvent event);
200:
201: public abstract void adaptToPreferenceChange(
202: PropertyChangeEvent event);
203:
204: public abstract void dispose();
205:
206: }
|