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
;
31 import com
.intellij
.openapi
.ui
.TextComponentAccessor
;
35 import java
.awt
.event
.*;
36 import java
.util
.ArrayList
;
41 public class EditorComboBox
extends JComboBox
implements DocumentListener
{
42 public static TextComponentAccessor
<EditorComboBox
> COMPONENT_ACCESSOR
= new TextComponentAccessor
<EditorComboBox
>() {
43 public String
getText(EditorComboBox component
) {
44 return component
.getText();
47 public void setText(EditorComboBox component
, String text
) {
48 component
.setText(text
);
51 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ui.EditorTextField");
53 private Document myDocument
;
54 private Project myProject
;
55 private EditorTextField myEditorField
= null;
56 private final ArrayList
<DocumentListener
> myDocumentListeners
= new ArrayList
<DocumentListener
>();
57 private boolean myIsListenerInstalled
= false;
58 private boolean myInheritSwingFont
= true;
59 private final FileType myFileType
;
60 private final boolean myIsViewer
;
62 public EditorComboBox(String text
) {
63 this(EditorFactory
.getInstance().createDocument(text
), null, FileTypes
.PLAIN_TEXT
);
66 public EditorComboBox(String text
, Project project
, FileType fileType
) {
67 this(EditorFactory
.getInstance().createDocument(text
), project
, fileType
, false);
70 public EditorComboBox(Document document
, Project project
, FileType fileType
) {
71 this(document
, project
, fileType
, false);
74 public EditorComboBox(Document document
, Project project
, FileType fileType
, boolean isViewer
) {
75 myFileType
= fileType
;
76 myIsViewer
= isViewer
;
77 setDocument(document
);
79 enableEvents(AWTEvent
.KEY_EVENT_MASK
);
81 addActionListener(new ActionListener() {
82 public void actionPerformed(ActionEvent e
) {
83 final Editor editor
= myEditorField
.getEditor();
85 editor
.getSelectionModel().removeSelection();
89 setHistory(new String
[]{""});
93 public void setFontInheritedFromLAF(boolean b
) {
94 myInheritSwingFont
= b
;
95 setDocument(myDocument
); // reinit editor.
98 public String
getText() {
99 return myDocument
.getText();
102 public void addDocumentListener(DocumentListener listener
) {
103 myDocumentListeners
.add(listener
);
104 installDocumentListener();
107 public void removeDocumentListener(DocumentListener listener
) {
108 myDocumentListeners
.remove(listener
);
109 uninstallDocumentListener(false);
112 public void beforeDocumentChange(DocumentEvent event
) {
113 for (DocumentListener documentListener
: myDocumentListeners
) {
114 documentListener
.beforeDocumentChange(event
);
118 public void documentChanged(DocumentEvent event
) {
119 for (DocumentListener documentListener
: myDocumentListeners
) {
120 documentListener
.documentChanged(event
);
124 public Project
getProject() {
128 public Document
getDocument() {
132 public void setDocument(Document document
) {
133 if (myDocument
!= null) {
134 uninstallDocumentListener(true);
137 myDocument
= document
;
138 installDocumentListener();
139 if (myEditorField
== null) return;
141 myEditorField
.setDocument(document
);
144 private void installDocumentListener() {
145 if (myDocument
!= null && myDocumentListeners
.size() > 0 && !myIsListenerInstalled
) {
146 myIsListenerInstalled
= true;
147 myDocument
.addDocumentListener(this);
151 private void uninstallDocumentListener(boolean force
) {
152 if (myDocument
!= null && myIsListenerInstalled
&& (force
|| myDocumentListeners
.size() == 0)) {
153 myIsListenerInstalled
= false;
154 myDocument
.removeDocumentListener(this);
158 public void setText(final String text
) {
159 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
161 CommandProcessor
.getInstance().executeCommand(getProject(), new Runnable() {
163 myDocument
.replaceString(0, myDocument
.getTextLength(), text
);
164 if (myEditorField
!= null && myEditorField
.getEditor() != null) {
165 myEditorField
.getCaretModel().moveToOffset(myDocument
.getTextLength());
168 }, null, myDocument
);
173 public void removeSelection() {
174 if (myEditorField
!= null) {
175 final Editor editor
= myEditorField
.getEditor();
176 if (editor
!= null) {
177 editor
.getSelectionModel().removeSelection();
184 public CaretModel
getCaretModel() {
185 return myEditorField
.getCaretModel();
188 public void setHistory(final String
[] history
) {
189 setModel(new DefaultComboBoxModel(history
));
192 public void prependItem(String item
) {
193 ArrayList
<Object
> objects
= new ArrayList
<Object
>();
195 int count
= getItemCount();
196 for (int i
= 0; i
< count
; i
++) {
197 final Object itemAt
= getItemAt(i
);
198 if (!item
.equals(itemAt
)) {
202 setModel(new DefaultComboBoxModel(objects
.toArray(new Object
[objects
.size()])));
205 private class MyEditor
implements ComboBoxEditor
{
206 public void addActionListener(ActionListener l
) {
209 public Component
getEditorComponent() {
210 return myEditorField
;
213 public Object
getItem() {
214 return myDocument
.getText();
217 public void removeActionListener(ActionListener l
) {
220 public void selectAll() {
221 if (myEditorField
!= null) {
222 final Editor editor
= myEditorField
.getEditor();
223 if (editor
!= null) {
224 editor
.getSelectionModel().setSelection(0, myDocument
.getTextLength());
229 public void setItem(Object anObject
) {
230 if (anObject
!= null) {
231 EditorComboBox
.this.setText((String
)anObject
);
233 EditorComboBox
.this.setText("");
238 public void addNotify() {
245 private void setEditor() {
246 myEditorField
= new EditorTextField(myDocument
, myProject
, myFileType
, myIsViewer
) {
248 protected boolean shouldHaveBorder() {
249 return UIManager
.getBorder("ComboBox.border") == null;
252 final ComboBoxEditor editor
= new MyEditor();
254 setRenderer(new EditorComboBoxRenderer(editor
));
257 public void removeNotify() {
258 super.removeNotify();
259 if (myEditorField
!= null) {
261 myEditorField
= null;
265 private void releaseEditor() {
266 if (myEditorField
!= null) {
267 final Editor editor
= myEditorField
.getEditor();
268 if (editor
!= null) {
269 myEditorField
.releaseEditor(editor
);
274 public void setFont(Font font
) {
276 if (myEditorField
!= null && myEditorField
.getEditor() != null) {
277 setupEditorFont((EditorEx
)myEditorField
.getEditor());
281 private void setupEditorFont(final EditorEx editor
) {
282 if (myInheritSwingFont
) {
283 editor
.getColorsScheme().setEditorFontName(getFont().getFontName());
284 editor
.getColorsScheme().setEditorFontSize(getFont().getSize());
288 protected boolean shouldHaveBorder() {
292 public void setEnabled(boolean enabled
) {
293 super.setEnabled(enabled
);
294 if (myEditorField
== null) {
297 myEditorField
.setEnabled(enabled
);
300 public Dimension
getPreferredSize() {
301 if (myEditorField
!= null) {
302 final Dimension preferredSize
= new Dimension(myEditorField
.getComponent().getPreferredSize());
303 final Insets insets
= getInsets();
304 if (insets
!= null) {
305 preferredSize
.width
+= insets
.left
;
306 preferredSize
.width
+= insets
.right
;
307 preferredSize
.height
+= insets
.top
;
308 preferredSize
.height
+= insets
.bottom
;
310 return preferredSize
;
312 return new Dimension(100, 20);
315 protected boolean processKeyBinding(KeyStroke ks
, KeyEvent e
, int condition
, boolean pressed
) {
316 if (!((EditorEx
)myEditorField
.getEditor()).processKeyTyped(e
)) {
317 return super.processKeyBinding(ks
, e
, condition
, pressed
);
322 public EditorEx
getEditorEx() {
323 return myEditorField
!= null ?
(EditorEx
)myEditorField
.getEditor() : null;