diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleBlankLinesPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleBlankLinesPanel.java index 60094e2c33a3..b4b10abf0adb 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleBlankLinesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleBlankLinesPanel.java @@ -19,20 +19,19 @@ import com.intellij.lang.Language; import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Trinity; -import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.codeStyle.CustomCodeStyleSettings; import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider; import com.intellij.ui.OptionGroup; import com.intellij.ui.ScrollPaneFactory; -import com.intellij.ui.components.JBScrollPane; +import com.intellij.ui.components.JBLabel; import com.intellij.util.containers.MultiMap; +import com.intellij.util.ui.UIUtil; import gnu.trove.THashMap; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import javax.swing.border.Border; import java.awt.*; import java.lang.reflect.Field; import java.util.*; @@ -63,37 +62,41 @@ public class CodeStyleBlankLinesPanel extends MultilanguageCodeStyleAbstractPane super.init(); JPanel optionsPanel = new JPanel(new GridBagLayout()); - JPanel keepBlankLinesPanel = createKeepBlankLinesPanel(); - if (keepBlankLinesPanel != null) { - optionsPanel.add(keepBlankLinesPanel, + + OptionGroup keepBlankLinesOptionsGroup = createKeepBlankLinesOptionsGroup(); + OptionGroup blankLinesOptionsGroup = createBlankLinesOptionsGroup(); + if (keepBlankLinesOptionsGroup != null) { + keepBlankLinesOptionsGroup.setAnchor(keepBlankLinesOptionsGroup.findAnchor()); + optionsPanel.add(keepBlankLinesOptionsGroup.createPanel(), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } - JPanel blankLinesPanel = createBlankLinesPanel(); - if (blankLinesPanel != null) { - optionsPanel.add(blankLinesPanel, + if (blankLinesOptionsGroup != null) { + blankLinesOptionsGroup.setAnchor(blankLinesOptionsGroup.findAnchor()); + optionsPanel.add(blankLinesOptionsGroup.createPanel(), new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } + UIUtil.mergeComponentsWithAnchor(keepBlankLinesOptionsGroup, blankLinesOptionsGroup); + optionsPanel.add(new JPanel(), new GridBagConstraints(0, 2, 1, 1, 0, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); JScrollPane scroll = ScrollPaneFactory.createScrollPane(optionsPanel); - scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); - scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); scroll.setBorder(null); - scroll.setMinimumSize(new Dimension(optionsPanel.getPreferredSize().width + scroll.getVerticalScrollBar().getPreferredSize().width, + scroll.setMinimumSize(new Dimension(optionsPanel.getPreferredSize().width + scroll.getVerticalScrollBar().getPreferredSize().width + 5, optionsPanel.getPreferredSize().height)); + scroll.setPreferredSize(scroll.getMinimumSize()); myPanel .add(scroll, - new GridBagConstraints(0, 0, 1, 1, 0, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 4, 0, 4), 0, 0)); + new GridBagConstraints(0, 0, 1, 1, 0, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); final JPanel previewPanel = createPreviewPanel(); myPanel .add(previewPanel, - new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 4), 0, 0)); + new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); installPreviewPanel(previewPanel); addPanelToWatch(myPanel); @@ -107,7 +110,7 @@ public class CodeStyleBlankLinesPanel extends MultilanguageCodeStyleAbstractPane } @Nullable - private JPanel createBlankLinesPanel() { + private OptionGroup createBlankLinesOptionsGroup() { OptionGroup optionGroup = new OptionGroup(BLANK_LINES); createOption(optionGroup, ApplicationBundle.message("editbox.blanklines.before.package.statement"), "BLANK_LINES_BEFORE_PACKAGE"); @@ -127,11 +130,11 @@ public class CodeStyleBlankLinesPanel extends MultilanguageCodeStyleAbstractPane if (optionGroup.getComponents().length == 0) return null; - return optionGroup.createPanel(); + return optionGroup; } @Nullable - private JPanel createKeepBlankLinesPanel() { + private OptionGroup createKeepBlankLinesOptionsGroup() { OptionGroup optionGroup = new OptionGroup(BLANK_LINES_KEEP); createOption(optionGroup, ApplicationBundle.message("editbox.keep.blanklines.in.declarations"), "KEEP_BLANK_LINES_IN_DECLARATIONS"); @@ -141,7 +144,7 @@ public class CodeStyleBlankLinesPanel extends MultilanguageCodeStyleAbstractPane if (optionGroup.getComponents().length == 0) return null; - return optionGroup.createPanel(); + return optionGroup; } private void initCustomOptions(OptionGroup optionGroup, String groupName) { @@ -160,7 +163,7 @@ public class CodeStyleBlankLinesPanel extends MultilanguageCodeStyleAbstractPane String renamed = myRenamedFields.get(fieldName); if (renamed != null) title = renamed; - JLabel l = new JLabel(title); + JBLabel l = new JBLabel(title); optionGroup.add(l, option.myTextField); myOptions.add(option); } diff --git a/platform/platform-api/src/com/intellij/ui/OptionGroup.java b/platform/platform-api/src/com/intellij/ui/OptionGroup.java index b281c0fe2d2f..400933e5288c 100644 --- a/platform/platform-api/src/com/intellij/ui/OptionGroup.java +++ b/platform/platform-api/src/com/intellij/ui/OptionGroup.java @@ -17,6 +17,7 @@ package com.intellij.ui; import com.intellij.openapi.util.Pair; import com.intellij.ui.border.IdeaTitledBorder; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; @@ -26,10 +27,11 @@ import java.util.List; /** * This class creates a nicely formatted panel with components. Useful for option panels. */ -public class OptionGroup { +public class OptionGroup implements PanelWithAnchor { private String myTitle; private List myOptions; private List myIsShifted; + private JComponent anchor; public OptionGroup(String title) { myTitle = title; @@ -109,6 +111,22 @@ public class OptionGroup { return panel; } + @Override + public JComponent getAnchor() { + return anchor; + } + + @Override + public void setAnchor(@Nullable JComponent anchor) { + this.anchor = anchor; + for (Object o : myOptions) { + if (o instanceof Pair && + ((Pair)o).getFirst() instanceof AnchorableComponent) { + ((AnchorableComponent)((Pair)o).getFirst()).setAnchor(anchor); + } + } + } + private static int getFill(JComponent component) { if (component instanceof JCheckBox) { return GridBagConstraints.NONE; @@ -118,8 +136,7 @@ public class OptionGroup { public JComponent[] getComponents() { ArrayList components = new ArrayList(); - for (int i = 0; i < myOptions.size(); i++) { - Object o = myOptions.get(i); + for (Object o : myOptions) { if (o instanceof Pair) { components.add((JComponent)((Pair)o).first); components.add((JComponent)((Pair)o).second); @@ -130,4 +147,19 @@ public class OptionGroup { } return components.toArray(new JComponent[components.size()]); } + + @Nullable + public JComponent findAnchor() { + double maxWidth = -1; + JComponent ans = null; + for (Object o : myOptions) { + if (o instanceof Pair && + ((Pair)o).getFirst() instanceof AnchorableComponent && + ((JComponent)((Pair)o).getFirst()).getPreferredSize().getWidth() > maxWidth) { + maxWidth = ((JComponent)((Pair)o).getFirst()).getPreferredSize().getWidth(); + ans = (JComponent)((Pair)o).getFirst(); + } + } + return ans; + } } diff --git a/platform/util/src/com/intellij/util/ui/UIUtil.java b/platform/util/src/com/intellij/util/ui/UIUtil.java index 1727b9501704..8ce77d2e354c 100644 --- a/platform/util/src/com/intellij/util/ui/UIUtil.java +++ b/platform/util/src/com/intellij/util/ui/UIUtil.java @@ -2432,6 +2432,8 @@ public class UIUtil { } public static void mergeComponentsWithAnchor(PanelWithAnchor c1,PanelWithAnchor c2) { + if (c1 == null || c2 == null) return; + if (c1.getAnchor() == null) { c1.setAnchor(c2.getAnchor()); } else {