mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Right margin field implemented according to UI spec (IDEA-173488)
- A default value is shown if empty - "Reset" link (sets the default value)
This commit is contained in:
+10
-10
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.codeStyle.RightMarginForm">
|
||||
<grid id="27dc6" binding="myTopPanel" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="27dc6" binding="myTopPanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="10" left="10" bottom="10" right="10"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="500" height="400"/>
|
||||
@@ -37,17 +37,9 @@
|
||||
</component>
|
||||
<hspacer id="3b391">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<component id="5ef52" class="javax.swing.JCheckBox" binding="myDefaultGeneralCheckBox" default-binding="true">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/ApplicationBundle" key="settings.code.style.default.general"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6511a" class="com.intellij.ui.components.fields.IntegerField" binding="myRightMarginField" custom-create="true">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
@@ -86,6 +78,14 @@
|
||||
<text resource-bundle="messages/ApplicationBundle" key="margin.columns"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="82576" class="com.intellij.ui.components.labels.ActionLink" binding="myResetLink" custom-create="true">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Reset"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
|
||||
+31
-53
@@ -16,18 +16,19 @@
|
||||
package com.intellij.application.options.codeStyle;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.application.ApplicationBundle;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.project.DumbAwareAction;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
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.labels.ActionLink;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
/**
|
||||
* Can be used for languages which do not use standard "Wrapping and Braces" panel.
|
||||
@@ -46,28 +47,16 @@ import javax.swing.event.ChangeListener;
|
||||
*/
|
||||
public class RightMarginForm {
|
||||
private IntegerField myRightMarginField;
|
||||
private JCheckBox myDefaultGeneralCheckBox;
|
||||
private JPanel myTopPanel;
|
||||
private JComboBox myWrapOnTypingCombo;
|
||||
private CommaSeparatedIntegersField myVisualGuidesField;
|
||||
@SuppressWarnings("unused") private ActionLink myResetLink;
|
||||
private final Language myLanguage;
|
||||
private final int myDefaultRightMargin;
|
||||
private CodeStyleSettings mySettings;
|
||||
|
||||
public RightMarginForm(@NotNull Language language, @NotNull CodeStyleSettings settings) {
|
||||
myLanguage = language;
|
||||
myDefaultRightMargin = settings.getDefaultRightMargin();
|
||||
myDefaultGeneralCheckBox.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
if (myDefaultGeneralCheckBox.isSelected()) {
|
||||
myRightMarginField.setText(Integer.toString(myDefaultRightMargin));
|
||||
myRightMarginField.setEnabled(false);
|
||||
}
|
||||
else {
|
||||
myRightMarginField.setEnabled(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
mySettings = settings;
|
||||
|
||||
//noinspection unchecked
|
||||
myWrapOnTypingCombo.setModel(new DefaultComboBoxModel(
|
||||
@@ -76,24 +65,30 @@ public class RightMarginForm {
|
||||
}
|
||||
|
||||
void createUIComponents() {
|
||||
myRightMarginField = new IntegerField(ApplicationBundle.message("editbox.right.margin.columns"), 0, CodeStyleSettings.MAX_RIGHT_MARGIN);
|
||||
//noinspection ConstantConditions
|
||||
myRightMarginField = new IntegerField(ApplicationBundle.message("editbox.right.margin.columns"), 0, CodeStyleSettings.MAX_RIGHT_MARGIN) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getEmptyValueText() {
|
||||
return getDefaultRightMarginText();
|
||||
}
|
||||
};
|
||||
myRightMarginField.setCanBeEmpty(true);
|
||||
myRightMarginField.setDefaultValue(-1);
|
||||
myVisualGuidesField = new CommaSeparatedIntegersField(ApplicationBundle.message("settings.code.style.visual.guides"), 0, CodeStyleSettings.MAX_RIGHT_MARGIN, "Optional");
|
||||
myResetLink = new ActionLink("Reset", new ResetRightMarginAction());
|
||||
}
|
||||
|
||||
private class ResetRightMarginAction extends DumbAwareAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
myRightMarginField.resetToDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public void reset(@NotNull CodeStyleSettings settings) {
|
||||
CommonCodeStyleSettings langSettings = settings.getCommonSettings(myLanguage);
|
||||
if (langSettings != settings && langSettings.RIGHT_MARGIN >= 0) {
|
||||
myDefaultGeneralCheckBox.setSelected(false);
|
||||
myRightMarginField.setText(Integer.toString(langSettings.RIGHT_MARGIN));
|
||||
}
|
||||
else {
|
||||
myDefaultGeneralCheckBox.setSelected(true);
|
||||
myRightMarginField.setText(Integer.toString(settings.getDefaultRightMargin()));
|
||||
if (langSettings == settings) {
|
||||
myDefaultGeneralCheckBox.setEnabled(false);
|
||||
myRightMarginField.setEnabled(false);
|
||||
}
|
||||
}
|
||||
myRightMarginField.setValue(langSettings.RIGHT_MARGIN);
|
||||
for (int i = 0; i < CodeStyleSettingsCustomizable.WRAP_ON_TYPING_VALUES.length; i ++) {
|
||||
if (langSettings.WRAP_ON_TYPING == CodeStyleSettingsCustomizable.WRAP_ON_TYPING_VALUES[i]) {
|
||||
myWrapOnTypingCombo.setSelectedIndex(i);
|
||||
@@ -108,12 +103,7 @@ public class RightMarginForm {
|
||||
myVisualGuidesField.validateContent();
|
||||
CommonCodeStyleSettings langSettings = settings.getCommonSettings(myLanguage);
|
||||
if (langSettings != settings) {
|
||||
if (myDefaultGeneralCheckBox.isSelected()) {
|
||||
langSettings.RIGHT_MARGIN = -1;
|
||||
}
|
||||
else {
|
||||
langSettings.RIGHT_MARGIN = getFieldRightMargin(settings.getDefaultRightMargin());
|
||||
}
|
||||
langSettings.RIGHT_MARGIN = myRightMarginField.getValue();
|
||||
}
|
||||
langSettings.WRAP_ON_TYPING = getSelectedWrapOnTypingValue();
|
||||
settings.setSoftMargins(myLanguage, myVisualGuidesField.getValue());
|
||||
@@ -121,27 +111,11 @@ public class RightMarginForm {
|
||||
|
||||
public boolean isModified(@NotNull CodeStyleSettings settings) {
|
||||
CommonCodeStyleSettings langSettings = settings.getCommonSettings(myLanguage);
|
||||
boolean rightMarginModified =
|
||||
myDefaultGeneralCheckBox.isSelected() ?
|
||||
langSettings.RIGHT_MARGIN >= 0 :
|
||||
langSettings.RIGHT_MARGIN != getFieldRightMargin(settings.getDefaultRightMargin());
|
||||
return rightMarginModified ||
|
||||
return langSettings.RIGHT_MARGIN != myRightMarginField.getValue() ||
|
||||
langSettings.WRAP_ON_TYPING != getSelectedWrapOnTypingValue() ||
|
||||
!settings.getSoftMargins(myLanguage).equals(myVisualGuidesField.getValue());
|
||||
}
|
||||
|
||||
private int getFieldRightMargin(int fallBackValue) {
|
||||
String strValue = myRightMarginField.getText();
|
||||
if (!strValue.trim().isEmpty()) {
|
||||
try {
|
||||
return Integer.parseInt(strValue);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
myRightMarginField.setText(Integer.toString(fallBackValue));
|
||||
}
|
||||
}
|
||||
return fallBackValue;
|
||||
}
|
||||
|
||||
private int getSelectedWrapOnTypingValue() {
|
||||
int i = myWrapOnTypingCombo.getSelectedIndex();
|
||||
@@ -154,4 +128,8 @@ public class RightMarginForm {
|
||||
public JPanel getTopPanel() {
|
||||
return myTopPanel;
|
||||
}
|
||||
|
||||
private String getDefaultRightMarginText() {
|
||||
return "Default: " + mySettings.getDefaultRightMargin();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,9 +33,15 @@ public class IntegerField extends JBTextField {
|
||||
}
|
||||
|
||||
public IntegerField(@Nullable String valueName, int minValue, int maxValue) {
|
||||
myValueEditor = new IntegerValueEditor(this, valueName, minValue);
|
||||
myValueEditor = new IntegerValueEditor(this, valueName, minValue) {
|
||||
@Override
|
||||
protected void onEmptyValue() {
|
||||
updateEmptyValueText();
|
||||
}
|
||||
};
|
||||
myValueEditor.setMinValue(minValue);
|
||||
myValueEditor.setMaxValue(maxValue);
|
||||
updateEmptyValueText();
|
||||
}
|
||||
|
||||
public int getMinValue() {
|
||||
@@ -93,4 +99,17 @@ public class IntegerField extends JBTextField {
|
||||
public Integer getDefaultValue() {
|
||||
return myValueEditor.getDefaultValue();
|
||||
}
|
||||
|
||||
public void resetToDefault() {
|
||||
myValueEditor.setValue(myValueEditor.getDefaultValue());
|
||||
}
|
||||
|
||||
public void updateEmptyValueText() {
|
||||
getEmptyText().setText(getEmptyValueText());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getEmptyValueText() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -46,6 +46,9 @@ 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);
|
||||
return null;
|
||||
}
|
||||
@@ -74,4 +77,6 @@ public abstract class TextFieldValueEditor<T> extends AbstractValueEditor<T> {
|
||||
myField.setText(text);
|
||||
}
|
||||
|
||||
protected void onEmptyValue() {}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user