source: java/FramclipsePlugin/src/main/java/com/framsticks/framclipse/editors/FramclipseEditor.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 6.0 KB
Line 
1package com.framsticks.framclipse.editors;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.ResourceBundle;
7
8import org.eclipse.jface.action.IAction;
9import org.eclipse.jface.text.Position;
10import org.eclipse.jface.text.TextSelection;
11import org.eclipse.jface.text.TextViewer;
12import org.eclipse.jface.text.source.Annotation;
13import org.eclipse.jface.text.source.ISourceViewer;
14import org.eclipse.jface.text.source.IVerticalRuler;
15import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
16import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
17import org.eclipse.jface.text.source.projection.ProjectionSupport;
18import org.eclipse.jface.text.source.projection.ProjectionViewer;
19import org.eclipse.jface.viewers.ISelectionChangedListener;
20import org.eclipse.jface.viewers.SelectionChangedEvent;
21import org.eclipse.swt.widgets.Composite;
22import org.eclipse.ui.editors.text.TextEditor;
23import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
24import org.eclipse.ui.texteditor.TextOperationAction;
25import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
26
27import com.framsticks.framclipse.editors.configuration.FramscriptSourceViewerConfiguration;
28import com.framsticks.framclipse.editors.folding.FramclipseFoldingAnnotation;
29import com.framsticks.framclipse.editors.outline.FramclipseEditorOutlinePage;
30import com.framsticks.framclipse.internal.parser.ElementWithOffset;
31import com.framsticks.framclipse.internal.parser.Node;
32
33
34public 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}
Note: See TracBrowser for help on using the repository browser.