layout fixes for "create class" dialog (IDEADEV-40489)
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / EditorComboBox.java
blob93c933a961d9eda55ea10e39325a46ffcd736c7f
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.ui;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.command.CommandProcessor;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.editor.CaretModel;
22 import com.intellij.openapi.editor.Document;
23 import com.intellij.openapi.editor.EditorFactory;
24 import com.intellij.openapi.editor.Editor;
25 import com.intellij.openapi.editor.event.DocumentEvent;
26 import com.intellij.openapi.editor.event.DocumentListener;
27 import com.intellij.openapi.editor.ex.EditorEx;
28 import com.intellij.openapi.fileTypes.FileType;
29 import com.intellij.openapi.fileTypes.FileTypes;
30 import com.intellij.openapi.project.Project;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.util.ArrayList;
37 /**
38 * @author max
40 public class EditorComboBox extends JComboBox implements DocumentListener {
41 private static final Logger LOG = Logger.getInstance("#com.intellij.ui.EditorTextField");
43 private Document myDocument;
44 private Project myProject;
45 private EditorTextField myEditorField = null;
46 private final ArrayList<DocumentListener> myDocumentListeners = new ArrayList<DocumentListener>();
47 private boolean myIsListenerInstalled = false;
48 private boolean myInheritSwingFont = true;
49 private final FileType myFileType;
50 private final boolean myIsViewer;
52 public EditorComboBox(String text) {
53 this(EditorFactory.getInstance().createDocument(text), null, FileTypes.PLAIN_TEXT);
56 public EditorComboBox(String text, Project project, FileType fileType) {
57 this(EditorFactory.getInstance().createDocument(text), project, fileType, false);
60 public EditorComboBox(Document document, Project project, FileType fileType) {
61 this(document, project, fileType, false);
64 public EditorComboBox(Document document, Project project, FileType fileType, boolean isViewer) {
65 myFileType = fileType;
66 myIsViewer = isViewer;
67 setDocument(document);
68 myProject = project;
69 enableEvents(AWTEvent.KEY_EVENT_MASK);
71 addActionListener(new ActionListener() {
72 public void actionPerformed(ActionEvent e) {
73 final Editor editor = myEditorField.getEditor();
74 if (editor != null) {
75 editor.getSelectionModel().removeSelection();
78 });
79 setHistory(new String[]{""});
80 setEditable(true);
83 public void setFontInheritedFromLAF(boolean b) {
84 myInheritSwingFont = b;
85 setDocument(myDocument); // reinit editor.
88 public String getText() {
89 return myDocument.getText();
92 public void addDocumentListener(DocumentListener listener) {
93 myDocumentListeners.add(listener);
94 installDocumentListener();
97 public void removeDocumentListener(DocumentListener listener) {
98 myDocumentListeners.remove(listener);
99 uninstallDocumentListener(false);
102 public void beforeDocumentChange(DocumentEvent event) {
103 for (DocumentListener documentListener : myDocumentListeners) {
104 documentListener.beforeDocumentChange(event);
108 public void documentChanged(DocumentEvent event) {
109 for (DocumentListener documentListener : myDocumentListeners) {
110 documentListener.documentChanged(event);
114 public Project getProject() {
115 return myProject;
118 public Document getDocument() {
119 return myDocument;
122 public void setDocument(Document document) {
123 if (myDocument != null) {
124 uninstallDocumentListener(true);
127 myDocument = document;
128 installDocumentListener();
129 if (myEditorField == null) return;
131 myEditorField.setDocument(document);
134 private void installDocumentListener() {
135 if (myDocument != null && myDocumentListeners.size() > 0 && !myIsListenerInstalled) {
136 myIsListenerInstalled = true;
137 myDocument.addDocumentListener(this);
141 private void uninstallDocumentListener(boolean force) {
142 if (myDocument != null && myIsListenerInstalled && (force || myDocumentListeners.size() == 0)) {
143 myIsListenerInstalled = false;
144 myDocument.removeDocumentListener(this);
148 public void setText(final String text) {
149 ApplicationManager.getApplication().runWriteAction(new Runnable() {
150 public void run() {
151 CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
152 public void run() {
153 myDocument.replaceString(0, myDocument.getTextLength(), text);
154 if (myEditorField != null && myEditorField.getEditor() != null) {
155 myEditorField.getCaretModel().moveToOffset(myDocument.getTextLength());
158 }, null, myDocument);
163 public void removeSelection() {
164 if (myEditorField != null) {
165 final Editor editor = myEditorField.getEditor();
166 if (editor != null) {
167 editor.getSelectionModel().removeSelection();
170 else {
174 public CaretModel getCaretModel() {
175 return myEditorField.getCaretModel();
178 public void setHistory(final String[] history) {
179 setModel(new DefaultComboBoxModel(history));
182 public void prependItem(String item) {
183 ArrayList<Object> objects = new ArrayList<Object>();
184 objects.add(item);
185 int count = getItemCount();
186 for (int i = 0; i < count; i++) {
187 final Object itemAt = getItemAt(i);
188 if (!item.equals(itemAt)) {
189 objects.add(itemAt);
192 setModel(new DefaultComboBoxModel(objects.toArray(new Object[objects.size()])));
195 private class MyEditor implements ComboBoxEditor {
196 public void addActionListener(ActionListener l) {
199 public Component getEditorComponent() {
200 return myEditorField;
203 public Object getItem() {
204 return myDocument.getText();
207 public void removeActionListener(ActionListener l) {
210 public void selectAll() {
211 if (myEditorField != null) {
212 final Editor editor = myEditorField.getEditor();
213 if (editor != null) {
214 editor.getSelectionModel().setSelection(0, myDocument.getTextLength());
219 public void setItem(Object anObject) {
220 if (anObject != null) {
221 EditorComboBox.this.setText((String)anObject);
222 } else {
223 EditorComboBox.this.setText("");
228 public void addNotify() {
229 releaseEditor();
230 setEditor();
232 super.addNotify();
235 private void setEditor() {
236 myEditorField = new EditorTextField(myDocument, myProject, myFileType, myIsViewer) {
237 @Override
238 protected boolean shouldHaveBorder() {
239 return UIManager.getBorder("ComboBox.border") == null;
242 final ComboBoxEditor editor = new MyEditor();
243 setEditor(editor);
244 setRenderer(new EditorComboBoxRenderer(editor));
247 public void removeNotify() {
248 super.removeNotify();
249 if (myEditorField != null) {
250 releaseEditor();
251 myEditorField = null;
255 private void releaseEditor() {
256 if (myEditorField != null) {
257 final Editor editor = myEditorField.getEditor();
258 if (editor != null) {
259 myEditorField.releaseEditor(editor);
264 public void setFont(Font font) {
265 super.setFont(font);
266 if (myEditorField != null && myEditorField.getEditor() != null) {
267 setupEditorFont((EditorEx)myEditorField.getEditor());
271 private void setupEditorFont(final EditorEx editor) {
272 if (myInheritSwingFont) {
273 editor.getColorsScheme().setEditorFontName(getFont().getFontName());
274 editor.getColorsScheme().setEditorFontSize(getFont().getSize());
278 protected boolean shouldHaveBorder() {
279 return true;
282 public void setEnabled(boolean enabled) {
283 super.setEnabled(enabled);
284 if (myEditorField == null) {
285 return;
287 myEditorField.setEnabled(enabled);
290 public Dimension getPreferredSize() {
291 if (myEditorField != null) {
292 final Dimension preferredSize = new Dimension(myEditorField.getComponent().getPreferredSize());
293 final Insets insets = getInsets();
294 if (insets != null) {
295 preferredSize.width += insets.left;
296 preferredSize.width += insets.right;
297 preferredSize.height += insets.top;
298 preferredSize.height += insets.bottom;
300 return preferredSize;
302 return new Dimension(100, 20);
305 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
306 if (!((EditorEx)myEditorField.getEditor()).processKeyTyped(e)) {
307 return super.processKeyBinding(ks, e, condition, pressed);
309 return true;
312 public EditorEx getEditorEx() {
313 return myEditorField != null ? (EditorEx)myEditorField.getEditor() : null;