[13] | 1 | package com.framsticks.framclipse.editors; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.HashMap; |
---|
| 5 | import java.util.List; |
---|
| 6 | import java.util.ResourceBundle; |
---|
| 7 | |
---|
| 8 | import org.eclipse.jface.action.IAction; |
---|
| 9 | import org.eclipse.jface.text.Position; |
---|
| 10 | import org.eclipse.jface.text.TextSelection; |
---|
| 11 | import org.eclipse.jface.text.TextViewer; |
---|
| 12 | import org.eclipse.jface.text.source.Annotation; |
---|
| 13 | import org.eclipse.jface.text.source.ISourceViewer; |
---|
| 14 | import org.eclipse.jface.text.source.IVerticalRuler; |
---|
| 15 | import org.eclipse.jface.text.source.projection.ProjectionAnnotation; |
---|
| 16 | import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; |
---|
| 17 | import org.eclipse.jface.text.source.projection.ProjectionSupport; |
---|
| 18 | import org.eclipse.jface.text.source.projection.ProjectionViewer; |
---|
| 19 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
---|
| 20 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
---|
| 21 | import org.eclipse.swt.widgets.Composite; |
---|
| 22 | import org.eclipse.ui.editors.text.TextEditor; |
---|
| 23 | import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; |
---|
| 24 | import org.eclipse.ui.texteditor.TextOperationAction; |
---|
| 25 | import org.eclipse.ui.views.contentoutline.IContentOutlinePage; |
---|
| 26 | |
---|
| 27 | import com.framsticks.framclipse.editors.configuration.FramscriptSourceViewerConfiguration; |
---|
| 28 | import com.framsticks.framclipse.editors.folding.FramclipseFoldingAnnotation; |
---|
| 29 | import com.framsticks.framclipse.editors.outline.FramclipseEditorOutlinePage; |
---|
| 30 | import com.framsticks.framclipse.internal.parser.ElementWithOffset; |
---|
| 31 | import com.framsticks.framclipse.internal.parser.Node; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | public class FramclipseEditor extends TextEditor { |
---|
| 35 | |
---|
| 36 | protected final EditorType editorType; |
---|
| 37 | private ProjectionSupport projectionSupport; |
---|
| 38 | private List<FramclipseFoldingAnnotation> prevAnnotations = new ArrayList<FramclipseFoldingAnnotation>(); |
---|
| 39 | private ProjectionAnnotationModel annotationModel; |
---|
| 40 | |
---|
| 41 | private FramclipseEditorOutlinePage outlinePage = null; |
---|
| 42 | private boolean outlineRequested = false; |
---|
| 43 | |
---|
| 44 | protected Node fileModel = null; |
---|
| 45 | |
---|
| 46 | public FramclipseEditor(EditorType editorType) { |
---|
| 47 | System.out.println("starting framclipse editor of type: " + editorType); |
---|
| 48 | this.editorType = editorType; |
---|
| 49 | setSourceViewerConfiguration(new FramscriptSourceViewerConfiguration(editorType, this)); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | @Override |
---|
| 53 | public void createPartControl(Composite parent) { |
---|
| 54 | |
---|
| 55 | super.createPartControl(parent); |
---|
| 56 | ProjectionViewer viewer =(ProjectionViewer)getSourceViewer(); |
---|
| 57 | |
---|
| 58 | projectionSupport = new ProjectionSupport(viewer,getAnnotationAccess(),getSharedColors()); |
---|
| 59 | projectionSupport.install(); |
---|
| 60 | |
---|
| 61 | //turn projection mode on |
---|
| 62 | viewer.doOperation(ProjectionViewer.TOGGLE); |
---|
| 63 | |
---|
| 64 | annotationModel = viewer.getProjectionAnnotationModel(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * The <code>FramscriptEditor</code> implementation of this |
---|
| 69 | * <code>AbstractTextEditor</code> method extend the actions to add those |
---|
| 70 | * specific to the receiver. |
---|
| 71 | */ |
---|
| 72 | @Override |
---|
| 73 | protected void createActions() { |
---|
| 74 | super.createActions(); |
---|
| 75 | |
---|
| 76 | ResourceBundle resourceBundle = FramclipseEditorMessages.getResourceBundle(); |
---|
| 77 | String prefix = "ContentAssistProposal."; |
---|
| 78 | int operationCode = ISourceViewer.CONTENTASSIST_PROPOSALS; |
---|
| 79 | IAction a = new TextOperationAction(resourceBundle, prefix, this, operationCode); |
---|
| 80 | String id = ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS; |
---|
| 81 | a.setActionDefinitionId(id); |
---|
| 82 | setAction("ContentAssistProposal", a); //$NON-NLS-1$ |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public EditorType getEditorType() { |
---|
| 86 | return editorType; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | @Override |
---|
| 90 | protected ISourceViewer createSourceViewer(Composite parent, |
---|
| 91 | IVerticalRuler ruler, int styles) { |
---|
| 92 | ISourceViewer viewer = new ProjectionViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles); |
---|
| 93 | |
---|
| 94 | // ensure decoration support has been created and configured. |
---|
| 95 | getSourceViewerDecorationSupport(viewer); |
---|
| 96 | |
---|
| 97 | ((TextViewer)viewer).addPostSelectionChangedListener(new ISelectionChangedListener() { |
---|
| 98 | |
---|
| 99 | public void selectionChanged(SelectionChangedEvent event) { |
---|
| 100 | |
---|
| 101 | TextSelection selection = (TextSelection) event.getSelection(); |
---|
| 102 | |
---|
| 103 | if(getFileModel() != null) |
---|
| 104 | { |
---|
| 105 | Node element = ((ElementWithOffset)getFileModel()).getElementForOffset(selection.getOffset()); |
---|
| 106 | //System.out.println("selected: " + element); |
---|
| 107 | if(outlinePage != null) |
---|
| 108 | outlinePage.setSelectedElement(element); |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | }); |
---|
| 115 | |
---|
| 116 | return viewer; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | public void updateFoldingStructure(List<FramclipseFoldingAnnotation> annotations) |
---|
| 120 | { |
---|
| 121 | List<Annotation> removals = new ArrayList<Annotation>(); |
---|
| 122 | HashMap newAnnotations = new HashMap(); |
---|
| 123 | |
---|
| 124 | //System.out.println("got " + annotations.size() + " annotations."); |
---|
| 125 | |
---|
| 126 | for(int l = 0; l < prevAnnotations.size(); ++l) |
---|
| 127 | { |
---|
| 128 | FramclipseFoldingAnnotation equiv = prevAnnotations.get(l).hasEquivalent(annotations, getSourceViewer().getDocument()); |
---|
| 129 | if(equiv == null) |
---|
| 130 | { |
---|
| 131 | removals.add(prevAnnotations.get(l)); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | annotations.remove(equiv); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | for(Annotation annot : removals) |
---|
| 140 | prevAnnotations.remove(annot); |
---|
| 141 | |
---|
| 142 | for(FramclipseFoldingAnnotation annot : annotations) |
---|
| 143 | { |
---|
| 144 | //System.out.println("adding for: " + annot.getAnnotatedElement()); |
---|
| 145 | prevAnnotations.add(annot); |
---|
| 146 | newAnnotations.put(annot, new Position(annot.getAnnotatedElement().getBeginOffset(), |
---|
| 147 | annot.getAnnotationLength())); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | ProjectionAnnotation[] toDelete = new ProjectionAnnotation[removals.size()]; |
---|
| 151 | removals.toArray(toDelete); |
---|
| 152 | |
---|
| 153 | annotationModel.modifyAnnotations(toDelete,newAnnotations,null); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | public Node getFileModel() { |
---|
| 157 | return fileModel; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | public void setFileModel(Node fileModel) { |
---|
| 161 | this.fileModel = fileModel; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | @Override |
---|
| 165 | public Object getAdapter(Class adapter) { |
---|
| 166 | if(IContentOutlinePage.class.equals(adapter)) |
---|
| 167 | { |
---|
| 168 | outlineRequested = true; |
---|
| 169 | |
---|
| 170 | if(outlinePage == null) |
---|
| 171 | { |
---|
| 172 | outlinePage = new FramclipseEditorOutlinePage(this); |
---|
| 173 | if(getFileModel() != null) |
---|
| 174 | outlinePage.setInput(getFileModel()); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | return outlinePage; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | return super.getAdapter(adapter); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | public void updateOutlinePage() |
---|
| 184 | { |
---|
| 185 | if(outlineRequested) |
---|
| 186 | { |
---|
| 187 | if(outlinePage == null) |
---|
| 188 | { |
---|
| 189 | outlinePage = new FramclipseEditorOutlinePage(this); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | //if(getFileModel() != null) |
---|
| 193 | outlinePage.setInput(getFileModel()); |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | } |
---|