ui: remove code duplication with VerticalLayout

GitOrigin-RevId: e9ead14bb925e0b1a3777e93af85cf70d0b7f514
This commit is contained in:
Aleksey Pivovarov
2021-03-24 12:42:00 +00:00
committed by intellij-monorepo-bot
parent 4fe7571aad
commit 0d2e309105
6 changed files with 42 additions and 83 deletions
@@ -121,7 +121,7 @@ class DiffContentPanel extends JPanel {
totalHeight += size.height;
if (component == myTitle && size.height != 0) {
totalHeight += DiffUtil.TITLE_GAP;
totalHeight += DiffUtil.TITLE_GAP.get();
}
}
@@ -142,7 +142,7 @@ class DiffContentPanel extends JPanel {
myTitle.setBounds(0, y, width, titleSize.height);
y += titleSize.height;
if (titleSize.height != 0) y += DiffUtil.TITLE_GAP;
if (titleSize.height != 0) y += DiffUtil.TITLE_GAP.get();
myTopBreadcrumbs.setBounds(0, y, width, topSize.height);
y += topSize.height;
@@ -87,12 +87,13 @@ import com.intellij.testFramework.LightVirtualFile;
import com.intellij.ui.*;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.scale.JBUIScale;
import com.intellij.ui.components.panels.VerticalLayout;
import com.intellij.util.*;
import com.intellij.util.concurrency.annotations.RequiresEdt;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Convertor;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBValue;
import com.intellij.util.ui.SingleComponentCenteringLayout;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.components.BorderLayoutPanel;
@@ -118,7 +119,7 @@ public final class DiffUtil {
public static final Key<Boolean> TEMP_FILE_KEY = Key.create("Diff.TempFile");
@NotNull @NonNls public static final String DIFF_CONFIG = "diff.xml";
public static final int TITLE_GAP = JBUIScale.scale(2);
public static final JBValue TITLE_GAP = new JBValue.Float(2);
public static final NotNullLazyValue<List<Image>> DIFF_FRAME_ICONS = NotNullLazyValue.createValue(() -> {
return Arrays.asList(
@@ -695,8 +696,8 @@ public final class DiffUtil {
}
@NotNull
public static JComponent createStackedComponents(@NotNull List<? extends JComponent> components, int vGap) {
JPanel panel = new JPanel(new VerticalStackLayout(vGap));
public static JComponent createStackedComponents(@NotNull List<? extends JComponent> components, @NotNull JBValue vGap) {
JPanel panel = new JPanel(new VerticalLayout(vGap, VerticalLayout.FILL));
for (JComponent component : components) {
panel.add(component);
}
@@ -1,52 +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.intellij.diff.util;
import com.intellij.util.ui.AbstractLayoutManager;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
class VerticalStackLayout extends AbstractLayoutManager {
private final int myVGap;
VerticalStackLayout(int vGap) {
myVGap = vGap;
}
@Override
public Dimension preferredLayoutSize(Container parent) {
int totalWidth = 0;
int totalHeight = 0;
for (Component component : parent.getComponents()) {
Dimension size = getPreferredSize(component);
totalWidth = Math.max(size.width, totalWidth);
if (size.height != 0 && totalHeight != 0) totalHeight += myVGap;
totalHeight += size.height;
}
return new Dimension(totalWidth, totalHeight);
}
@Override
public void layoutContainer(@NotNull Container parent) {
int width = parent.getWidth();
int y = 0;
for (Component component : parent.getComponents()) {
Dimension size = getPreferredSize(component);
component.setBounds(0, y, width, size.height);
if (size.height != 0) y += myVGap;
y += size.height;
}
}
@NotNull
private static Dimension getPreferredSize(@NotNull Component component) {
return component.isVisible() ? component.getPreferredSize() : new Dimension();
}
}
@@ -2,7 +2,8 @@
package com.intellij.ui.components.panels;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBValue;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
@@ -19,6 +20,7 @@ import java.util.List;
* @see VerticalLayout
*/
public final class HorizontalLayout implements LayoutManager2 {
public static final int FILL = -1;
public static final String LEFT = "LEFT";
public static final String RIGHT = "RIGHT";
public static final String CENTER = "CENTER";
@@ -27,7 +29,7 @@ public final class HorizontalLayout implements LayoutManager2 {
private final ArrayList<Component> myRight = new ArrayList<>();
private final ArrayList<Component> myCenter = new ArrayList<>();
private final int myAlignment;
private final int myGap;
private final JBValue myGap;
/**
* Creates a layout with the specified gap.
@@ -35,11 +37,10 @@ public final class HorizontalLayout implements LayoutManager2 {
* but their heights will be set according to the container.
* The gap will be scaled automatically.
*
* @param gap horizontal gap between components
* @param gap horizontal gap between components, without DPI scaling
*/
public HorizontalLayout(int gap) {
myGap = gap;
myAlignment = -1;
this(gap, FILL);
}
/**
@@ -47,16 +48,20 @@ public final class HorizontalLayout implements LayoutManager2 {
* All components will have preferred sizes.
* The gap will be scaled automatically.
*
* @param gap horizontal gap between components
* @param gap horizontal gap between components, without DPI scaling
* @param alignment vertical alignment for components
*
* @see SwingConstants#TOP
* @see SwingConstants#BOTTOM
* @see SwingConstants#CENTER
*/
public HorizontalLayout(int gap, int alignment) {
this(new JBValue.Float(Math.max(0, gap)), alignment);
}
public HorizontalLayout(@NotNull JBValue gap, int alignment) {
myGap = gap;
switch (alignment) {
case FILL:
case SwingConstants.TOP:
case SwingConstants.BOTTOM:
case SwingConstants.CENTER:
@@ -133,7 +138,7 @@ public final class HorizontalLayout implements LayoutManager2 {
@Override
public void layoutContainer(Container container) {
int gap = myGap <= 0 ? 0 : JBUI.scale(myGap);
int gap = myGap.get();
synchronized (container.getTreeLock()) {
Dimension left = getPreferredSize(myLeft);
Dimension right = getPreferredSize(myRight);
@@ -177,12 +182,12 @@ public final class HorizontalLayout implements LayoutManager2 {
}
private int layout(List<? extends Component> list, int x, int height, Insets insets) {
int gap = myGap <= 0 ? 0 : JBUI.scale(myGap);
int gap = myGap.get();
for (Component component : list) {
if (component.isVisible()) {
Dimension size = component.getPreferredSize();
int y = 0;
if (myAlignment == -1) {
if (myAlignment == FILL) {
size.height = height;
}
else if (myAlignment != SwingConstants.TOP) {
@@ -213,7 +218,7 @@ public final class HorizontalLayout implements LayoutManager2 {
}
private Dimension getPreferredSize(List<? extends Component> list) {
int gap = myGap <= 0 ? 0 : JBUI.scale(myGap);
int gap = myGap.get();
Dimension result = null;
for (Component component : list) {
if (component.isVisible()) {
@@ -224,7 +229,7 @@ public final class HorizontalLayout implements LayoutManager2 {
}
private Dimension getPreferredSize(Container container, boolean aligned) {
int gap2 = myGap <= 0 ? 0 : 2 * JBUI.scale(myGap);
int gap2 = 2 * myGap.get();
synchronized (container.getTreeLock()) {
Dimension left = getPreferredSize(myLeft);
Dimension right = getPreferredSize(myRight);
@@ -2,7 +2,8 @@
package com.intellij.ui.components.panels;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBValue;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
@@ -19,6 +20,7 @@ import java.util.List;
* @see HorizontalLayout
*/
public final class VerticalLayout implements LayoutManager2 {
public static final int FILL = -1;
public static final String TOP = "TOP";
public static final String BOTTOM = "BOTTOM";
public static final String CENTER = "CENTER";
@@ -27,7 +29,7 @@ public final class VerticalLayout implements LayoutManager2 {
private final ArrayList<Component> myBottom = new ArrayList<>();
private final ArrayList<Component> myCenter = new ArrayList<>();
private final int myAlignment;
private final int myGap;
private final JBValue myGap;
/**
* Creates a layout with the specified gap.
@@ -35,11 +37,10 @@ public final class VerticalLayout implements LayoutManager2 {
* but their widths will be set according to the container.
* The gap will be scaled automatically.
*
* @param gap vertical gap between components
* @param gap vertical gap between components, without DPI scaling
*/
public VerticalLayout(int gap) {
myGap = gap;
myAlignment = -1;
this(gap, FILL);
}
/**
@@ -47,16 +48,20 @@ public final class VerticalLayout implements LayoutManager2 {
* All components will have preferred sizes.
* The gap will be scaled automatically.
*
* @param gap vertical gap between components
* @param gap vertical gap between components, without DPI scaling
* @param alignment horizontal alignment for components
*
* @see SwingConstants#LEFT
* @see SwingConstants#RIGHT
* @see SwingConstants#CENTER
*/
public VerticalLayout(int gap, int alignment) {
this(new JBValue.Float(Math.max(0, gap)), alignment);
}
public VerticalLayout(@NotNull JBValue gap, int alignment) {
myGap = gap;
switch (alignment) {
case FILL:
case SwingConstants.LEFT:
case SwingConstants.RIGHT:
case SwingConstants.CENTER:
@@ -133,7 +138,7 @@ public final class VerticalLayout implements LayoutManager2 {
@Override
public void layoutContainer(Container container) {
int gap = myGap <= 0 ? 0 : JBUI.scale(myGap);
int gap = myGap.get();
synchronized (container.getTreeLock()) {
Dimension top = getPreferredSize(myTop);
Dimension bottom = getPreferredSize(myBottom);
@@ -177,12 +182,12 @@ public final class VerticalLayout implements LayoutManager2 {
}
private int layout(List<? extends Component> list, int y, int width, Insets insets) {
int gap = myGap <= 0 ? 0 : JBUI.scale(myGap);
int gap = myGap.get();
for (Component component : list) {
if (component.isVisible()) {
Dimension size = component.getPreferredSize();
int x = 0;
if (myAlignment == -1) {
if (myAlignment == FILL) {
size.width = width;
}
else if (myAlignment != SwingConstants.LEFT) {
@@ -213,7 +218,7 @@ public final class VerticalLayout implements LayoutManager2 {
}
private Dimension getPreferredSize(List<? extends Component> list) {
int gap = myGap <= 0 ? 0 : JBUI.scale(myGap);
int gap = myGap.get();
Dimension result = null;
for (Component component : list) {
if (component.isVisible()) {
@@ -224,7 +229,7 @@ public final class VerticalLayout implements LayoutManager2 {
}
private Dimension getPreferredSize(Container container, boolean aligned) {
int gap2 = myGap <= 0 ? 0 : 2 * JBUI.scale(myGap);
int gap2 = 2 * myGap.get();
synchronized (container.getTreeLock()) {
Dimension top = getPreferredSize(myTop);
Dimension bottom = getPreferredSize(myBottom);
@@ -69,7 +69,7 @@ final class PatchDiffTool implements FrameDiffTool {
myEditor = DiffUtil.createEditor(document, myProject, true, true);
myPrevNextDifferenceIterable = new MyPrevNextDifferenceIterable();
Wrapper editorPanel = new Wrapper(new BorderLayout(0, DiffUtil.TITLE_GAP), myEditor.getComponent());
Wrapper editorPanel = new Wrapper(new BorderLayout(0, DiffUtil.TITLE_GAP.get()), myEditor.getComponent());
String panelTitle = request.getPanelTitle();
if (panelTitle != null) {
editorPanel.add(DiffUtil.createTitle(panelTitle), BorderLayout.NORTH);