2 * Copyright 2006 Sascha Weinreuter
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 org
.intellij
.plugins
.intelliLang
.util
;
18 import com
.intellij
.ide
.highlighter
.HighlighterFactory
;
19 import com
.intellij
.lang
.Language
;
20 import com
.intellij
.openapi
.editor
.Document
;
21 import com
.intellij
.openapi
.editor
.EditorFactory
;
22 import com
.intellij
.openapi
.editor
.ex
.EditorEx
;
23 import com
.intellij
.openapi
.fileTypes
.FileType
;
24 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.psi
.PsiDocumentManager
;
27 import com
.intellij
.psi
.PsiFile
;
28 import com
.intellij
.psi
.PsiFileFactory
;
29 import com
.intellij
.ui
.EditorTextField
;
30 import com
.intellij
.util
.Consumer
;
31 import com
.intellij
.util
.LocalTimeCounter
;
32 import org
.jetbrains
.annotations
.NotNull
;
33 import org
.jetbrains
.annotations
.Nullable
;
35 public class LanguageTextField
extends EditorTextField
{
36 private final Language myLanguage
;
37 private final Project myProject
;
39 public LanguageTextField(Language language
, @NotNull Project project
, @NotNull String value
) {
40 this(language
, project
, value
, null);
43 public LanguageTextField(@Nullable Language language
,
44 @NotNull Project project
,
45 @NotNull String value
,
46 @Nullable Consumer
<PsiFile
> tagger
) {
47 super(createDocument(value
, language
, project
, tagger
), project
,
48 language
!= null ? language
.getAssociatedFileType() : StdFileTypes
.PLAIN_TEXT
, language
== null);
50 myLanguage
= language
;
53 setEnabled(language
!= null);
55 ShiftTabAction
.attachTo(this);
58 private static Document
createDocument(String value
, @Nullable Language language
, Project project
, @Nullable Consumer
<PsiFile
> tagger
) {
59 if (language
!= null) {
60 final PsiFileFactory factory
= PsiFileFactory
.getInstance(project
);
61 final FileType fileType
= language
.getAssociatedFileType();
62 assert fileType
!= null;
64 final long stamp
= LocalTimeCounter
.currentTime();
65 final PsiFile psiFile
= factory
.createFileFromText("Dummy." + fileType
.getDefaultExtension(), fileType
, value
, stamp
, true, false);
67 tagger
.consume(psiFile
);
69 final Document document
= PsiDocumentManager
.getInstance(project
).getDocument(psiFile
);
70 assert document
!= null;
74 return EditorFactory
.getInstance().createDocument(value
);
78 protected EditorEx
createEditor() {
79 final EditorEx ex
= super.createEditor();
81 if (myLanguage
!= null) {
82 final FileType fileType
= myLanguage
.getAssociatedFileType();
83 ex
.setHighlighter(HighlighterFactory
.createHighlighter(myProject
, fileType
));
85 ex
.setEmbeddedIntoDialogWrapper(true);