2 * Copyright 2000-2007 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
.ui
;
18 import com
.intellij
.CommonBundle
;
19 import com
.intellij
.openapi
.application
.Application
;
20 import com
.intellij
.openapi
.application
.ApplicationManager
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.util
.TextRange
;
24 import com
.intellij
.openapi
.util
.text
.StringUtil
;
25 import com
.intellij
.ui
.BrowserHyperlinkListener
;
26 import com
.intellij
.ui
.DocumentAdapter
;
27 import com
.intellij
.ui
.InsertPathAction
;
28 import com
.intellij
.ui
.ScrollPaneFactory
;
29 import com
.intellij
.util
.ui
.UIUtil
;
30 import org
.jetbrains
.annotations
.Nls
;
31 import org
.jetbrains
.annotations
.NonNls
;
32 import org
.jetbrains
.annotations
.Nullable
;
33 import org
.jetbrains
.annotations
.TestOnly
;
36 import javax
.swing
.event
.DocumentEvent
;
37 import javax
.swing
.plaf
.basic
.BasicHTML
;
38 import javax
.swing
.text
.html
.HTMLEditorKit
;
40 import java
.awt
.event
.ActionEvent
;
41 import java
.awt
.event
.ItemEvent
;
42 import java
.awt
.event
.ItemListener
;
44 public class Messages
{
45 private static TestDialog ourTestImplementation
= TestDialog
.DEFAULT
;
46 private static final TestInputDialog ourTestInputImplementation
= TestInputDialog
.DEFAULT
;
48 private static final Logger LOG
= Logger
.getInstance("#com.intellij.openapi.ui.Messages");
49 protected static final String OK_BUTTON
= CommonBundle
.getOkButtonText();
50 protected static final String YES_BUTTON
= CommonBundle
.getYesButtonText();
51 protected static final String NO_BUTTON
= CommonBundle
.getNoButtonText();
52 protected static final String CANCEL_BUTTON
= CommonBundle
.getCancelButtonText();
55 public static TestDialog
setTestDialog(TestDialog newValue
) {
56 Application application
= ApplicationManager
.getApplication();
57 if (application
!= null) {
58 LOG
.assertTrue(application
.isUnitTestMode(), "This methos is available for tests only");
60 TestDialog oldValue
= ourTestImplementation
;
61 ourTestImplementation
= newValue
;
65 public static Icon
getErrorIcon() {
66 return UIUtil
.getErrorIcon();
69 public static Icon
getInformationIcon() {
70 return UIUtil
.getInformationIcon();
73 public static Icon
getWarningIcon() {
74 return UIUtil
.getWarningIcon();
77 public static Icon
getQuestionIcon() {
78 return UIUtil
.getQuestionIcon();
81 public static int showDialog(Project project
, String message
, String title
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
82 final Application application
= ApplicationManager
.getApplication();
83 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
84 return ourTestImplementation
.show(message
);
87 MessageDialog dialog
= new MessageDialog(project
, message
, title
, options
, defaultOptionIndex
, icon
);
89 return dialog
.getExitCode();
93 public static int showDialog(Component parent
, String message
, String title
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
94 final Application application
= ApplicationManager
.getApplication();
95 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
96 return ourTestImplementation
.show(message
);
99 MessageDialog dialog
= new MessageDialog(parent
, message
, title
, options
, defaultOptionIndex
, icon
);
101 return dialog
.getExitCode();
106 * Use this method only if you do not know project or component
108 * @see #showDialog(Project, String, String, String[], int, Icon)
109 * @see #showDialog(Component, String, String, String[], int, Icon)
111 public static int showDialog(String message
, String title
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
113 final Application application
= ApplicationManager
.getApplication();
114 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
115 return ourTestImplementation
.show(message
);
118 //what's it? if (application.isUnitTestMode()) throw new RuntimeException(message);
119 MessageDialog dialog
= new MessageDialog(message
, title
, options
, defaultOptionIndex
, icon
);
121 return dialog
.getExitCode();
128 * @see com.intellij.openapi.ui.DialogWrapper#DialogWrapper(Project,boolean)
130 public static void showMessageDialog(Project project
, String message
, String title
, Icon icon
) {
131 showDialog(project
, message
, title
, new String
[]{OK_BUTTON
}, 0, icon
);
134 public static void showMessageDialog(Component parent
, String message
, String title
, Icon icon
) {
135 showDialog(parent
, message
, title
, new String
[]{OK_BUTTON
}, 0, icon
);
139 * Use this method only if you do not know project or component
141 * @see #showMessageDialog(Project, String, String, Icon)
142 * @see #showMessageDialog(Component, String, String, Icon)
144 public static void showMessageDialog(String message
, String title
, Icon icon
) {
145 showDialog(message
, title
, new String
[]{OK_BUTTON
}, 0, icon
);
149 * @return <code>0</code> if user pressed "Yes" and returns <code>1</code> if user pressed "No" button.
151 public static int showYesNoDialog(Project project
, String message
, String title
, Icon icon
) {
152 return showDialog(project
, message
, title
, new String
[]{YES_BUTTON
, NO_BUTTON
}, 0, icon
);
156 * @return <code>0</code> if user pressed "Yes" and returns <code>1</code> if user pressed "No" button.
158 public static int showYesNoDialog(Component parent
, String message
, String title
, Icon icon
) {
159 return showDialog(parent
, message
, title
, new String
[]{YES_BUTTON
, NO_BUTTON
}, 0, icon
);
163 * Use this method only if you do not know project or component
165 * @return <code>0</code> if user pressed "Yes" and returns <code>1</code> if user pressed "No" button.
166 * @see #showYesNoDialog(Project, String, String, Icon)
167 * @see #showYesNoDialog(Component, String, String, Icon)
169 public static int showYesNoDialog(String message
, String title
, Icon icon
) {
170 return showDialog(message
, title
, new String
[]{YES_BUTTON
, NO_BUTTON
}, 0, icon
);
173 public static int showOkCancelDialog(Project project
, String message
, String title
, Icon icon
) {
174 return showDialog(project
, message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
177 public static int showOkCancelDialog(Component parent
, String message
, String title
, Icon icon
) {
178 return showDialog(parent
, message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
182 * Use this method only if you do not know project or component
184 * @see #showOkCancelDialog(Project, String, String, Icon)
185 * @see #showOkCancelDialog(Component, String, String, Icon)
187 public static int showOkCancelDialog(String message
, String title
, Icon icon
) {
188 return showDialog(message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
191 public static void showErrorDialog(Project project
, @Nls String message
, @Nls String title
) {
192 showDialog(project
, message
, title
, new String
[]{OK_BUTTON
}, 0, getErrorIcon());
195 public static void showErrorDialog(Component component
, String message
, @Nls String title
) {
196 showDialog(component
, message
, title
, new String
[]{OK_BUTTON
}, 0, getErrorIcon());
199 public static void showErrorDialog(Component component
, String message
) {
200 showDialog(component
, message
, CommonBundle
.getErrorTitle(), new String
[]{OK_BUTTON
}, 0, getErrorIcon());
204 * Use this method only if you do not know project or component
206 * @see #showErrorDialog(Project, String, String)
207 * @see #showErrorDialog(Component, String, String)
209 public static void showErrorDialog(String message
, String title
) {
210 showDialog(message
, title
, new String
[]{OK_BUTTON
}, 0, getErrorIcon());
213 public static void showWarningDialog(Project project
, String message
, String title
) {
214 showDialog(project
, message
, title
, new String
[]{OK_BUTTON
}, 0, getWarningIcon());
217 public static void showWarningDialog(Component component
, String message
, String title
) {
218 showDialog(component
, message
, title
, new String
[]{OK_BUTTON
}, 0, getWarningIcon());
222 * Use this method only if you do not know project or component
224 * @see #showWarningDialog(Project, String, String)
225 * @see #showWarningDialog(Component, String, String)
227 public static void showWarningDialog(String message
, String title
) {
228 showDialog(message
, title
, new String
[]{OK_BUTTON
}, 0, getWarningIcon());
231 public static int showYesNoCancelDialog(Project project
, String message
, String title
, Icon icon
) {
232 return showDialog(project
, message
, title
, new String
[]{YES_BUTTON
, NO_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
235 public static int showYesNoCancelDialog(Component parent
, String message
, String title
, Icon icon
) {
236 return showDialog(parent
, message
, title
, new String
[]{YES_BUTTON
, NO_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
240 * Use this method only if you do not know project or component
242 * @see #showYesNoCancelDialog(Project, String, String, Icon)
243 * @see #showYesNoCancelDialog(Component, String, String, Icon)
245 public static int showYesNoCancelDialog(String message
, String title
, Icon icon
) {
246 return showDialog(message
, title
, new String
[]{YES_BUTTON
, NO_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
250 * @return trimmed inpit string or <code>null</code> if user cancelled dialog.
253 public static String
showInputDialog(Project project
, String message
, String title
, Icon icon
) {
254 return showInputDialog(project
, message
, title
, icon
, null, null);
258 * @return trimmed inpit string or <code>null</code> if user cancelled dialog.
261 public static String
showInputDialog(Component parent
, String message
, String title
, Icon icon
) {
262 return showInputDialog(parent
, message
, title
, icon
, null, null);
266 * Use this method only if you do not know project or component
268 * @see #showInputDialog(Project, String, String, Icon)
269 * @see #showInputDialog(Component, String, String, Icon)
272 public static String
showInputDialog(String message
, String title
, Icon icon
) {
273 return showInputDialog(message
, title
, icon
, null, null);
277 public static String
showInputDialog(Project project
,
281 @NonNls String initialValue
,
282 @Nullable InputValidator validator
) {
283 final Application application
= ApplicationManager
.getApplication();
284 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
285 return ourTestInputImplementation
.show(message
);
288 InputDialog dialog
= new InputDialog(project
, message
, title
, icon
, initialValue
, validator
);
290 return dialog
.getInputString();
295 public static String
showInputDialog(Project project
,
299 @NonNls String initialValue
,
300 @Nullable InputValidator validator
,
301 @Nullable TextRange selection
) {
302 final Application application
= ApplicationManager
.getApplication();
303 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
304 return ourTestInputImplementation
.show(message
);
307 InputDialog dialog
= new InputDialog(project
, message
, title
, icon
, initialValue
, validator
);
309 final JTextField field
= dialog
.getTextField();
310 if (selection
!= null) {
311 // set custom selection
312 field
.select(selection
.getStartOffset(),
313 selection
.getEndOffset());
316 final int length
= field
.getDocument().getLength();
317 field
.select(length
, length
);
319 field
.putClientProperty(DialogWrapperPeer
.HAVE_INITIAL_SELECTION
, true);
322 return dialog
.getInputString();
327 public static String
showInputDialog(Component parent
,
331 @NonNls String initialValue
,
332 InputValidator validator
) {
333 final Application application
= ApplicationManager
.getApplication();
334 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
335 return ourTestInputImplementation
.show(message
);
339 InputDialog dialog
= new InputDialog(parent
, message
, title
, icon
, initialValue
, validator
);
341 return dialog
.getInputString();
346 * Use this method only if you do not know project or component
348 * @see #showInputDialog(Project, String, String, Icon, String, InputValidator)
349 * @see #showInputDialog(Component, String, String, Icon, String, InputValidator)
352 public static String
showInputDialog(String message
, String title
, Icon icon
, @NonNls String initialValue
, InputValidator validator
) {
353 final Application application
= ApplicationManager
.getApplication();
354 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
355 return ourTestInputImplementation
.show(message
);
358 InputDialog dialog
= new InputDialog(message
, title
, icon
, initialValue
, validator
);
360 return dialog
.getInputString();
365 public static String
showEditableChooseDialog(String message
,
370 InputValidator validator
) {
371 final Application application
= ApplicationManager
.getApplication();
372 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
373 return ourTestInputImplementation
.show(message
);
376 ChooseDialog dialog
= new ChooseDialog(message
, title
, icon
, values
, initialValue
);
377 dialog
.setValidator(validator
);
378 dialog
.getComboBox().setEditable(true);
379 dialog
.getComboBox().getEditor().setItem(initialValue
);
380 dialog
.getComboBox().setSelectedItem(initialValue
);
382 return dialog
.getInputString();
386 public static int showChooseDialog(String message
, String title
, String
[] values
, String initialValue
, Icon icon
) {
387 final Application application
= ApplicationManager
.getApplication();
388 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
389 return ourTestImplementation
.show(message
);
392 ChooseDialog dialog
= new ChooseDialog(message
, title
, icon
, values
, initialValue
);
394 return dialog
.getSelectedIndex();
398 public static int showChooseDialog(Component parent
, String message
, String title
, String
[] values
, String initialValue
, Icon icon
) {
399 final Application application
= ApplicationManager
.getApplication();
400 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
401 return ourTestImplementation
.show(message
);
404 ChooseDialog dialog
= new ChooseDialog(parent
, message
, title
, icon
, values
, initialValue
);
406 return dialog
.getSelectedIndex();
411 * @see com.intellij.openapi.ui.DialogWrapper#DialogWrapper(Project,boolean)
413 public static int showChooseDialog(Project project
, String message
, String title
, Icon icon
, String
[] values
, String initialValue
) {
414 final Application application
= ApplicationManager
.getApplication();
415 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
416 return ourTestImplementation
.show(message
);
419 ChooseDialog dialog
= new ChooseDialog(project
, message
, title
, icon
, values
, initialValue
);
421 return dialog
.getSelectedIndex();
426 * Shows dialog with given message and title, infomation icon {@link #getInformationIcon()} and OK button
428 public static void showInfoMessage(Component component
, String message
, String title
) {
429 showMessageDialog(component
, message
, title
, getInformationIcon());
433 * Shows dialog with given message and title, infomation icon {@link #getInformationIcon()} and OK button
435 public static void showInfoMessage(Project project
, @Nls String message
, @Nls String title
) {
436 showMessageDialog(project
, message
, title
, getInformationIcon());
440 * Shows dialog with given message and title, infomation icon {@link #getInformationIcon()} and OK button
442 public static void showInfoMessage(String message
, String title
) {
443 showMessageDialog(message
, title
, getInformationIcon());
447 * Shows dialog with text area to edit long strings that don't fit in text field
449 public static void showTextAreaDialog(final JTextField textField
, final String title
, @NonNls final String dimensionServiceKey
) {
450 final Application application
= ApplicationManager
.getApplication();
451 if (application
.isUnitTestMode() || application
.isHeadlessEnvironment()) {
452 ourTestImplementation
.show(title
);
455 final JTextArea textArea
= new JTextArea(10, 50);
456 textArea
.setWrapStyleWord(true);
457 textArea
.setLineWrap(true);
458 textArea
.getDocument().addDocumentListener(new DocumentAdapter() {
459 protected void textChanged(final DocumentEvent e
) {
460 textField
.setText(textArea
.getText());
463 textArea
.setText(textField
.getText().replaceAll("[\\ ]*=[\\ ]*", "=").replaceAll(" ", "\n"));
464 InsertPathAction
.copyFromTo(textField
, textArea
);
465 DialogBuilder builder
= new DialogBuilder(textField
);
466 JScrollPane scrollPane
= ScrollPaneFactory
.createScrollPane(textArea
);
467 builder
.setDimensionServiceKey(dimensionServiceKey
);
468 builder
.setCenterPanel(scrollPane
);
469 builder
.setPreferedFocusComponent(textArea
);
470 String rawText
= title
;
471 if (StringUtil
.endsWithChar(rawText
, ':')) {
472 rawText
= rawText
.substring(0, rawText
.length() - 1);
474 builder
.setTitle(rawText
);
475 builder
.addCloseButton();
480 private static class MessageDialog
extends DialogWrapper
{
481 protected String myMessage
;
482 protected String
[] myOptions
;
483 protected int myDefaultOptionIndex
;
484 protected Icon myIcon
;
486 public MessageDialog(Project project
, String message
, String title
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
487 super(project
, false);
488 _init(title
, message
, options
, defaultOptionIndex
, icon
);
491 public MessageDialog(Component parent
, String message
, String title
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
492 super(parent
, false);
493 _init(title
, message
, options
, defaultOptionIndex
, icon
);
496 public MessageDialog(String message
, String title
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
498 _init(title
, message
, options
, defaultOptionIndex
, icon
);
501 private void _init(String title
, String message
, String
[] options
, int defaultOptionIndex
, Icon icon
) {
505 myDefaultOptionIndex
= defaultOptionIndex
;
507 setButtonsAlignment(SwingUtilities
.CENTER
);
511 protected Action
[] createActions() {
512 Action
[] actions
= new Action
[myOptions
.length
];
513 for (int i
= 0; i
< myOptions
.length
; i
++) {
514 String option
= myOptions
[i
];
515 final int exitCode
= i
;
516 actions
[i
] = new AbstractAction(option
) {
517 public void actionPerformed(ActionEvent e
) {
521 if (i
== myDefaultOptionIndex
) {
522 actions
[i
].putValue(DEFAULT_ACTION
, Boolean
.TRUE
);
524 assignMnemonic(option
, actions
[i
]);
530 private void assignMnemonic(String option
, Action action
) {
531 int mnemoPos
= option
.indexOf("&");
532 if (mnemoPos
>= 0 && mnemoPos
< option
.length() - 2) {
533 String mnemoChar
= option
.substring(mnemoPos
+ 1, mnemoPos
+ 2).trim();
534 if (mnemoChar
.length() == 1) {
535 action
.putValue(Action
.MNEMONIC_KEY
, new Integer(mnemoChar
.charAt(0)));
540 public void doCancelAction() {
544 protected JComponent
createNorthPanel() {
548 protected JComponent
createCenterPanel() {
549 JPanel panel
= new JPanel(new BorderLayout(15, 0));
550 if (myIcon
!= null) {
551 JLabel iconLabel
= new JLabel(myIcon
);
552 Container container
= new Container();
553 container
.setLayout(new BorderLayout());
554 container
.add(iconLabel
, BorderLayout
.NORTH
);
555 panel
.add(container
, BorderLayout
.WEST
);
557 if (myMessage
!= null) {
558 JLabel label
= new JLabel();
559 final JTextPane messageComponent
= new JTextPane();
560 messageComponent
.setFont(label
.getFont());
561 if (BasicHTML
.isHTMLString(myMessage
)) {
562 final HTMLEditorKit editorKit
= new HTMLEditorKit();
563 editorKit
.getStyleSheet().addRule(UIUtil
.displayPropertiesToCSS(label
.getFont(), label
.getForeground()));
564 messageComponent
.setEditorKit(editorKit
);
565 messageComponent
.setContentType("text/html");
566 messageComponent
.addHyperlinkListener(new BrowserHyperlinkListener());
568 messageComponent
.setText(myMessage
);
569 messageComponent
.setEditable(false);
570 if (messageComponent
.getCaret() != null) {
571 messageComponent
.setCaretPosition(0);
573 messageComponent
.setBackground(UIUtil
.getOptionPaneBackground());
574 messageComponent
.setForeground(label
.getForeground());
576 final Dimension screenSize
= messageComponent
.getToolkit().getScreenSize();
577 final Dimension textSize
= messageComponent
.getPreferredSize();
578 if (textSize
.width
> screenSize
.width
* 4 / 5 || textSize
.height
> screenSize
.height
/ 2) {
579 final JScrollPane pane
= ScrollPaneFactory
.createScrollPane(messageComponent
);
580 pane
.setBorder(BorderFactory
.createEmptyBorder(0, 0, 0, 0));
581 pane
.setVerticalScrollBarPolicy(JScrollPane
.VERTICAL_SCROLLBAR_AS_NEEDED
);
582 pane
.setHorizontalScrollBarPolicy(JScrollPane
.HORIZONTAL_SCROLLBAR_AS_NEEDED
);
583 final int scrollSize
= (int)new JScrollBar(JScrollBar
.VERTICAL
).getPreferredSize().getWidth();
584 final Dimension preferredSize
=
585 new Dimension(Math
.min(textSize
.width
, screenSize
.width
* 4 / 5) + scrollSize
, Math
.min(textSize
.height
, screenSize
.height
/2) + scrollSize
);
586 pane
.setPreferredSize(preferredSize
);
587 panel
.add(pane
, BorderLayout
.CENTER
);
588 SwingUtilities
.invokeLater(new Runnable() {
590 final Dimension textSize
= messageComponent
.getPreferredSize();
591 final Dimension preferredSize
= new Dimension(Math
.min(textSize
.width
, screenSize
.width
* 4 / 5) + scrollSize
,
592 Math
.min(textSize
.height
, screenSize
.height
/ 2) + scrollSize
);
593 pane
.setPreferredSize(preferredSize
);
594 SwingUtilities
.getWindowAncestor(pane
).pack();
599 panel
.add(messageComponent
, BorderLayout
.CENTER
);
606 protected void doHelpAction() {
611 protected static class InputDialog
extends MessageDialog
{
612 private JTextField myField
;
613 private InputValidator myValidator
;
615 public InputDialog(Project project
,
620 InputValidator validator
,
623 super(project
, message
, title
, options
, defaultOption
, icon
);
624 myValidator
= validator
;
625 myField
.setText(initialValue
);
628 public InputDialog(Project project
, String message
, String title
, Icon icon
, String initialValue
, InputValidator validator
) {
629 this(project
, message
, title
, icon
, initialValue
, validator
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0);
632 public InputDialog(Component parent
, String message
, String title
, Icon icon
, String initialValue
, InputValidator validator
) {
633 super(parent
, message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
634 myValidator
= validator
;
635 myField
.setText(initialValue
);
638 public InputDialog(String message
, String title
, Icon icon
, String initialValue
, InputValidator validator
) {
639 super(message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
640 myValidator
= validator
;
641 myField
.setText(initialValue
);
644 protected Action
[] createActions() {
645 final Action
[] actions
= new Action
[myOptions
.length
];
646 for (int i
= 0; i
< myOptions
.length
; i
++) {
647 String option
= myOptions
[i
];
648 final int exitCode
= i
;
649 if (i
== 0) { // "OK" is default button. It has index 0.
650 actions
[i
] = new AbstractAction(option
) {
651 public void actionPerformed(ActionEvent e
) {
652 String inputString
= myField
.getText().trim();
654 myValidator
== null ||
655 myValidator
.checkInput(inputString
) &&
656 myValidator
.canClose(inputString
)
662 actions
[i
].putValue(DEFAULT_ACTION
, Boolean
.TRUE
);
663 myField
.getDocument().addDocumentListener(new DocumentAdapter() {
664 public void textChanged(DocumentEvent event
) {
665 final String text
= myField
.getText().trim();
666 actions
[exitCode
].setEnabled(myValidator
== null || myValidator
.checkInput(text
));
667 if (myValidator
instanceof InputValidatorEx
) {
668 setErrorText(((InputValidatorEx
) myValidator
).getErrorText(text
));
674 actions
[i
] = new AbstractAction(option
) {
675 public void actionPerformed(ActionEvent e
) {
684 protected JComponent
createCenterPanel() {
688 protected JComponent
createNorthPanel() {
689 JPanel panel
= new JPanel(new BorderLayout(15, 0));
690 if (myIcon
!= null) {
691 JLabel iconLabel
= new JLabel(myIcon
);
692 Container container
= new Container();
693 container
.setLayout(new BorderLayout());
694 container
.add(iconLabel
, BorderLayout
.NORTH
);
695 panel
.add(container
, BorderLayout
.WEST
);
698 JPanel messagePanel
= new JPanel(new BorderLayout());
699 if (myMessage
!= null) {
700 JLabel textLabel
= new JLabel(myMessage
);
701 textLabel
.setBorder(BorderFactory
.createEmptyBorder(0, 0, 5, 0));
702 textLabel
.setUI(new MultiLineLabelUI());
703 messagePanel
.add(textLabel
, BorderLayout
.NORTH
);
706 myField
= new JTextField(30);
707 messagePanel
.add(myField
, BorderLayout
.SOUTH
);
708 panel
.add(messagePanel
, BorderLayout
.CENTER
);
713 public JTextField
getTextField() {
717 public JComponent
getPreferredFocusedComponent() {
722 public String
getInputString() {
723 if (getExitCode() == 0) {
724 return myField
.getText().trim();
732 protected static class ChooseDialog
extends MessageDialog
{
733 private ComboBox myComboBox
;
734 private InputValidator myValidator
;
736 public ChooseDialog(Project project
,
744 super(project
, message
, title
, options
, defaultOption
, icon
);
745 myComboBox
.setModel(new DefaultComboBoxModel(values
));
746 myComboBox
.setSelectedItem(initialValue
);
749 public ChooseDialog(Project project
, String message
, String title
, Icon icon
, String
[] values
, String initialValue
) {
750 this(project
, message
, title
, icon
, values
, initialValue
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0);
753 public ChooseDialog(Component parent
, String message
, String title
, Icon icon
, String
[] values
, String initialValue
) {
754 super(parent
, message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
755 myComboBox
.setModel(new DefaultComboBoxModel(values
));
756 myComboBox
.setSelectedItem(initialValue
);
759 public ChooseDialog(String message
, String title
, Icon icon
, String
[] values
, String initialValue
) {
760 super(message
, title
, new String
[]{OK_BUTTON
, CANCEL_BUTTON
}, 0, icon
);
761 myComboBox
.setModel(new DefaultComboBoxModel(values
));
762 myComboBox
.setSelectedItem(initialValue
);
765 protected Action
[] createActions() {
766 final Action
[] actions
= new Action
[myOptions
.length
];
767 for (int i
= 0; i
< myOptions
.length
; i
++) {
768 String option
= myOptions
[i
];
769 final int exitCode
= i
;
770 if (i
== myDefaultOptionIndex
) {
771 actions
[i
] = new AbstractAction(option
) {
772 public void actionPerformed(ActionEvent e
) {
773 if (myValidator
== null || myValidator
.checkInput(myComboBox
.getSelectedItem().toString().trim())) {
778 actions
[i
].putValue(DEFAULT_ACTION
, Boolean
.TRUE
);
779 myComboBox
.addItemListener(new ItemListener() {
780 public void itemStateChanged(ItemEvent e
) {
781 actions
[exitCode
].setEnabled(myValidator
== null || myValidator
.checkInput(myComboBox
.getSelectedItem().toString().trim()));
784 final JTextField textField
= (JTextField
)myComboBox
.getEditor().getEditorComponent();
785 textField
.getDocument().addDocumentListener(new DocumentAdapter() {
786 public void textChanged(DocumentEvent event
) {
787 actions
[exitCode
].setEnabled(myValidator
== null || myValidator
.checkInput(textField
.getText().trim()));
791 else { // "Cancel" action
792 actions
[i
] = new AbstractAction(option
) {
793 public void actionPerformed(ActionEvent e
) {
802 protected JComponent
createCenterPanel() {
806 protected JComponent
createNorthPanel() {
807 JPanel panel
= new JPanel(new BorderLayout(15, 0));
808 if (myIcon
!= null) {
809 JLabel iconLabel
= new JLabel(myIcon
);
810 Container container
= new Container();
811 container
.setLayout(new BorderLayout());
812 container
.add(iconLabel
, BorderLayout
.NORTH
);
813 panel
.add(container
, BorderLayout
.WEST
);
816 JPanel messagePanel
= new JPanel(new BorderLayout());
817 if (myMessage
!= null) {
818 JLabel textLabel
= new JLabel(myMessage
);
819 textLabel
.setBorder(BorderFactory
.createEmptyBorder(0, 0, 5, 0));
820 textLabel
.setUI(new MultiLineLabelUI());
821 messagePanel
.add(textLabel
, BorderLayout
.NORTH
);
824 myComboBox
= new ComboBox(220);
825 messagePanel
.add(myComboBox
, BorderLayout
.SOUTH
);
826 panel
.add(messagePanel
, BorderLayout
.CENTER
);
830 protected void doOKAction() {
831 String inputString
= myComboBox
.getSelectedItem().toString().trim();
832 if (myValidator
== null ||
833 myValidator
.checkInput(inputString
) &&
834 myValidator
.canClose(inputString
)) {
839 public JComponent
getPreferredFocusedComponent() {
844 public String
getInputString() {
845 if (getExitCode() == 0) {
846 return myComboBox
.getSelectedItem().toString();
853 public int getSelectedIndex() {
854 if (getExitCode() == 0) {
855 return myComboBox
.getSelectedIndex();
862 public JComboBox
getComboBox() {
866 public void setValidator(InputValidator validator
) {
867 myValidator
= validator
;