You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
4.0 KiB
126 lines
4.0 KiB
package com.tptj.tool.hg.fineui.swing.element.layout.impl; |
|
|
|
import com.tptj.tool.hg.fineui.swing.element.AbstractElement; |
|
import com.tptj.tool.hg.fineui.swing.element.Element; |
|
|
|
import javax.swing.*; |
|
import java.awt.*; |
|
import java.util.List; |
|
|
|
/** |
|
* @author 秃破天际 |
|
* @version 10.0 |
|
* Created by 秃破天际 on 2021/11/1 |
|
**/ |
|
public abstract class BaseFineUILayout implements LayoutManager2 { |
|
public static final int NOT_EXIST = -99999; |
|
public static final int MAX_SIZE = 99999; |
|
protected Element element; |
|
|
|
public BaseFineUILayout(Element element) { |
|
this.element = element; |
|
} |
|
|
|
@Override |
|
public void addLayoutComponent(Component comp, Object constraints) { |
|
//不需要了 |
|
} |
|
|
|
@Override |
|
public Dimension maximumLayoutSize(Container target) { |
|
return null; |
|
} |
|
|
|
@Override |
|
public float getLayoutAlignmentX(Container target) { |
|
return 0; |
|
} |
|
|
|
@Override |
|
public float getLayoutAlignmentY(Container target) { |
|
return 0; |
|
} |
|
|
|
@Override |
|
public void invalidateLayout(Container target) { |
|
System.out.println(""); |
|
} |
|
|
|
@Override |
|
public void addLayoutComponent(String name, Component comp) { |
|
//不需要了 |
|
} |
|
|
|
@Override |
|
public void removeLayoutComponent(Component comp) { |
|
//暂时不需要了 |
|
} |
|
|
|
@Override |
|
public Dimension preferredLayoutSize(Container parent) { |
|
List<AbstractElement> children = element.children(); |
|
int p_width = 0; |
|
int p_height = 0; |
|
int width = parent.getParent().getWidth(); |
|
int height = parent.getParent().getHeight(); |
|
for (Element child : children) { |
|
Rectangle rect = getFineUIRect(child, width, height); |
|
if (rect.x + rect.width > p_width) { |
|
p_width = rect.x + rect.width; |
|
} |
|
if (rect.y + rect.height > p_height) { |
|
p_height = rect.y + rect.height; |
|
} |
|
} |
|
return BaseFineUILayout.preferredLayoutSize(element, new Dimension(p_width, p_height)); |
|
} |
|
|
|
@Override |
|
public Dimension minimumLayoutSize(Container parent) { |
|
return null; |
|
} |
|
|
|
@Override |
|
public void layoutContainer(Container parent) { |
|
int width = parent.getWidth(); |
|
int height = parent.getHeight(); |
|
List<AbstractElement> children = element.children(); |
|
for (Element child : children) { |
|
Rectangle rect = getFineUIRect(child, width, height); |
|
child.getWrapper().setBounds(rect.x, rect.y, rect.width, rect.height); |
|
} |
|
} |
|
|
|
protected abstract Rectangle getFineUIRect(Element child, int width, int height); |
|
|
|
public static Dimension preferredLayoutSize(Element element, Dimension dimension) { |
|
int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); |
|
int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); |
|
Dimension realDim = new Dimension(w, h); |
|
if (w == MAX_SIZE || w == NOT_EXIST) { |
|
realDim.width = dimension.width; |
|
} |
|
if (h == MAX_SIZE || h == NOT_EXIST) { |
|
realDim.height = dimension.height; |
|
} |
|
if (element.hasScrollbar()) { |
|
// element.getWrapper().setPreferredSize(realDim); |
|
return dimension; |
|
} |
|
return realDim; |
|
} |
|
|
|
public static void optimiseElement(Element element) { |
|
int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); |
|
int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); |
|
int ml = element.getRectStyleAttr("margin-left", MAX_SIZE, 0); |
|
int mr = element.getRectStyleAttr("margin-right", MAX_SIZE, 0); |
|
int mt = element.getRectStyleAttr("margin-top", MAX_SIZE, 0); |
|
int mb = element.getRectStyleAttr("margin-bottom", MAX_SIZE, 0); |
|
Dimension dimension = new Dimension(w, h); |
|
if (element.hasScrollbar()) { |
|
element.getWrapper().setPreferredSize(dimension); |
|
} |
|
element.getWrapper().setBorder(BorderFactory.createEmptyBorder(mt, ml, mb, mr)); |
|
} |
|
}
|
|
|