001: package org.drools.eclipse.editors;
002:
003: import java.util.HashMap;
004: import java.util.List;
005:
006: import org.drools.eclipse.DroolsEclipsePlugin;
007: import org.drools.eclipse.editors.outline.RuleContentOutlinePage;
008: import org.drools.eclipse.editors.scanners.RuleEditorMessages;
009: import org.drools.eclipse.preferences.IDroolsConstants;
010: import org.eclipse.core.resources.IResource;
011: import org.eclipse.core.runtime.IProgressMonitor;
012: import org.eclipse.debug.ui.actions.ToggleBreakpointAction;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.jface.preference.PreferenceConverter;
015: import org.eclipse.jface.text.source.Annotation;
016: import org.eclipse.jface.text.source.ISourceViewer;
017: import org.eclipse.jface.text.source.IVerticalRuler;
018: import org.eclipse.jface.text.source.SourceViewerConfiguration;
019: import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
020: import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
021: import org.eclipse.jface.text.source.projection.ProjectionSupport;
022: import org.eclipse.jface.text.source.projection.ProjectionViewer;
023: import org.eclipse.swt.graphics.RGB;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.ui.IFileEditorInput;
026: import org.eclipse.ui.editors.text.TextEditor;
027: import org.eclipse.ui.texteditor.IDocumentProvider;
028: import org.eclipse.ui.texteditor.ITextEditorActionConstants;
029: import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
030: import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
031: import org.eclipse.ui.texteditor.TextOperationAction;
032: import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
033: import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
034:
035: /**
036: * Abstract text-based rule editor.
037: *
038: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
039: */
040: public class AbstractRuleEditor extends TextEditor {
041:
042: protected RuleContentOutlinePage ruleContentOutline = null;
043:
044: protected Annotation[] oldAnnotations;
045: protected ProjectionAnnotationModel annotationModel;
046: protected DroolsPairMatcher bracketMatcher = new DroolsPairMatcher();
047:
048: public AbstractRuleEditor() {
049: setSourceViewerConfiguration(createSourceViewerConfiguration());
050: setDocumentProvider(createDocumentProvider());
051: getPreferenceStore().setDefault(
052: IDroolsConstants.DRL_EDITOR_MATCHING_BRACKETS, true);
053: PreferenceConverter.setDefault(getPreferenceStore(),
054: IDroolsConstants.DRL_EDITOR_MATCHING_BRACKETS_COLOR,
055: new RGB(192, 192, 192));
056: }
057:
058: protected SourceViewerConfiguration createSourceViewerConfiguration() {
059: return new DRLSourceViewerConfig(this );
060: }
061:
062: protected IDocumentProvider createDocumentProvider() {
063: return new DRLDocumentProvider();
064: }
065:
066: public void createPartControl(Composite parent) {
067: super .createPartControl(parent);
068: ProjectionViewer viewer = (ProjectionViewer) getSourceViewer();
069: ProjectionSupport projectionSupport = new ProjectionSupport(
070: viewer, getAnnotationAccess(), getSharedColors());
071: projectionSupport.install();
072: // turn projection mode on
073: viewer.doOperation(ProjectionViewer.TOGGLE);
074: annotationModel = viewer.getProjectionAnnotationModel();
075: }
076:
077: protected ISourceViewer createSourceViewer(Composite parent,
078: IVerticalRuler ruler, int styles) {
079: ISourceViewer viewer = new ProjectionViewer(parent, ruler,
080: getOverviewRuler(), isOverviewRulerVisible(), styles);
081: // ensure decoration support has been created and configured.
082: getSourceViewerDecorationSupport(viewer);
083: return viewer;
084: }
085:
086: protected void configureSourceViewerDecorationSupport(
087: SourceViewerDecorationSupport support) {
088: support.setCharacterPairMatcher(bracketMatcher);
089: support.setMatchingCharacterPainterPreferenceKeys(
090: IDroolsConstants.DRL_EDITOR_MATCHING_BRACKETS,
091: IDroolsConstants.DRL_EDITOR_MATCHING_BRACKETS_COLOR);
092: super .configureSourceViewerDecorationSupport(support);
093: }
094:
095: public void updateFoldingStructure(List positions) {
096: Annotation[] annotations = new Annotation[positions.size()];
097: // this will hold the new annotations along
098: // with their corresponding positions
099: HashMap newAnnotations = new HashMap();
100: for (int i = 0; i < positions.size(); i++) {
101: ProjectionAnnotation annotation = new ProjectionAnnotation();
102: newAnnotations.put(annotation, positions.get(i));
103: annotations[i] = annotation;
104: }
105: annotationModel.modifyAnnotations(oldAnnotations,
106: newAnnotations, null);
107: oldAnnotations = annotations;
108: }
109:
110: /** For user triggered content assistance */
111: protected void createActions() {
112: super .createActions();
113:
114: IAction a = new TextOperationAction(RuleEditorMessages
115: .getResourceBundle(), "ContentAssistProposal.", this ,
116: ISourceViewer.CONTENTASSIST_PROPOSALS);
117: a
118: .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
119: setAction("ContentAssistProposal", a);
120:
121: a = new TextOperationAction(
122: RuleEditorMessages.getResourceBundle(),
123: "ContentAssistTip.", this , ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION); //$NON-NLS-1$
124: a
125: .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
126: setAction("ContentAssistTip", a);
127:
128: a = new ToggleBreakpointAction(getSite().getPart(), null,
129: getVerticalRuler());
130: setAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK, a);
131:
132: }
133:
134: protected ContentOutlinePage getContentOutline() {
135: if (ruleContentOutline == null) {
136: ruleContentOutline = new RuleContentOutlinePage(this );
137: ruleContentOutline.update();
138: }
139: return ruleContentOutline;
140: }
141:
142: public String getContent() {
143: return getSourceViewer().getDocument().get();
144: }
145:
146: public IResource getResource() {
147: if (getEditorInput() instanceof IFileEditorInput) {
148: return ((IFileEditorInput) getEditorInput()).getFile();
149: }
150: return null;
151: }
152:
153: public Object getAdapter(Class adapter) {
154: if (adapter.equals(IContentOutlinePage.class)) {
155: return getContentOutline();
156: }
157: return super .getAdapter(adapter);
158: }
159:
160: public void doSave(IProgressMonitor monitor) {
161: // invalidate cached parsed rules
162: DroolsEclipsePlugin.getDefault().invalidateResource(
163: getResource());
164: // save
165: super .doSave(monitor);
166: // update outline view
167: if (ruleContentOutline != null) {
168: ruleContentOutline.update();
169: }
170: }
171:
172: public void dispose() {
173: super.dispose();
174: if (bracketMatcher != null) {
175: bracketMatcher.dispose();
176: bracketMatcher = null;
177: }
178: }
179: }
|