update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / util / xml / ui / TextControl.java
blob485b2b049ecd5ab4b3df43dc1019e8593c821bb4
1 /*
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.util.xml.ui;
18 import com.intellij.openapi.editor.Document;
19 import com.intellij.openapi.editor.ex.EditorEx;
20 import com.intellij.openapi.fileTypes.FileTypes;
21 import com.intellij.openapi.fileTypes.PlainTextLanguage;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.ui.DialogBuilder;
24 import com.intellij.psi.PsiDocumentManager;
25 import com.intellij.psi.PsiFileFactory;
26 import com.intellij.ui.EditorTextField;
27 import com.intellij.ui.ReferenceEditorWithBrowseButton;
28 import com.intellij.ui.UIBundle;
29 import com.intellij.util.Function;
30 import org.jetbrains.annotations.NotNull;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
37 /**
38 * @author peter
40 public class TextControl extends EditorTextFieldControl<TextPanel> {
42 public TextControl(final DomWrapper<String> domWrapper) {
43 super(domWrapper);
46 public TextControl(final DomWrapper<String> domWrapper, final boolean commitOnEveryChange) {
47 super(domWrapper, commitOnEveryChange);
50 protected EditorTextField getEditorTextField(@NotNull final TextPanel panel) {
51 final Component component = panel.getComponent(0);
52 if (component instanceof ReferenceEditorWithBrowseButton) {
53 return ((ReferenceEditorWithBrowseButton)component).getEditorTextField();
55 return (EditorTextField)component;
58 protected TextPanel createMainComponent(TextPanel boundedComponent, final Project project) {
59 if (boundedComponent == null) {
60 boundedComponent = new TextPanel();
62 boundedComponent.removeAll();
63 final Function<String, Document> factory = new Function<String, Document>() {
64 public Document fun(final String s) {
65 return PsiDocumentManager.getInstance(project)
66 .getDocument(PsiFileFactory.getInstance(project).createFileFromText("a.txt", PlainTextLanguage.INSTANCE, "", true, false));
69 final TextPanel boundedComponent1 = boundedComponent;
70 final EditorTextField editorTextField = new EditorTextField(factory.fun(""), project, FileTypes.PLAIN_TEXT) {
71 protected EditorEx createEditor() {
72 final EditorEx editor = super.createEditor();
73 return boundedComponent1 instanceof MultiLineTextPanel ? makeBigEditor(editor, ((MultiLineTextPanel)boundedComponent1).getRowCount()) : editor;
77 if (boundedComponent instanceof BigTextPanel) {
78 final ReferenceEditorWithBrowseButton editor = new ReferenceEditorWithBrowseButton(null, editorTextField, factory);
79 boundedComponent.add(editor);
80 editor.addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent e) {
82 EditorTextField textArea = new EditorTextField(editorTextField.getDocument(), project, FileTypes.PLAIN_TEXT) {
83 protected EditorEx createEditor() {
84 final EditorEx editor = super.createEditor();
85 editor.setEmbeddedIntoDialogWrapper(true);
86 return makeBigEditor(editor, 5);
89 DialogBuilder builder = new DialogBuilder(project);
90 builder.setDimensionServiceKey("TextControl");
91 builder.setCenterPanel(textArea);
92 builder.setPreferedFocusComponent(textArea);
93 builder.setTitle(UIBundle.message("big.text.control.window.title"));
94 builder.addCloseButton();
95 builder.show();
97 });
98 return boundedComponent;
101 boundedComponent.add(editorTextField);
102 return boundedComponent;
105 private static EditorEx makeBigEditor(final EditorEx editor, int rowCount) {
106 editor.setVerticalScrollbarVisible(true);
107 editor.setOneLineMode(false);
108 final JTextArea area = new JTextArea(10, 50);
109 area.setRows(rowCount);
110 editor.getComponent().setPreferredSize(area.getPreferredSize());
111 return editor;