IDEA-215789 Update appearance of group headers (in Preferences)

GitOrigin-RevId: b832857516cc3dfeb7569f780b4a3cc237d8c770
This commit is contained in:
Pavel Porvatov
2021-12-09 09:34:29 +00:00
committed by intellij-monorepo-bot
parent fb13d93418
commit 9c668650ce
6 changed files with 482 additions and 743 deletions
@@ -203,7 +203,7 @@ internal class PanelBuilder(val rows: List<RowImpl>, val dialogPanelConfig: Dial
// todo visualPaddings
val gaps = cell.customGaps ?: Gaps(left = leftGap, right = rightGap)
val subGrid = builder.subGrid(width = width, horizontalAlign = cell.horizontalAlign, verticalAlign = cell.verticalAlign,
gaps = gaps)
resizableColumn = cell.resizableColumn, gaps = gaps)
val prevSpacingConfiguration = dialogPanelConfig.spacing
cell.spacingConfiguration?.let {
@@ -52,7 +52,7 @@ public class CopyrightFormattingConfigurable extends SearchableConfigurable.Pare
private TemplateCommentPanel getOrCreateMainPanel() {
if (myPanel == null) {
myPanel = new TemplateCommentPanel(null, null, null, myProject);
myPanel = new TemplateCommentPanel(null, null, myProject);
}
return myPanel;
}
@@ -17,17 +17,17 @@ public final class FileTypeCopyrightConfigurableFactory {
public static Configurable createFileTypeConfigurable(Project project, FileType fileType, TemplateCommentPanel parentPanel) {
if (fileType.equals(StdFileTypes.JAVA)) {
return new TemplateCommentPanel(fileType, parentPanel, new String[] {
CopyrightBundle.message("radio.location.in.file.before.package"),
CopyrightBundle.message("radio.location.in.file.before.imports"),
CopyrightBundle.message("radio.location.in.file.before.class")}, project);
return new TemplateCommentPanel(fileType, parentPanel, project,
CopyrightBundle.message("radio.location.in.file.before.package"),
CopyrightBundle.message("radio.location.in.file.before.imports"),
CopyrightBundle.message("radio.location.in.file.before.class"));
}
if (fileType.equals(XmlFileType.INSTANCE) ||
fileType.equals(HtmlFileType.INSTANCE) || fileType.equals(StdFileTypes.JSP) || fileType.equals(StdFileTypes.JSPX)) {
return new TemplateCommentPanel(fileType, parentPanel, new String[]{
CopyrightBundle.message("radio.location.in.file.before.doctype"),
CopyrightBundle.message("radio.location.in.file.before.root.tag")}, project);
return new TemplateCommentPanel(fileType, parentPanel, project,
CopyrightBundle.message("radio.location.in.file.before.doctype"),
CopyrightBundle.message("radio.location.in.file.before.root.tag"));
}
return new TemplateCommentPanel(fileType, parentPanel, null, project);
return new TemplateCommentPanel(fileType, parentPanel, project);
}
}
@@ -1,269 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.maddyhome.idea.copyright.ui.TemplateCommentPanel">
<grid id="715a5" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="36" y="27" width="918" height="735"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="c9bd3" binding="mainPanel" layout-manager="GridLayoutManager" row-count="7" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="fa04f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<scrollpane id="9e821" class="com.intellij.ui.components.JBScrollPane">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="85e7f" class="javax.swing.JTextArea" binding="preview">
<constraints/>
<properties>
<editable value="false"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
<component id="55c45" class="javax.swing.JRadioButton" binding="myUseDefaultSettingsRadioButton" default-binding="true">
<constraints>
<grid row="1" column="0" 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/CopyrightBundle" key="settings.copyright.formatting.use.default.settings"/>
</properties>
</component>
<component id="56d91" class="javax.swing.JRadioButton" binding="myUseCustomFormattingOptionsRadioButton" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.use.custom.formatting.options"/>
</properties>
</component>
<grid id="fe40a" binding="fileLocationPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="0" vgap="0">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="4" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="etched" title-resource-bundle="messages/CopyrightBundle" title-key="settings.copyright.formatting.location.in.file"/>
<children/>
</grid>
<component id="29b5" class="javax.swing.JRadioButton" binding="myNoCopyright">
<constraints>
<grid row="0" column="0" 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/CopyrightBundle" key="settings.copyright.formatting.no.copyright"/>
</properties>
</component>
<grid id="dc4b5" binding="myCommentTypePanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="etched" title-resource-bundle="messages/CopyrightBundle" title-key="settings.copyright.formatting.comment.type"/>
<children>
<component id="c8599" class="javax.swing.JRadioButton" binding="rbBlockComment">
<constraints>
<grid row="0" column="0" 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/CopyrightBundle" key="settings.copyright.formatting.use.block.comment"/>
</properties>
</component>
<component id="2b1b2" class="javax.swing.JRadioButton" binding="rbLineComment">
<constraints>
<grid row="2" column="0" 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/CopyrightBundle" key="settings.copyright.formatting.use.line.comment"/>
</properties>
</component>
<component id="52d04" class="javax.swing.JCheckBox" binding="cbPrefixLines">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="2" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.prefix.each.line"/>
</properties>
</component>
</children>
</grid>
<grid id="3c54b" binding="myBorderPanel" layout-manager="GridLayoutManager" row-count="6" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="etched" title-resource-bundle="messages/CopyrightBundle" title-key="settings.copyright.formatting.borders"/>
<children>
<component id="3c05" class="javax.swing.JCheckBox" binding="cbSeparatorBefore">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.separator.before"/>
</properties>
</component>
<component id="20e5e" class="javax.swing.JCheckBox" binding="cbSeparatorAfter">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.separator.after"/>
</properties>
</component>
<component id="74372" class="javax.swing.JLabel" binding="lblLengthBefore">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="fcbea"/>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.length"/>
</properties>
</component>
<component id="7a6fe" class="javax.swing.JLabel" binding="lblLengthAfter">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="ee688"/>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.length"/>
</properties>
</component>
<component id="fcbea" class="javax.swing.JTextField" binding="txtLengthBefore">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ee688" class="javax.swing.JTextField" binding="txtLengthAfter">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="a3078" class="javax.swing.JCheckBox" binding="cbAddBlank">
<constraints>
<grid row="5" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.add.blank.line.after"/>
</properties>
</component>
<component id="81563" class="javax.swing.JLabel" binding="mySeparatorCharLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="679a0"/>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.separator"/>
</properties>
</component>
<component id="30865" class="javax.swing.JCheckBox" binding="cbBox">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.box"/>
</properties>
</component>
<component id="679a0" class="javax.swing.JTextField" binding="txtFiller">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="73fc4" class="javax.swing.JCheckBox" binding="cbAddBlankBefore">
<constraints>
<grid row="4" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CopyrightBundle" key="settings.copyright.formatting.add.blank.line.before"/>
</properties>
</component>
</children>
</grid>
<grid id="ba5e7" binding="myRelativeLocationPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="0" vgap="0">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="etched" title-resource-bundle="messages/CopyrightBundle" title-key="settings.copyright.formatting.relative.location"/>
<children>
<component id="83e39" class="javax.swing.JRadioButton" binding="rbBefore">
<constraints>
<grid row="0" column="0" 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/CopyrightBundle" key="settings.copyright.formatting.before.other.comments"/>
</properties>
</component>
<component id="c0972" class="javax.swing.JRadioButton" binding="rbAfter">
<constraints>
<grid row="1" column="0" 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/CopyrightBundle" key="settings.copyright.formatting.after.other.comments"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</children>
</grid>
<buttonGroups>
<group name="buttonGroup1">
<member id="55c45"/>
<member id="56d91"/>
<member id="29b5"/>
</group>
<group name="buttonGroup2">
<member id="c8599"/>
<member id="2b1b2"/>
</group>
<group name="buttonGroup3">
<member id="83e39"/>
<member id="c0972"/>
</group>
</buttonGroups>
</form>
@@ -1,464 +0,0 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.maddyhome.idea.copyright.ui;
import com.intellij.copyright.CopyrightManager;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.ui.DocumentAdapter;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.util.ui.UIUtil;
import com.maddyhome.idea.copyright.CopyrightProfileKt;
import com.maddyhome.idea.copyright.CopyrightUpdaters;
import com.maddyhome.idea.copyright.options.LanguageOptions;
import com.maddyhome.idea.copyright.options.Options;
import com.maddyhome.idea.copyright.pattern.EntityUtil;
import com.maddyhome.idea.copyright.pattern.VelocityHelper;
import com.maddyhome.idea.copyright.psi.UpdateCopyrightsProvider;
import com.maddyhome.idea.copyright.util.FileTypeUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.EventListenerList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TemplateCommentPanel implements SearchableConfigurable {
private final @Nullable FileType fileType;
private final TemplateCommentPanel parentPanel;
private final Project myProject;
private JRadioButton[] fileLocations = null;
private JTextArea preview;
private JPanel mainPanel;
private JRadioButton rbBefore;
private JRadioButton rbAfter;
private JPanel fileLocationPanel;
private JRadioButton myUseDefaultSettingsRadioButton;
private JRadioButton myUseCustomFormattingOptionsRadioButton;
private JRadioButton myNoCopyright;
private JRadioButton rbLineComment;
private JCheckBox cbPrefixLines;
private JRadioButton rbBlockComment;
private JPanel myCommentTypePanel;
private JCheckBox cbSeparatorBefore;
private JTextField txtLengthBefore;
private JTextField txtLengthAfter;
private JCheckBox cbAddBlank;
private JCheckBox cbAddBlankBefore;
private JCheckBox cbSeparatorAfter;
private JCheckBox cbBox;
private JTextField txtFiller;
private JPanel myBorderPanel;
private JLabel lblLengthBefore;
private JLabel lblLengthAfter;
private JLabel mySeparatorCharLabel;
private JPanel myRelativeLocationPanel;
private void updateBox() {
boolean enable = true;
if (!cbSeparatorBefore.isSelected() || !cbSeparatorAfter.isSelected()) {
enable = false;
}
else {
if (!txtLengthBefore.getText().equals(txtLengthAfter.getText())) {
enable = false;
}
}
boolean either = cbSeparatorBefore.isSelected() || cbSeparatorAfter.isSelected();
cbBox.setEnabled(enable);
txtFiller.setEnabled(either);
}
private final EventListenerList listeners = new EventListenerList();
private final boolean allowBlock;
public TemplateCommentPanel(FileType fileType, TemplateCommentPanel parentPanel, String @NlsContexts.RadioButton [] locations, Project project) {
this.parentPanel = parentPanel;
myProject = project;
if (fileType == null) {
myUseDefaultSettingsRadioButton.setVisible(false);
myUseCustomFormattingOptionsRadioButton.setVisible(false);
myNoCopyright.setVisible(false);
}
this.fileType = fileType != null ? fileType : StdFileTypes.JAVA;
allowBlock = FileTypeUtil.hasBlockComment(this.fileType);
if (parentPanel != null) {
parentPanel.addOptionChangeListener(new TemplateOptionsPanelListener() {
@Override
public void optionChanged() {
updateOverride();
}
});
}
ButtonGroup group = new ButtonGroup();
group.add(rbBefore);
group.add(rbAfter);
if (locations == null) {
fileLocationPanel.setBorder(BorderFactory.createEmptyBorder());
}
else {
fileLocations = new JRadioButton[locations.length];
group = new ButtonGroup();
for (int i = 0; i < fileLocations.length; i++) {
fileLocations[i] = new JRadioButton(locations[i]);
group.add(fileLocations[i]);
fileLocationPanel.add(fileLocations[i], new GridConstraints(i, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
GridConstraints.SIZEPOLICY_CAN_SHRINK |
GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
null, null, null));
}
}
addOptionChangeListener(new TemplateOptionsPanelListener() {
@Override
public void optionChanged() {
showPreview(getOptions());
}
});
preview.setFont(EditorFontType.getGlobalPlainFont());
myUseDefaultSettingsRadioButton.setSelected(true);
final ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateOverride();
}
};
myUseDefaultSettingsRadioButton.addActionListener(listener);
myUseCustomFormattingOptionsRadioButton.addActionListener(listener);
myNoCopyright.addActionListener(listener);
txtLengthBefore.setText("80");
txtLengthAfter.setText("80");
rbBlockComment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
cbPrefixLines.setEnabled(rbBlockComment.isSelected());
fireChangeEvent();
}
});
rbLineComment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
cbPrefixLines.setEnabled(rbBlockComment.isSelected());
fireChangeEvent();
}
});
cbPrefixLines.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
fireChangeEvent();
}
});
cbSeparatorBefore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
lblLengthBefore.setEnabled(cbSeparatorBefore.isSelected());
txtLengthBefore.setEnabled(cbSeparatorBefore.isSelected());
fireChangeEvent();
updateBox();
}
});
cbSeparatorAfter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
lblLengthAfter.setEnabled(cbSeparatorAfter.isSelected());
txtLengthAfter.setEnabled(cbSeparatorAfter.isSelected());
fireChangeEvent();
updateBox();
}
});
final DocumentAdapter documentAdapter = new DocumentAdapter() {
@Override
protected void textChanged(@NotNull DocumentEvent e) {
fireChangeEvent();
updateBox();
}
};
txtLengthBefore.getDocument().addDocumentListener(documentAdapter);
txtLengthAfter.getDocument().addDocumentListener(documentAdapter);
txtFiller.getDocument().addDocumentListener(documentAdapter);
cbBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
fireChangeEvent();
}
});
}
public LanguageOptions getOptions() {
// If this is a fully custom comment we should really ensure there are no blank lines in the comments outside
// of a block comment. If there are any blank lines the replacement logic will fall apart.
final LanguageOptions res = new LanguageOptions();
res.setBlock(rbBlockComment.isSelected());
res.setPrefixLines(!allowBlock || cbPrefixLines.isSelected());
res.setSeparateAfter(cbSeparatorAfter.isSelected());
res.setSeparateBefore(cbSeparatorBefore.isSelected());
try {
res.setLenBefore(Integer.parseInt(txtLengthBefore.getText()));
res.setLenAfter(Integer.parseInt(txtLengthAfter.getText()));
}
catch (NumberFormatException e) {
//leave blank
}
res.setBox(cbBox.isSelected());
String filler = txtFiller.getText();
if (filler.length() > 0) {
res.setFiller(filler);
}
else {
res.setFiller(LanguageOptions.DEFAULT_FILLER);
}
res.setFileTypeOverride(getOverrideChoice());
res.setRelativeBefore(rbBefore.isSelected());
res.setAddBlankAfter(cbAddBlank.isSelected());
res.setAddBlankBefore(cbAddBlankBefore.isSelected());
if (fileLocations != null) {
for (int i = 0; i < fileLocations.length; i++) {
if (fileLocations[i].isSelected()) {
res.setFileLocation(i + 1);
}
}
}
return res;
}
private int getOverrideChoice() {
return myUseDefaultSettingsRadioButton.isSelected()
? LanguageOptions.USE_TEMPLATE
: myNoCopyright.isSelected() ? LanguageOptions.NO_COPYRIGHT : LanguageOptions.USE_TEXT;
}
private void updateOverride() {
int choice = getOverrideChoice();
LanguageOptions parentOpts = parentPanel != null ? parentPanel.getOptions() : null;
switch (choice) {
case LanguageOptions.NO_COPYRIGHT:
enableFormattingOptions(false);
showPreview(getOptions());
myRelativeLocationPanel.setEnabled(false);
rbBefore.setEnabled(false);
rbAfter.setEnabled(false);
cbAddBlank.setEnabled(false);
cbAddBlankBefore.setEnabled(false);
if (fileLocations != null) {
for (JRadioButton fileLocation : fileLocations) {
fileLocation.setEnabled(false);
}
}
break;
case LanguageOptions.USE_TEMPLATE:
final boolean isTemplate = parentPanel == null;
enableFormattingOptions(isTemplate);
showPreview(parentOpts != null ? parentOpts : getOptions());
myRelativeLocationPanel.setEnabled(isTemplate);
rbBefore.setEnabled(isTemplate);
rbAfter.setEnabled(isTemplate);
cbAddBlank.setEnabled(isTemplate);
cbAddBlankBefore.setEnabled(isTemplate);
if (fileLocations != null) {
for (JRadioButton fileLocation : fileLocations) {
fileLocation.setEnabled(true);
}
}
break;
case LanguageOptions.USE_TEXT:
enableFormattingOptions(true);
showPreview(getOptions());
myRelativeLocationPanel.setEnabled(true);
rbBefore.setEnabled(true);
rbAfter.setEnabled(true);
cbAddBlank.setEnabled(true);
cbAddBlankBefore.setEnabled(true);
if (fileLocations != null) {
for (JRadioButton fileLocation : fileLocations) {
fileLocation.setEnabled(true);
}
}
break;
}
}
private void enableFormattingOptions(boolean enable) {
if (enable) {
myCommentTypePanel.setEnabled(true);
myBorderPanel.setEnabled(true);
rbBlockComment.setEnabled(true);
rbLineComment.setEnabled(true);
cbPrefixLines.setEnabled(allowBlock);
cbSeparatorBefore.setEnabled(true);
cbSeparatorAfter.setEnabled(true);
lblLengthBefore.setEnabled(cbSeparatorBefore.isSelected());
txtLengthBefore.setEnabled(cbSeparatorBefore.isSelected());
lblLengthAfter.setEnabled(cbSeparatorAfter.isSelected());
txtLengthAfter.setEnabled(cbSeparatorAfter.isSelected());
mySeparatorCharLabel.setEnabled(true);
updateBox();
} else {
UIUtil.setEnabled(myCommentTypePanel, false, true);
UIUtil.setEnabled(myBorderPanel, false, true);
}
}
private void showPreview(LanguageOptions options) {
final String defaultCopyrightText = myNoCopyright.isSelected() ? "" : FileTypeUtil
.buildComment(fileType, VelocityHelper.evaluate(null, null, null, EntityUtil.decode(CopyrightProfileKt.DEFAULT_COPYRIGHT_NOTICE)), options);
SwingUtilities.invokeLater(() -> preview.setText(defaultCopyrightText));
}
public FileType getFileType() {
return fileType;
}
@Override
@Nls
public String getDisplayName() {
return fileType instanceof LanguageFileType
? ((LanguageFileType)fileType).getLanguage().getDisplayName()
: fileType.getDisplayName();
}
@Override
@NotNull
public String getHelpTopic() {
return "copyright.filetypes";
}
@Override
public JComponent createComponent() {
return mainPanel;
}
@Override
public boolean isModified() {
if (parentPanel == null) return !getCopyrightOptions().getTemplateOptions().equals(getOptions());
return !getCopyrightOptions().getOptions(fileType.getName()).equals(getOptions());
}
@NotNull
private Options getCopyrightOptions() {
return CopyrightManager.getInstance(myProject).getOptions();
}
@Override
public void apply() throws ConfigurationException {
final Options options = getCopyrightOptions();
if (parentPanel == null) {
options.setTemplateOptions(getOptions());
}
else {
options.setOptions(fileType.getName(), getOptions());
}
}
@Override
public void reset() {
final LanguageOptions options =
parentPanel == null ? getCopyrightOptions().getTemplateOptions() : getCopyrightOptions().getOptions(fileType.getName());
boolean isBlock = options.isBlock();
if (isBlock) {
rbBlockComment.setSelected(true);
}
else {
rbLineComment.setSelected(true);
}
cbPrefixLines.setSelected(!allowBlock || options.isPrefixLines());
cbSeparatorAfter.setSelected(options.isSeparateAfter());
cbSeparatorBefore.setSelected(options.isSeparateBefore());
txtLengthBefore.setText(String.valueOf(options.getLenBefore()));
txtLengthAfter.setText(String.valueOf(options.getLenAfter()));
txtFiller.setText(options.getFiller() == LanguageOptions.DEFAULT_FILLER ? "" : options.getFiller());
cbBox.setSelected(options.isBox());
final int fileTypeOverride = options.getFileTypeOverride();
myUseDefaultSettingsRadioButton.setSelected(fileTypeOverride == LanguageOptions.USE_TEMPLATE);
myUseCustomFormattingOptionsRadioButton.setSelected(fileTypeOverride == LanguageOptions.USE_TEXT);
myNoCopyright.setSelected(fileTypeOverride == LanguageOptions.NO_COPYRIGHT);
if (options.isRelativeBefore()) {
rbBefore.setSelected(true);
}
else {
rbAfter.setSelected(true);
}
cbAddBlank.setSelected(options.isAddBlankAfter());
cbAddBlankBefore.setSelected(options.isAddBlankBefore());
if (fileLocations != null) {
int choice = options.getFileLocation() - 1;
choice = Math.max(0, Math.min(choice, fileLocations.length - 1));
fileLocations[choice].setSelected(true);
}
updateOverride();
}
public void addOptionChangeListener(TemplateOptionsPanelListener listener) {
listeners.add(TemplateOptionsPanelListener.class, listener);
}
private void fireChangeEvent() {
Object[] fires = listeners.getListenerList();
for (int i = fires.length - 2; i >= 0; i -= 2) {
if (fires[i] == TemplateOptionsPanelListener.class) {
((TemplateOptionsPanelListener)fires[i + 1]).optionChanged();
}
}
}
@Override
@NotNull
public String getId() {
return fileType != null ? getHelpTopic() + "." + fileType.getName() : getHelpTopic();
}
@NotNull
@Override
public Class<?> getOriginalClass() {
final UpdateCopyrightsProvider provider = fileType != null ? CopyrightUpdaters.INSTANCE.forFileType(fileType) : null;
return provider != null ? provider.getClass() : super.getClass();
}
}
@@ -0,0 +1,472 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.maddyhome.idea.copyright.ui
import com.intellij.copyright.CopyrightBundle
import com.intellij.copyright.CopyrightManager.Companion.getInstance
import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.fileTypes.StdFileTypes
import com.intellij.openapi.options.SearchableConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsContexts
import com.intellij.ui.DocumentAdapter
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBRadioButton
import com.intellij.ui.components.JBTextField
import com.intellij.ui.dsl.builder.RightGap
import com.intellij.ui.dsl.builder.Row
import com.intellij.ui.dsl.builder.RowLayout
import com.intellij.ui.dsl.builder.columns
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.gridLayout.HorizontalAlign
import com.intellij.ui.dsl.gridLayout.VerticalAlign
import com.intellij.ui.layout.*
import com.maddyhome.idea.copyright.CopyrightUpdaters
import com.maddyhome.idea.copyright.DEFAULT_COPYRIGHT_NOTICE
import com.maddyhome.idea.copyright.options.LanguageOptions
import com.maddyhome.idea.copyright.options.Options
import com.maddyhome.idea.copyright.pattern.EntityUtil
import com.maddyhome.idea.copyright.pattern.VelocityHelper
import com.maddyhome.idea.copyright.util.FileTypeUtil
import java.awt.event.ActionListener
import javax.swing.JComponent
import javax.swing.JRadioButton
import javax.swing.JTextArea
import javax.swing.SwingUtilities
import javax.swing.event.DocumentEvent
import javax.swing.event.EventListenerList
import kotlin.math.max
import kotlin.math.min
class TemplateCommentPanel(_fileType: FileType?,
private val parentPanel: TemplateCommentPanel?,
private val project: Project,
vararg locations: @NlsContexts.RadioButton String) : SearchableConfigurable {
val fileType: FileType
private val allowBlock: Boolean
private val listeners = EventListenerList()
private val fileLocations = mutableListOf<JRadioButton>()
private lateinit var noCopyright: JBRadioButton
private lateinit var useDefaultSettingsRadioButton: JBRadioButton
private lateinit var useCustomFormattingOptionsRadioButton: JBRadioButton
private lateinit var commentTypeGroup: Row
private lateinit var blockComment: JBRadioButton
private lateinit var prefixLines: JBCheckBox
private lateinit var lineComment: JBRadioButton
private lateinit var relativeLocationGroup: Row
private lateinit var before: JBRadioButton
private lateinit var after: JBRadioButton
private lateinit var borderGroup: Row
private lateinit var separatorBefore: JBCheckBox
private lateinit var lengthBefore: JBTextField
private lateinit var separatorAfter: JBCheckBox
private lateinit var lengthAfter: JBTextField
private lateinit var filler: JBTextField
private lateinit var box: JBCheckBox
private lateinit var addBlankBefore: JBCheckBox
private lateinit var addBlank: JBCheckBox
private lateinit var preview: JTextArea
val panel = panel {
val updateOverrideListener = ActionListener { updateOverride() }
val changeEventListener = ActionListener { fireChangeEvent() }
val updateBoxListener = ActionListener {
fireChangeEvent()
updateBox()
}
val documentAdapter = object : DocumentAdapter() {
override fun textChanged(e: DocumentEvent) {
fireChangeEvent()
updateBox()
}
}
buttonsGroup {
row {
noCopyright = radioButton(CopyrightBundle.message("settings.copyright.formatting.no.copyright"))
.applyToComponent { addActionListener(updateOverrideListener) }
.component
}
row {
useDefaultSettingsRadioButton = radioButton(CopyrightBundle.message("settings.copyright.formatting.use.default.settings"))
.applyToComponent { addActionListener(updateOverrideListener) }
.component
}
row {
useCustomFormattingOptionsRadioButton = radioButton(
CopyrightBundle.message("settings.copyright.formatting.use.custom.formatting.options"))
.applyToComponent { addActionListener(updateOverrideListener) }
.component
}
}
row {
panel {
commentTypeGroup = group(CopyrightBundle.message("settings.copyright.formatting.comment.type")) {
buttonsGroup {
row {
blockComment = radioButton(CopyrightBundle.message("settings.copyright.formatting.use.block.comment"))
.applyToComponent { addActionListener(changeEventListener) }
.component
}
indent {
row {
prefixLines = checkBox(CopyrightBundle.message("settings.copyright.formatting.prefix.each.line"))
.applyToComponent { addActionListener(changeEventListener) }
.enabledIf(blockComment.selected)
.component
}
}
row {
lineComment = radioButton(CopyrightBundle.message("settings.copyright.formatting.use.line.comment"))
.applyToComponent { addActionListener(changeEventListener) }
.component
}
}
}
relativeLocationGroup = group(CopyrightBundle.message("settings.copyright.formatting.relative.location")) {
buttonsGroup {
row {
before = radioButton(CopyrightBundle.message("settings.copyright.formatting.before.other.comments"))
.component
}
row {
after = radioButton(CopyrightBundle.message("settings.copyright.formatting.after.other.comments"))
.component
}
}
}
}.resizableColumn()
.verticalAlign(VerticalAlign.TOP)
.gap(RightGap.COLUMNS)
panel {
borderGroup = group(CopyrightBundle.message("settings.copyright.formatting.borders")) {
row {
separatorBefore = checkBox(CopyrightBundle.message("settings.copyright.formatting.separator.before"))
.applyToComponent { addActionListener(updateBoxListener) }
.component
lengthBefore = intTextField()
.label(CopyrightBundle.message("settings.copyright.formatting.length"))
.applyToComponent { document.addDocumentListener(documentAdapter) }
.enabledIf(separatorBefore.selected)
.component
}.layout(RowLayout.PARENT_GRID)
row {
separatorAfter = checkBox(CopyrightBundle.message("settings.copyright.formatting.separator.after"))
.applyToComponent { addActionListener(updateBoxListener) }
.component
lengthAfter = intTextField()
.label(CopyrightBundle.message("settings.copyright.formatting.length"))
.applyToComponent { document.addDocumentListener(documentAdapter) }
.enabledIf(separatorAfter.selected)
.component
}.layout(RowLayout.PARENT_GRID)
row {
filler = textField()
.label(CopyrightBundle.message("settings.copyright.formatting.separator"))
.applyToComponent { document.addDocumentListener(documentAdapter) }
.columns(4)
.component
}
row {
box = checkBox(CopyrightBundle.message("settings.copyright.formatting.box"))
.applyToComponent { addActionListener(changeEventListener) }
.component
}
row {
addBlankBefore = checkBox(CopyrightBundle.message("settings.copyright.formatting.add.blank.line.before"))
.component
}
row {
addBlank = checkBox(CopyrightBundle.message("settings.copyright.formatting.add.blank.line.after"))
.component
}
}
}.resizableColumn()
.verticalAlign(VerticalAlign.TOP)
}
if (locations.isNotEmpty()) {
group(CopyrightBundle.message("settings.copyright.formatting.location.in.file")) {
buttonsGroup {
for (location in locations) {
row {
fileLocations += radioButton(location).component
}
}
}
}
}
row {
preview = textArea()
.horizontalAlign(HorizontalAlign.FILL)
.verticalAlign(VerticalAlign.FILL)
.applyToComponent {
isEditable = false
font = EditorFontType.getGlobalPlainFont()
}
.component
}.resizableRow()
}
init {
if (_fileType == null) {
useDefaultSettingsRadioButton.isVisible = false
useCustomFormattingOptionsRadioButton.isVisible = false
noCopyright.isVisible = false
}
fileType = _fileType ?: StdFileTypes.JAVA
allowBlock = FileTypeUtil.hasBlockComment(fileType)
parentPanel?.addOptionChangeListener(TemplateOptionsPanelListener { updateOverride() })
addOptionChangeListener { showPreview(getOptions()) }
}
override fun getDisplayName(): String {
return if (fileType is LanguageFileType) fileType.language.displayName else fileType.displayName
}
override fun getHelpTopic(): String {
return "copyright.filetypes"
}
override fun getOriginalClass(): Class<*> {
val provider = CopyrightUpdaters.INSTANCE.forFileType(fileType)
return provider?.javaClass ?: super.getOriginalClass()
}
override fun getId(): String {
return helpTopic + "." + fileType.name
}
override fun createComponent(): JComponent {
return panel
}
override fun isModified(): Boolean {
if (parentPanel == null) {
return getCopyrightOptions().templateOptions != getOptions()
}
return getCopyrightOptions().getOptions(fileType.name) != getOptions()
}
override fun apply() {
val options = getCopyrightOptions()
if (parentPanel == null) {
options.templateOptions = getOptions()
}
else {
options.setOptions(fileType.name, getOptions())
}
}
override fun reset() {
val options = if (parentPanel == null) getCopyrightOptions().templateOptions
else getCopyrightOptions().getOptions(fileType.name)
val isBlock = options.isBlock
if (isBlock) {
blockComment.isSelected = true
}
else {
lineComment.isSelected = true
}
prefixLines.isSelected = !allowBlock || options.isPrefixLines
separatorAfter.isSelected = options.isSeparateAfter
separatorBefore.isSelected = options.isSeparateBefore
lengthBefore.text = options.getLenBefore().toString()
lengthAfter.text = options.getLenAfter().toString()
filler.text = if (options.getFiller() === LanguageOptions.DEFAULT_FILLER) "" else options.getFiller()
box.isSelected = options.isBox
val fileTypeOverride = options.getFileTypeOverride()
useDefaultSettingsRadioButton.isSelected = fileTypeOverride == LanguageOptions.USE_TEMPLATE
useCustomFormattingOptionsRadioButton.isSelected = fileTypeOverride == LanguageOptions.USE_TEXT
noCopyright.isSelected = fileTypeOverride == LanguageOptions.NO_COPYRIGHT
if (options.isRelativeBefore) {
before.isSelected = true
}
else {
after.isSelected = true
}
addBlank.isSelected = options.isAddBlankAfter
addBlankBefore.isSelected = options.isAddBlankBefore
if (fileLocations.isNotEmpty()) {
var choice = options.getFileLocation() - 1
choice = max(0, min(choice, fileLocations.size - 1))
fileLocations[choice].isSelected = true
}
updateOverride()
}
private fun getOptions(): LanguageOptions {
// If this is a fully custom comment we should really ensure there are no blank lines in the comments outside
// of a block comment. If there are any blank lines the replacement logic will fall apart.
val result = LanguageOptions()
result.isBlock = blockComment.isSelected
result.isPrefixLines = !allowBlock || prefixLines.isSelected
result.isSeparateAfter = separatorAfter.isSelected
result.isSeparateBefore = separatorBefore.isSelected
try {
result.setLenBefore(lengthBefore.text.toInt())
result.setLenAfter(lengthAfter.text.toInt())
}
catch (e: NumberFormatException) {
//leave blank
}
result.isBox = box.isSelected
val filler = filler.text
if (filler.isNotEmpty()) {
result.setFiller(filler)
}
else {
result.setFiller(LanguageOptions.DEFAULT_FILLER)
}
result.setFileTypeOverride(getOverrideChoice())
result.isRelativeBefore = before.isSelected
result.isAddBlankAfter = addBlank.isSelected
result.isAddBlankBefore = addBlankBefore.isSelected
if (fileLocations.isNotEmpty()) {
for (i in fileLocations.indices) {
if (fileLocations[i].isSelected) {
result.setFileLocation(i + 1)
}
}
}
return result
}
private fun getOverrideChoice(): Int {
return if (useDefaultSettingsRadioButton.isSelected) LanguageOptions.USE_TEMPLATE
else
if (noCopyright.isSelected) LanguageOptions.NO_COPYRIGHT else LanguageOptions.USE_TEXT
}
private fun updateOverride() {
val choice = getOverrideChoice()
val parentOpts = parentPanel?.getOptions()
when (choice) {
LanguageOptions.NO_COPYRIGHT -> {
enableFormattingOptions(false)
showPreview(getOptions())
relativeLocationGroup.enabled(false)
before.isEnabled = false
after.isEnabled = false
addBlank.isEnabled = false
addBlankBefore.isEnabled = false
if (fileLocations.isNotEmpty()) {
for (fileLocation in fileLocations) {
fileLocation.isEnabled = false
}
}
}
LanguageOptions.USE_TEMPLATE -> {
val isTemplate = parentPanel == null
enableFormattingOptions(isTemplate)
showPreview(parentOpts ?: getOptions())
relativeLocationGroup.enabled(isTemplate)
before.isEnabled = isTemplate
after.isEnabled = isTemplate
addBlank.isEnabled = isTemplate
addBlankBefore.isEnabled = isTemplate
if (fileLocations.isNotEmpty()) {
for (fileLocation in fileLocations) {
fileLocation.isEnabled = true
}
}
}
LanguageOptions.USE_TEXT -> {
enableFormattingOptions(true)
showPreview(getOptions())
relativeLocationGroup.enabled(true)
before.isEnabled = true
after.isEnabled = true
addBlank.isEnabled = true
addBlankBefore.isEnabled = true
if (fileLocations.isNotEmpty()) {
for (fileLocation in fileLocations) {
fileLocation.isEnabled = true
}
}
}
}
}
private fun enableFormattingOptions(enable: Boolean) {
if (enable) {
commentTypeGroup.enabled(true)
borderGroup.enabled(true)
blockComment.isEnabled = true
lineComment.isEnabled = true
prefixLines.isEnabled = allowBlock
separatorBefore.isEnabled = true
separatorAfter.isEnabled = true
lengthBefore.isEnabled = separatorBefore.isSelected
lengthBefore.isEnabled = separatorBefore.isSelected
lengthAfter.isEnabled = separatorAfter.isSelected
lengthAfter.isEnabled = separatorAfter.isSelected
updateBox()
}
else {
commentTypeGroup.enabled(false)
borderGroup.enabled(false)
}
}
private fun updateBox() {
var enable = true
if (!separatorBefore.isSelected || !separatorAfter.isSelected) {
enable = false
}
else {
if (lengthBefore.text != lengthAfter.text) {
enable = false
}
}
val either = separatorBefore.isSelected || separatorAfter.isSelected
box.isEnabled = enable
filler.isEnabled = either
}
private fun showPreview(options: LanguageOptions) {
val defaultCopyrightText = if (noCopyright.isSelected) ""
else FileTypeUtil
.buildComment(fileType, VelocityHelper.evaluate(null, null, null, EntityUtil.decode(DEFAULT_COPYRIGHT_NOTICE)), options)
SwingUtilities.invokeLater { preview.text = defaultCopyrightText }
}
private fun addOptionChangeListener(listener: TemplateOptionsPanelListener) {
listeners.add(TemplateOptionsPanelListener::class.java, listener)
}
private fun fireChangeEvent() {
val fires = listeners.listenerList
var i = fires.size - 2
while (i >= 0) {
if (fires[i] === TemplateOptionsPanelListener::class.java) {
(fires[i + 1] as TemplateOptionsPanelListener).optionChanged()
}
i -= 2
}
}
private fun getCopyrightOptions(): Options {
return getInstance(project).options
}
}