2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.fileEditor.impl.text;
18 import com.intellij.codeHighlighting.BackgroundEditorHighlighter;
19 import com.intellij.ide.structureView.StructureViewBuilder;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.openapi.editor.Document;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.editor.LogicalPosition;
25 import com.intellij.openapi.editor.ScrollType;
26 import com.intellij.openapi.editor.ex.EditorEx;
27 import com.intellij.openapi.editor.ex.util.EditorUtil;
28 import com.intellij.openapi.fileEditor.*;
29 import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
30 import com.intellij.openapi.fileTypes.BinaryFileTypeDecompilers;
31 import com.intellij.openapi.fileTypes.FileType;
32 import com.intellij.openapi.project.DumbAware;
33 import com.intellij.openapi.project.Project;
34 import com.intellij.openapi.project.ProjectManager;
35 import com.intellij.openapi.util.Disposer;
36 import com.intellij.openapi.util.Key;
37 import com.intellij.openapi.util.UserDataHolderBase;
38 import com.intellij.openapi.vfs.VirtualFile;
39 import com.intellij.pom.Navigatable;
40 import org.jdom.Element;
41 import org.jetbrains.annotations.NonNls;
42 import org.jetbrains.annotations.NotNull;
43 import org.jetbrains.annotations.Nullable;
46 import java.beans.PropertyChangeListener;
49 * @author Anton Katilin
50 * @author Vladimir Kondratyev
52 public class TextEditorProvider implements FileEditorProvider, DumbAware {
53 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.fileEditor.impl.text.TextEditorProvider");
54 private static final Key<TextEditor> TEXT_EDITOR_KEY = Key.create("textEditor");
56 @NonNls private static final String TYPE_ID = "text-editor";
57 @NonNls private static final String LINE_ATTR = "line";
58 @NonNls private static final String COLUMN_ATTR = "column";
59 @NonNls private static final String SELECTION_START_ATTR = "selection-start";
60 @NonNls private static final String SELECTION_END_ATTR = "selection-end";
61 @NonNls private static final String VERTICAL_SCROLL_PROPORTION_ATTR = "vertical-scroll-proportion";
63 public static TextEditorProvider getInstance() {
64 return ApplicationManager.getApplication().getComponent(TextEditorProvider.class);
67 public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
68 if (file.isDirectory() || !file.isValid()) {
72 final FileType ft = file.getFileType();
73 return !ft.isBinary() || BinaryFileTypeDecompilers.INSTANCE.forFileType(ft) != null;
77 public FileEditor createEditor(@NotNull Project project, @NotNull final VirtualFile file) {
78 LOG.assertTrue(accept(project, file));
79 return new TextEditorImpl(project, file, this);
82 public void disposeEditor(@NotNull FileEditor editor) {
83 Disposer.dispose(editor);
87 public FileEditorState readState(@NotNull Element element, @NotNull Project project, @NotNull VirtualFile file) {
88 TextEditorState state = new TextEditorState();
91 state.LINE = Integer.parseInt(element.getAttributeValue(LINE_ATTR));
92 state.COLUMN = Integer.parseInt(element.getAttributeValue(COLUMN_ATTR));
93 state.SELECTION_START = Integer.parseInt(element.getAttributeValue(SELECTION_START_ATTR));
94 state.SELECTION_END = Integer.parseInt(element.getAttributeValue(SELECTION_END_ATTR));
95 state.VERTICAL_SCROLL_PROPORTION = Float.parseFloat(element.getAttributeValue(VERTICAL_SCROLL_PROPORTION_ATTR));
97 catch (NumberFormatException ignored) {
103 public void writeState(@NotNull FileEditorState _state, @NotNull Project project, @NotNull Element element) {
104 TextEditorState state = (TextEditorState)_state;
106 element.setAttribute(LINE_ATTR, Integer.toString(state.LINE));
107 element.setAttribute(COLUMN_ATTR, Integer.toString(state.COLUMN));
108 element.setAttribute(SELECTION_START_ATTR, Integer.toString(state.SELECTION_START));
109 element.setAttribute(SELECTION_END_ATTR, Integer.toString(state.SELECTION_END));
110 element.setAttribute(VERTICAL_SCROLL_PROPORTION_ATTR, Float.toString(state.VERTICAL_SCROLL_PROPORTION));
114 public String getEditorTypeId() {
119 public FileEditorPolicy getPolicy() {
120 return FileEditorPolicy.NONE;
124 public TextEditor getTextEditor(@NotNull Editor editor) {
125 TextEditor textEditor = editor.getUserData(TEXT_EDITOR_KEY);
126 if (textEditor == null) {
127 textEditor = createWrapperForEditor(editor);
128 putTextEditor(editor, textEditor);
134 protected EditorWrapper createWrapperForEditor(final Editor editor) {
135 return new EditorWrapper(editor);
139 public static Document[] getDocuments(@NotNull FileEditor editor) {
140 if (editor instanceof DocumentsEditor) {
141 DocumentsEditor documentsEditor = (DocumentsEditor)editor;
142 Document[] documents = documentsEditor.getDocuments();
143 return documents.length > 0 ? documents : null;
146 if (editor instanceof TextEditor) {
147 Document document = ((TextEditor)editor).getEditor().getDocument();
148 return new Document[]{document};
151 Project[] projects = ProjectManager.getInstance().getOpenProjects();
152 for (int i = projects.length - 1; i >= 0; i--) {
153 VirtualFile file = FileEditorManagerEx.getInstanceEx(projects[i]).getFile(editor);
155 Document document = FileDocumentManager.getInstance().getDocument(file);
156 if (document != null) {
157 return new Document[]{document};
165 static void putTextEditor(Editor editor, TextEditor textEditor) {
166 editor.putUserData(TEXT_EDITOR_KEY, textEditor);
169 protected TextEditorState getStateImpl(final Project project, final Editor editor, @NotNull FileEditorStateLevel level){
170 TextEditorState state = new TextEditorState();
171 state.LINE = editor.getCaretModel().getLogicalPosition().line;
172 state.COLUMN = editor.getCaretModel().getLogicalPosition().column;
173 state.SELECTION_START = editor.getSelectionModel().getSelectionStart();
174 state.SELECTION_END = editor.getSelectionModel().getSelectionEnd();
176 // Saving scrolling proportion on UNDO may cause undesirable results of undo action fails to perform since
177 // scrolling proportion restored slightly differs from what have been saved.
178 state.VERTICAL_SCROLL_PROPORTION = level == FileEditorStateLevel.UNDO ? -1 : EditorUtil.calcVerticalScrollProportion(editor);
182 protected void setStateImpl(final Project project, final Editor editor, final TextEditorState state){
183 LogicalPosition pos = new LogicalPosition(state.LINE, state.COLUMN);
184 editor.getCaretModel().moveToLogicalPosition(pos);
185 editor.getSelectionModel().removeSelection();
186 editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
188 if (state.VERTICAL_SCROLL_PROPORTION != -1) {
189 EditorUtil.setVerticalScrollProportion(editor, state.VERTICAL_SCROLL_PROPORTION);
192 final Document document = editor.getDocument();
194 if (state.SELECTION_START == state.SELECTION_END) {
195 editor.getSelectionModel().removeSelection();
198 int startOffset = Math.min(state.SELECTION_START, document.getTextLength());
199 int endOffset = Math.min(state.SELECTION_END, document.getTextLength());
200 editor.getSelectionModel().setSelection(startOffset, endOffset);
202 ((EditorEx) editor).stopOptimizedScrolling();
203 editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
206 protected class EditorWrapper extends UserDataHolderBase implements TextEditor {
207 private final Editor myEditor;
209 public EditorWrapper(Editor editor) {
214 public Editor getEditor() {
219 public JComponent getComponent() {
220 return myEditor.getComponent();
223 public JComponent getPreferredFocusedComponent() {
224 return myEditor.getContentComponent();
228 public String getName() {
232 public StructureViewBuilder getStructureViewBuilder() {
233 VirtualFile file = FileDocumentManager.getInstance().getFile(myEditor.getDocument());
234 if (file == null) return null;
236 final Project project = myEditor.getProject();
237 LOG.assertTrue(project != null);
238 return StructureViewBuilder.PROVIDER.getStructureViewBuilder(file.getFileType(), file, project);
242 public FileEditorState getState(@NotNull FileEditorStateLevel level) {
243 return getStateImpl(null, myEditor, level);
246 public void setState(@NotNull FileEditorState state) {
247 setStateImpl(null, myEditor, (TextEditorState)state);
250 public boolean isModified() {
254 public boolean isValid() {
258 public void dispose() { }
260 public void selectNotify() { }
262 public void deselectNotify() { }
264 public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) { }
266 public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) { }
268 public BackgroundEditorHighlighter getBackgroundHighlighter() {
272 public FileEditorLocation getCurrentLocation() {
276 public boolean canNavigateTo(@NotNull final Navigatable navigatable) {
280 public void navigateTo(@NotNull final Navigatable navigatable) {