|
7 | 7 |
|
8 | 8 | import com.intellij.util.ui.UIUtil;
|
9 | 9 | import com.magento.idea.magento2plugin.actions.generation.dialog.AbstractDialog;
|
| 10 | +import com.magento.idea.magento2plugin.actions.generation.dialog.reflection.ExtractComponentFromFieldUtil; |
10 | 11 | import java.awt.Color;
|
11 | 12 | import java.awt.event.FocusEvent;
|
12 | 13 | import java.awt.event.FocusListener;
|
13 | 14 | import java.lang.reflect.Field;
|
14 | 15 | import javax.swing.BorderFactory;
|
| 16 | +import javax.swing.JComboBox; |
15 | 17 | import javax.swing.JComponent;
|
16 | 18 | import javax.swing.JLabel;
|
17 |
| - |
18 |
| -import com.magento.idea.magento2plugin.actions.generation.dialog.reflection.ExtractComponentFromFieldUtil; |
| 19 | +import javax.swing.JTextField; |
| 20 | +import javax.swing.UIManager; |
| 21 | +import javax.swing.border.Border; |
19 | 22 | import org.jetbrains.annotations.NotNull;
|
20 | 23 |
|
21 | 24 | public final class DialogFieldErrorUtil {
|
@@ -49,26 +52,75 @@ public static void highlightField(
|
49 | 52 | * @param fieldComponent JComponent
|
50 | 53 | */
|
51 | 54 | public static void highlightField(final @NotNull JComponent fieldComponent) {
|
52 |
| - final Color defaultBackgroundColor = fieldComponent.getBackground(); |
53 |
| - |
54 | 55 | fieldComponent.setBorder(BorderFactory.createLineBorder(ERROR_COLOR));
|
55 | 56 | fieldComponent.setBackground(ERROR_BACKGROUND_COLOR);
|
56 | 57 |
|
57 | 58 | fieldComponent.addFocusListener(new FocusListener() {
|
58 | 59 | @Override
|
59 | 60 | public void focusGained(final FocusEvent event) {
|
60 |
| - fieldComponent.setBorder(null); |
61 |
| - fieldComponent.setBackground(defaultBackgroundColor); |
| 61 | + resetComponentHighlighting(fieldComponent); |
62 | 62 | }
|
63 | 63 |
|
64 | 64 | @Override
|
65 | 65 | public void focusLost(final FocusEvent event) {
|
66 |
| - fieldComponent.setBorder(null); |
67 |
| - fieldComponent.setBackground(defaultBackgroundColor); |
| 66 | + resetComponentHighlighting(fieldComponent); |
68 | 67 | }
|
69 | 68 | });
|
70 | 69 | }
|
71 | 70 |
|
| 71 | + /** |
| 72 | + * Reset field highlighting. |
| 73 | + * |
| 74 | + * @param field Field |
| 75 | + * @param dialog AbstractDialog |
| 76 | + */ |
| 77 | + public static void resetFieldHighlighting( |
| 78 | + final @NotNull Field field, |
| 79 | + final @NotNull AbstractDialog dialog |
| 80 | + ) { |
| 81 | + final JComponent fieldComponent = ExtractComponentFromFieldUtil.extract(field, dialog); |
| 82 | + |
| 83 | + if (fieldComponent == null) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + resetComponentHighlighting(fieldComponent); |
| 88 | + final JLabel messageHolder = getMessageHolderForField(dialog, field); |
| 89 | + |
| 90 | + if (messageHolder == null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + messageHolder.setVisible(false); |
| 95 | + messageHolder.setText(""); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Reset component highlighting. |
| 100 | + * |
| 101 | + * @param fieldComponent JComponent |
| 102 | + */ |
| 103 | + public static void resetComponentHighlighting( |
| 104 | + final @NotNull JComponent fieldComponent |
| 105 | + ) { |
| 106 | + Color defaultBackgroundColor; |
| 107 | + Border defaultBorder; |
| 108 | + |
| 109 | + if (fieldComponent instanceof JTextField) { |
| 110 | + defaultBackgroundColor = UIManager.getColor("TextField.background"); |
| 111 | + defaultBorder = UIManager.getBorder("TextField.border"); |
| 112 | + } else if (fieldComponent instanceof JComboBox) { |
| 113 | + defaultBackgroundColor = UIManager.getColor("ComboBox.background"); |
| 114 | + defaultBorder = UIManager.getBorder("ComboBox.border"); |
| 115 | + } else { |
| 116 | + defaultBackgroundColor = UIManager.getColor("TextField.background"); |
| 117 | + defaultBorder = UIManager.getBorder("TextField.border"); |
| 118 | + } |
| 119 | + |
| 120 | + fieldComponent.setBackground(defaultBackgroundColor); |
| 121 | + fieldComponent.setBorder(defaultBorder); |
| 122 | + } |
| 123 | + |
72 | 124 | /**
|
73 | 125 | * Show error message for field.
|
74 | 126 | *
|
|
0 commit comments