mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ValueEditor listener
This commit is contained in:
+12
-9
@@ -25,6 +25,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.ui.components.fields.CommaSeparatedIntegersField;
|
||||
import com.intellij.ui.components.fields.IntegerField;
|
||||
import com.intellij.ui.components.fields.valueEditors.ValueEditor;
|
||||
import com.intellij.ui.components.labels.ActionLink;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -52,7 +53,7 @@ public class RightMarginForm {
|
||||
private CommaSeparatedIntegersField myVisualGuidesField;
|
||||
@SuppressWarnings("unused") private ActionLink myResetLink;
|
||||
private final Language myLanguage;
|
||||
private CodeStyleSettings mySettings;
|
||||
private final CodeStyleSettings mySettings;
|
||||
|
||||
public RightMarginForm(@NotNull Language language, @NotNull CodeStyleSettings settings) {
|
||||
myLanguage = language;
|
||||
@@ -65,14 +66,14 @@ public class RightMarginForm {
|
||||
}
|
||||
|
||||
void createUIComponents() {
|
||||
//noinspection ConstantConditions
|
||||
myRightMarginField = new IntegerField(ApplicationBundle.message("editbox.right.margin.columns"), 0, CodeStyleSettings.MAX_RIGHT_MARGIN) {
|
||||
@NotNull
|
||||
myRightMarginField = new IntegerField(ApplicationBundle.message("editbox.right.margin.columns"), 0, CodeStyleSettings.MAX_RIGHT_MARGIN);
|
||||
myRightMarginField.getValueEditor().addListener(new ValueEditor.Listener<Integer>() {
|
||||
@Override
|
||||
protected String getEmptyValueText() {
|
||||
return getDefaultRightMarginText();
|
||||
public void valueChanged(@NotNull Integer newValue) {
|
||||
myResetLink.setVisible(!newValue.equals(myRightMarginField.getDefaultValue()));
|
||||
myRightMarginField.getEmptyText().setText(getDefaultRightMarginText(mySettings));
|
||||
}
|
||||
};
|
||||
});
|
||||
myRightMarginField.setCanBeEmpty(true);
|
||||
myRightMarginField.setDefaultValue(-1);
|
||||
myVisualGuidesField = new CommaSeparatedIntegersField(ApplicationBundle.message("settings.code.style.visual.guides"), 0, CodeStyleSettings.MAX_RIGHT_MARGIN, "Optional");
|
||||
@@ -96,6 +97,8 @@ public class RightMarginForm {
|
||||
}
|
||||
}
|
||||
myVisualGuidesField.setValue(settings.getSoftMargins(myLanguage));
|
||||
myResetLink.setVisible(langSettings.RIGHT_MARGIN >= 0);
|
||||
myRightMarginField.getEmptyText().setText(getDefaultRightMarginText(settings));
|
||||
}
|
||||
|
||||
public void apply(@NotNull CodeStyleSettings settings) throws ConfigurationException {
|
||||
@@ -129,7 +132,7 @@ public class RightMarginForm {
|
||||
return myTopPanel;
|
||||
}
|
||||
|
||||
private String getDefaultRightMarginText() {
|
||||
return "Default: " + mySettings.getDefaultRightMargin();
|
||||
private static String getDefaultRightMarginText(@NotNull CodeStyleSettings settings) {
|
||||
return "Default: " + settings.getDefaultRightMargin();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,15 +33,9 @@ public class IntegerField extends JBTextField {
|
||||
}
|
||||
|
||||
public IntegerField(@Nullable String valueName, int minValue, int maxValue) {
|
||||
myValueEditor = new IntegerValueEditor(this, valueName, minValue) {
|
||||
@Override
|
||||
protected void onEmptyValue() {
|
||||
updateEmptyValueText();
|
||||
}
|
||||
};
|
||||
myValueEditor = new IntegerValueEditor(this, valueName, minValue);
|
||||
myValueEditor.setMinValue(minValue);
|
||||
myValueEditor.setMaxValue(maxValue);
|
||||
updateEmptyValueText();
|
||||
}
|
||||
|
||||
public int getMinValue() {
|
||||
@@ -104,12 +98,7 @@ public class IntegerField extends JBTextField {
|
||||
myValueEditor.setValue(myValueEditor.getDefaultValue());
|
||||
}
|
||||
|
||||
public void updateEmptyValueText() {
|
||||
getEmptyText().setText(getEmptyValueText());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getEmptyValueText() {
|
||||
return "";
|
||||
public IntegerValueEditor getValueEditor() {
|
||||
return myValueEditor;
|
||||
}
|
||||
}
|
||||
|
||||
+13
@@ -20,10 +20,14 @@ import com.intellij.openapi.util.InvalidDataException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractValueEditor<T> implements ValueEditor<T> {
|
||||
|
||||
private @NotNull T myDefaultValue;
|
||||
private @Nullable String myValueName;
|
||||
private final List<Listener<T>> myListeners = new ArrayList<>();
|
||||
|
||||
protected AbstractValueEditor(@Nullable String valueName,
|
||||
@NotNull T defaultValue) {
|
||||
@@ -90,4 +94,13 @@ public abstract class AbstractValueEditor<T> implements ValueEditor<T> {
|
||||
public T getDefaultValue() {
|
||||
return myDefaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(@NotNull Listener<T> editorListener) {
|
||||
myListeners.add(editorListener);
|
||||
}
|
||||
|
||||
public void fireValueChanged(@NotNull T newValue) {
|
||||
for (Listener<T> listener : myListeners) listener.valueChanged(newValue);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-6
@@ -46,10 +46,8 @@ public abstract class TextFieldValueEditor<T> extends AbstractValueEditor<T> {
|
||||
@SuppressWarnings("unused")
|
||||
protected String validateTextOnChange(String text, DocumentEvent e) {
|
||||
try {
|
||||
if (StringUtil.isEmptyOrSpaces(text)) {
|
||||
onEmptyValue();
|
||||
}
|
||||
parseValue(text);
|
||||
T newValue = parseValue(text);
|
||||
fireValueChanged(newValue);
|
||||
return null;
|
||||
}
|
||||
catch (InvalidDataException ex) {
|
||||
@@ -77,6 +75,4 @@ public abstract class TextFieldValueEditor<T> extends AbstractValueEditor<T> {
|
||||
myField.setText(text);
|
||||
}
|
||||
|
||||
protected void onEmptyValue() {}
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -84,4 +84,10 @@ public interface ValueEditor<T> {
|
||||
* @param value The value to check.
|
||||
*/
|
||||
void assertValid(@NotNull T value);
|
||||
|
||||
void addListener(@NotNull Listener<T> editorListener);
|
||||
|
||||
interface Listener<T> {
|
||||
void valueChanged(@NotNull T newValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user