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
.editor
.impl
;
18 import com
.intellij
.injected
.editor
.DocumentWindow
;
19 import com
.intellij
.openapi
.application
.ModalityStateListener
;
20 import com
.intellij
.openapi
.application
.impl
.LaterInvocator
;
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
.EditorFactory
;
25 import com
.intellij
.openapi
.editor
.event
.EditorEventMulticaster
;
26 import com
.intellij
.openapi
.editor
.event
.EditorFactoryEvent
;
27 import com
.intellij
.openapi
.editor
.event
.EditorFactoryListener
;
28 import com
.intellij
.openapi
.editor
.ex
.EditorEx
;
29 import com
.intellij
.openapi
.editor
.highlighter
.EditorHighlighterFactory
;
30 import com
.intellij
.openapi
.editor
.impl
.event
.EditorEventMulticasterImpl
;
31 import com
.intellij
.openapi
.fileTypes
.FileType
;
32 import com
.intellij
.openapi
.project
.Project
;
33 import com
.intellij
.openapi
.project
.ProjectManager
;
34 import com
.intellij
.openapi
.project
.ProjectManagerAdapter
;
35 import com
.intellij
.openapi
.util
.Key
;
36 import com
.intellij
.openapi
.util
.text
.StringUtil
;
37 import com
.intellij
.util
.EventDispatcher
;
38 import com
.intellij
.util
.SmartList
;
39 import com
.intellij
.util
.text
.CharArrayCharSequence
;
40 import org
.jetbrains
.annotations
.NotNull
;
43 import java
.util
.ArrayList
;
44 import java
.util
.List
;
46 public class EditorFactoryImpl
extends EditorFactory
{
47 private static final Logger LOG
= Logger
.getInstance("#com.intellij.openapi.editor.impl.EditorFactoryImpl");
48 private final EditorEventMulticasterImpl myEditorEventMulticaster
= new EditorEventMulticasterImpl();
49 private final EventDispatcher
<EditorFactoryListener
> myEditorFactoryEventDispatcher
= EventDispatcher
.create(EditorFactoryListener
.class);
50 private final ArrayList
<Editor
> myEditors
= new ArrayList
<Editor
>();
51 private static final Key
<String
> EDITOR_CREATOR
= new Key
<String
>("Editor creator");
52 private final ModalityStateListener myModalityStateListener
= new ModalityStateListener() {
53 public void beforeModalityStateChanged(boolean entering
) {
54 for (Editor editor
: myEditors
) {
55 ((EditorImpl
)editor
).beforeModalityStateChanged();
60 public EditorFactoryImpl(ProjectManager projectManager
) {
61 projectManager
.addProjectManagerListener(new ProjectManagerAdapter() {
62 public void projectClosed(final Project project
) {
63 SwingUtilities
.invokeLater(new Runnable() {
65 validateEditorsAreReleased(project
);
73 public String
getComponentName() {
74 return "EditorFactory";
77 public void initComponent() {
78 LaterInvocator
.addModalityStateListener(myModalityStateListener
);
81 public void validateEditorsAreReleased(Project project
) {
82 for (Editor editor
: myEditors
) {
83 if (editor
.getProject() == project
|| editor
.getProject() == null) {
84 fireEditorNotReleasedError(editor
);
89 private static void fireEditorNotReleasedError(final Editor editor
) {
90 final String creator
= editor
.getUserData(EDITOR_CREATOR
);
91 if (creator
== null) {
92 LOG
.error("Editor for the document with class:" + editor
.getClass().getName() +
93 " and the following text hasn't been released:\n" + editor
.getDocument().getText());
96 LOG
.error("Editor with class:" + editor
.getClass().getName() + " hasn't been released:\n" + creator
);
100 public void disposeComponent() {
101 LaterInvocator
.removeModalityStateListener(myModalityStateListener
);
105 public Document
createDocument(@NotNull char[] text
) {
106 return createDocument(new CharArrayCharSequence(text
));
110 public Document
createDocument(@NotNull CharSequence text
) {
111 DocumentImpl document
= new DocumentImpl(text
);
112 myEditorEventMulticaster
.registerDocument(document
);
117 public Document
createDocument(boolean allowUpdatesWithoutWriteAction
) {
118 DocumentImpl document
= new DocumentImpl(allowUpdatesWithoutWriteAction
);
119 myEditorEventMulticaster
.registerDocument(document
);
123 public void refreshAllEditors() {
124 for (Editor editor
: myEditors
) {
125 ((EditorEx
)editor
).reinitSettings();
129 public Editor
createEditor(@NotNull Document document
) {
130 return createEditor(document
, false, null);
133 public Editor
createViewer(@NotNull Document document
) {
134 return createEditor(document
, true, null);
137 public Editor
createEditor(@NotNull Document document
, Project project
) {
138 return createEditor(document
, false, project
);
141 public Editor
createViewer(@NotNull Document document
, Project project
) {
142 return createEditor(document
, true, project
);
145 public Editor
createEditor(@NotNull final Document document
, final Project project
, @NotNull final FileType fileType
, final boolean isViewer
) {
146 Editor editor
= createEditor(document
, isViewer
, project
);
147 ((EditorEx
)editor
).setHighlighter(EditorHighlighterFactory
.getInstance().createEditorHighlighter(project
, fileType
));
151 private Editor
createEditor(@NotNull Document document
, boolean isViewer
, Project project
) {
152 Document hostDocument
= document
instanceof DocumentWindow ?
((DocumentWindow
)document
).getDelegate() : document
;
153 EditorImpl editor
= new EditorImpl(hostDocument
, isViewer
, project
);
154 myEditors
.add(editor
);
155 myEditorEventMulticaster
.registerEditor(editor
);
156 myEditorFactoryEventDispatcher
.getMulticaster().editorCreated(new EditorFactoryEvent(this, editor
));
158 if (LOG
.isDebugEnabled()) {
159 LOG
.debug("number of Editor's:" + myEditors
.size());
160 //Thread.dumpStack();
163 String text
= StringUtil
.getThrowableText(new RuntimeException("Editor created"));
164 editor
.putUserData(EDITOR_CREATOR
, text
);
169 public void releaseEditor(@NotNull Editor editor
) {
170 editor
.putUserData(EDITOR_CREATOR
, null);
171 ((EditorImpl
)editor
).release();
172 myEditors
.remove(editor
);
173 myEditorFactoryEventDispatcher
.getMulticaster().editorReleased(new EditorFactoryEvent(this, editor
));
175 if (LOG
.isDebugEnabled()) {
176 LOG
.debug("number of Editor's:" + myEditors
.size());
177 //Thread.dumpStack();
182 public Editor
[] getEditors(@NotNull Document document
, Project project
) {
183 List
<Editor
> list
= null;
184 for (Editor editor
: myEditors
) {
185 Project project1
= editor
.getProject();
186 if (editor
.getDocument().equals(document
) && (project
== null || project1
== null || project1
.equals(project
))) {
187 if (list
== null) list
= new SmartList
<Editor
>();
191 return list
== null ? Editor
.EMPTY_ARRAY
: list
.toArray(new Editor
[list
.size()]);
195 public Editor
[] getEditors(@NotNull Document document
) {
196 return getEditors(document
, null);
200 public Editor
[] getAllEditors() {
201 return myEditors
.toArray(new Editor
[myEditors
.size()]);
204 public void addEditorFactoryListener(@NotNull EditorFactoryListener listener
) {
205 myEditorFactoryEventDispatcher
.addListener(listener
);
208 public void removeEditorFactoryListener(@NotNull EditorFactoryListener listener
) {
209 myEditorFactoryEventDispatcher
.removeListener(listener
);
213 public EditorEventMulticaster
getEventMulticaster() {
214 return myEditorEventMulticaster
;