cucumber, qick fix: preselection in new step dialog
[fedora-idea.git] / platform / platform-api / src / com / intellij / openapi / ui / Messages.java
blobf3447e0c3ab6895f49867a8465140ec7bf21d4a7
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.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;
35 import javax.swing.*;
36 import javax.swing.event.DocumentEvent;
37 import javax.swing.plaf.basic.BasicHTML;
38 import javax.swing.text.html.HTMLEditorKit;
39 import java.awt.*;
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();
54 @TestOnly
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;
62 return oldValue;
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);
86 else {
87 MessageDialog dialog = new MessageDialog(project, message, title, options, defaultOptionIndex, icon);
88 dialog.show();
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);
98 else {
99 MessageDialog dialog = new MessageDialog(parent, message, title, options, defaultOptionIndex, icon);
100 dialog.show();
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);
117 else {
118 //what's it? if (application.isUnitTestMode()) throw new RuntimeException(message);
119 MessageDialog dialog = new MessageDialog(message, title, options, defaultOptionIndex, icon);
120 dialog.show();
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.
252 @Nullable
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.
260 @Nullable
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)
271 @Nullable
272 public static String showInputDialog(String message, String title, Icon icon) {
273 return showInputDialog(message, title, icon, null, null);
276 @Nullable
277 public static String showInputDialog(Project project,
278 @Nls String message,
279 @Nls String title,
280 Icon icon,
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);
287 else {
288 InputDialog dialog = new InputDialog(project, message, title, icon, initialValue, validator);
289 dialog.show();
290 return dialog.getInputString();
294 @Nullable
295 public static String showInputDialog(Project project,
296 @Nls String message,
297 @Nls String title,
298 Icon icon,
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);
306 else {
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());
314 } else {
315 // reset selection
316 final int length = field.getDocument().getLength();
317 field.select(length, length);
319 field.putClientProperty(DialogWrapperPeer.HAVE_INITIAL_SELECTION, true);
321 dialog.show();
322 return dialog.getInputString();
326 @Nullable
327 public static String showInputDialog(Component parent,
328 String message,
329 String title,
330 Icon icon,
331 @NonNls String initialValue,
332 InputValidator validator) {
333 final Application application = ApplicationManager.getApplication();
334 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
335 return ourTestInputImplementation.show(message);
337 else {
339 InputDialog dialog = new InputDialog(parent, message, title, icon, initialValue, validator);
340 dialog.show();
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)
351 @Nullable
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);
357 else {
358 InputDialog dialog = new InputDialog(message, title, icon, initialValue, validator);
359 dialog.show();
360 return dialog.getInputString();
364 @Nullable
365 public static String showEditableChooseDialog(String message,
366 String title,
367 Icon icon,
368 String[] values,
369 String initialValue,
370 InputValidator validator) {
371 final Application application = ApplicationManager.getApplication();
372 if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
373 return ourTestInputImplementation.show(message);
375 else {
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);
381 dialog.show();
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);
391 else {
392 ChooseDialog dialog = new ChooseDialog(message, title, icon, values, initialValue);
393 dialog.show();
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);
403 else {
404 ChooseDialog dialog = new ChooseDialog(parent, message, title, icon, values, initialValue);
405 dialog.show();
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);
418 else {
419 ChooseDialog dialog = new ChooseDialog(project, message, title, icon, values, initialValue);
420 dialog.show();
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);
454 else {
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();
476 builder.show();
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) {
497 super(false);
498 _init(title, message, options, defaultOptionIndex, icon);
501 private void _init(String title, String message, String[] options, int defaultOptionIndex, Icon icon) {
502 setTitle(title);
503 myMessage = message;
504 myOptions = options;
505 myDefaultOptionIndex = defaultOptionIndex;
506 myIcon = icon;
507 setButtonsAlignment(SwingUtilities.CENTER);
508 init();
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) {
518 close(exitCode);
521 if (i == myDefaultOptionIndex) {
522 actions[i].putValue(DEFAULT_ACTION, Boolean.TRUE);
524 assignMnemonic(option, actions[i]);
527 return actions;
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() {
541 close(-1);
544 protected JComponent createNorthPanel() {
545 return null;
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() {
589 public void run() {
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();
598 else {
599 panel.add(messageComponent, BorderLayout.CENTER);
602 return panel;
605 @Override
606 protected void doHelpAction() {
607 // do nothing
611 protected static class InputDialog extends MessageDialog {
612 private JTextField myField;
613 private InputValidator myValidator;
615 public InputDialog(Project project,
616 String message,
617 String title,
618 Icon icon,
619 String initialValue,
620 InputValidator validator,
621 String[] options,
622 int defaultOption) {
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();
653 if (
654 myValidator == null ||
655 myValidator.checkInput(inputString) &&
656 myValidator.canClose(inputString)
658 close(exitCode);
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));
673 else {
674 actions[i] = new AbstractAction(option) {
675 public void actionPerformed(ActionEvent e) {
676 close(exitCode);
681 return actions;
684 protected JComponent createCenterPanel() {
685 return null;
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);
710 return panel;
713 public JTextField getTextField() {
714 return myField;
717 public JComponent getPreferredFocusedComponent() {
718 return myField;
721 @Nullable
722 public String getInputString() {
723 if (getExitCode() == 0) {
724 return myField.getText().trim();
726 else {
727 return null;
732 protected static class ChooseDialog extends MessageDialog {
733 private ComboBox myComboBox;
734 private InputValidator myValidator;
736 public ChooseDialog(Project project,
737 String message,
738 String title,
739 Icon icon,
740 String[] values,
741 String initialValue,
742 String[] options,
743 int defaultOption) {
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())) {
774 close(exitCode);
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) {
794 close(exitCode);
799 return actions;
802 protected JComponent createCenterPanel() {
803 return null;
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);
827 return panel;
830 protected void doOKAction() {
831 String inputString = myComboBox.getSelectedItem().toString().trim();
832 if (myValidator == null ||
833 myValidator.checkInput(inputString) &&
834 myValidator.canClose(inputString)) {
835 super.doOKAction();
839 public JComponent getPreferredFocusedComponent() {
840 return myComboBox;
843 @Nullable
844 public String getInputString() {
845 if (getExitCode() == 0) {
846 return myComboBox.getSelectedItem().toString();
848 else {
849 return null;
853 public int getSelectedIndex() {
854 if (getExitCode() == 0) {
855 return myComboBox.getSelectedIndex();
857 else {
858 return -1;
862 public JComboBox getComboBox() {
863 return myComboBox;
866 public void setValidator(InputValidator validator) {
867 myValidator = validator;