IndentOptionsEditor's text fields migrated to IntegerField

This commit is contained in:
Rustam Vishnyakov
2017-09-22 13:30:45 +03:00
parent d88c9c82b8
commit e511e792be
6 changed files with 104 additions and 44 deletions
@@ -20,11 +20,21 @@ import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.ui.OptionGroup;
import com.intellij.ui.components.fields.IntegerField;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import static com.intellij.psi.codeStyle.CodeStyleConstraints.*;
import static com.intellij.psi.codeStyle.CodeStyleDefaults.DEFAULT_INDENT_SIZE;
import static com.intellij.psi.codeStyle.CodeStyleDefaults.DEFAULT_TAB_SIZE;
@SuppressWarnings("Duplicates")
public class IndentOptionsEditor extends OptionGroup {
private static final String INDENT_LABEL = ApplicationBundle.message("editbox.indent.indent");
private static final String TAB_SIZE_LABEL = ApplicationBundle.message("editbox.indent.tab.size");
protected JTextField myIndentField;
protected JCheckBox myCbUseTab;
protected JTextField myTabSizeField;
@@ -34,25 +44,39 @@ public class IndentOptionsEditor extends OptionGroup {
@Override
public JPanel createPanel() {
addComponents();
final JPanel result = super.createPanel();
return result;
return super.createPanel();
}
protected void addComponents() {
addTabOptions();
addTabSizeField();
addIndentField();
}
myTabSizeField = createIndentTextField();
myTabSizeLabel = new JLabel(ApplicationBundle.message("editbox.indent.tab.size"));
add(myTabSizeLabel, myTabSizeField);
myIndentField = createIndentTextField();
myIndentLabel = new JLabel(ApplicationBundle.message("editbox.indent.indent"));
protected void addIndentField() {
myIndentField = createIndentTextField(INDENT_LABEL, MIN_INDENT_SIZE, MAX_INDENT_SIZE, DEFAULT_INDENT_SIZE);
myIndentLabel = new JLabel(INDENT_LABEL);
add(myIndentLabel, myIndentField);
}
protected void addTabSizeField() {
myTabSizeField = createIndentTextField(TAB_SIZE_LABEL, MIN_TAB_SIZE, MAX_TAB_SIZE, DEFAULT_TAB_SIZE);
myTabSizeLabel = new JLabel(TAB_SIZE_LABEL);
add(myTabSizeLabel, myTabSizeField);
}
/**
* @deprecated Use {@link #createIndentTextField(String, int, int, int)}
*/
@Deprecated
protected JTextField createIndentTextField() {
JTextField field = new JTextField(4);
return createIndentTextField(null, Integer.MIN_VALUE, Integer.MAX_VALUE, 0);
}
protected IntegerField createIndentTextField(@Nullable String valueName, int minSize, int maxSize, int defaultValue) {
IntegerField field = new IntegerField(valueName, minSize, maxSize);
field.setDefaultValue(defaultValue);
field.setColumns(4);
field.setMinimumSize(field.getPreferredSize());
return field;
}
@@ -67,6 +91,7 @@ public class IndentOptionsEditor extends OptionGroup {
}
protected static boolean isFieldModified(JTextField textField, int value) {
if (textField instanceof IntegerField) return ((IntegerField)textField).getValue() != value;
try {
int fieldValue = Integer.parseInt(textField.getText().trim());
return fieldValue != value;
@@ -76,15 +101,6 @@ public class IndentOptionsEditor extends OptionGroup {
}
}
protected int getFieldValue(JTextField field, int minValue, int defValue) {
try {
return Math.max(Integer.parseInt(field.getText()), minValue);
}
catch (NumberFormatException e) {
return defValue;
}
}
public boolean isModified(final CodeStyleSettings settings, CommonCodeStyleSettings.IndentOptions options) {
boolean isModified;
isModified = isFieldModified(myTabSizeField, options.TAB_SIZE);
@@ -95,11 +111,13 @@ public class IndentOptionsEditor extends OptionGroup {
}
protected int getUIIndent() {
return getFieldValue(myIndentField, 0, 4);
assert myIndentField instanceof IntegerField;
return ((IntegerField)myIndentField).getValue();
}
protected int getUITabSize() {
return getFieldValue(myTabSizeField, 1, 4);
assert myTabSizeField instanceof IntegerField;
return ((IntegerField)myTabSizeField).getValue();
}
public void apply(final CodeStyleSettings settings, CommonCodeStyleSettings.IndentOptions options) {
@@ -109,10 +127,27 @@ public class IndentOptionsEditor extends OptionGroup {
}
public void reset(@NotNull CodeStyleSettings settings, @NotNull CommonCodeStyleSettings.IndentOptions options) {
myTabSizeField.setText(String.valueOf(options.TAB_SIZE));
((IntegerField)myTabSizeField).setValue(options.TAB_SIZE);
myCbUseTab.setSelected(options.USE_TAB_CHARACTER);
myIndentField.setText(String.valueOf(options.INDENT_SIZE));
((IntegerField)myIndentField).setValue(options.INDENT_SIZE);
}
/**
* @deprecated Create {@link IntegerField} and use {@link IntegerField#getValue()} instead.
*/
protected int getFieldValue(JTextField field, int minValue, int defValue) {
if (field instanceof IntegerField) {
return ((IntegerField)field).getValue();
}
else {
try {
return Math.max(Integer.parseInt(field.getText()), minValue);
}
catch (NumberFormatException e) {
return defValue;
}
}
}
public void setEnabled(boolean enabled) {
@@ -19,16 +19,22 @@ package com.intellij.application.options;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.ui.components.fields.IntegerField;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import static com.intellij.psi.codeStyle.CodeStyleConstraints.MAX_INDENT_SIZE;
import static com.intellij.psi.codeStyle.CodeStyleConstraints.MIN_INDENT_SIZE;
import static com.intellij.psi.codeStyle.CodeStyleDefaults.DEFAULT_CONTINUATION_INDENT_SIZE;
/**
* @author yole
*/
public class SmartIndentOptionsEditor extends IndentOptionsEditor {
public static final String CONTINUATION_INDENT_LABEL = ApplicationBundle.message("editbox.indent.continuation.indent");
private JCheckBox myCbSmartTabs;
private JTextField myContinuationIndentField;
private IntegerField myContinuationIndentField;
private JLabel myContinuationIndentLabel;
private JCheckBox myCbKeepIndentsOnEmptyLines;
@@ -44,8 +50,9 @@ public class SmartIndentOptionsEditor extends IndentOptionsEditor {
protected void addComponents() {
super.addComponents();
myContinuationIndentField = createIndentTextField();
myContinuationIndentLabel = new JLabel(ApplicationBundle.message("editbox.indent.continuation.indent"));
myContinuationIndentField =
createIndentTextField(CONTINUATION_INDENT_LABEL, MIN_INDENT_SIZE, MAX_INDENT_SIZE, DEFAULT_CONTINUATION_INDENT_SIZE);
myContinuationIndentLabel = new JLabel(CONTINUATION_INDENT_LABEL);
add(myContinuationIndentLabel, myContinuationIndentField);
myCbKeepIndentsOnEmptyLines = new JCheckBox(ApplicationBundle.message("checkbox.indent.keep.indents.on.empty.lines"));
@@ -64,7 +71,7 @@ public class SmartIndentOptionsEditor extends IndentOptionsEditor {
@Override
public void apply(final CodeStyleSettings settings, final CommonCodeStyleSettings.IndentOptions options) {
super.apply(settings, options);
options.CONTINUATION_INDENT_SIZE = getFieldValue(myContinuationIndentField, 0, options.CONTINUATION_INDENT_SIZE);
options.CONTINUATION_INDENT_SIZE = myContinuationIndentField.getValue();
options.SMART_TABS = isSmartTabValid(options.INDENT_SIZE, options.TAB_SIZE) && myCbSmartTabs.isSelected();
options.KEEP_INDENTS_ON_EMPTY_LINES = myCbKeepIndentsOnEmptyLines.isSelected();
}
@@ -72,7 +79,7 @@ public class SmartIndentOptionsEditor extends IndentOptionsEditor {
@Override
public void reset(@NotNull final CodeStyleSettings settings, @NotNull final CommonCodeStyleSettings.IndentOptions options) {
super.reset(settings, options);
myContinuationIndentField.setText(String.valueOf(options.CONTINUATION_INDENT_SIZE));
myContinuationIndentField.setValue(options.CONTINUATION_INDENT_SIZE);
myCbSmartTabs.setSelected(options.SMART_TABS);
myCbKeepIndentsOnEmptyLines.setSelected(options.KEEP_INDENTS_ON_EMPTY_LINES);
}
@@ -17,4 +17,8 @@ package com.intellij.psi.codeStyle;
public interface CodeStyleConstraints {
int MAX_RIGHT_MARGIN = 1000;
int MIN_INDENT_SIZE = 0;
int MAX_INDENT_SIZE = 16;
int MIN_TAB_SIZE = 1;
int MAX_TAB_SIZE = 16;
}
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.codeStyle;
public interface CodeStyleDefaults {
int DEFAULT_INDENT_SIZE = 4;
int DEFAULT_TAB_SIZE = 4;
int DEFAULT_CONTINUATION_INDENT_SIZE = 8;
}
@@ -38,6 +38,8 @@ import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import static com.intellij.psi.codeStyle.CodeStyleDefaults.*;
/**
* Common code style settings can be used by several programming languages. Each language may have its own
* instance of {@code CommonCodeStyleSettings}.
@@ -925,9 +927,9 @@ public class CommonCodeStyleSettings {
//-------------------------Indent options-------------------------------------------------
public static class IndentOptions implements Cloneable, JDOMExternalizable {
public int INDENT_SIZE = 4;
public int CONTINUATION_INDENT_SIZE = 8;
public int TAB_SIZE = 4;
public int INDENT_SIZE = DEFAULT_INDENT_SIZE;
public int CONTINUATION_INDENT_SIZE = DEFAULT_CONTINUATION_INDENT_SIZE;
public int TAB_SIZE = DEFAULT_TAB_SIZE;
public boolean USE_TAB_CHARACTER = false;
public boolean SMART_TABS = false;
public int LABEL_INDENT_SIZE = 0;
@@ -2,13 +2,10 @@ package org.jetbrains.yaml;
import com.intellij.application.options.IndentOptionsEditor;
import com.intellij.lang.Language;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
/**
* @author oleg
*/
@@ -42,18 +39,11 @@ public class YAMLLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
@Override
protected void addComponents() {
addTabOptions();
super.addComponents();
// Tabs in YAML are not allowed
myCbUseTab.setEnabled(false);
myTabSizeField = createIndentTextField();
myTabSizeLabel = new JLabel(ApplicationBundle.message("editbox.indent.tab.size"));
// Do not add
//add(myTabSizeLabel, myTabSizeField);
myIndentField = createIndentTextField();
myIndentLabel = new JLabel(ApplicationBundle.message("editbox.indent.indent"));
add(myIndentLabel, myIndentField);
myTabSizeField.setVisible(false);
myTabSizeLabel.setVisible(false);
}
public void setEnabled(boolean enabled) {