@ -11,6 +11,7 @@ import com.fr.form.ui.Widget;
import com.fr.form.ui.WidgetTitle ;
import com.fr.form.ui.WidgetValue ;
import com.fr.form.ui.container.WTitleLayout ;
import com.fr.general.Background ;
import com.fr.general.ComparatorUtils ;
import com.fr.general.act.BorderPacker ;
import com.fr.general.act.TitlePacker ;
@ -18,7 +19,11 @@ import com.fr.stable.Constants;
import com.fr.stable.StringUtils ;
import javax.swing.* ;
import javax.swing.border.Border ;
import javax.swing.border.LineBorder ;
import java.awt.* ;
import java.awt.geom.Rectangle2D ;
import java.awt.geom.RoundRectangle2D ;
/ * *
* Created with IntelliJ IDEA .
@ -29,6 +34,7 @@ import java.awt.*;
public class XBorderStyleWidgetCreator extends XWidgetCreator {
protected static final Dimension BORDER_PREFERRED_SIZE = new Dimension ( 250 , 150 ) ;
protected Background background4Painting ; // 设计器预览界面中绘制组件背景图
public XBorderStyleWidgetCreator ( Widget widget , Dimension initSize ) {
super ( widget , initSize ) ;
@ -39,6 +45,15 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{
}
}
public Background getBackground4Painting ( ) {
return this . background4Painting ;
}
public void setBackground4Painting ( Background background4Painting ) {
this . background4Painting = background4Painting ;
this . repaint ( ) ;
}
/ * *
* 返回容器对应的widget
* @return 同上
@ -58,22 +73,37 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{
protected void initStyle ( ) {
BorderPacker style = toData ( ) . getBorderStyle ( ) ;
initBorderStyle ( ) ;
initBorderAndBackground Style ( ) ;
if ( ComparatorUtils . equals ( style . getType ( ) , LayoutBorderStyle . TITLE ) ) {
initTitleStyle ( style ) ;
} else {
clearTitleWidget ( ) ;
}
// 编辑右侧栏组件样式面板后,重新调整组件/标题/边框的显示效果
reshuffleBorderAndBackgroundPaintingEffectIfTitleExists ( ) ;
}
// 边框默认值设为NONE,不然像scalelayout这种只用默认边框的会不显示边框
protected void initBorderStyle ( ) {
protected void initBorderAndBackgroundStyle ( ) {
BorderPacker style = toData ( ) . getBorderStyle ( ) ;
if ( style ! = null & & style . getBorder ( ) ! = Constants . LINE_NONE ) {
this . setBorder ( new UIRoundedBorder ( style . getBorder ( ) , style . getColor ( ) , style . getBorderRadius ( ) ) ) ;
LineBorder DEFAULT_LINE_BORDER = ( LineBorder ) DEFALUTBORDER ;
// 边框默认值设为NONE,不然像scalelayout这种只用默认边框的会不显示边框
Border border = DEFAULT_LINE_BORDER ;
if ( style ! = null ) {
int radius = style . getBorderRadius ( ) ; // 不管有无边框线, 圆角裁剪总是生效
if ( style . getBorder ( ) ! = Constants . LINE_NONE ) {
// 设置了边框线
border = new UIRoundedBorder ( style . getBorder ( ) , style . getColor ( ) , radius ) ;
} else {
this . setBorder ( DEFALUTBORDER ) ;
// 为设置边框线
border = new UIRoundedBorder ( DEFAULT_LINE_BORDER . getLineColor ( ) , DEFAULT_LINE_BORDER . getThickness ( ) , radius ) ;
}
}
this . setBorder ( border ) ;
this . setBackground4Painting ( style ! = null ? style . getBackground ( ) : null ) ;
}
private void clearTitleWidget ( ) {
@ -143,6 +173,75 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{
return new WidgetValue ( value ) ;
}
// 如果存在标题栏,则需要重新弄调整边框和背景的显示效果,使得边框和背景的视觉效果出现在包含组件和标题的整体区域上,
// 而不是组件和标题各自出现独立的边框和背景,同时如存在标题栏,则标题栏下方还应有底部边框,作为标题和内容的分界线
protected void reshuffleBorderAndBackgroundPaintingEffectIfTitleExists ( ) {
Container parent = this . getParent ( ) ;
if ( parent instanceof XWTitleLayout ) {
XWTitleLayout titleParent = ( XWTitleLayout ) parent ;
if ( parent . getComponentCount ( ) > 1 ) {
XCreator titleCreator = titleParent . getTitleCreator ( ) ;
XCreator bodyXCreator = titleParent . getBodyCreator ( ) ;
Border border = bodyXCreator . getBorder ( ) ;
titleParent . setBorder ( border ) ; // 容器绘制完整边框
bodyXCreator . setBorder ( BorderFactory . createEmptyBorder ( ) ) ; // body不绘制边框
titleCreator . setBorder ( BorderFactory . createEmptyBorder ( ) ) ; // title绘制底部边框
if ( border instanceof LineBorder ) {
Color color = ( ( LineBorder ) border ) . getLineColor ( ) ;
int thickness = ( ( LineBorder ) border ) . getThickness ( ) ;
titleCreator . setBorder ( new BottomLineBorder ( color , thickness ) ) ;
}
if ( bodyXCreator instanceof XBorderStyleWidgetCreator ) {
XBorderStyleWidgetCreator styledBodyXCreator = ( XBorderStyleWidgetCreator ) bodyXCreator ;
Background background4Painting = styledBodyXCreator . getBackground4Painting ( ) ;
styledBodyXCreator . setBackground4Painting ( null ) ; // body不绘制背景
titleParent . setBackground4Painting ( background4Painting ) ; // 容器绘制完整背景
}
}
}
}
// 根据当前组件边框裁剪内容,如果当前组件存在圆角,则应当按圆角裁剪内容
private void clipByRoundedBorder ( Graphics2D g2d ) {
Border currentBorder = getBorder ( ) ;
if ( currentBorder instanceof UIRoundedBorder ) {
int thickness = ( ( UIRoundedBorder ) currentBorder ) . getThickness ( ) ;
int radius = ( ( UIRoundedBorder ) currentBorder ) . getRoundedCorner ( ) ;
int currentClipX = 0 ;
int currentClipY = 0 ;
int currentClipWidth = getWidth ( ) ;
int currentClipHeight = getHeight ( ) ;
int currentClipRadius = Math . max ( radius - thickness , 0 ) ; // 应沿着边框外围进行裁剪
g2d . clip ( new RoundRectangle2D . Double ( currentClipX , currentClipY , currentClipWidth , currentClipHeight , currentClipRadius , currentClipRadius ) ) ;
}
}
// 设计器预览界面中绘制组件背景效果
private void paintBackground ( Graphics2D g2d ) {
Background background4Painting = getBackground4Painting ( ) ;
if ( background4Painting ! = null ) {
background4Painting . paint ( g2d , new Rectangle2D . Double ( 0 , 0 , getWidth ( ) , getHeight ( ) ) ) ;
}
}
@Override
protected void paintComponent ( Graphics g ) {
this . paintBackground ( ( Graphics2D ) g ) ;
super . paintComponent ( g ) ;
}
@Override
public void paint ( Graphics g ) {
this . clipByRoundedBorder ( ( Graphics2D ) g ) ;
super . paint ( g ) ;
paintBorder ( g ) ;
}
@Override
protected String getIconName ( ) {
return StringUtils . EMPTY ;
@ -173,4 +272,29 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{
}
// 适用于标题栏的底部边框
public static class BottomLineBorder extends LineBorder {
private BottomLineBorder ( Color color , int thickness ) {
super ( color , thickness ) ;
}
@Override
public void paintBorder ( Component c , Graphics g , int x , int y , int width , int height ) {
Graphics2D g2d = ( Graphics2D ) g ;
Color oldColor = g2d . getColor ( ) ;
Stroke oldStroke = g2d . getStroke ( ) ;
g2d . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ;
g2d . setColor ( getLineColor ( ) ) ;
g2d . setStroke ( new BasicStroke ( getThickness ( ) ) ) ;
g2d . drawLine ( 0 , height , width , height ) ;
g2d . setStroke ( oldStroke ) ;
g2d . setColor ( oldColor ) ;
g2d . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_OFF ) ;
}
}
}