Skip to content

Commit 8a6846b

Browse files
Added highlighting reseting before validation
1 parent fc40807 commit 8a6846b

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

src/com/magento/idea/magento2plugin/actions/generation/dialog/AbstractDialog.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public AbstractDialog() {
5252

5353
protected void centerDialog(final AbstractDialog dialog) {
5454
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
55-
final int cordX = screenSize.width / 2 - dialog.getSize().width / 2;
56-
final int cordY = screenSize.height / 2 - dialog.getSize().height / 2;
57-
dialog.setLocation(cordX, cordY);
55+
final int coordinateX = screenSize.width / 2 - dialog.getSize().width / 2;
56+
final int coordinateY = screenSize.height / 2 - dialog.getSize().height / 2;
57+
dialog.setLocation(coordinateX, coordinateY);
5858
}
5959

6060
protected void onCancel() {
@@ -70,6 +70,7 @@ protected void onCancel() {
7070
protected boolean validateFormFields() {
7171
boolean dialogHasErrors;
7272
isValidationErrorShown = dialogHasErrors = false;
73+
clearValidationHighlighting();
7374

7475
for (final FieldValidationData fieldValidationData : getFieldsToValidate()) {
7576
final Field field = fieldValidationData.getField();
@@ -103,6 +104,15 @@ protected boolean validateFormFields() {
103104
return !dialogHasErrors;
104105
}
105106

107+
/**
108+
* Reset highlighting for fields.
109+
*/
110+
protected void clearValidationHighlighting() {
111+
for (final FieldValidationData fieldValidationData : fieldsValidationsList) {
112+
DialogFieldErrorUtil.resetFieldHighlighting(fieldValidationData.getField(), this);
113+
}
114+
}
115+
106116
/**
107117
* Override this method to change which fields should or shouldn't be validated.
108118
*

src/com/magento/idea/magento2plugin/actions/generation/dialog/util/DialogFieldErrorUtil.java

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77

88
import com.intellij.util.ui.UIUtil;
99
import com.magento.idea.magento2plugin.actions.generation.dialog.AbstractDialog;
10+
import com.magento.idea.magento2plugin.actions.generation.dialog.reflection.ExtractComponentFromFieldUtil;
1011
import java.awt.Color;
1112
import java.awt.event.FocusEvent;
1213
import java.awt.event.FocusListener;
1314
import java.lang.reflect.Field;
1415
import javax.swing.BorderFactory;
16+
import javax.swing.JComboBox;
1517
import javax.swing.JComponent;
1618
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;
1922
import org.jetbrains.annotations.NotNull;
2023

2124
public final class DialogFieldErrorUtil {
@@ -49,26 +52,75 @@ public static void highlightField(
4952
* @param fieldComponent JComponent
5053
*/
5154
public static void highlightField(final @NotNull JComponent fieldComponent) {
52-
final Color defaultBackgroundColor = fieldComponent.getBackground();
53-
5455
fieldComponent.setBorder(BorderFactory.createLineBorder(ERROR_COLOR));
5556
fieldComponent.setBackground(ERROR_BACKGROUND_COLOR);
5657

5758
fieldComponent.addFocusListener(new FocusListener() {
5859
@Override
5960
public void focusGained(final FocusEvent event) {
60-
fieldComponent.setBorder(null);
61-
fieldComponent.setBackground(defaultBackgroundColor);
61+
resetComponentHighlighting(fieldComponent);
6262
}
6363

6464
@Override
6565
public void focusLost(final FocusEvent event) {
66-
fieldComponent.setBorder(null);
67-
fieldComponent.setBackground(defaultBackgroundColor);
66+
resetComponentHighlighting(fieldComponent);
6867
}
6968
});
7069
}
7170

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+
72124
/**
73125
* Show error message for field.
74126
*

0 commit comments

Comments
 (0)