support for hyperlinks in MessageDialog
[fedora-idea.git] / platform-api / src / com / intellij / openapi / ui / Messages.java
blob6e957676d9755a9cb8dc78b1ddc3de06a167d5b5
1 /*
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.text.StringUtil;
24 import com.intellij.ui.DocumentAdapter;
25 import com.intellij.ui.InsertPathAction;
26 import com.intellij.ui.ScrollPaneFactory;
27 import com.intellij.ui.BrowserHyperlinkListener;
28 import com.intellij.util.ui.UIUtil;
29 import org.jetbrains.annotations.Nls;
30 import org.jetbrains.annotations.NonNls;
31 import org.jetbrains.annotations.Nullable;
33 import javax.swing.*;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.plaf.basic.BasicHTML;
36 import javax.swing.text.html.HTMLEditorKit;
37 import java.awt.*;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ItemEvent;
40 import java.awt.event.ItemListener;
42 public class Messages {
43 private static TestDialog ourTestImplementation = TestDialog.DEFAULT;
44 private static TestInputDialog ourTestInputImplementation = TestInputDialog.DEFAULT;
46 private static Logger LOG = Logger.getInstance("#com.intellij.openapi.ui.Messages");
47 protected static final String OK_BUTTON = CommonBundle.getOkButtonText();
48 protected static final String YES_BUTTON = CommonBundle.getYesButtonText();
49 protected static final String NO_BUTTON = CommonBundle.getNoButtonText();
50 protected static final String CANCEL_BUTTON = CommonBundle.getCancelButtonText();
52 public static TestDialog setTestDialog(TestDialog newValue) {
53 Application application = ApplicationManager.getApplication();
54 if (application != null) {
55 LOG.assertTrue(application.isUnitTestMode(), "This methos is available for tests only");
57 TestDialog oldValue = ourTestImplementation;
58 ourTestImplementation = newValue;
59 return oldValue;
62 public static Icon getErrorIcon() {
63 return UIUtil.getErrorIcon();
66 public static Icon getInformationIcon() {
67 return UIUtil.getInformationIcon();
70 public static Icon getWarningIcon() {
71 return UIUtil.getWarningIcon();
74 public static Icon getQuestionIcon() {
75 return UIUtil.getQuestionIcon();
78 public static int showDialog(Project project, String message, String title, String[] options, int defaultOptionIndex, Icon icon) {
79 final Application application = ApplicationManager.getApplication();
80 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
81 return ourTestImplementation.show(message);
83 else {
84 MessageDialog dialog = new MessageDialog(project, message, title, options, defaultOptionIndex, icon);
85 dialog.show();
86 return dialog.getExitCode();
90 public static int showDialog(Component parent, String message, String title, String[] options, int defaultOptionIndex, Icon icon) {
91 final Application application = ApplicationManager.getApplication();
92 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
93 return ourTestImplementation.show(message);
95 else {
96 MessageDialog dialog = new MessageDialog(parent, message, title, options, defaultOptionIndex, icon);
97 dialog.show();
98 return dialog.getExitCode();
103 * Use this method only if you do not know project or component
105 * @see #showDialog(Project, String, String, String[], int, Icon)
106 * @see #showDialog(Component, String, String, String[], int, Icon)
108 public static int showDialog(String message, String title, String[] options, int defaultOptionIndex, Icon icon) {
110 final Application application = ApplicationManager.getApplication();
111 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
112 return ourTestImplementation.show(message);
114 else {
115 //what's it? if (application.isUnitTestMode()) throw new RuntimeException(message);
116 MessageDialog dialog = new MessageDialog(message, title, options, defaultOptionIndex, icon);
117 dialog.show();
118 return dialog.getExitCode();
125 * @see com.intellij.openapi.ui.DialogWrapper#DialogWrapper(Project,boolean)
127 public static void showMessageDialog(Project project, String message, String title, Icon icon) {
128 showDialog(project, message, title, new String[]{OK_BUTTON}, 0, icon);
131 public static void showMessageDialog(Component parent, String message, String title, Icon icon) {
132 showDialog(parent, message, title, new String[]{OK_BUTTON}, 0, icon);
136 * Use this method only if you do not know project or component
138 * @see #showMessageDialog(Project, String, String, Icon)
139 * @see #showMessageDialog(Component, String, String, Icon)
141 public static void showMessageDialog(String message, String title, Icon icon) {
142 showDialog(message, title, new String[]{OK_BUTTON}, 0, icon);
146 * @return <code>0</code> if user pressed "Yes" and returns <code>1</code> if user pressed "No" button.
148 public static int showYesNoDialog(Project project, String message, String title, Icon icon) {
149 return showDialog(project, message, title, new String[]{YES_BUTTON, NO_BUTTON}, 0, icon);
153 * @return <code>0</code> if user pressed "Yes" and returns <code>1</code> if user pressed "No" button.
155 public static int showYesNoDialog(Component parent, String message, String title, Icon icon) {
156 return showDialog(parent, message, title, new String[]{YES_BUTTON, NO_BUTTON}, 0, icon);
160 * Use this method only if you do not know project or component
162 * @return <code>0</code> if user pressed "Yes" and returns <code>1</code> if user pressed "No" button.
163 * @see #showYesNoDialog(Project, String, String, Icon)
164 * @see #showYesNoDialog(Component, String, String, Icon)
166 public static int showYesNoDialog(String message, String title, Icon icon) {
167 return showDialog(message, title, new String[]{YES_BUTTON, NO_BUTTON}, 0, icon);
170 public static int showOkCancelDialog(Project project, String message, String title, Icon icon) {
171 return showDialog(project, message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
174 public static int showOkCancelDialog(Component parent, String message, String title, Icon icon) {
175 return showDialog(parent, message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
179 * Use this method only if you do not know project or component
181 * @see #showOkCancelDialog(Project, String, String, Icon)
182 * @see #showOkCancelDialog(Component, String, String, Icon)
184 public static int showOkCancelDialog(String message, String title, Icon icon) {
185 return showDialog(message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
188 public static void showErrorDialog(Project project, @Nls String message, @Nls String title) {
189 showDialog(project, message, title, new String[]{OK_BUTTON}, 0, getErrorIcon());
192 public static void showErrorDialog(Component component, String message, @Nls String title) {
193 showDialog(component, message, title, new String[]{OK_BUTTON}, 0, getErrorIcon());
196 public static void showErrorDialog(Component component, String message) {
197 showDialog(component, message, CommonBundle.getErrorTitle(), new String[]{OK_BUTTON}, 0, getErrorIcon());
201 * Use this method only if you do not know project or component
203 * @see #showErrorDialog(Project, String, String)
204 * @see #showErrorDialog(Component, String, String)
206 public static void showErrorDialog(String message, String title) {
207 showDialog(message, title, new String[]{OK_BUTTON}, 0, getErrorIcon());
210 public static void showWarningDialog(Project project, String message, String title) {
211 showDialog(project, message, title, new String[]{OK_BUTTON}, 0, getWarningIcon());
214 public static void showWarningDialog(Component component, String message, String title) {
215 showDialog(component, message, title, new String[]{OK_BUTTON}, 0, getWarningIcon());
219 * Use this method only if you do not know project or component
221 * @see #showWarningDialog(Project, String, String)
222 * @see #showWarningDialog(Component, String, String)
224 public static void showWarningDialog(String message, String title) {
225 showDialog(message, title, new String[]{OK_BUTTON}, 0, getWarningIcon());
228 public static int showYesNoCancelDialog(Project project, String message, String title, Icon icon) {
229 return showDialog(project, message, title, new String[]{YES_BUTTON, NO_BUTTON, CANCEL_BUTTON}, 0, icon);
232 public static int showYesNoCancelDialog(Component parent, String message, String title, Icon icon) {
233 return showDialog(parent, message, title, new String[]{YES_BUTTON, NO_BUTTON, CANCEL_BUTTON}, 0, icon);
237 * Use this method only if you do not know project or component
239 * @see #showYesNoCancelDialog(Project, String, String, Icon)
240 * @see #showYesNoCancelDialog(Component, String, String, Icon)
242 public static int showYesNoCancelDialog(String message, String title, Icon icon) {
243 return showDialog(message, title, new String[]{YES_BUTTON, NO_BUTTON, CANCEL_BUTTON}, 0, icon);
247 * @return trimmed inpit string or <code>null</code> if user cancelled dialog.
249 @Nullable
250 public static String showInputDialog(Project project, String message, String title, Icon icon) {
251 return showInputDialog(project, message, title, icon, null, null);
255 * @return trimmed inpit string or <code>null</code> if user cancelled dialog.
257 @Nullable
258 public static String showInputDialog(Component parent, String message, String title, Icon icon) {
259 return showInputDialog(parent, message, title, icon, null, null);
263 * Use this method only if you do not know project or component
265 * @see #showInputDialog(Project, String, String, Icon)
266 * @see #showInputDialog(Component, String, String, Icon)
268 @Nullable
269 public static String showInputDialog(String message, String title, Icon icon) {
270 return showInputDialog(message, title, icon, null, null);
273 @Nullable
274 public static String showInputDialog(Project project,
275 @Nls String message,
276 @Nls String title,
277 Icon icon,
278 String initialValue,
279 @Nullable InputValidator validator) {
280 final Application application = ApplicationManager.getApplication();
281 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
282 return ourTestInputImplementation.show(message);
284 else {
285 InputDialog dialog = new InputDialog(project, message, title, icon, initialValue, validator);
286 dialog.show();
287 return dialog.getInputString();
291 @Nullable
292 public static String showInputDialog(Component parent,
293 String message,
294 String title,
295 Icon icon,
296 String initialValue,
297 InputValidator validator) {
298 final Application application = ApplicationManager.getApplication();
299 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
300 return ourTestInputImplementation.show(message);
302 else {
304 InputDialog dialog = new InputDialog(parent, message, title, icon, initialValue, validator);
305 dialog.show();
306 return dialog.getInputString();
311 * Use this method only if you do not know project or component
313 * @see #showInputDialog(Project, String, String, Icon, String, InputValidator)
314 * @see #showInputDialog(Component, String, String, Icon, String, InputValidator)
316 @Nullable
317 public static String showInputDialog(String message, String title, Icon icon, String initialValue, InputValidator validator) {
318 final Application application = ApplicationManager.getApplication();
319 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
320 return ourTestInputImplementation.show(message);
322 else {
323 InputDialog dialog = new InputDialog(message, title, icon, initialValue, validator);
324 dialog.show();
325 return dialog.getInputString();
329 @Nullable
330 public static String showEditableChooseDialog(String message,
331 String title,
332 Icon icon,
333 String[] values,
334 String initialValue,
335 InputValidator validator) {
336 final Application application = ApplicationManager.getApplication();
337 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
338 return ourTestInputImplementation.show(message);
340 else {
341 ChooseDialog dialog = new ChooseDialog(message, title, icon, values, initialValue);
342 dialog.setValidator(validator);
343 dialog.getComboBox().setEditable(true);
344 dialog.getComboBox().getEditor().setItem(initialValue);
345 dialog.getComboBox().setSelectedItem(initialValue);
346 dialog.show();
347 return dialog.getInputString();
351 public static int showChooseDialog(String message, String title, String[] values, String initialValue, Icon icon) {
352 final Application application = ApplicationManager.getApplication();
353 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
354 return ourTestImplementation.show(message);
356 else {
357 ChooseDialog dialog = new ChooseDialog(message, title, icon, values, initialValue);
358 dialog.show();
359 return dialog.getSelectedIndex();
363 public static int showChooseDialog(Component parent, String message, String title, String[] values, String initialValue, Icon icon) {
364 final Application application = ApplicationManager.getApplication();
365 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
366 return ourTestImplementation.show(message);
368 else {
369 ChooseDialog dialog = new ChooseDialog(parent, message, title, icon, values, initialValue);
370 dialog.show();
371 return dialog.getSelectedIndex();
376 * @see com.intellij.openapi.ui.DialogWrapper#DialogWrapper(Project,boolean)
378 public static int showChooseDialog(Project project, String message, String title, Icon icon, String[] values, String initialValue) {
379 final Application application = ApplicationManager.getApplication();
380 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
381 return ourTestImplementation.show(message);
383 else {
384 ChooseDialog dialog = new ChooseDialog(project, message, title, icon, values, initialValue);
385 dialog.show();
386 return dialog.getSelectedIndex();
391 * Shows dialog with given message and title, infomation icon {@link #getInformationIcon()} and OK button
393 public static void showInfoMessage(Component component, String message, String title) {
394 showMessageDialog(component, message, title, getInformationIcon());
398 * Shows dialog with given message and title, infomation icon {@link #getInformationIcon()} and OK button
400 public static void showInfoMessage(Project project, @Nls String message, @Nls String title) {
401 showMessageDialog(project, message, title, getInformationIcon());
405 * Shows dialog with given message and title, infomation icon {@link #getInformationIcon()} and OK button
407 public static void showInfoMessage(String message, String title) {
408 showMessageDialog(message, title, getInformationIcon());
412 * Shows dialog with text area to edit long strings that don't fit in text field
414 public static void showTextAreaDialog(final JTextField textField, final String title, @NonNls final String dimensionServiceKey) {
415 final Application application = ApplicationManager.getApplication();
416 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
417 ourTestImplementation.show(title);
419 else {
420 final JTextArea textArea = new JTextArea(10, 50);
421 textArea.setWrapStyleWord(true);
422 textArea.setLineWrap(true);
423 textArea.getDocument().addDocumentListener(new DocumentAdapter() {
424 protected void textChanged(final DocumentEvent e) {
425 textField.setText(textArea.getText());
428 textArea.setText(textField.getText().replaceAll("[\\ ]*=[\\ ]*", "=").replaceAll(" ", "\n"));
429 InsertPathAction.copyFromTo(textField, textArea);
430 DialogBuilder builder = new DialogBuilder(textField);
431 JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(textArea);
432 builder.setDimensionServiceKey(dimensionServiceKey);
433 builder.setCenterPanel(scrollPane);
434 builder.setPreferedFocusComponent(textArea);
435 String rawText = title;
436 if (StringUtil.endsWithChar(rawText, ':')) {
437 rawText = rawText.substring(0, rawText.length() - 1);
439 builder.setTitle(rawText);
440 builder.addCloseButton();
441 builder.show();
445 private static class MessageDialog extends DialogWrapper {
446 protected String myMessage;
447 protected String[] myOptions;
448 protected int myDefaultOptionIndex;
449 protected Icon myIcon;
451 public MessageDialog(Project project, String message, String title, String[] options, int defaultOptionIndex, Icon icon) {
452 super(project, false);
453 _init(title, message, options, defaultOptionIndex, icon);
456 public MessageDialog(Component parent, String message, String title, String[] options, int defaultOptionIndex, Icon icon) {
457 super(parent, false);
458 _init(title, message, options, defaultOptionIndex, icon);
461 public MessageDialog(String message, String title, String[] options, int defaultOptionIndex, Icon icon) {
462 super(false);
463 _init(title, message, options, defaultOptionIndex, icon);
466 private void _init(String title, String message, String[] options, int defaultOptionIndex, Icon icon) {
467 setTitle(title);
468 myMessage = message;
469 myOptions = options;
470 myDefaultOptionIndex = defaultOptionIndex;
471 myIcon = icon;
472 setButtonsAlignment(SwingUtilities.CENTER);
473 init();
476 protected Action[] createActions() {
477 Action[] actions = new Action[myOptions.length];
478 for (int i = 0; i < myOptions.length; i++) {
479 String option = myOptions[i];
480 final int exitCode = i;
481 actions[i] = new AbstractAction(option) {
482 public void actionPerformed(ActionEvent e) {
483 close(exitCode);
486 if (i == myDefaultOptionIndex) {
487 actions[i].putValue(DEFAULT_ACTION, Boolean.TRUE);
489 assignMnemonic(option, actions[i]);
492 return actions;
495 private void assignMnemonic(String option, Action action) {
496 int mnemoPos = option.indexOf("&");
497 if (mnemoPos >= 0 && mnemoPos < option.length() - 2) {
498 String mnemoChar = option.substring(mnemoPos + 1, mnemoPos + 2).trim();
499 if (mnemoChar.length() == 1) {
500 action.putValue(Action.MNEMONIC_KEY, new Integer(mnemoChar.charAt(0)));
505 public void doCancelAction() {
506 close(-1);
509 protected JComponent createNorthPanel() {
510 return null;
513 protected JComponent createCenterPanel() {
514 JPanel panel = new JPanel(new BorderLayout(15, 0));
515 if (myIcon != null) {
516 JLabel iconLabel = new JLabel(myIcon);
517 Container container = new Container();
518 container.setLayout(new BorderLayout());
519 container.add(iconLabel, BorderLayout.NORTH);
520 panel.add(container, BorderLayout.WEST);
522 if (myMessage != null) {
523 JLabel label = new JLabel();
524 final JTextPane messageComponent = new JTextPane();
525 messageComponent.setFont(label.getFont());
526 if (BasicHTML.isHTMLString(myMessage)) {
527 final HTMLEditorKit editorKit = new HTMLEditorKit();
528 editorKit.getStyleSheet().addRule(UIUtil.displayPropertiesToCSS(label.getFont(), label.getForeground()));
529 messageComponent.setEditorKit(editorKit);
530 messageComponent.setContentType("text/html");
531 messageComponent.addHyperlinkListener(new BrowserHyperlinkListener());
533 messageComponent.setText(myMessage);
534 messageComponent.setEditable(false);
535 if (messageComponent.getCaret() != null) {
536 messageComponent.setCaretPosition(0);
538 messageComponent.setBackground(UIUtil.getOptionPaneBackground());
539 messageComponent.setForeground(label.getForeground());
541 final Dimension screenSize = messageComponent.getToolkit().getScreenSize();
542 final Dimension textSize = messageComponent.getPreferredSize();
543 if (textSize.width > screenSize.width * 4 / 5 || textSize.height > screenSize.height / 2) {
544 final JScrollPane pane = ScrollPaneFactory.createScrollPane(messageComponent);
545 pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
546 pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
547 pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
548 final int scrollSize = (int)new JScrollBar(JScrollBar.VERTICAL).getPreferredSize().getWidth();
549 final Dimension preferredSize =
550 new Dimension(Math.min(textSize.width, screenSize.width * 4 / 5) + scrollSize, Math.min(textSize.height, screenSize.height/2) + scrollSize);
551 pane.setPreferredSize(preferredSize);
552 panel.add(pane, BorderLayout.CENTER);
553 SwingUtilities.invokeLater(new Runnable() {
554 public void run() {
555 final Dimension textSize = messageComponent.getPreferredSize();
556 final Dimension preferredSize = new Dimension(Math.min(textSize.width, screenSize.width * 4 / 5) + scrollSize,
557 Math.min(textSize.height, screenSize.height / 2) + scrollSize);
558 pane.setPreferredSize(preferredSize);
559 SwingUtilities.getWindowAncestor(pane).pack();
563 else {
564 panel.add(messageComponent, BorderLayout.CENTER);
567 return panel;
572 protected static class InputDialog extends MessageDialog {
573 private JTextField myField;
574 private InputValidator myValidator;
576 public InputDialog(Project project,
577 String message,
578 String title,
579 Icon icon,
580 String initialValue,
581 InputValidator validator,
582 String[] options,
583 int defaultOption) {
584 super(project, message, title, options, defaultOption, icon);
585 myValidator = validator;
586 myField.setText(initialValue);
589 public InputDialog(Project project, String message, String title, Icon icon, String initialValue, InputValidator validator) {
590 this(project, message, title, icon, initialValue, validator, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0);
593 public InputDialog(Component parent, String message, String title, Icon icon, String initialValue, InputValidator validator) {
594 super(parent, message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
595 myValidator = validator;
596 myField.setText(initialValue);
599 public InputDialog(String message, String title, Icon icon, String initialValue, InputValidator validator) {
600 super(message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
601 myValidator = validator;
602 myField.setText(initialValue);
605 protected Action[] createActions() {
606 final Action[] actions = new Action[myOptions.length];
607 for (int i = 0; i < myOptions.length; i++) {
608 String option = myOptions[i];
609 final int exitCode = i;
610 if (i == 0) { // "OK" is default button. It has index 0.
611 actions[i] = new AbstractAction(option) {
612 public void actionPerformed(ActionEvent e) {
613 String inputString = myField.getText().trim();
614 if (
615 myValidator == null ||
616 myValidator.checkInput(inputString) &&
617 myValidator.canClose(inputString)
619 close(exitCode);
623 actions[i].putValue(DEFAULT_ACTION, Boolean.TRUE);
624 myField.getDocument().addDocumentListener(new DocumentAdapter() {
625 public void textChanged(DocumentEvent event) {
626 actions[exitCode].setEnabled(myValidator == null || myValidator.checkInput(myField.getText().trim()));
630 else {
631 actions[i] = new AbstractAction(option) {
632 public void actionPerformed(ActionEvent e) {
633 close(exitCode);
638 return actions;
641 protected JComponent createCenterPanel() {
642 return null;
645 protected JComponent createNorthPanel() {
646 JPanel panel = new JPanel(new BorderLayout(15, 0));
647 if (myIcon != null) {
648 JLabel iconLabel = new JLabel(myIcon);
649 Container container = new Container();
650 container.setLayout(new BorderLayout());
651 container.add(iconLabel, BorderLayout.NORTH);
652 panel.add(container, BorderLayout.WEST);
655 JPanel messagePanel = new JPanel(new BorderLayout());
656 if (myMessage != null) {
657 JLabel textLabel = new JLabel(myMessage);
658 textLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
659 textLabel.setUI(new MultiLineLabelUI());
660 messagePanel.add(textLabel, BorderLayout.NORTH);
663 myField = new JTextField(30);
664 messagePanel.add(myField, BorderLayout.SOUTH);
665 panel.add(messagePanel, BorderLayout.CENTER);
667 return panel;
670 public JTextField getTextField() {
671 return myField;
674 public JComponent getPreferredFocusedComponent() {
675 return myField;
678 @Nullable
679 public String getInputString() {
680 if (getExitCode() == 0) {
681 return myField.getText().trim();
683 else {
684 return null;
689 protected static class ChooseDialog extends MessageDialog {
690 private ComboBox myComboBox;
691 private InputValidator myValidator;
693 public ChooseDialog(Project project,
694 String message,
695 String title,
696 Icon icon,
697 String[] values,
698 String initialValue,
699 String[] options,
700 int defaultOption) {
701 super(project, message, title, options, defaultOption, icon);
702 myComboBox.setModel(new DefaultComboBoxModel(values));
703 myComboBox.setSelectedItem(initialValue);
706 public ChooseDialog(Project project, String message, String title, Icon icon, String[] values, String initialValue) {
707 this(project, message, title, icon, values, initialValue, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0);
710 public ChooseDialog(Component parent, String message, String title, Icon icon, String[] values, String initialValue) {
711 super(parent, message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
712 myComboBox.setModel(new DefaultComboBoxModel(values));
713 myComboBox.setSelectedItem(initialValue);
716 public ChooseDialog(String message, String title, Icon icon, String[] values, String initialValue) {
717 super(message, title, new String[]{OK_BUTTON, CANCEL_BUTTON}, 0, icon);
718 myComboBox.setModel(new DefaultComboBoxModel(values));
719 myComboBox.setSelectedItem(initialValue);
722 protected Action[] createActions() {
723 final Action[] actions = new Action[myOptions.length];
724 for (int i = 0; i < myOptions.length; i++) {
725 String option = myOptions[i];
726 final int exitCode = i;
727 if (i == myDefaultOptionIndex) {
728 actions[i] = new AbstractAction(option) {
729 public void actionPerformed(ActionEvent e) {
730 if (myValidator == null || myValidator.checkInput(myComboBox.getSelectedItem().toString().trim())) {
731 close(exitCode);
735 actions[i].putValue(DEFAULT_ACTION, Boolean.TRUE);
736 myComboBox.addItemListener(new ItemListener() {
737 public void itemStateChanged(ItemEvent e) {
738 actions[exitCode].setEnabled(myValidator == null || myValidator.checkInput(myComboBox.getSelectedItem().toString().trim()));
741 final JTextField textField = (JTextField)myComboBox.getEditor().getEditorComponent();
742 textField.getDocument().addDocumentListener(new DocumentAdapter() {
743 public void textChanged(DocumentEvent event) {
744 actions[exitCode].setEnabled(myValidator == null || myValidator.checkInput(textField.getText().trim()));
748 else { // "Cancel" action
749 actions[i] = new AbstractAction(option) {
750 public void actionPerformed(ActionEvent e) {
751 close(exitCode);
756 return actions;
759 protected JComponent createCenterPanel() {
760 return null;
763 protected JComponent createNorthPanel() {
764 JPanel panel = new JPanel(new BorderLayout(15, 0));
765 if (myIcon != null) {
766 JLabel iconLabel = new JLabel(myIcon);
767 Container container = new Container();
768 container.setLayout(new BorderLayout());
769 container.add(iconLabel, BorderLayout.NORTH);
770 panel.add(container, BorderLayout.WEST);
773 JPanel messagePanel = new JPanel(new BorderLayout());
774 if (myMessage != null) {
775 JLabel textLabel = new JLabel(myMessage);
776 textLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
777 textLabel.setUI(new MultiLineLabelUI());
778 messagePanel.add(textLabel, BorderLayout.NORTH);
781 myComboBox = new ComboBox(220);
782 messagePanel.add(myComboBox, BorderLayout.SOUTH);
783 panel.add(messagePanel, BorderLayout.CENTER);
784 return panel;
787 protected void doOKAction() {
788 String inputString = myComboBox.getSelectedItem().toString().trim();
789 if (myValidator == null ||
790 myValidator.checkInput(inputString) &&
791 myValidator.canClose(inputString)) {
792 super.doOKAction();
796 public JComponent getPreferredFocusedComponent() {
797 return myComboBox;
800 @Nullable
801 public String getInputString() {
802 if (getExitCode() == 0) {
803 return myComboBox.getSelectedItem().toString();
805 else {
806 return null;
810 public int getSelectedIndex() {
811 if (getExitCode() == 0) {
812 return myComboBox.getSelectedIndex();
814 else {
815 return -1;
819 public JComboBox getComboBox() {
820 return myComboBox;
823 public void setValidator(InputValidator validator) {
824 myValidator = validator;