forked from fanruan/design
neil
8 years ago
7 changed files with 1128 additions and 1138 deletions
@ -1,346 +1,341 @@
|
||||
package com.fr.design.designer.beans.adapters.layout; |
||||
|
||||
import com.fr.design.beans.GroupModel; |
||||
import com.fr.design.designer.beans.ConstraintsGroupModel; |
||||
import com.fr.design.designer.beans.HoverPainter; |
||||
import com.fr.design.designer.beans.painters.FRAbsoluteLayoutPainter; |
||||
import com.fr.design.designer.creator.*; |
||||
import com.fr.design.designer.properties.BoundsGroupModel; |
||||
import com.fr.design.designer.properties.FRAbsoluteLayoutPropertiesGroupModel; |
||||
import com.fr.design.utils.ComponentUtils; |
||||
import com.fr.design.utils.gui.LayoutUtils; |
||||
import com.fr.form.ui.container.WAbsoluteLayout; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.general.FRLogger; |
||||
|
||||
import java.awt.*; |
||||
|
||||
public class FRAbsoluteLayoutAdapter extends FRBodyLayoutAdapter { |
||||
//是不是添加到父容器上
|
||||
private boolean isAdd2ParentLayout = false; |
||||
private HoverPainter painter; |
||||
|
||||
public FRAbsoluteLayoutAdapter(XLayoutContainer container) { |
||||
super(container); |
||||
painter = new FRAbsoluteLayoutPainter(container); |
||||
initMinSize(); |
||||
} |
||||
|
||||
private void initMinSize() { |
||||
XWAbsoluteLayout layout = (XWAbsoluteLayout) container; |
||||
minWidth = layout.getActualMinWidth(); |
||||
minHeight = layout.getActualMinHeight(); |
||||
actualVal = layout.getAcualInterval(); |
||||
margin = layout.toData().getMargin(); |
||||
} |
||||
|
||||
@Override |
||||
public HoverPainter getPainter() { |
||||
return painter; |
||||
} |
||||
|
||||
/** |
||||
* 是否能在指定位置添加组件 |
||||
* |
||||
* @param creator 组件 |
||||
* @param x 坐标x |
||||
* @param y 坐标y |
||||
* @return 能则返回true |
||||
*/ |
||||
//这个地方的逻辑非常复杂,
|
||||
// 1.当前绝对布局是不可编辑且是最外层,那么其他控件添加在它周围,
|
||||
// 2.当前绝对布局是不可编辑且不是最外层,那么控件不可添加,(嵌套)
|
||||
// 3.当前绝对布局可编辑,那么控件添加
|
||||
@Override |
||||
public boolean accept(XCreator creator, int x, int y) { |
||||
Component comp = container.getComponentAt(x, y); |
||||
//布局控件要先判断是不是可编辑
|
||||
//可以编辑,按原有逻辑判断
|
||||
//不可编辑,当成一整个控件处理
|
||||
if (comp == null) { |
||||
return false; |
||||
} |
||||
//参数面板内的组件不允许拖往绝对布局中
|
||||
if (creator.getParent() != null && ((XCreator) creator.getParent()).acceptType(XWParameterLayout.class)) { |
||||
Rectangle rec = creator.getBounds(); |
||||
rec.y = creator.getParent().getHeight() - rec.height; |
||||
creator.setBounds(rec); |
||||
return false; |
||||
} |
||||
//判断组件能不能拖入绝对布局
|
||||
if (!creator.canEnterIntoAbsolutePane()) { |
||||
return false; |
||||
} |
||||
XLayoutContainer topLayout = XCreatorUtils.getHotspotContainer((XCreator) comp).getTopLayout(); |
||||
if (topLayout != null) { |
||||
if (topLayout.isEditable()) { |
||||
return topLayoutAccept(creator, x, y); |
||||
} |
||||
//绝对布局嵌套,处于内层,不可编辑,不添加,topLayout只能获取到最外层可编辑的布局
|
||||
else if (((XLayoutContainer) topLayout.getParent()).acceptType(XWAbsoluteLayout.class)) { |
||||
return false; |
||||
} else { |
||||
return acceptWidget(x, y); |
||||
} |
||||
} else { |
||||
FRLogger.getLogger().error("top layout is null!"); |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
//topLayout假如可以编辑的话就往里面添加组件
|
||||
private boolean topLayoutAccept(XCreator creator, int x, int y) { |
||||
//允许组件重叠,可以不判断有没有和当前控件重叠
|
||||
//先计算当前控件的位置
|
||||
int creatorX, creatorY; |
||||
if (XCreatorUtils.getParentXLayoutContainer(creator) != null) { |
||||
Rectangle creatorRectangle = ComponentUtils.getRelativeBounds(creator); |
||||
creatorX = creatorRectangle.x; |
||||
creatorY = creatorRectangle.y; |
||||
} else { |
||||
//这边计算得到的组件其实位置是正确的,
|
||||
//因为传入的x和y已经加上了宽度或者高度的一半,再减去相同的宽度和高度的一半是没区别的,
|
||||
// 例如高度为21,那么就是+10-10;
|
||||
// 高度为20,那么就是+10-10; 没区别
|
||||
int w = creator.getWidth() / 2; |
||||
int h = creator.getHeight() / 2; |
||||
creatorX = x - w; |
||||
creatorY = y - h; |
||||
} |
||||
if (creatorX < 0 || creatorX + creator.getWidth() > container.getWidth()) { |
||||
return false; |
||||
} |
||||
if (creatorY < 0 || creatorY + creator.getHeight() > container.getHeight()) { |
||||
return false; |
||||
} |
||||
return x >= 0 && y >= 0 && creator.getHeight() <= container.getHeight() |
||||
&& creator.getWidth() <= container.getWidth(); |
||||
} |
||||
|
||||
/** |
||||
* 判断是否鼠标在组件的三等分区域,如果组件在布局管理器中间,上下左右都可能会三等分 |
||||
* |
||||
* @param parentComp 鼠标所在区域的组件 |
||||
* @param x 坐标x |
||||
* @param y 坐标y |
||||
* @return 是则返回true |
||||
*/ |
||||
public boolean isTrisectionArea(Component parentComp, int x, int y) { |
||||
XCreator creator = (XCreator) parentComp; |
||||
trisectAreaDirect = 0; |
||||
if (container.getComponentCount() <= 1) { |
||||
return false; |
||||
} |
||||
int maxWidth = parentComp.getWidth(); |
||||
int maxHeight = parentComp.getHeight(); |
||||
int xL = parentComp.getX(); |
||||
int yL = parentComp.getY(); |
||||
// 组件宽高的十分之一和默认值取大
|
||||
int minRangeWidth = Math.max(maxWidth / BORDER_PROPORTION, DEFAULT_AREA_LENGTH); |
||||
int minRangeHeight = Math.max(maxHeight / BORDER_PROPORTION, DEFAULT_AREA_LENGTH); |
||||
if (y < yL + minRangeHeight) { |
||||
// 在组件上侧三等分
|
||||
trisectAreaDirect = COMP_TOP; |
||||
} else if (y > yL + maxHeight - minRangeHeight) { |
||||
// 在组件下侧三等分
|
||||
trisectAreaDirect = COMP_BOTTOM; |
||||
} else if (x < xL + minRangeWidth) { |
||||
// 在组件左侧三等分
|
||||
trisectAreaDirect = COMP_LEFT; |
||||
} else if (x > xL + maxWidth - minRangeWidth) { |
||||
// 在组件右侧三等分
|
||||
trisectAreaDirect = COMP_RIGHT; |
||||
} |
||||
// tab布局的边界特殊处理,不进行三等分
|
||||
if (!creator.getTargetChildrenList().isEmpty()) { |
||||
return false; |
||||
} |
||||
|
||||
return !ComparatorUtils.equals(trisectAreaDirect, 0); |
||||
} |
||||
|
||||
//当前绝对布局不可编辑,就当成一个控件,组件添加在周围
|
||||
private boolean acceptWidget(int x, int y) { |
||||
isFindRelatedComps = false; |
||||
//拖入组件判断时,先判断是否为交叉点区域,其次三等分区域,再次平分区域
|
||||
Component comp = container.getComponentAt(x, y); |
||||
//如果当前处于边缘地带, 那么就把他贴到父容器上
|
||||
XLayoutContainer parent = container.findNearestFit(); |
||||
container = parent != null ? parent : container; |
||||
isAdd2ParentLayout = true; |
||||
|
||||
int componentHeight = comp.getHeight(); |
||||
int componentWidth = comp.getWidth(); |
||||
//上半部分高度
|
||||
int upHeight = (int) (componentHeight * TOP_HALF) + comp.getY(); |
||||
//下半部分高度
|
||||
int downHeight = (int) (componentHeight * BOTTOM_HALF) + comp.getY(); |
||||
|
||||
if (isCrossPointArea(comp, x, y)) { |
||||
return canAcceptWhileCrossPoint(comp, x, y); |
||||
} |
||||
|
||||
if (isTrisectionArea(comp, x, y)) { |
||||
return canAcceptWhileTrisection(comp, x, y); |
||||
} |
||||
|
||||
boolean horizonValid = componentWidth >= minWidth * 2 + actualVal; |
||||
boolean verticalValid = componentHeight >= minHeight * 2 + actualVal; |
||||
return y > upHeight && y < downHeight ? horizonValid : verticalValid; |
||||
} |
||||
|
||||
/** |
||||
* 组件的ComponentAdapter在添加组件时,如果发现布局管理器不为空,会继而调用该布局管理器的 |
||||
* addComp方法来完成组件的具体添加。在该方法内,布局管理器可以提供额外的功能。 |
||||
* |
||||
* @param creator 被添加的新组件 |
||||
* @param x 添加的位置x,该位置是相对于container的 |
||||
* @param y 添加的位置y,该位置是相对于container的 |
||||
* @return 是否添加成功,成功返回true,否则false |
||||
*/ |
||||
@Override |
||||
public boolean addBean(XCreator creator, int x, int y) { |
||||
Rectangle rect = ComponentUtils.getRelativeBounds(container); |
||||
|
||||
int posX = x + rect.x; |
||||
int posY = y + rect.y; |
||||
if (!accept(creator, x, y)) { |
||||
return false; |
||||
} |
||||
addComp(creator, posX, posY); |
||||
((XWidgetCreator) creator).recalculateChildrenSize(); |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
protected void addComp(XCreator creator, int x, int y) { |
||||
if (!isAdd2ParentLayout) { |
||||
Rectangle r = ComponentUtils.getRelativeBounds(container); |
||||
x = x - r.x; |
||||
y = y - r.y; |
||||
if (XCreatorUtils.getParentXLayoutContainer(creator) != null) { |
||||
|
||||
Rectangle creatorRectangle = ComponentUtils.getRelativeBounds(creator); |
||||
x = creatorRectangle.x - r.x; |
||||
y = creatorRectangle.y - r.y; |
||||
} else { |
||||
int w = creator.getWidth() / 2; |
||||
int h = creator.getHeight() / 2; |
||||
x = x - w; |
||||
y = y - h; |
||||
} |
||||
fix(creator, x, y); |
||||
|
||||
if (creator.hasTitleStyle()) { |
||||
addParentCreator(creator); |
||||
} else { |
||||
container.add(creator, creator.toData().getWidgetName(), 0); |
||||
} |
||||
XWAbsoluteLayout layout = (XWAbsoluteLayout) container; |
||||
layout.updateBoundsWidget(creator); |
||||
updateCreatorBackBound(); |
||||
LayoutUtils.layoutRootContainer(container); |
||||
} else { |
||||
fixAbsolute(creator, x, y); |
||||
if (creator.shouldScaleCreator() || creator.hasTitleStyle()) { |
||||
addParentCreator(creator); |
||||
} else { |
||||
container.add(creator, creator.toData().getWidgetName(), 0); |
||||
} |
||||
XWFitLayout layout = (XWFitLayout) container; |
||||
// 更新对应的BoundsWidget
|
||||
layout.updateBoundsWidget(); |
||||
updateCreatorBackBound(); |
||||
} |
||||
} |
||||
|
||||
private void updateCreatorBackBound() { |
||||
for (int i = 0, size = container.getComponentCount(); i < size; i++) { |
||||
XCreator creator = (XCreator) container.getComponent(i); |
||||
creator.updateChildBound(minHeight); |
||||
creator.setBackupBound(creator.getBounds()); |
||||
} |
||||
} |
||||
|
||||
private void addParentCreator(XCreator child) { |
||||
XLayoutContainer parentPanel = child.initCreatorWrapper(child.getHeight()); |
||||
container.add(parentPanel, child.toData().getWidgetName(), 0); |
||||
} |
||||
|
||||
/** |
||||
* 新拖入组件时,计算调整其他关联组件位置大小 |
||||
* |
||||
* @param child 新拖入的组件 |
||||
* @param x 鼠标所在x坐标 |
||||
* @param y 鼠标所在y坐标 |
||||
*/ |
||||
private void fixAbsolute(XCreator child, int x, int y) { |
||||
Component parentComp = container.getComponentAt(x, y); |
||||
if (container.getComponentCount() == 0) { |
||||
child.setLocation(0, 0); |
||||
child.setSize(parentComp.getWidth(), parentComp.getHeight()); |
||||
} else if (isCrossPointArea(parentComp, x, y)) { |
||||
//交叉区域插入组件时,根据具体位置进行上下或者左右或者相邻三个组件的位置大小插入
|
||||
fixCrossPointArea(parentComp, child, x, y); |
||||
return; |
||||
} else if (isTrisectionArea(parentComp, x, y)) { |
||||
// 在边界三等分区域,就不再和组件二等分了
|
||||
fixTrisect(parentComp, child, x, y); |
||||
return; |
||||
} else { |
||||
fixHalve(parentComp, child, x, y); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 组件拖拽后调整大小 |
||||
* |
||||
* @param creator 组件 |
||||
*/ |
||||
@Override |
||||
public void fix(XCreator creator) { |
||||
WAbsoluteLayout wabs = (WAbsoluteLayout) container.toData(); |
||||
fix(creator, creator.getX(), creator.getY()); |
||||
wabs.setBounds(creator.toData(), creator.getBounds()); |
||||
|
||||
XWAbsoluteLayout layout = (XWAbsoluteLayout) container; |
||||
layout.updateBoundsWidget(creator); |
||||
} |
||||
|
||||
/** |
||||
* 调整组件大小到合适尺寸位置 |
||||
* |
||||
* @param creator 组件 |
||||
* @param x 坐标x |
||||
* @param y 坐标y |
||||
*/ |
||||
public void fix(XCreator creator, int x, int y) { |
||||
int height = creator.getHeight(); |
||||
int width = creator.getWidth(); |
||||
if (x < 0) { |
||||
x = container.getX(); |
||||
} else if (x + creator.getWidth() > container.getWidth()) { |
||||
x = container.getWidth() - width; |
||||
} |
||||
|
||||
if (y < 0) { |
||||
y = container.getY(); |
||||
} else if (y + creator.getHeight() > container.getHeight()) { |
||||
y = container.getHeight() - height; |
||||
} |
||||
|
||||
creator.setBounds(x, y, width, height); |
||||
} |
||||
|
||||
@Override |
||||
public ConstraintsGroupModel getLayoutConstraints(XCreator creator) { |
||||
return new BoundsGroupModel((XWAbsoluteLayout) container, creator); |
||||
} |
||||
|
||||
@Override |
||||
public GroupModel getLayoutProperties() { |
||||
XWAbsoluteLayout xwAbsoluteLayout = (XWAbsoluteLayout) container; |
||||
return new FRAbsoluteLayoutPropertiesGroupModel(xwAbsoluteLayout); |
||||
} |
||||
package com.fr.design.designer.beans.adapters.layout; |
||||
|
||||
import com.fr.design.beans.GroupModel; |
||||
import com.fr.design.designer.beans.ConstraintsGroupModel; |
||||
import com.fr.design.designer.beans.HoverPainter; |
||||
import com.fr.design.designer.beans.painters.FRAbsoluteLayoutPainter; |
||||
import com.fr.design.designer.creator.*; |
||||
import com.fr.design.designer.properties.BoundsGroupModel; |
||||
import com.fr.design.designer.properties.FRAbsoluteLayoutPropertiesGroupModel; |
||||
import com.fr.design.utils.ComponentUtils; |
||||
import com.fr.design.utils.gui.LayoutUtils; |
||||
import com.fr.form.ui.container.WAbsoluteLayout; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.general.FRLogger; |
||||
|
||||
import java.awt.*; |
||||
|
||||
public class FRAbsoluteLayoutAdapter extends FRBodyLayoutAdapter { |
||||
//是不是添加到父容器上
|
||||
private boolean isAdd2ParentLayout = false; |
||||
private HoverPainter painter; |
||||
|
||||
public FRAbsoluteLayoutAdapter(XLayoutContainer container) { |
||||
super(container); |
||||
painter = new FRAbsoluteLayoutPainter(container); |
||||
initMinSize(); |
||||
} |
||||
|
||||
private void initMinSize() { |
||||
XWAbsoluteLayout layout = (XWAbsoluteLayout) container; |
||||
minWidth = layout.getActualMinWidth(); |
||||
minHeight = layout.getActualMinHeight(); |
||||
actualVal = layout.getAcualInterval(); |
||||
margin = layout.toData().getMargin(); |
||||
} |
||||
|
||||
@Override |
||||
public HoverPainter getPainter() { |
||||
return painter; |
||||
} |
||||
|
||||
/** |
||||
* 是否能在指定位置添加组件 |
||||
* @param creator 组件 |
||||
* @param x 坐标x |
||||
* @param y 坐标y |
||||
* @return 能则返回true |
||||
*/ |
||||
//这个地方的逻辑非常复杂,
|
||||
// 1.当前绝对布局是不可编辑且是最外层,那么其他控件添加在它周围,
|
||||
// 2.当前绝对布局是不可编辑且不是最外层,那么控件不可添加,(嵌套)
|
||||
// 3.当前绝对布局可编辑,那么控件添加
|
||||
@Override |
||||
public boolean accept(XCreator creator, int x, int y) { |
||||
Component comp = container.getComponentAt(x, y); |
||||
//布局控件要先判断是不是可编辑
|
||||
//可以编辑,按原有逻辑判断
|
||||
//不可编辑,当成一整个控件处理
|
||||
if (comp == null){ |
||||
return false; |
||||
} |
||||
//参数面板内的组件不允许拖往绝对布局中
|
||||
if (creator.getParent() != null && ((XCreator)creator.getParent()).acceptType(XWParameterLayout.class)){ |
||||
Rectangle rec = creator.getBounds(); |
||||
rec.y = creator.getParent().getHeight() - rec.height; |
||||
creator.setBounds(rec); |
||||
return false; |
||||
} |
||||
//判断下组件能不能拖入绝对布局
|
||||
if (!creator.canEnterIntoAbsolutePane()){ |
||||
return false; |
||||
} |
||||
XLayoutContainer topLayout = XCreatorUtils.getHotspotContainer((XCreator)comp).getTopLayout(); |
||||
if(topLayout != null){ |
||||
if (topLayout.isEditable()){ |
||||
return topLayoutAccept(creator, x, y); |
||||
} |
||||
//绝对布局嵌套,处于内层,不可编辑,不添加,topLayout只能获取到最外层可编辑的布局
|
||||
else if (((XLayoutContainer)topLayout.getParent()).acceptType(XWAbsoluteLayout.class)) { |
||||
return false; |
||||
} else { |
||||
return acceptWidget(x, y); |
||||
} |
||||
} else { |
||||
FRLogger.getLogger().error("top layout is null!"); |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
//topLayout假如可以编辑的话就往里面添加组件
|
||||
private boolean topLayoutAccept(XCreator creator, int x, int y) { |
||||
//允许组件重叠,可以不判断有没有和当前控件重叠
|
||||
//先计算当前控件的位置
|
||||
int creatorX, creatorY; |
||||
if (XCreatorUtils.getParentXLayoutContainer(creator) != null) { |
||||
Rectangle creatorRectangle = ComponentUtils.getRelativeBounds(creator); |
||||
creatorX = creatorRectangle.x; |
||||
creatorY = creatorRectangle.y; |
||||
} else { |
||||
//这边计算得到的组件其实位置是正确的,
|
||||
//因为传入的x和y已经加上了宽度或者高度的一半,再减去相同的宽度和高度的一半是没区别的,
|
||||
// 例如高度为21,那么就是+10-10;
|
||||
// 高度为20,那么就是+10-10; 没区别
|
||||
int w = creator.getWidth() / 2; |
||||
int h = creator.getHeight() / 2; |
||||
creatorX = x - w; |
||||
creatorY = y - h; |
||||
} |
||||
if (creatorX < 0 || creatorX + creator.getWidth() > container.getWidth()) { |
||||
return false; |
||||
} |
||||
if (creatorY < 0 || creatorY + creator.getHeight() > container.getHeight()){ |
||||
return false; |
||||
} |
||||
return x >= 0 && y >= 0 && creator.getHeight() <= container.getHeight() |
||||
&& creator.getWidth() <= container.getWidth(); |
||||
} |
||||
|
||||
/** |
||||
* 判断是否鼠标在组件的三等分区域,如果组件在布局管理器中间,上下左右都可能会三等分 |
||||
* @param parentComp 鼠标所在区域的组件 |
||||
* @param x 坐标x |
||||
* @param y 坐标y |
||||
* @return 是则返回true |
||||
*/ |
||||
public boolean isTrisectionArea(Component parentComp, int x, int y) { |
||||
XCreator creator = (XCreator)parentComp; |
||||
trisectAreaDirect = 0; |
||||
if (container.getComponentCount()<=1) { |
||||
return false; |
||||
} |
||||
int maxWidth = parentComp.getWidth(); |
||||
int maxHeight = parentComp.getHeight(); |
||||
int xL = parentComp.getX(); |
||||
int yL = parentComp.getY(); |
||||
// 组件宽高的十分之一和默认值取大
|
||||
int minRangeWidth = Math.max(maxWidth/BORDER_PROPORTION, DEFAULT_AREA_LENGTH); |
||||
int minRangeHeight = Math.max(maxHeight/BORDER_PROPORTION, DEFAULT_AREA_LENGTH); |
||||
if(y<yL+minRangeHeight ) { |
||||
// 在组件上侧三等分
|
||||
trisectAreaDirect = COMP_TOP; |
||||
} else if(y>yL+maxHeight-minRangeHeight) { |
||||
// 在组件下侧三等分
|
||||
trisectAreaDirect = COMP_BOTTOM; |
||||
} else if (x<xL+minRangeWidth) { |
||||
// 在组件左侧三等分
|
||||
trisectAreaDirect = COMP_LEFT; |
||||
} else if(x>xL+maxWidth-minRangeWidth) { |
||||
// 在组件右侧三等分
|
||||
trisectAreaDirect = COMP_RIGHT; |
||||
} |
||||
// tab布局的边界特殊处理,不进行三等分
|
||||
if(!creator.getTargetChildrenList().isEmpty()){ |
||||
return false; |
||||
} |
||||
|
||||
return !ComparatorUtils.equals(trisectAreaDirect, 0); |
||||
} |
||||
|
||||
//当前绝对布局不可编辑,就当成一个控件,组件添加在周围
|
||||
private boolean acceptWidget(int x, int y) { |
||||
isFindRelatedComps = false; |
||||
//拖入组件判断时,先判断是否为交叉点区域,其次三等分区域,再次平分区域
|
||||
Component comp = container.getComponentAt(x, y); |
||||
//如果当前处于边缘地带, 那么就把他贴到父容器上
|
||||
XLayoutContainer parent = container.findNearestFit(); |
||||
container = parent != null ? parent : container; |
||||
isAdd2ParentLayout = true; |
||||
|
||||
int componentHeight = comp.getHeight(); |
||||
int componentWidth = comp.getWidth(); |
||||
//上半部分高度
|
||||
int upHeight = (int) (componentHeight * TOP_HALF) + comp.getY(); |
||||
//下半部分高度
|
||||
int downHeight = (int) (componentHeight * BOTTOM_HALF) + comp.getY(); |
||||
|
||||
if (isCrossPointArea(comp, x, y)) { |
||||
return canAcceptWhileCrossPoint(comp, x, y); |
||||
} |
||||
|
||||
if (isTrisectionArea(comp, x, y)) { |
||||
return canAcceptWhileTrisection(comp, x, y); |
||||
} |
||||
|
||||
boolean horizonValid = componentWidth >= minWidth * 2 + actualVal; |
||||
boolean verticalValid = componentHeight >= minHeight * 2 + actualVal; |
||||
return y > upHeight && y < downHeight ? horizonValid : verticalValid; |
||||
} |
||||
|
||||
/** |
||||
* 组件的ComponentAdapter在添加组件时,如果发现布局管理器不为空,会继而调用该布局管理器的 |
||||
* addComp方法来完成组件的具体添加。在该方法内,布局管理器可以提供额外的功能。 |
||||
* |
||||
* @param creator 被添加的新组件 |
||||
* @param x 添加的位置x,该位置是相对于container的 |
||||
* @param y 添加的位置y,该位置是相对于container的 |
||||
* @return 是否添加成功,成功返回true,否则false |
||||
*/ |
||||
@Override |
||||
public boolean addBean(XCreator creator, int x, int y) { |
||||
Rectangle rect = ComponentUtils.getRelativeBounds(container); |
||||
|
||||
int posX = x + rect.x; |
||||
int posY = y + rect.y; |
||||
if (!accept(creator, x, y)) { |
||||
return false; |
||||
} |
||||
addComp(creator, posX, posY); |
||||
((XWidgetCreator) creator).recalculateChildrenSize(); |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
protected void addComp(XCreator creator, int x, int y) { |
||||
if(!isAdd2ParentLayout) { |
||||
Rectangle r = ComponentUtils.getRelativeBounds(container); |
||||
x = x - r.x; |
||||
y = y - r.y; |
||||
if (XCreatorUtils.getParentXLayoutContainer(creator) != null) { |
||||
|
||||
Rectangle creatorRectangle = ComponentUtils.getRelativeBounds(creator); |
||||
x = creatorRectangle.x - r.x; |
||||
y = creatorRectangle.y - r.y; |
||||
} else { |
||||
int w = creator.getWidth() / 2; |
||||
int h = creator.getHeight() / 2; |
||||
x = x - w; |
||||
y = y - h; |
||||
} |
||||
fix(creator, x, y); |
||||
|
||||
if (creator.hasTitleStyle()) { |
||||
addParentCreator(creator); |
||||
} else { |
||||
container.add(creator, creator.toData().getWidgetName(),0); |
||||
} |
||||
XWAbsoluteLayout layout = (XWAbsoluteLayout) container; |
||||
layout.updateBoundsWidget(creator); |
||||
updateCreatorBackBound(); |
||||
LayoutUtils.layoutRootContainer(container); |
||||
}else{ |
||||
fixAbsolute(creator, x, y); |
||||
if (creator.shouldScaleCreator() || creator.hasTitleStyle()) { |
||||
addParentCreator(creator); |
||||
} else { |
||||
container.add(creator, creator.toData().getWidgetName(),0); |
||||
} |
||||
XWFitLayout layout = (XWFitLayout) container; |
||||
// 更新对应的BoundsWidget
|
||||
layout.updateBoundsWidget(); |
||||
updateCreatorBackBound(); |
||||
} |
||||
} |
||||
|
||||
private void updateCreatorBackBound() { |
||||
for (int i=0,size=container.getComponentCount(); i<size; i++) { |
||||
XCreator creator = (XCreator) container.getComponent(i); |
||||
creator.updateChildBound(minHeight); |
||||
creator.setBackupBound(creator.getBounds()); |
||||
} |
||||
} |
||||
|
||||
private void addParentCreator(XCreator child) { |
||||
XLayoutContainer parentPanel = child.initCreatorWrapper(child.getHeight()); |
||||
container.add(parentPanel, child.toData().getWidgetName(),0); |
||||
} |
||||
|
||||
/** |
||||
* 新拖入组件时,计算调整其他关联组件位置大小 |
||||
* @param child 新拖入的组件 |
||||
* @param x 鼠标所在x坐标 |
||||
* @param y 鼠标所在y坐标 |
||||
*/ |
||||
private void fixAbsolute(XCreator child, int x, int y) { |
||||
Component parentComp = container.getComponentAt(x, y); |
||||
if (container.getComponentCount()==0){ |
||||
child.setLocation(0, 0); |
||||
child.setSize(parentComp.getWidth(), parentComp.getHeight()); |
||||
} else if(isCrossPointArea(parentComp, x, y)){ |
||||
//交叉区域插入组件时,根据具体位置进行上下或者左右或者相邻三个组件的位置大小插入
|
||||
fixCrossPointArea(parentComp, child, x, y); |
||||
return; |
||||
} else if (isTrisectionArea(parentComp, x, y)) { |
||||
// 在边界三等分区域,就不再和组件二等分了
|
||||
fixTrisect(parentComp, child, x, y); |
||||
return; |
||||
} else{ |
||||
fixHalve(parentComp, child, x, y); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 组件拖拽后调整大小 |
||||
* @param creator 组件 |
||||
*/ |
||||
@Override |
||||
public void fix(XCreator creator) { |
||||
WAbsoluteLayout wabs = (WAbsoluteLayout)container.toData(); |
||||
fix(creator,creator.getX(),creator.getY()); |
||||
wabs.setBounds(creator.toData(),creator.getBounds()); |
||||
|
||||
XWAbsoluteLayout layout = (XWAbsoluteLayout) container; |
||||
layout.updateBoundsWidget(creator); |
||||
} |
||||
|
||||
/** |
||||
* 调整组件大小到合适尺寸位置 |
||||
* @param creator 组件 |
||||
* @param x 坐标x |
||||
* @param y 坐标y |
||||
*/ |
||||
public void fix(XCreator creator ,int x, int y) { |
||||
int height = creator.getHeight(); |
||||
int width = creator.getWidth(); |
||||
if (x < 0) { |
||||
x = container.getX(); |
||||
} else if (x + creator.getWidth() > container.getWidth()) { |
||||
x = container.getWidth() - width; |
||||
} |
||||
|
||||
if (y < 0) { |
||||
y = container.getY(); |
||||
} else if (y + creator.getHeight() > container.getHeight()) { |
||||
y = container.getHeight() - height; |
||||
} |
||||
|
||||
creator.setBounds(x, y, width, height); |
||||
} |
||||
|
||||
@Override |
||||
public ConstraintsGroupModel getLayoutConstraints(XCreator creator) { |
||||
return new BoundsGroupModel((XWAbsoluteLayout)container, creator); |
||||
} |
||||
|
||||
@Override |
||||
public GroupModel getLayoutProperties() { |
||||
XWAbsoluteLayout xwAbsoluteLayout = (XWAbsoluteLayout) container; |
||||
return new FRAbsoluteLayoutPropertiesGroupModel(xwAbsoluteLayout); |
||||
} |
||||
} |
@ -1,141 +1,141 @@
|
||||
package com.fr.design.designer.beans.models; |
||||
|
||||
import java.awt.Rectangle; |
||||
|
||||
import com.fr.design.designer.creator.XWAbsoluteLayout; |
||||
import com.fr.design.mainframe.FormDesigner; |
||||
import com.fr.design.designer.beans.AdapterBus; |
||||
import com.fr.design.designer.beans.ComponentAdapter; |
||||
import com.fr.design.designer.beans.adapters.component.CompositeComponentAdapter; |
||||
import com.fr.design.designer.creator.XCreator; |
||||
import com.fr.design.designer.creator.XLayoutContainer; |
||||
import com.fr.design.designer.creator.XWParameterLayout; |
||||
import com.fr.design.utils.ComponentUtils; |
||||
import com.fr.general.ComparatorUtils; |
||||
|
||||
/** |
||||
* 添加状态下的model |
||||
*/ |
||||
public class AddingModel { |
||||
|
||||
// 当前要添加的组件
|
||||
private XCreator creator; |
||||
// 记录当前鼠标的位置信息
|
||||
private int currentX; |
||||
private int currentY; |
||||
private boolean added; |
||||
|
||||
public AddingModel(FormDesigner designer, XCreator xCreator) { |
||||
String creatorName = getXCreatorName(designer, xCreator); |
||||
this.creator = xCreator; |
||||
instantiateCreator(designer, creatorName); |
||||
// 初始的时候隐藏该组件的图标
|
||||
currentY = -this.creator.getWidth(); |
||||
currentX = -this.creator.getHeight(); |
||||
} |
||||
|
||||
/** |
||||
* 待说明 |
||||
* |
||||
* @param designer 设计器 |
||||
* @param creatorName 组件名 |
||||
*/ |
||||
public void instantiateCreator(FormDesigner designer, String creatorName) { |
||||
creator.toData().setWidgetName(creatorName); |
||||
ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator); |
||||
adapter.initialize(); |
||||
creator.addNotify(); |
||||
creator.putClientProperty(AdapterBus.CLIENT_PROPERTIES, adapter); |
||||
} |
||||
|
||||
public AddingModel(XCreator xCreator, int x, int y) { |
||||
this.creator = xCreator; |
||||
this.creator.backupCurrentSize(); |
||||
this.creator.backupParent(); |
||||
this.creator.setSize(xCreator.initEditorSize()); |
||||
currentX = x - (xCreator.getWidth() / 2); |
||||
currentY = y - (xCreator.getHeight() / 2); |
||||
} |
||||
|
||||
/** |
||||
* 隐藏当前组件的图标 |
||||
*/ |
||||
public void reset() { |
||||
currentX = -this.creator.getWidth(); |
||||
currentY = -this.creator.getHeight(); |
||||
} |
||||
|
||||
public String getXCreatorName(FormDesigner designer, XCreator x) { |
||||
String def = x.createDefaultName(); |
||||
if (x.acceptType(XWParameterLayout.class)) { |
||||
return def; |
||||
} |
||||
int i = 0; |
||||
while (designer.getTarget().isNameExist(def + i)) { |
||||
i++; |
||||
} |
||||
return def + i; |
||||
} |
||||
|
||||
public int getCurrentX() { |
||||
return currentX; |
||||
} |
||||
|
||||
public int getCurrentY() { |
||||
return currentY; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 移动组件图标到鼠标事件发生的位置 |
||||
* |
||||
* @param x 坐标 |
||||
* @param y 坐标 |
||||
*/ |
||||
public void moveTo(int x, int y) { |
||||
currentX = x - (this.creator.getWidth() / 2); |
||||
currentY = y - (this.creator.getHeight() / 2); |
||||
} |
||||
|
||||
public XCreator getXCreator() { |
||||
return this.creator; |
||||
} |
||||
|
||||
/** |
||||
* 当前组件是否已经添加到某个容器中 |
||||
* |
||||
* @return 是返回true |
||||
*/ |
||||
public boolean isCreatorAdded() { |
||||
return added; |
||||
} |
||||
|
||||
/** |
||||
* 加入容器 |
||||
* |
||||
* @param designer 设计器 |
||||
* @param container 容器 |
||||
* @param x 坐标 |
||||
* @param y 坐标 |
||||
* @return 成功返回true |
||||
*/ |
||||
public boolean add2Container(FormDesigner designer, XLayoutContainer container, int x, int y) { |
||||
//考虑不同布局嵌套的情况,获取顶层容器
|
||||
XLayoutContainer xLayoutContainer = container.getTopLayout(); |
||||
if (xLayoutContainer != null && xLayoutContainer.acceptType(XWAbsoluteLayout.class)) { |
||||
container = xLayoutContainer; |
||||
} |
||||
|
||||
Rectangle rect = ComponentUtils.getRelativeBounds(container); |
||||
if (!ComparatorUtils.equals(container.getOuterLayout(), container.getBackupParent())) { |
||||
added = container.getLayoutAdapter().addBean(creator, |
||||
x + designer.getArea().getHorizontalValue(), |
||||
y + designer.getArea().getVerticalValue()); |
||||
return added; |
||||
} |
||||
added = container.getLayoutAdapter().addBean(creator, |
||||
x + designer.getArea().getHorizontalValue() - rect.x, |
||||
y + designer.getArea().getVerticalValue() - rect.y); |
||||
return added; |
||||
} |
||||
package com.fr.design.designer.beans.models; |
||||
|
||||
import java.awt.Rectangle; |
||||
|
||||
import com.fr.design.designer.creator.XWAbsoluteLayout; |
||||
import com.fr.design.mainframe.FormDesigner; |
||||
import com.fr.design.designer.beans.AdapterBus; |
||||
import com.fr.design.designer.beans.ComponentAdapter; |
||||
import com.fr.design.designer.beans.adapters.component.CompositeComponentAdapter; |
||||
import com.fr.design.designer.creator.XCreator; |
||||
import com.fr.design.designer.creator.XLayoutContainer; |
||||
import com.fr.design.designer.creator.XWParameterLayout; |
||||
import com.fr.design.utils.ComponentUtils; |
||||
import com.fr.general.ComparatorUtils; |
||||
|
||||
/** |
||||
* 添加状态下的model |
||||
*/ |
||||
public class AddingModel { |
||||
|
||||
// 当前要添加的组件
|
||||
private XCreator creator; |
||||
// 记录当前鼠标的位置信息
|
||||
private int currentX; |
||||
private int currentY; |
||||
private boolean added; |
||||
|
||||
public AddingModel(FormDesigner designer, XCreator xCreator) { |
||||
String creatorName = getXCreatorName(designer, xCreator); |
||||
this.creator = xCreator; |
||||
instantiateCreator(designer, creatorName); |
||||
// 初始的时候隐藏该组件的图标
|
||||
currentY = -this.creator.getWidth(); |
||||
currentX = -this.creator.getHeight(); |
||||
} |
||||
|
||||
/** |
||||
* 待说明 |
||||
* |
||||
* @param designer 设计器 |
||||
* @param creatorName 组件名 |
||||
*/ |
||||
public void instantiateCreator(FormDesigner designer, String creatorName) { |
||||
creator.toData().setWidgetName(creatorName); |
||||
ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator); |
||||
adapter.initialize(); |
||||
creator.addNotify(); |
||||
creator.putClientProperty(AdapterBus.CLIENT_PROPERTIES, adapter); |
||||
} |
||||
|
||||
public AddingModel(XCreator xCreator, int x, int y) { |
||||
this.creator = xCreator; |
||||
this.creator.backupCurrentSize(); |
||||
this.creator.backupParent(); |
||||
this.creator.setSize(xCreator.initEditorSize()); |
||||
currentX = x - (xCreator.getWidth() / 2); |
||||
currentY = y - (xCreator.getHeight() / 2); |
||||
} |
||||
|
||||
/** |
||||
* 隐藏当前组件的图标 |
||||
*/ |
||||
public void reset() { |
||||
currentX = -this.creator.getWidth(); |
||||
currentY = -this.creator.getHeight(); |
||||
} |
||||
|
||||
public String getXCreatorName(FormDesigner designer, XCreator x) { |
||||
String def = x.createDefaultName(); |
||||
if (x.acceptType(XWParameterLayout.class)) { |
||||
return def; |
||||
} |
||||
int i = 0; |
||||
while (designer.getTarget().isNameExist(def + i)) { |
||||
i++; |
||||
} |
||||
return def + i; |
||||
} |
||||
|
||||
public int getCurrentX() { |
||||
return currentX; |
||||
} |
||||
|
||||
public int getCurrentY() { |
||||
return currentY; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 移动组件图标到鼠标事件发生的位置 |
||||
* |
||||
* @param x 坐标 |
||||
* @param y 坐标 |
||||
*/ |
||||
public void moveTo(int x, int y) { |
||||
currentX = x - (this.creator.getWidth() / 2); |
||||
currentY = y - (this.creator.getHeight() / 2); |
||||
} |
||||
|
||||
public XCreator getXCreator() { |
||||
return this.creator; |
||||
} |
||||
|
||||
/** |
||||
* 当前组件是否已经添加到某个容器中 |
||||
* |
||||
* @return 是返回true |
||||
*/ |
||||
public boolean isCreatorAdded() { |
||||
return added; |
||||
} |
||||
|
||||
/** |
||||
* 加入容器 |
||||
* |
||||
* @param designer 设计器 |
||||
* @param container 容器 |
||||
* @param x 坐标 |
||||
* @param y 坐标 |
||||
* @return 成功返回true |
||||
*/ |
||||
public boolean add2Container(FormDesigner designer, XLayoutContainer container, int x, int y) { |
||||
//考虑不同布局嵌套的情况,获取顶层容器
|
||||
XLayoutContainer xLayoutContainer = container.getTopLayout(); |
||||
if (xLayoutContainer != null && xLayoutContainer.acceptType(XWAbsoluteLayout.class)) { |
||||
container = xLayoutContainer; |
||||
} |
||||
|
||||
Rectangle rect = ComponentUtils.getRelativeBounds(container); |
||||
if (!ComparatorUtils.equals(container.getOuterLayout(), container.getBackupParent())) { |
||||
added = container.getLayoutAdapter().addBean(creator, |
||||
x + designer.getArea().getHorizontalValue(), |
||||
y + designer.getArea().getVerticalValue()); |
||||
return added; |
||||
} |
||||
added = container.getLayoutAdapter().addBean(creator, |
||||
x + designer.getArea().getHorizontalValue() - rect.x, |
||||
y + designer.getArea().getVerticalValue() - rect.y); |
||||
return added; |
||||
} |
||||
} |
@ -1,476 +1,476 @@
|
||||
package com.fr.design.designer.beans.models; |
||||
|
||||
import com.fr.design.beans.location.Absorptionline; |
||||
import com.fr.design.designer.beans.AdapterBus; |
||||
import com.fr.design.designer.beans.HoverPainter; |
||||
import com.fr.design.designer.beans.LayoutAdapter; |
||||
import com.fr.design.designer.beans.events.DesignerEvent; |
||||
import com.fr.design.designer.beans.location.Direction; |
||||
import com.fr.design.designer.beans.location.Location; |
||||
import com.fr.design.designer.creator.*; |
||||
import com.fr.design.mainframe.FormDesigner; |
||||
import com.fr.design.mainframe.FormSelectionUtils; |
||||
import com.fr.design.utils.ComponentUtils; |
||||
|
||||
import java.awt.*; |
||||
import java.awt.event.MouseEvent; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* 普通模式下的状态model |
||||
*/ |
||||
public class StateModel { |
||||
// 对应的selection model
|
||||
|
||||
private SelectionModel selectionModel; |
||||
// 当前鼠标进入拖拽区域的位置类型
|
||||
private Direction driection; |
||||
|
||||
// 当前拖拽的起始位置
|
||||
private int currentX; |
||||
private int currentY; |
||||
|
||||
//拖拽组件原始位置大小备份
|
||||
private Rectangle selectedPositionBackup; |
||||
|
||||
private Point startPoint = new Point(); |
||||
private Point currentPoint = new Point(); |
||||
|
||||
private Absorptionline lineInX; |
||||
private Absorptionline lineInY; |
||||
//等距线
|
||||
private Absorptionline lineEquidistant; |
||||
|
||||
// 当前是否处于拖拽选择状态
|
||||
private boolean selecting; |
||||
private boolean dragging; |
||||
|
||||
private boolean addable; |
||||
|
||||
private FormDesigner designer; |
||||
|
||||
public StateModel(FormDesigner designer) { |
||||
this.designer = designer; |
||||
selectionModel = designer.getSelectionModel(); |
||||
} |
||||
|
||||
/** |
||||
* 返回direction |
||||
* |
||||
* @return direction方向 |
||||
*/ |
||||
public Direction getDirection() { |
||||
return driection; |
||||
} |
||||
|
||||
/** |
||||
* 是否有组件正被选中 |
||||
* |
||||
* @return true 如果至少一个组件被选中 |
||||
*/ |
||||
public boolean isSelecting() { |
||||
return selecting; |
||||
} |
||||
|
||||
/** |
||||
* 是否能拖拽 |
||||
* |
||||
* @return 非outer且选中为空 |
||||
*/ |
||||
public boolean dragable() { |
||||
return ((driection != Location.outer) && !selecting); |
||||
} |
||||
|
||||
/** |
||||
* 拖拽中是否可以转换为添加模式: |
||||
* 如果拖拽组件只有一个,鼠标当前所在位置的最底层表单容器与这个组件的容器不同; |
||||
* 如果拖拽组件为多个,鼠标当前所在位置的最底层表单容器除了要求要跟这些组件的容器不同外,还必须是绝对定位布局 |
||||
*/ |
||||
private void checkAddable(MouseEvent e) { |
||||
addable = false; |
||||
designer.setPainter(null); |
||||
|
||||
if (driection != Location.inner) { |
||||
return; |
||||
} |
||||
|
||||
XCreator comp = designer.getComponentAt(e.getX(), e.getY(), selectionModel.getSelection().getSelectedCreators()); |
||||
XLayoutContainer container = XCreatorUtils.getHotspotContainer(comp); |
||||
XCreator creator = selectionModel.getSelection().getSelectedCreator(); |
||||
Component creatorContainer = XCreatorUtils.getParentXLayoutContainer(creator); |
||||
if (creatorContainer != null && creatorContainer != container |
||||
&& (selectionModel.getSelection().size() == 1 || container instanceof XWAbsoluteLayout)) { |
||||
HoverPainter painter = AdapterBus.getContainerPainter(designer, container); |
||||
designer.setPainter(painter); |
||||
if (painter != null) { |
||||
Rectangle rect = ComponentUtils.getRelativeBounds(container); |
||||
rect.x -= designer.getArea().getHorizontalValue(); |
||||
rect.y -= designer.getArea().getVerticalValue(); |
||||
painter.setRenderingBounds(rect); |
||||
painter.setHotspot(new Point(e.getX(), e.getY())); |
||||
painter.setCreator(creator); |
||||
} |
||||
addable = true; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param container 容器 |
||||
* @param mouseX 鼠标释放位置X |
||||
* @param mouseY 鼠标释放位置Y |
||||
* @return 是否成功 |
||||
*/ |
||||
private boolean addBean(XLayoutContainer container, int mouseX, int mouseY) { |
||||
LayoutAdapter adapter = container.getLayoutAdapter(); |
||||
Rectangle rectangleContainer = ComponentUtils.getRelativeBounds(container); |
||||
if (selectionModel.getSelection().size() == 1) { |
||||
return adapter.addBean(selectionModel.getSelection().getSelectedCreator(), |
||||
mouseX + designer.getArea().getHorizontalValue() - rectangleContainer.x, |
||||
mouseY + designer.getArea().getVerticalValue() - rectangleContainer.y); |
||||
} |
||||
for (XCreator creator : selectionModel.getSelection().getSelectedCreators()) { |
||||
adapter.addBean(creator, |
||||
mouseX + designer.getArea().getHorizontalValue() - rectangleContainer.x, |
||||
mouseY + designer.getArea().getVerticalValue() - rectangleContainer.y); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* @param mouseReleasedX 鼠标释放位置X |
||||
* @param mouseReleasedY 鼠标释放位置Y |
||||
*/ |
||||
private void adding(int mouseReleasedX, int mouseReleasedY) { |
||||
// 当前鼠标所在的组件
|
||||
XCreator hoveredComponent = designer.getComponentAt(mouseReleasedX, mouseReleasedY, selectionModel.getSelection().getSelectedCreators()); |
||||
|
||||
// 获取该组件所在的焦点容器
|
||||
XLayoutContainer container = XCreatorUtils.getHotspotContainer(hoveredComponent); |
||||
|
||||
boolean success = false; |
||||
|
||||
if (container != null) { |
||||
// 如果是容器,则调用其acceptComponent接受组件
|
||||
success = addBean(container, mouseReleasedX, mouseReleasedY); |
||||
} |
||||
|
||||
if (success) { |
||||
FormSelectionUtils.rebuildSelection(designer); |
||||
designer.getEditListenerTable().fireCreatorModified( |
||||
selectionModel.getSelection().getSelectedCreator(), DesignerEvent.CREATOR_ADDED); |
||||
} else { |
||||
selectionModel.getSelection().setSelectionBounds(selectedPositionBackup, designer); |
||||
Toolkit.getDefaultToolkit().beep(); |
||||
} |
||||
// 取消提示
|
||||
designer.setPainter(null); |
||||
} |
||||
|
||||
/** |
||||
* 是否拖拽 |
||||
* |
||||
* @return dragging状态 |
||||
*/ |
||||
public boolean isDragging() { |
||||
return dragging; |
||||
} |
||||
|
||||
/** |
||||
* 是否可以开始画线 |
||||
* |
||||
* @return startPoint不为空返回true |
||||
*/ |
||||
public boolean prepareForDrawLining() { |
||||
return startPoint != null; |
||||
} |
||||
|
||||
/** |
||||
* 设置开始位置 |
||||
* |
||||
* @param p point位置 |
||||
*/ |
||||
public void setStartPoint(Point p) { |
||||
this.startPoint = p; |
||||
} |
||||
|
||||
/** |
||||
* 返回开始位置 |
||||
* |
||||
* @return 点位置 |
||||
*/ |
||||
public Point getStartPoint() { |
||||
return startPoint; |
||||
} |
||||
|
||||
/** |
||||
* 返回当前点位置 |
||||
* |
||||
* @return 点位置 |
||||
*/ |
||||
public Point getEndPoint() { |
||||
return currentPoint; |
||||
} |
||||
|
||||
/** |
||||
* 当前选中组件 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void startSelecting(MouseEvent e) { |
||||
selecting = true; |
||||
selectionModel.setHotspotBounds(new Rectangle()); |
||||
currentX = getMouseXY(e).x; |
||||
currentY = getMouseXY(e).y; |
||||
} |
||||
|
||||
/** |
||||
* 当前鼠标的xy |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void startResizing(MouseEvent e) { |
||||
if (!selectionModel.getSelection().isEmpty()) { |
||||
driection.backupBounds(designer); |
||||
} |
||||
currentX = getMouseXY(e).x; |
||||
currentY = getMouseXY(e).y; |
||||
} |
||||
|
||||
/** |
||||
* 起始点开始DrawLine |
||||
* |
||||
* @param p 点位置 |
||||
*/ |
||||
public void startDrawLine(Point p) { |
||||
this.startPoint = p; |
||||
if (p != null) { |
||||
try { |
||||
designer.setCursor(XConnector.connectorCursor); |
||||
} catch (Exception e) { |
||||
} |
||||
} else { |
||||
designer.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 鼠标释放时所在的区域及圈中的组件 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void selectCreators(MouseEvent e) { |
||||
int x = getMouseXY(e).x; |
||||
int y = getMouseXY(e).y; |
||||
|
||||
Rectangle bounds = createCurrentBounds(x, y); |
||||
|
||||
if ((x != currentX) || (y != currentY)) { |
||||
selectionModel.setSelectedCreators(getHotspotCreators(bounds, designer.getRootComponent())); |
||||
} |
||||
selectionModel.setHotspotBounds(null); |
||||
} |
||||
|
||||
/** |
||||
* 画所在区域线 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void drawLine(MouseEvent e) { |
||||
designer.getDrawLineHelper().setDrawLine(true); |
||||
Point p = designer.getDrawLineHelper().getNearWidgetPoint(e); |
||||
if (p != null) { |
||||
currentPoint = p; |
||||
} else { |
||||
currentPoint.x = e.getX() + designer.getArea().getHorizontalValue(); |
||||
currentPoint.y = e.getY() + designer.getArea().getVerticalValue(); |
||||
} |
||||
} |
||||
|
||||
private Rectangle createCurrentBounds(int x, int y) { |
||||
Rectangle bounds = new Rectangle(); |
||||
|
||||
bounds.x = Math.min(x, currentX); |
||||
bounds.y = Math.min(y, currentY); |
||||
bounds.width = Math.max(x, currentX) - bounds.x; |
||||
bounds.height = Math.max(y, currentY) - bounds.y; |
||||
|
||||
return bounds; |
||||
} |
||||
|
||||
private ArrayList<XCreator> getHotspotCreators(Rectangle selection, XCreator root) { |
||||
ArrayList<XCreator> creators = new ArrayList<>(); |
||||
|
||||
if (!root.isVisible() && !designer.isRoot(root)) { |
||||
return creators; |
||||
} |
||||
|
||||
if (root instanceof XLayoutContainer) { |
||||
XLayoutContainer container = (XLayoutContainer) root; |
||||
int count = container.getXCreatorCount(); |
||||
Rectangle clipped = new Rectangle(selection); |
||||
|
||||
for (int i = count - 1; i >= 0; i--) { |
||||
XCreator child = container.getXCreator(i); |
||||
|
||||
if (selection.contains(child.getBounds())) { |
||||
creators.add(child); |
||||
} else { |
||||
clipped.x = selection.x - child.getX(); |
||||
clipped.y = selection.y - child.getY(); |
||||
creators.addAll(getHotspotCreators(clipped, child)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return creators; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 重置model |
||||
*/ |
||||
public void resetModel() { |
||||
dragging = false; |
||||
selecting = false; |
||||
} |
||||
|
||||
/** |
||||
* 重置 |
||||
*/ |
||||
public void reset() { |
||||
driection = Location.outer; |
||||
dragging = false; |
||||
selecting = false; |
||||
} |
||||
|
||||
/** |
||||
* 取消拖拽 |
||||
*/ |
||||
public void draggingCancel() { |
||||
designer.repaint(); |
||||
reset(); |
||||
} |
||||
|
||||
/** |
||||
* 设置可拉伸方向 |
||||
* |
||||
* @param dir 拉伸方向 |
||||
*/ |
||||
public void setDirection(Direction dir) { |
||||
if (driection != dir) { |
||||
this.driection = dir; |
||||
driection.updateCursor(designer); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* x吸附线赋值 |
||||
* |
||||
* @param line 线 |
||||
*/ |
||||
public void setXAbsorptionline(Absorptionline line) { |
||||
this.lineInX = line; |
||||
} |
||||
|
||||
/** |
||||
* y吸附线赋值 |
||||
* |
||||
* @param line 线 |
||||
*/ |
||||
public void setYAbsorptionline(Absorptionline line) { |
||||
this.lineInY = line; |
||||
} |
||||
|
||||
/** |
||||
* 等距线赋值 |
||||
* |
||||
* @param line 线 |
||||
*/ |
||||
public void setEquidistantLine(Absorptionline line) { |
||||
this.lineEquidistant = line; |
||||
} |
||||
|
||||
/** |
||||
* 画吸附线 |
||||
* |
||||
* @param g Graphics类 |
||||
*/ |
||||
public void paintAbsorptionline(Graphics g) { |
||||
if (lineInX != null) { |
||||
lineInX.paint(g, designer.getArea()); |
||||
} |
||||
if (lineInY != null) { |
||||
lineInY.paint(g, designer.getArea()); |
||||
} |
||||
if (lineEquidistant != null) { |
||||
lineEquidistant.paint(g, designer.getArea()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 拖拽 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void dragging(MouseEvent e) { |
||||
//进入dragging状态时备份组件大小和位置
|
||||
if (!dragging) { |
||||
selectedPositionBackup = selectionModel.getSelection().getRelativeBounds(); |
||||
} |
||||
checkAddable(e); |
||||
setDependLinePainter(e); |
||||
driection.drag(getMouseXY(e).x - currentX, getMouseXY(e).y - currentY, designer); |
||||
this.dragging = true; |
||||
} |
||||
|
||||
// 拖拽时画依附线用到的painter
|
||||
private void setDependLinePainter(MouseEvent e) { |
||||
XCreator comp = designer.getComponentAt(e.getX(), e.getY(), selectionModel.getSelection().getSelectedCreators()); |
||||
XLayoutContainer container = XCreatorUtils.getHotspotContainer(comp); |
||||
XCreator creator = selectionModel.getSelection().getSelectedCreator(); |
||||
HoverPainter painter = AdapterBus.getContainerPainter(designer, container); |
||||
designer.setPainter(painter); |
||||
if (painter != null) { |
||||
painter.setHotspot(new Point(e.getX(), e.getY())); |
||||
painter.setCreator(creator); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 释放捕获 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void releaseDragging(MouseEvent e) { |
||||
this.dragging = false; |
||||
if (addable) { |
||||
adding(e.getX(), e.getY()); |
||||
} else if (!selectionModel.getSelection().isEmpty()) { |
||||
selectionModel.releaseDragging(); |
||||
} |
||||
designer.repaint(); |
||||
} |
||||
|
||||
/** |
||||
* 改变选择区域 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void changeSelection(MouseEvent e) { |
||||
Rectangle bounds = createCurrentBounds(getMouseXY(e).x, getMouseXY(e).y); |
||||
selectionModel.setHotspotBounds(bounds); |
||||
} |
||||
|
||||
/** |
||||
* 返回鼠标所在的x、y 考虑滚动条的值 |
||||
* |
||||
* @param e 鼠标事件 |
||||
* @return xy值 |
||||
*/ |
||||
public Point getMouseXY(MouseEvent e) { |
||||
Point p1 = new Point(e.getX() + designer.getArea().getHorizontalValue(), e.getY() |
||||
+ designer.getArea().getVerticalValue()); |
||||
return p1; |
||||
} |
||||
|
||||
package com.fr.design.designer.beans.models; |
||||
|
||||
import com.fr.design.beans.location.Absorptionline; |
||||
import com.fr.design.designer.beans.AdapterBus; |
||||
import com.fr.design.designer.beans.HoverPainter; |
||||
import com.fr.design.designer.beans.LayoutAdapter; |
||||
import com.fr.design.designer.beans.events.DesignerEvent; |
||||
import com.fr.design.designer.beans.location.Direction; |
||||
import com.fr.design.designer.beans.location.Location; |
||||
import com.fr.design.designer.creator.*; |
||||
import com.fr.design.mainframe.FormDesigner; |
||||
import com.fr.design.mainframe.FormSelectionUtils; |
||||
import com.fr.design.utils.ComponentUtils; |
||||
|
||||
import java.awt.*; |
||||
import java.awt.event.MouseEvent; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* 普通模式下的状态model |
||||
*/ |
||||
public class StateModel { |
||||
// 对应的selection model
|
||||
|
||||
private SelectionModel selectionModel; |
||||
// 当前鼠标进入拖拽区域的位置类型
|
||||
private Direction driection; |
||||
|
||||
// 当前拖拽的起始位置
|
||||
private int currentX; |
||||
private int currentY; |
||||
|
||||
//拖拽组件原始位置大小备份
|
||||
private Rectangle selectedPositionBackup; |
||||
|
||||
private Point startPoint = new Point(); |
||||
private Point currentPoint = new Point(); |
||||
|
||||
private Absorptionline lineInX; |
||||
private Absorptionline lineInY; |
||||
//等距线
|
||||
private Absorptionline lineEquidistant; |
||||
|
||||
// 当前是否处于拖拽选择状态
|
||||
private boolean selecting; |
||||
private boolean dragging; |
||||
|
||||
private boolean addable; |
||||
|
||||
private FormDesigner designer; |
||||
|
||||
public StateModel(FormDesigner designer) { |
||||
this.designer = designer; |
||||
selectionModel = designer.getSelectionModel(); |
||||
} |
||||
|
||||
/** |
||||
* 返回direction |
||||
* |
||||
* @return direction方向 |
||||
*/ |
||||
public Direction getDirection() { |
||||
return driection; |
||||
} |
||||
|
||||
/** |
||||
* 是否有组件正被选中 |
||||
* |
||||
* @return true 如果至少一个组件被选中 |
||||
*/ |
||||
public boolean isSelecting() { |
||||
return selecting; |
||||
} |
||||
|
||||
/** |
||||
* 是否能拖拽 |
||||
* |
||||
* @return 非outer且选中为空 |
||||
*/ |
||||
public boolean dragable() { |
||||
return ((driection != Location.outer) && !selecting); |
||||
} |
||||
|
||||
/** |
||||
* 拖拽中是否可以转换为添加模式: |
||||
* 如果拖拽组件只有一个,鼠标当前所在位置的最底层表单容器与这个组件的容器不同; |
||||
* 如果拖拽组件为多个,鼠标当前所在位置的最底层表单容器除了要求要跟这些组件的容器不同外,还必须是绝对定位布局 |
||||
*/ |
||||
private void checkAddable(MouseEvent e) { |
||||
addable = false; |
||||
designer.setPainter(null); |
||||
|
||||
if (driection != Location.inner) { |
||||
return; |
||||
} |
||||
|
||||
XCreator comp = designer.getComponentAt(e.getX(), e.getY(), selectionModel.getSelection().getSelectedCreators()); |
||||
XLayoutContainer container = XCreatorUtils.getHotspotContainer(comp); |
||||
XCreator creator = selectionModel.getSelection().getSelectedCreator(); |
||||
Component creatorContainer = XCreatorUtils.getParentXLayoutContainer(creator); |
||||
if (creatorContainer != null && creatorContainer != container |
||||
&& (selectionModel.getSelection().size() == 1 || container instanceof XWAbsoluteLayout)) { |
||||
HoverPainter painter = AdapterBus.getContainerPainter(designer, container); |
||||
designer.setPainter(painter); |
||||
if (painter != null) { |
||||
Rectangle rect = ComponentUtils.getRelativeBounds(container); |
||||
rect.x -= designer.getArea().getHorizontalValue(); |
||||
rect.y -= designer.getArea().getVerticalValue(); |
||||
painter.setRenderingBounds(rect); |
||||
painter.setHotspot(new Point(e.getX(), e.getY())); |
||||
painter.setCreator(creator); |
||||
} |
||||
addable = true; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param container 容器 |
||||
* @param mouseX 鼠标释放位置X |
||||
* @param mouseY 鼠标释放位置Y |
||||
* @return 是否成功 |
||||
*/ |
||||
private boolean addBean(XLayoutContainer container, int mouseX, int mouseY) { |
||||
LayoutAdapter adapter = container.getLayoutAdapter(); |
||||
Rectangle rectangleContainer = ComponentUtils.getRelativeBounds(container); |
||||
if (selectionModel.getSelection().size() == 1) { |
||||
return adapter.addBean(selectionModel.getSelection().getSelectedCreator(), |
||||
mouseX + designer.getArea().getHorizontalValue() - rectangleContainer.x, |
||||
mouseY + designer.getArea().getVerticalValue() - rectangleContainer.y); |
||||
} |
||||
for (XCreator creator : selectionModel.getSelection().getSelectedCreators()) { |
||||
adapter.addBean(creator, |
||||
mouseX + designer.getArea().getHorizontalValue() - rectangleContainer.x, |
||||
mouseY + designer.getArea().getVerticalValue() - rectangleContainer.y); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* @param mouseReleasedX 鼠标释放位置X |
||||
* @param mouseReleasedY 鼠标释放位置Y |
||||
*/ |
||||
private void adding(int mouseReleasedX, int mouseReleasedY) { |
||||
// 当前鼠标所在的组件
|
||||
XCreator hoveredComponent = designer.getComponentAt(mouseReleasedX, mouseReleasedY, selectionModel.getSelection().getSelectedCreators()); |
||||
|
||||
// 获取该组件所在的焦点容器
|
||||
XLayoutContainer container = XCreatorUtils.getHotspotContainer(hoveredComponent); |
||||
|
||||
boolean success = false; |
||||
|
||||
if (container != null) { |
||||
// 如果是容器,则调用其acceptComponent接受组件
|
||||
success = addBean(container, mouseReleasedX, mouseReleasedY); |
||||
} |
||||
|
||||
if (success) { |
||||
FormSelectionUtils.rebuildSelection(designer); |
||||
designer.getEditListenerTable().fireCreatorModified( |
||||
selectionModel.getSelection().getSelectedCreator(), DesignerEvent.CREATOR_ADDED); |
||||
} else { |
||||
selectionModel.getSelection().setSelectionBounds(selectedPositionBackup, designer); |
||||
Toolkit.getDefaultToolkit().beep(); |
||||
} |
||||
// 取消提示
|
||||
designer.setPainter(null); |
||||
} |
||||
|
||||
/** |
||||
* 是否拖拽 |
||||
* |
||||
* @return dragging状态 |
||||
*/ |
||||
public boolean isDragging() { |
||||
return dragging; |
||||
} |
||||
|
||||
/** |
||||
* 是否可以开始画线 |
||||
* |
||||
* @return startPoint不为空返回true |
||||
*/ |
||||
public boolean prepareForDrawLining() { |
||||
return startPoint != null; |
||||
} |
||||
|
||||
/** |
||||
* 设置开始位置 |
||||
* |
||||
* @param p point位置 |
||||
*/ |
||||
public void setStartPoint(Point p) { |
||||
this.startPoint = p; |
||||
} |
||||
|
||||
/** |
||||
* 返回开始位置 |
||||
* |
||||
* @return 点位置 |
||||
*/ |
||||
public Point getStartPoint() { |
||||
return startPoint; |
||||
} |
||||
|
||||
/** |
||||
* 返回当前点位置 |
||||
* |
||||
* @return 点位置 |
||||
*/ |
||||
public Point getEndPoint() { |
||||
return currentPoint; |
||||
} |
||||
|
||||
/** |
||||
* 当前选中组件 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void startSelecting(MouseEvent e) { |
||||
selecting = true; |
||||
selectionModel.setHotspotBounds(new Rectangle()); |
||||
currentX = getMouseXY(e).x; |
||||
currentY = getMouseXY(e).y; |
||||
} |
||||
|
||||
/** |
||||
* 当前鼠标的xy |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void startResizing(MouseEvent e) { |
||||
if (!selectionModel.getSelection().isEmpty()) { |
||||
driection.backupBounds(designer); |
||||
} |
||||
currentX = getMouseXY(e).x; |
||||
currentY = getMouseXY(e).y; |
||||
} |
||||
|
||||
/** |
||||
* 起始点开始DrawLine |
||||
* |
||||
* @param p 点位置 |
||||
*/ |
||||
public void startDrawLine(Point p) { |
||||
this.startPoint = p; |
||||
if (p != null) { |
||||
try { |
||||
designer.setCursor(XConnector.connectorCursor); |
||||
} catch (Exception e) { |
||||
} |
||||
} else { |
||||
designer.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 鼠标释放时所在的区域及圈中的组件 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void selectCreators(MouseEvent e) { |
||||
int x = getMouseXY(e).x; |
||||
int y = getMouseXY(e).y; |
||||
|
||||
Rectangle bounds = createCurrentBounds(x, y); |
||||
|
||||
if ((x != currentX) || (y != currentY)) { |
||||
selectionModel.setSelectedCreators(getHotspotCreators(bounds, designer.getRootComponent())); |
||||
} |
||||
selectionModel.setHotspotBounds(null); |
||||
} |
||||
|
||||
/** |
||||
* 画所在区域线 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void drawLine(MouseEvent e) { |
||||
designer.getDrawLineHelper().setDrawLine(true); |
||||
Point p = designer.getDrawLineHelper().getNearWidgetPoint(e); |
||||
if (p != null) { |
||||
currentPoint = p; |
||||
} else { |
||||
currentPoint.x = e.getX() + designer.getArea().getHorizontalValue(); |
||||
currentPoint.y = e.getY() + designer.getArea().getVerticalValue(); |
||||
} |
||||
} |
||||
|
||||
private Rectangle createCurrentBounds(int x, int y) { |
||||
Rectangle bounds = new Rectangle(); |
||||
|
||||
bounds.x = Math.min(x, currentX); |
||||
bounds.y = Math.min(y, currentY); |
||||
bounds.width = Math.max(x, currentX) - bounds.x; |
||||
bounds.height = Math.max(y, currentY) - bounds.y; |
||||
|
||||
return bounds; |
||||
} |
||||
|
||||
private ArrayList<XCreator> getHotspotCreators(Rectangle selection, XCreator root) { |
||||
ArrayList<XCreator> creators = new ArrayList<>(); |
||||
|
||||
if (!root.isVisible() && !designer.isRoot(root)) { |
||||
return creators; |
||||
} |
||||
|
||||
if (root instanceof XLayoutContainer) { |
||||
XLayoutContainer container = (XLayoutContainer) root; |
||||
int count = container.getXCreatorCount(); |
||||
Rectangle clipped = new Rectangle(selection); |
||||
|
||||
for (int i = count - 1; i >= 0; i--) { |
||||
XCreator child = container.getXCreator(i); |
||||
|
||||
if (selection.contains(child.getBounds())) { |
||||
creators.add(child); |
||||
} else { |
||||
clipped.x = selection.x - child.getX(); |
||||
clipped.y = selection.y - child.getY(); |
||||
creators.addAll(getHotspotCreators(clipped, child)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return creators; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 重置model |
||||
*/ |
||||
public void resetModel() { |
||||
dragging = false; |
||||
selecting = false; |
||||
} |
||||
|
||||
/** |
||||
* 重置 |
||||
*/ |
||||
public void reset() { |
||||
driection = Location.outer; |
||||
dragging = false; |
||||
selecting = false; |
||||
} |
||||
|
||||
/** |
||||
* 取消拖拽 |
||||
*/ |
||||
public void draggingCancel() { |
||||
designer.repaint(); |
||||
reset(); |
||||
} |
||||
|
||||
/** |
||||
* 设置可拉伸方向 |
||||
* |
||||
* @param dir 拉伸方向 |
||||
*/ |
||||
public void setDirection(Direction dir) { |
||||
if (driection != dir) { |
||||
this.driection = dir; |
||||
driection.updateCursor(designer); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* x吸附线赋值 |
||||
* |
||||
* @param line 线 |
||||
*/ |
||||
public void setXAbsorptionline(Absorptionline line) { |
||||
this.lineInX = line; |
||||
} |
||||
|
||||
/** |
||||
* y吸附线赋值 |
||||
* |
||||
* @param line 线 |
||||
*/ |
||||
public void setYAbsorptionline(Absorptionline line) { |
||||
this.lineInY = line; |
||||
} |
||||
|
||||
/** |
||||
* 等距线赋值 |
||||
* |
||||
* @param line 线 |
||||
*/ |
||||
public void setEquidistantLine(Absorptionline line) { |
||||
this.lineEquidistant = line; |
||||
} |
||||
|
||||
/** |
||||
* 画吸附线 |
||||
* |
||||
* @param g Graphics类 |
||||
*/ |
||||
public void paintAbsorptionline(Graphics g) { |
||||
if (lineInX != null) { |
||||
lineInX.paint(g, designer.getArea()); |
||||
} |
||||
if (lineInY != null) { |
||||
lineInY.paint(g, designer.getArea()); |
||||
} |
||||
if (lineEquidistant != null) { |
||||
lineEquidistant.paint(g, designer.getArea()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 拖拽 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void dragging(MouseEvent e) { |
||||
//进入dragging状态时备份组件大小和位置
|
||||
if (!dragging) { |
||||
selectedPositionBackup = selectionModel.getSelection().getRelativeBounds(); |
||||
} |
||||
checkAddable(e); |
||||
setDependLinePainter(e); |
||||
driection.drag(getMouseXY(e).x - currentX, getMouseXY(e).y - currentY, designer); |
||||
this.dragging = true; |
||||
} |
||||
|
||||
// 拖拽时画依附线用到的painter
|
||||
private void setDependLinePainter(MouseEvent e) { |
||||
XCreator comp = designer.getComponentAt(e.getX(), e.getY(), selectionModel.getSelection().getSelectedCreators()); |
||||
XLayoutContainer container = XCreatorUtils.getHotspotContainer(comp); |
||||
XCreator creator = selectionModel.getSelection().getSelectedCreator(); |
||||
HoverPainter painter = AdapterBus.getContainerPainter(designer, container); |
||||
designer.setPainter(painter); |
||||
if (painter != null) { |
||||
painter.setHotspot(new Point(e.getX(), e.getY())); |
||||
painter.setCreator(creator); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 释放捕获 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void releaseDragging(MouseEvent e) { |
||||
this.dragging = false; |
||||
if (addable) { |
||||
adding(e.getX(), e.getY()); |
||||
} else if (!selectionModel.getSelection().isEmpty()) { |
||||
selectionModel.releaseDragging(); |
||||
} |
||||
designer.repaint(); |
||||
} |
||||
|
||||
/** |
||||
* 改变选择区域 |
||||
* |
||||
* @param e 鼠标事件 |
||||
*/ |
||||
public void changeSelection(MouseEvent e) { |
||||
Rectangle bounds = createCurrentBounds(getMouseXY(e).x, getMouseXY(e).y); |
||||
selectionModel.setHotspotBounds(bounds); |
||||
} |
||||
|
||||
/** |
||||
* 返回鼠标所在的x、y 考虑滚动条的值 |
||||
* |
||||
* @param e 鼠标事件 |
||||
* @return xy值 |
||||
*/ |
||||
public Point getMouseXY(MouseEvent e) { |
||||
Point p1 = new Point(e.getX() + designer.getArea().getHorizontalValue(), e.getY() |
||||
+ designer.getArea().getVerticalValue()); |
||||
return p1; |
||||
} |
||||
|
||||
} |
@ -1,96 +1,96 @@
|
||||
package com.fr.design.designer.beans.painters; |
||||
|
||||
import java.awt.Color; |
||||
import java.awt.Graphics; |
||||
import java.awt.Graphics2D; |
||||
import java.awt.Point; |
||||
import java.awt.Rectangle; |
||||
import java.awt.Stroke; |
||||
|
||||
import com.fr.design.designer.beans.HoverPainter; |
||||
import com.fr.design.designer.creator.XCreator; |
||||
import com.fr.design.designer.creator.XLayoutContainer; |
||||
import com.fr.design.form.util.XCreatorConstants; |
||||
import com.fr.general.Inter; |
||||
|
||||
public abstract class AbstractPainter implements HoverPainter { |
||||
|
||||
protected Point hotspot; |
||||
protected Rectangle hotspot_bounds; |
||||
protected XLayoutContainer container; |
||||
protected XCreator creator; |
||||
|
||||
/** |
||||
* 构造函数 |
||||
* |
||||
* @param container 容器 |
||||
*/ |
||||
public AbstractPainter(XLayoutContainer container) { |
||||
this.container = container; |
||||
} |
||||
|
||||
@Override |
||||
public void setHotspot(Point p) { |
||||
hotspot = p; |
||||
} |
||||
|
||||
/** |
||||
* 画初始区域 |
||||
* |
||||
* @param g 画图类 |
||||
* @param startX 起始x位置 |
||||
* @param startY 起始y位置 |
||||
*/ |
||||
public void paint(Graphics g, int startX, int startY) { |
||||
if (hotspot_bounds != null) { |
||||
drawHotspot(g, hotspot_bounds.x, hotspot_bounds.y, hotspot_bounds.width, hotspot_bounds.height, Color.lightGray, true, false); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 设置边界 |
||||
* |
||||
* @param rect 位置 |
||||
*/ |
||||
@Override |
||||
public void setRenderingBounds(Rectangle rect) { |
||||
hotspot_bounds = rect; |
||||
} |
||||
|
||||
@Override |
||||
public void setCreator(XCreator component) { |
||||
this.creator = component; |
||||
} |
||||
|
||||
protected void drawHotspot(Graphics g, int x, int y, int width, int height, boolean accept) { |
||||
Color bColor = accept ? XCreatorConstants.LAYOUT_HOTSPOT_COLOR : XCreatorConstants.LAYOUT_FORBIDDEN_COLOR; |
||||
drawHotspot(g, x, y, width, height, bColor, accept, false); |
||||
} |
||||
|
||||
/** |
||||
* 自适应布局那边渲染提示,要画整个背景,不是画边框 |
||||
*/ |
||||
protected void drawRegionBackground(Graphics g, int x, int y, int width, int height, Color bColor, boolean accept) { |
||||
drawHotspot(g, x, y, width, height, bColor, accept, true); |
||||
} |
||||
|
||||
protected void drawHotspot(Graphics g, int x, int y, int width, int height, Color bColor, boolean accept, boolean drawBackground) { |
||||
Graphics2D g2d = (Graphics2D) g; |
||||
Color color = g2d.getColor(); |
||||
Stroke backup = g2d.getStroke(); |
||||
// 设置线条的样式
|
||||
g2d.setStroke(XCreatorConstants.STROKE); |
||||
g2d.setColor(bColor); |
||||
if (!accept) { |
||||
g2d.drawString(Inter.getLocText("Cannot-Add_To_This_Area") + "!", x + width / 3, y + height / 2); |
||||
} else if (drawBackground) { |
||||
g2d.fillRect(x, y, width, height); |
||||
} else { |
||||
g2d.drawRect(x, y, width, height); |
||||
} |
||||
g2d.setStroke(backup); |
||||
g2d.setColor(color); |
||||
} |
||||
|
||||
|
||||
package com.fr.design.designer.beans.painters; |
||||
|
||||
import java.awt.Color; |
||||
import java.awt.Graphics; |
||||
import java.awt.Graphics2D; |
||||
import java.awt.Point; |
||||
import java.awt.Rectangle; |
||||
import java.awt.Stroke; |
||||
|
||||
import com.fr.design.designer.beans.HoverPainter; |
||||
import com.fr.design.designer.creator.XCreator; |
||||
import com.fr.design.designer.creator.XLayoutContainer; |
||||
import com.fr.design.form.util.XCreatorConstants; |
||||
import com.fr.general.Inter; |
||||
|
||||
public abstract class AbstractPainter implements HoverPainter { |
||||
|
||||
protected Point hotspot; |
||||
protected Rectangle hotspot_bounds; |
||||
protected XLayoutContainer container; |
||||
protected XCreator creator; |
||||
|
||||
/** |
||||
* 构造函数 |
||||
* |
||||
* @param container 容器 |
||||
*/ |
||||
public AbstractPainter(XLayoutContainer container) { |
||||
this.container = container; |
||||
} |
||||
|
||||
@Override |
||||
public void setHotspot(Point p) { |
||||
hotspot = p; |
||||
} |
||||
|
||||
/** |
||||
* 画初始区域 |
||||
* |
||||
* @param g 画图类 |
||||
* @param startX 起始x位置 |
||||
* @param startY 起始y位置 |
||||
*/ |
||||
public void paint(Graphics g, int startX, int startY) { |
||||
if (hotspot_bounds != null) { |
||||
drawHotspot(g, hotspot_bounds.x, hotspot_bounds.y, hotspot_bounds.width, hotspot_bounds.height, Color.lightGray, true, false); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 设置边界 |
||||
* |
||||
* @param rect 位置 |
||||
*/ |
||||
@Override |
||||
public void setRenderingBounds(Rectangle rect) { |
||||
hotspot_bounds = rect; |
||||
} |
||||
|
||||
@Override |
||||
public void setCreator(XCreator component) { |
||||
this.creator = component; |
||||
} |
||||
|
||||
protected void drawHotspot(Graphics g, int x, int y, int width, int height, boolean accept) { |
||||
Color bColor = accept ? XCreatorConstants.LAYOUT_HOTSPOT_COLOR : XCreatorConstants.LAYOUT_FORBIDDEN_COLOR; |
||||
drawHotspot(g, x, y, width, height, bColor, accept, false); |
||||
} |
||||
|
||||
/** |
||||
* 自适应布局那边渲染提示,要画整个背景,不是画边框 |
||||
*/ |
||||
protected void drawRegionBackground(Graphics g, int x, int y, int width, int height, Color bColor, boolean accept) { |
||||
drawHotspot(g, x, y, width, height, bColor, accept, true); |
||||
} |
||||
|
||||
protected void drawHotspot(Graphics g, int x, int y, int width, int height, Color bColor, boolean accept, boolean drawBackground) { |
||||
Graphics2D g2d = (Graphics2D) g; |
||||
Color color = g2d.getColor(); |
||||
Stroke backup = g2d.getStroke(); |
||||
// 设置线条的样式
|
||||
g2d.setStroke(XCreatorConstants.STROKE); |
||||
g2d.setColor(bColor); |
||||
if (!accept) { |
||||
g2d.drawString(Inter.getLocText("Cannot-Add_To_This_Area") + "!", x + width / 3, y + height / 2); |
||||
} else if (drawBackground) { |
||||
g2d.fillRect(x, y, width, height); |
||||
} else { |
||||
g2d.drawRect(x, y, width, height); |
||||
} |
||||
g2d.setStroke(backup); |
||||
g2d.setColor(color); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue