From baba369ca7346706326d53a92b46c7b04822cf45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E7=A3=8A?= <294531121@qq.com> Date: Fri, 31 Dec 2021 17:36:24 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-64149=20=E9=87=8D=E6=9E=84=E6=8B=96?= =?UTF-8?q?=E6=8B=BD=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/beans/ComponentAdapter.java | 27 +-- .../design/designer/beans/LayoutAdapter.java | 22 +++ .../component/CompositeComponentAdapter.java | 17 -- .../layout/AbstractLayoutAdapter.java | 31 ++++ .../layout/FRAbsoluteLayoutAdapter.java | 15 ++ .../adapters/layout/FRBodyLayoutAdapter.java | 8 +- .../adapters/layout/FRFitLayoutAdapter.java | 28 ++++ .../designer/beans/models/AddingModel.java | 42 +---- .../designer/beans/models/DraggingModel.java | 88 ++++++++++ .../mainframe/EditingMouseListener.java | 155 ++++++------------ .../mainframe/FormCreatorDropTarget.java | 16 +- .../com/fr/design/mainframe/FormDesigner.java | 69 +++++--- .../fr/design/mainframe/FormDesignerUI.java | 32 ++-- .../fr/design/mainframe/ToolBarButton.java | 17 +- .../share/ui/block/LocalWidgetBlock.java | 4 +- .../share/ui/block/OnlineWidgetBlock.java | 2 +- .../guide/creator/GuideCreateUtils.java | 2 +- 17 files changed, 333 insertions(+), 242 deletions(-) create mode 100644 designer-form/src/main/java/com/fr/design/designer/beans/models/DraggingModel.java diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/ComponentAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/ComponentAdapter.java index 174f56ea2d..b296da48b5 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/ComponentAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/ComponentAdapter.java @@ -1,35 +1,24 @@ package com.fr.design.designer.beans; -import java.awt.Graphics; -import java.awt.event.MouseEvent; -import java.util.ArrayList; - -import javax.swing.JComponent; -import javax.swing.JPopupMenu; - import com.fr.design.beans.GroupModel; import com.fr.design.designer.beans.events.DesignerEditor; import com.fr.design.designer.creator.PropertyGroupPane; +import javax.swing.JComponent; +import javax.swing.JPopupMenu; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + /** * 组件适配器接口 * 主要目的是为具体组件提供特殊设计行为 */ public interface ComponentAdapter { - /** - * 在组件选择面板上选择了组件类型后,在设计界面上跟随鼠标移动用来代表当前要添加组件的图形 - * 一般使用组件自身的图形代替。 - * - * @param component 要添加的组件 - * @param g 当前设计器的图形上下文对象 - */ - void paintComponentMascot(Graphics g); - /** * 当鼠标在此设计组件上右键点击时,该方法根据上下文和组件类型提供弹出响应的菜单 * - * @param 引发弹出菜单的鼠标事件 + * @param e 引发弹出菜单的鼠标事件 * * @return 弹出菜单 */ @@ -49,7 +38,7 @@ public interface ComponentAdapter { /** * 提供双击设计器的编辑器 - * @param bean 鼠标双击的被设计组件 + * * @return 被设计的编辑器 */ public DesignerEditor getDesignerEditor(); @@ -57,5 +46,5 @@ public interface ComponentAdapter { /** * 实例化组件的适配器后,在这儿进行初始化 */ - void initialize(); + void initialize(); } \ No newline at end of file diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/LayoutAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/LayoutAdapter.java index 3c0c2bd5f2..060bbbc909 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/LayoutAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/LayoutAdapter.java @@ -3,11 +3,15 @@ package com.fr.design.designer.beans; import com.fr.design.beans.GroupModel; import com.fr.design.designer.beans.adapters.layout.DefaultDesignerBaseOperate; import com.fr.design.designer.beans.adapters.layout.DesignerBaseOperate; +import com.fr.design.designer.beans.models.DraggingModel; import com.fr.design.designer.beans.models.SelectionModel; import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.treeview.DefaultXCreatorTreeCellRender; import com.fr.design.designer.treeview.XCreatorTreeCellRender; +import java.awt.Dimension; +import java.awt.Graphics; + /** * 该接口是LayoutManager的BeanInfo类。标准Java平台没有提供布局管理器的BeanInfo类, * 对于界面设计工具来说还需一些特殊的行为。 @@ -140,4 +144,22 @@ public interface LayoutAdapter { default void dragOver(XCreator xCreator, SelectionModel selectionModel, int x, int y) { }; + + void dragging(DraggingModel model); + + /** + * 绘制组件拖拽阴影 + * + * @param g 当前设计器的图形上下文对象 + * @param xCreator 被拖拽的组件 + */ + void paintComponentMascot(Graphics g, XCreator xCreator); + + /** + * 描述组件被拖拽的时候应该显示的尺寸 + * + * @param creator 被拖拽的组件 + * @return 组件大小 + */ + Dimension getDragSize(XCreator creator); } diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/component/CompositeComponentAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/component/CompositeComponentAdapter.java index cb7a7a6207..d97b85c5ae 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/component/CompositeComponentAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/component/CompositeComponentAdapter.java @@ -23,13 +23,9 @@ import com.fr.stable.core.PropertyChangeAdapter; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JPopupMenu; -import java.awt.AlphaComposite; import java.awt.Component; import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; import java.awt.event.MouseEvent; -import java.awt.image.BufferedImage; import java.beans.IntrospectionException; import java.util.ArrayList; import java.util.Collections; @@ -63,19 +59,6 @@ public class CompositeComponentAdapter implements ComponentAdapter { } } - @Override - public void paintComponentMascot(Graphics g) { - //自适应交叉点渲染有点问题,拖拽的控件设置成半透明 - Graphics2D g2d = (Graphics2D) g; - AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f); - g2d.setComposite(composite); - BufferedImage im = new BufferedImage(xCreator.getWidth(), xCreator.getHeight(), BufferedImage.TYPE_INT_ARGB); - xCreator.paint(im.getGraphics()); - g.drawImage(im, 0, 0, xCreator.initEditorSize().width - 1, xCreator.initEditorSize().height - 1, null); - g.setColor(XCreatorConstants.RESIZE_BOX_BORDER_COLOR); - g.drawRect(0, 0, xCreator.initEditorSize().width - 1, xCreator.initEditorSize().height - 1); - } - @Override public JPopupMenu getContextPopupMenu(MouseEvent e) { JPopupMenu popupMenu = new JPopupMenu(); diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/AbstractLayoutAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/AbstractLayoutAdapter.java index 7d287e5a5a..e0f79e65ed 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/AbstractLayoutAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/AbstractLayoutAdapter.java @@ -4,16 +4,23 @@ 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.LayoutAdapter; +import com.fr.design.designer.beans.models.DraggingModel; import com.fr.design.designer.beans.models.SelectionModel; import com.fr.design.designer.beans.painters.NullPainter; import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.creator.XLayoutContainer; import com.fr.design.designer.creator.XWidgetCreator; +import com.fr.design.form.util.XCreatorConstants; import com.fr.design.utils.ComponentUtils; import com.fr.design.utils.gui.LayoutUtils; import com.fr.general.ComparatorUtils; +import java.awt.AlphaComposite; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.LayoutManager; +import java.awt.image.BufferedImage; public abstract class AbstractLayoutAdapter implements LayoutAdapter { @@ -209,4 +216,28 @@ public abstract class AbstractLayoutAdapter implements LayoutAdapter { selectionModel.removeCreator(xCreator, xCreator.getWidth(), xCreator.getHeight()); selectionModel.setSelectedCreator(container); } + + @Override + public void dragging(DraggingModel model) { + + } + + @Override + public void paintComponentMascot(Graphics g, XCreator xCreator) { + //自适应交叉点渲染有点问题,拖拽的控件设置成半透明 + int dragWidth = this.getDragSize(xCreator).width, dragHeight = this.getDragSize(xCreator).height; + Graphics2D g2d = (Graphics2D) g; + AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f); + g2d.setComposite(composite); + BufferedImage im = new BufferedImage(xCreator.getWidth(), xCreator.getHeight(), BufferedImage.TYPE_INT_ARGB); + xCreator.paint(im.getGraphics()); + g.drawImage(im, 0, 0, dragWidth - 1, dragHeight - 1, null); + g.setColor(XCreatorConstants.RESIZE_BOX_BORDER_COLOR); + g.drawRect(0, 0, dragWidth - 1, dragHeight - 1); + } + + @Override + public Dimension getDragSize(XCreator xCreator) { + return xCreator.initEditorSize(); + } } diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRAbsoluteLayoutAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRAbsoluteLayoutAdapter.java index e4426f61ad..3f49ebe3fc 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRAbsoluteLayoutAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRAbsoluteLayoutAdapter.java @@ -3,10 +3,12 @@ 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.models.DraggingModel; 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.mainframe.FormDesigner; import com.fr.design.utils.ComponentUtils; import com.fr.design.utils.gui.LayoutUtils; import com.fr.form.ui.Widget; @@ -15,6 +17,7 @@ import com.fr.general.ComparatorUtils; import com.fr.log.FineLoggerFactory; import java.awt.*; +import java.awt.event.MouseEvent; public class FRAbsoluteLayoutAdapter extends FRBodyLayoutAdapter { //是不是添加到父容器上 @@ -345,4 +348,16 @@ public class FRAbsoluteLayoutAdapter extends FRBodyLayoutAdapter { XWAbsoluteLayout xwAbsoluteLayout = (XWAbsoluteLayout) container; return new FRAbsoluteLayoutPropertiesGroupModel(xwAbsoluteLayout); } + + @Override + public void dragging(DraggingModel model) { + FormDesigner designer = model.getDesigner(); + MouseEvent dragEvent = model.getDragEvent(); + designer.getStateModel().dragging(dragEvent); + } + + @Override + public Dimension getDragSize(XCreator xCreator) { + return xCreator.getSize(); + } } diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRBodyLayoutAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRBodyLayoutAdapter.java index 5a31bab40d..f4c0458611 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRBodyLayoutAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRBodyLayoutAdapter.java @@ -6,12 +6,13 @@ import com.fr.design.designer.creator.XWParameterLayout; import com.fr.design.designer.creator.cardlayout.XWCardMainBorderLayout; import com.fr.design.utils.ComponentUtils; import com.fr.form.ui.PaddingMargin; -import com.fr.form.ui.container.WBorderLayout; import com.fr.form.ui.container.cardlayout.WCardMainBorderLayout; import com.fr.general.ComparatorUtils; -import java.awt.*; -import java.util.*; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Rectangle; +import java.util.ArrayList; /** * 这个类用作fit和absolute的父类,存放公共的方法 @@ -1182,5 +1183,4 @@ public class FRBodyLayoutAdapter extends AbstractLayoutAdapter { this.isCalculateChildPos = false; return childPosition; } - } diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRFitLayoutAdapter.java b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRFitLayoutAdapter.java index 9871d1b32f..db40eec0f7 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRFitLayoutAdapter.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/adapters/layout/FRFitLayoutAdapter.java @@ -8,6 +8,8 @@ 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.LayoutAdapter; +import com.fr.design.designer.beans.models.AddingModel; +import com.fr.design.designer.beans.models.DraggingModel; import com.fr.design.designer.beans.models.SelectionModel; import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.creator.XCreatorUtils; @@ -22,6 +24,8 @@ import com.fr.design.designer.properties.FRFitLayoutConstraints; import com.fr.design.designer.properties.FRFitLayoutPropertiesGroupModel; import com.fr.design.designer.treeview.XCreatorTreeCellRender; import com.fr.design.fun.FormWidgetOptionProvider; +import com.fr.design.mainframe.FormCreatorDropTarget; +import com.fr.design.mainframe.FormDesigner; import com.fr.design.utils.ComponentUtils; import com.fr.form.ui.LayoutBorderStyle; import com.fr.form.ui.container.WAbsoluteLayout; @@ -34,6 +38,7 @@ import com.fr.general.act.BorderPacker; import java.awt.Component; import java.awt.Dimension; import java.awt.Rectangle; +import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -1244,4 +1249,27 @@ public class FRFitLayoutAdapter extends FRBodyLayoutAdapter { public void dragOver(XCreator xCreator, SelectionModel selectionModel,int x, int y) { frLayoutState.dragOver(xCreator, selectionModel,x,y); } + + @Override + public void dragging(DraggingModel model) { + FormDesigner designer = model.getDesigner(); + MouseEvent lastPressEvent = model.getLastPressEvent(); + MouseEvent dragEvent = model.getDragEvent(); + XCreator creator = model.getCreator(); + if ((lastPressEvent == null) || (creator == null)) { + return; + } + + if (dragEvent.getPoint().distance(lastPressEvent.getPoint()) > 5) { + if (creator.isSupportDrag()) { + designer.setAddingModel(new AddingModel(creator, dragEvent.getX(), dragEvent.getY())); + designer.bindTransferHandler(lastPressEvent); + designer.fireParentLayoutDragStart(creator); + designer.setDropTarget(new FormCreatorDropTarget(designer)); + // 触发状态添加模式事件 + designer.repaint(); + } + dragEvent.consume(); + } + } } diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java b/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java index ba0cf5dd05..f33810bae0 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java @@ -20,18 +20,13 @@ public class AddingModel { // 当前要添加的组件 private XCreator creator; - // 记录当前鼠标的位置信息 - private int currentX; - private int currentY; private boolean added; private boolean addedIllegal = false; // 有时候是添加完成了,但是添加会造成某些控件size不合法,例如tab + private FormDesigner designer; public AddingModel(FormDesigner designer, XCreator xCreator) { this.creator = xCreator; instantiateCreator(designer); - // 初始的时候隐藏该组件的图标 - currentY = -this.creator.getWidth(); - currentX = -this.creator.getHeight(); } /** @@ -52,16 +47,6 @@ public class AddingModel { this.creator = xCreator; this.creator.backupCurrentSize(); this.creator.backupParent(); - currentX = x - (xCreator.initEditorSize().width / 2); - currentY = y - (xCreator.initEditorSize().height / 2); - } - - /** - * 隐藏当前组件的图标 - */ - public void reset() { - currentX = -this.creator.getWidth(); - currentY = -this.creator.getHeight(); } public String getXCreatorName(FormDesigner designer, XCreator x) { @@ -76,35 +61,10 @@ public class AddingModel { 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.initEditorSize().width / 2); - currentY = y - (this.creator.initEditorSize().height / 2); - } - public XCreator getXCreator() { return this.creator; } - public boolean need2paint(){ - return currentX + this.creator.getWidth() > 0 && currentY + this.creator.getHeight() > 0; - } - /** * 当前组件是否已经添加到某个容器中 * diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/models/DraggingModel.java b/designer-form/src/main/java/com/fr/design/designer/beans/models/DraggingModel.java new file mode 100644 index 0000000000..47a50327e4 --- /dev/null +++ b/designer-form/src/main/java/com/fr/design/designer/beans/models/DraggingModel.java @@ -0,0 +1,88 @@ +package com.fr.design.designer.beans.models; + +import com.fr.design.designer.beans.LayoutAdapter; +import com.fr.design.designer.creator.XCreator; +import com.fr.design.designer.creator.XLayoutContainer; +import com.fr.design.mainframe.FormDesigner; + +import java.awt.event.MouseEvent; + +public class DraggingModel { + private FormDesigner designer; + private XCreator creator; + private MouseEvent lastPressEvent; + private MouseEvent dragEvent; + private int creatorLeftTopX = -999; // 隐藏 + private int creatorLeftTopY = -999; // 隐藏 + + public DraggingModel() { + + } + + public DraggingModel designer(FormDesigner designer) { + this.designer = designer; + return this; + } + + public DraggingModel lastPressEvent(MouseEvent lastPressEvent) { + this.lastPressEvent = lastPressEvent; + return this; + } + + public DraggingModel dragEvent(MouseEvent dragEvent) { + this.dragEvent = dragEvent; + return this; + } + + public DraggingModel creator(XCreator creator) { + this.creator = creator; + return this; + } + + public FormDesigner getDesigner() { + return designer; + } + + public MouseEvent getLastPressEvent() { + return lastPressEvent; + } + + public MouseEvent getDragEvent() { + return dragEvent; + } + + public XCreator getCreator() { + return creator; + } + + /** + * 获取被拖拽组件当前随着鼠标移动时应当所在的左上角横坐标 + * + * @return + */ + public int getCreatorLeftTopX() { + return creatorLeftTopX; + } + + /** + * 获取被拖拽组件当前随着鼠标移动时应当所在的左上角纵坐标 + * + * @return + */ + public int getCreatorLeftTopY() { + return creatorLeftTopY; + } + + public void moveTo(int x, int y) { + XLayoutContainer container = designer.getDraggingHotspotLayout(); + LayoutAdapter adapter = container.getLayoutAdapter(); + + creatorLeftTopX = x - adapter.getDragSize(creator).width / 2; + creatorLeftTopY = y - adapter.getDragSize(creator).height / 2; + } + + public void reset() { + creatorLeftTopX = -creator.getWidth(); + creatorLeftTopY = -creator.getHeight(); + } +} diff --git a/designer-form/src/main/java/com/fr/design/mainframe/EditingMouseListener.java b/designer-form/src/main/java/com/fr/design/mainframe/EditingMouseListener.java index b0463a685a..5053ae0131 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/EditingMouseListener.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/EditingMouseListener.java @@ -1,14 +1,15 @@ package com.fr.design.mainframe; -import com.fr.base.BaseUtils; import com.fr.base.vcs.DesignerMode; import com.fr.common.inputevent.InputEventBaseOnOS; import com.fr.design.designer.beans.AdapterBus; import com.fr.design.designer.beans.ComponentAdapter; +import com.fr.design.designer.beans.LayoutAdapter; import com.fr.design.designer.beans.events.DesignerEditor; 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.beans.models.DraggingModel; import com.fr.design.designer.beans.models.SelectionModel; import com.fr.design.designer.beans.models.StateModel; import com.fr.design.designer.creator.XBorderStyleWidgetCreator; @@ -25,25 +26,18 @@ import com.fr.design.designer.creator.cardlayout.XCardAddButton; import com.fr.design.designer.creator.cardlayout.XCardSwitchButton; import com.fr.design.designer.creator.cardlayout.XWCardLayout; import com.fr.design.designer.creator.cardlayout.XWCardMainBorderLayout; -import com.fr.design.form.util.XCreatorConstants; -import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.imenu.UIPopupMenu; import com.fr.design.gui.xpane.ToolTipEditor; -import com.fr.design.icon.IconPathConstants; import com.fr.design.utils.ComponentUtils; import com.fr.design.utils.gui.GUICoreUtils; import com.fr.design.utils.gui.LayoutUtils; import com.fr.general.ComparatorUtils; import com.fr.stable.ArrayUtils; -import com.fr.stable.Constants; -import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JPopupMenu; -import javax.swing.JWindow; import javax.swing.SwingUtilities; import javax.swing.event.MouseInputAdapter; -import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import java.awt.Insets; @@ -95,9 +89,6 @@ public class EditingMouseListener extends MouseInputAdapter { private DesignerEditor currentEditor; private XCreator currentXCreator; - //备份开始拖动的位置和大小 - private Rectangle dragBackupBounds; - private int pressX; private int pressY; @@ -121,40 +112,12 @@ public class EditingMouseListener extends MouseInputAdapter { private XElementCase xElementCase; private XChartEditor xChartEditor; - private JWindow promptWindow = new JWindow(); - public EditingMouseListener(FormDesigner designer) { this.designer = designer; stateModel = designer.getStateModel(); selectionModel = designer.getSelectionModel(); - UIButton promptButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Form_Forbid_Drag_Into_Adapt_Pane"), BaseUtils.readIcon(IconPathConstants.FORBID_ICON_PATH)); - this.promptWindow.add(promptButton); - } - - private void promptUser(int x, int y, XLayoutContainer container) { - if (!selectionModel.getSelection().getSelectedCreator().canEnterIntoAdaptPane() && container.acceptType(XWFitLayout.class)) { - promptWidgetForbidEnter(x, y, container); - } else { - cancelPromptWidgetForbidEnter(); - } } - private void promptWidgetForbidEnter(int x, int y, XLayoutContainer container) { - container.setBorder(BorderFactory.createLineBorder(Color.RED, Constants.LINE_MEDIUM)); - int screenX = (int) designer.getArea().getLocationOnScreen().getX(); - int screenY = (int) designer.getArea().getLocationOnScreen().getY(); - this.promptWindow.setSize(promptWindow.getPreferredSize()); - this.promptWindow.setPreferredSize(promptWindow.getPreferredSize()); - promptWindow.setLocation(screenX + x + GAP, screenY + y + GAP); - promptWindow.setVisible(true); - } - - private void cancelPromptWidgetForbidEnter() { - designer.getRootComponent().setBorder(BorderFactory.createLineBorder(XCreatorConstants.LAYOUT_SEP_COLOR, Constants.LINE_THIN)); - promptWindow.setVisible(false); - } - - /** * 按下 * @@ -181,18 +144,14 @@ public class EditingMouseListener extends MouseInputAdapter { } if (dir == Location.outer) { - if (designer.isDrawLineMode()) { - designer.updateDrawLineMode(e); + if (selectionModel.hasSelectionComponent() + && selectionModel.getSelection().getRelativeBounds().contains( + designer.getHorizontalScaleValue() + e.getX(), + designer.getVerticalScaleValue() + e.getY())) { + lastPressEvent = e; + lastXCreator = selectionModel.getSelection().getSelectedCreator(); } else { - if (selectionModel.hasSelectionComponent() - && selectionModel.getSelection().getRelativeBounds().contains( - designer.getHorizontalScaleValue() + e.getX(), - designer.getVerticalScaleValue() + e.getY())) { - lastPressEvent = e; - lastXCreator = selectionModel.getSelection().getSelectedCreator(); - } else { - stateModel.startSelecting(e); - } + stateModel.startSelecting(e); } } else { stateModel.startResizing(e); @@ -258,13 +217,6 @@ public class EditingMouseListener extends MouseInputAdapter { private void mouseDraggingRelease(MouseEvent e) { // 当前鼠标所在的组件 XCreator hoveredComponent = designer.getComponentAt(e.getX(), e.getY()); - if (designer.isWidgetsIntersect() && dragBackupBounds != null && hoveredComponent != null) { - XCreator selectionXCreator = designer.getSelectionModel().getSelection().getSelectedCreator(); - if (selectionXCreator != null) { - selectionXCreator.setBounds(dragBackupBounds.x, dragBackupBounds.y, dragBackupBounds.width, dragBackupBounds.height); - } - } - dragBackupBounds = null; // 拉伸时鼠标拖动过快,导致所在组件获取会为空 if (hoveredComponent == null && e.getY() < 0) { // bug63538 @@ -285,7 +237,6 @@ public class EditingMouseListener extends MouseInputAdapter { selectionModel.deleteSelection(); designer.setPainter(null); } - cancelPromptWidgetForbidEnter(); } } @@ -499,61 +450,56 @@ public class EditingMouseListener extends MouseInputAdapter { if (DesignerMode.isAuthorityEditing()) { return; } - boolean shiftSelecting = e.isShiftDown(); - boolean ctrlSelecting = InputEventBaseOnOS.isControlDown(e); - int currentCursorType = this.designer.getCursor().getType(); - boolean shiftResizing = e.isShiftDown() && ( Cursor.SW_RESIZE_CURSOR <= currentCursorType && currentCursorType <= Cursor.E_RESIZE_CURSOR); - if ((shiftSelecting || ctrlSelecting) && !shiftResizing && !stateModel.isSelecting()) { + if (SwingUtilities.isRightMouseButton(e)) { + return; + } + + if (isMultiKeySelectingDrag(e)) { stateModel.startSelecting(e); } - // 如果当前是左键拖拽状态,拖拽组件 - if (stateModel.dragable()) { - if (SwingUtilities.isRightMouseButton(e)) { - return; - } else { - stateModel.dragging(e); - // 获取e所在的焦点组件 - XCreator hotspot = designer.getComponentAt(e.getX(), e.getY()); - if (dragBackupBounds == null) { - XCreator selectingXCreator = designer.getSelectionModel().getSelection().getSelectedCreator(); - if (selectingXCreator != null) { - dragBackupBounds = new Rectangle(selectingXCreator.getX(), selectingXCreator.getY(), selectingXCreator.getWidth(), selectingXCreator.getHeight()); - } - } - // 拉伸时鼠标拖动过快,导致所在组件获取会为空 - if (hotspot == null) { - return; - } - // 获取焦点组件所在的焦点容器 - XLayoutContainer container = XCreatorUtils.getHotspotContainer(hotspot); - //提示组件是否可以拖入 - promptUser(e.getX(), e.getY(), container); - } - } else if (designer.isDrawLineMode()) { - if (stateModel.prepareForDrawLining()) { - stateModel.drawLine(e); - } - } else if (stateModel.isSelecting() && (selectionModel.getHotspotBounds() != null)) { - // 如果是拖拽选择区域状态,则更新选择区域 + + if (isSelectionDrag()) { stateModel.changeSelection(e); - } else { - if ((lastPressEvent == null) || (lastXCreator == null)) { - return; - } - if (e.getPoint().distance(lastPressEvent.getPoint()) > minDragSize) { - //参数面板和自适应布局不支持拖拽 - if (lastXCreator.isSupportDrag()) { - designer.startDraggingComponent(lastXCreator, lastPressEvent, e.getX(), e.getY()); - } - e.consume(); - lastPressEvent = null; - } + return; } + + System.out.println("===set==="); + designer.setDraggingModel(new DraggingModel() + .designer(designer) + .creator(lastXCreator) + .lastPressEvent(lastPressEvent) + .dragEvent(e)); + XLayoutContainer container = designer.getDraggingHotspotLayout(); + LayoutAdapter adapter = container.getLayoutAdapter(); + adapter.dragging(designer.getDraggingModel()); + e.translatePoint(oldX - e.getX(), oldY - e.getY()); designer.repaint(); } + /** + * 当前拖拽是否是正在shift或者control拖拽一个选择框 + * @param e + * @return + */ + private boolean isMultiKeySelectingDrag(MouseEvent e) { + boolean shiftSelecting = e.isShiftDown(); + boolean ctrlSelecting = InputEventBaseOnOS.isControlDown(e); + int currentCursorType = this.designer.getCursor().getType(); + boolean shiftResizing = e.isShiftDown() && ( Cursor.SW_RESIZE_CURSOR <= currentCursorType && currentCursorType <= Cursor.E_RESIZE_CURSOR); + return (shiftSelecting || ctrlSelecting) && !shiftResizing && !stateModel.isSelecting(); + } + + /** + * 是否正在拖拽选择框 + * + * @return + */ + private boolean isSelectionDrag() { + return stateModel.isSelecting() && selectionModel.getHotspotBounds() != null; + } + //当前编辑的组件是在布局中,鼠标点击布局外部,需要一次性将布局及其父布局都置为不可编辑 private void setTopLayoutUnEditable(XLayoutContainer clickedTopLayout, XLayoutContainer clickingTopLayout) { //双击的前后点击click为相同对象,过滤掉 @@ -718,7 +664,6 @@ public class EditingMouseListener extends MouseInputAdapter { if (designer.getCursor().getType() != Cursor.DEFAULT_CURSOR && !(e.isShiftDown() || InputEventBaseOnOS.isControlDown(e))) { designer.setCursor(Cursor.getDefaultCursor()); } - cancelPromptWidgetForbidEnter(); e.translatePoint(oldX - e.getX(), oldY - e.getY()); } diff --git a/designer-form/src/main/java/com/fr/design/mainframe/FormCreatorDropTarget.java b/designer-form/src/main/java/com/fr/design/mainframe/FormCreatorDropTarget.java index 5eb354c6cb..2e4a5e6d4c 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/FormCreatorDropTarget.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/FormCreatorDropTarget.java @@ -168,21 +168,21 @@ public class FormCreatorDropTarget extends DropTarget { private void entering(int x, int y) { // 将要添加的组件图标移动到鼠标下的位置 - addingModel.moveTo(x, y); + designer.getDraggingModel().moveTo(x, y); designer.repaint(); } private void exiting() { cancelPromptWidgetForbidEnter(); // 隐藏组件图标 - addingModel.reset(); + designer.getDraggingModel().reset(); designer.setPainter(null); designer.repaint(); } private void hovering(int x, int y) { // 当前位置移植鼠标e所在的位置 - addingModel.moveTo(x, y); + designer.getDraggingModel().moveTo(x, y); // 获取e所在的焦点组件 XCreator hotspot = designer.getComponentAt(x, y); // 获取焦点组件所在的焦点容器 @@ -290,7 +290,15 @@ public class FormCreatorDropTarget extends DropTarget { int y = designer.getRelativeY(loc.y); hovering(x, y); tabDragInner.canDragIn(designer.getComponentAt(x, y), x, y); - designer.draggingComponent(x, y); + + XCreator xCreator = designer.getAddingModel().getXCreator(); + XLayoutContainer parent = XCreatorUtils.getParentXLayoutContainer(xCreator); + if (parent!= null && parent.getLayoutAdapter()!=null){ + parent.getLayoutAdapter().dragOver(xCreator, designer.getSelectionModel(), x, y); + } + + // 触发状态添加模式事件 + designer.repaint(); } /** diff --git a/designer-form/src/main/java/com/fr/design/mainframe/FormDesigner.java b/designer-form/src/main/java/com/fr/design/mainframe/FormDesigner.java index adc59581ac..ecf67bf544 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/FormDesigner.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/FormDesigner.java @@ -19,8 +19,8 @@ import com.fr.design.designer.beans.actions.MoveToTopAction; import com.fr.design.designer.beans.actions.MoveUpAction; import com.fr.design.designer.beans.actions.PasteAction; import com.fr.design.designer.beans.adapters.layout.DefaultDesignerBaseOperate; -import com.fr.design.designer.beans.adapters.layout.FRParameterLayoutAdapter; import com.fr.design.designer.beans.adapters.layout.DesignerBaseOperate; +import com.fr.design.designer.beans.adapters.layout.FRParameterLayoutAdapter; import com.fr.design.designer.beans.events.CreatorEventListenerTable; import com.fr.design.designer.beans.events.DesignerEditListener; import com.fr.design.designer.beans.events.DesignerEvent; @@ -28,6 +28,7 @@ import com.fr.design.designer.beans.location.Direction; import com.fr.design.designer.beans.location.Location; import com.fr.design.designer.beans.location.RootResizeDirection; import com.fr.design.designer.beans.models.AddingModel; +import com.fr.design.designer.beans.models.DraggingModel; import com.fr.design.designer.beans.models.SelectionModel; import com.fr.design.designer.beans.models.StateModel; import com.fr.design.designer.creator.XChartEditor; @@ -144,6 +145,7 @@ public class FormDesigner extends TargetComponent
implements TreeSelection private transient StateModel stateModel; // 添加状态下的model,存储添加状态下的临时状态,比如要添加的组件、当前鼠标位置等等 private transient AddingModel addingModel; + private transient DraggingModel draggingModel; // 当前负责额外渲染的painter,主要目的用来渲染添加组件的位置提示,它通常由外部类设置,在 // 设计器渲染时被调用渲染这些位置提示。 private transient Painter painter; @@ -1113,6 +1115,14 @@ public class FormDesigner extends TargetComponent implements TreeSelection this.addingModel = addingModel; } + public DraggingModel getDraggingModel() { + return draggingModel; + } + + public void setDraggingModel(DraggingModel draggingModel) { + this.draggingModel = draggingModel; + } + public XCreator getComponentAt(MouseEvent e) { return getComponentAt(e.getX(), e.getY()); } @@ -1183,7 +1193,7 @@ public class FormDesigner extends TargetComponent implements TreeSelection * * @param xCreator 组件 */ - public void startDraggingBean(XCreator xCreator) { + public void startDraggingNewWidget(XCreator xCreator) { // 根据所选择的组件的BeanInfo生成相应的AddingModel // AddingModel和StateModel不一样,适合当前选择的组件相关的 addingModel = new AddingModel(this, xCreator); @@ -1192,37 +1202,48 @@ public class FormDesigner extends TargetComponent implements TreeSelection repaint(); } + public void startDraggingNewWidget(XCreator xCreator, MouseEvent lastPressEvent, MouseEvent dragEvent) { + draggingModel = new DraggingModel() + .designer(this) + .creator(xCreator) + .lastPressEvent(lastPressEvent) + .dragEvent(dragEvent); + startDraggingNewWidget(xCreator); + } + /** - * 拖拽时相关处理 + * 绑定transferHandler,主要实现拖拽结束的回调,在拖拽已有组件的时候有用,需要在回调里面手动把拖拽失败的组件复原,否则组件会消失 * - * @param xCreator 组件 - * @param lastPressEvent 鼠标事件 - * @param x 坐标x - * @param y 坐标y + * @param e */ - public void startDraggingComponent(XCreator xCreator, MouseEvent lastPressEvent, int x, int y) { - // 根据所选择的组件的BeanInfo生成相应的AddingModel - // AddingModel和StateModel不一样,适合当前选择的组件相关的 - this.addingModel = new AddingModel(xCreator, x, y); + public void bindTransferHandler(MouseEvent e) { TransferHandler handler = new DesignerTransferHandler(this, addingModel); - setTransferHandler(handler); - handler.exportAsDrag(this, lastPressEvent, TransferHandler.COPY); - XLayoutContainer parent = XCreatorUtils.getParentXLayoutContainer(xCreator); - parent.getLayoutAdapter().dragStart(xCreator, selectionModel); - this.setDropTarget(new FormCreatorDropTarget(this)); - // 触发状态添加模式事件 - repaint(); + this.setTransferHandler(handler); + handler.exportAsDrag(this, e, TransferHandler.COPY); } - public void draggingComponent(int x, int y){ - XCreator xCreator = this.addingModel.getXCreator(); + /** + * 触发父组件布局的dragStart事件,目前只在非固定布局下有用,用来删除占位块 + * + * @param xCreator + */ + public void fireParentLayoutDragStart(XCreator xCreator) { XLayoutContainer parent = XCreatorUtils.getParentXLayoutContainer(xCreator); - if (parent!= null && parent.getLayoutAdapter()!=null){ - parent.getLayoutAdapter().dragOver(xCreator, selectionModel, x, y); + if (parent != null) { + parent.getLayoutAdapter().dragStart(xCreator, selectionModel); } + } - // 触发状态添加模式事件 - repaint(); + /** + * 获取拖拽当前鼠标所在位置的布局 + * + * @return + */ + public XLayoutContainer getDraggingHotspotLayout() { + DraggingModel model = this.draggingModel; + MouseEvent dragEvent = model.getDragEvent(); + XCreator hotspot = this.getComponentAt(dragEvent.getX(), dragEvent.getY()); + return XCreatorUtils.getHotspotContainer(hotspot); } /** diff --git a/designer-form/src/main/java/com/fr/design/mainframe/FormDesignerUI.java b/designer-form/src/main/java/com/fr/design/mainframe/FormDesignerUI.java index aab1b27b72..94b61737e7 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/FormDesignerUI.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/FormDesignerUI.java @@ -6,10 +6,10 @@ import com.fr.base.Utils; import com.fr.base.iofile.attr.WatermarkAttr; import com.fr.base.vcs.DesignerMode; import com.fr.design.constants.UIConstants; -import com.fr.design.designer.beans.AdapterBus; -import com.fr.design.designer.beans.ComponentAdapter; +import com.fr.design.designer.beans.LayoutAdapter; import com.fr.design.designer.beans.location.Direction; import com.fr.design.designer.beans.models.AddingModel; +import com.fr.design.designer.beans.models.DraggingModel; import com.fr.design.designer.beans.models.SelectionModel; import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.creator.XLayoutContainer; @@ -121,7 +121,7 @@ public class FormDesignerUI extends ComponentUI { if ((addingModel != null) && (addingModel.getXCreator() != null)) { // 当前正在添加的组件 - paintAddingBean(g, addingModel); + paintDraggingBean(g); } designer.getSpacingLineDrawer().draw(g); @@ -196,26 +196,24 @@ public class FormDesignerUI extends ComponentUI { } /** - * 渲染当前正在添加的组件,采用Renderer原理 + * 渲染当前正在拖拽的组件,采用Renderer原理 */ - private void paintAddingBean(Graphics g, final AddingModel addingModel) { - if (!addingModel.need2paint()) { - return; - } - XCreator bean = addingModel.getXCreator(); - int x = addingModel.getCurrentX(); - int y = addingModel.getCurrentY() ; - - int width = bean.initEditorSize().width; - int height = bean.initEditorSize().height; + private void paintDraggingBean(Graphics g) { + DraggingModel model = designer.getDraggingModel(); + XCreator bean = model.getCreator(); + int x = model.getCreatorLeftTopX(); + int y = model.getCreatorLeftTopY() ; + + XLayoutContainer container = designer.getDraggingHotspotLayout(); + LayoutAdapter adapter = container.getLayoutAdapter(); + int width = adapter.getDragSize(bean).width; + int height = adapter.getDragSize(bean).height; Graphics clipg = g.create(x, y, width, height); ArrayList dbcomponents = new ArrayList(); // 禁止双缓冲行为 ComponentUtils.disableBuffer(bean, dbcomponents); - ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, bean); - // 调用ComponentAdapter的paintComponentMascot方法渲染该组件添加提示 - adapter.paintComponentMascot(clipg); + adapter.paintComponentMascot(clipg, bean); clipg.dispose(); // 恢复双缓冲 ComponentUtils.resetBuffer(dbcomponents); diff --git a/designer-form/src/main/java/com/fr/design/mainframe/ToolBarButton.java b/designer-form/src/main/java/com/fr/design/mainframe/ToolBarButton.java index 53b9c52915..6b92cdaf0c 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/ToolBarButton.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/ToolBarButton.java @@ -2,26 +2,31 @@ package com.fr.design.mainframe; import com.fr.base.vcs.DesignerMode; import com.fr.design.designer.creator.XCreator; +import com.fr.design.designer.creator.XCreatorUtils; import com.fr.design.file.HistoryTemplateListCache; import com.fr.design.form.util.FormDesignerUtils; -import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.core.WidgetOption; -import com.fr.design.designer.creator.XCreatorUtils; +import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.ibutton.UIForbiddenButtonUI; import com.fr.form.ui.Widget; import com.fr.general.ComparatorUtils; -import java.awt.*; +import javax.swing.plaf.ButtonUI; +import java.awt.Dimension; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; -import java.awt.dnd.*; +import java.awt.dnd.DnDConstants; +import java.awt.dnd.DragGestureEvent; +import java.awt.dnd.DragGestureListener; +import java.awt.dnd.DragSource; +import java.awt.dnd.DragSourceAdapter; +import java.awt.dnd.DragSourceDragEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.io.IOException; import java.io.Serializable; -import javax.swing.plaf.ButtonUI; /* *august: 控件按钮 @@ -100,7 +105,7 @@ public class ToolBarButton extends UIButton implements MouseListener, MouseMotio } if (creatorSource != null) { XCreator xCreator = XCreatorUtils.createThemedXCreator(creatorSource); - WidgetToolBarPane.getTarget().startDraggingBean(xCreator); + WidgetToolBarPane.getTarget().startDraggingNewWidget(xCreator, lastPressEvent, e); FormDesignerUtils.addWidgetProcessInfo(xCreator.toData()); lastPressEvent = null; this.setBorder(null); diff --git a/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/LocalWidgetBlock.java b/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/LocalWidgetBlock.java index 72eb35d000..13bb226fae 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/LocalWidgetBlock.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/LocalWidgetBlock.java @@ -31,7 +31,6 @@ import com.fr.form.share.record.ShareWidgetInfoManager; import com.fr.form.ui.AbstractBorderStyleWidget; import com.fr.form.ui.Widget; import com.fr.general.ComparatorUtils; -import com.fr.general.FRFont; import com.fr.general.IOUtils; import com.fr.stable.Constants; import org.jetbrains.annotations.NotNull; @@ -61,7 +60,6 @@ import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.font.FontRenderContext; import java.awt.font.LineMetrics; -import java.awt.geom.Dimension2D; import java.awt.image.BufferedImage; import java.util.UUID; @@ -239,7 +237,7 @@ public class LocalWidgetBlock extends PreviewWidgetBlock if (xCreator == null) { return; } - WidgetToolBarPane.getTarget().startDraggingBean(xCreator); + WidgetToolBarPane.getTarget().startDraggingNewWidget(xCreator, lastPressEvent, e); lastPressEvent = null; this.setBorder(null); } diff --git a/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/OnlineWidgetBlock.java b/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/OnlineWidgetBlock.java index 38bcd99826..539b8d25ed 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/OnlineWidgetBlock.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/OnlineWidgetBlock.java @@ -200,7 +200,7 @@ public class OnlineWidgetBlock extends AbstractOnlineWidgetBlock { } else { xCreator = ShareComponentUtils.createXCreator(creatorSource, shareId, bindInfo); } - WidgetToolBarPane.getTarget().startDraggingBean(xCreator); + WidgetToolBarPane.getTarget().startDraggingNewWidget(xCreator, lastPressEvent, e); lastPressEvent = null; this.setBorder(null); } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/guide/creator/GuideCreateUtils.java b/designer-realize/src/main/java/com/fr/design/mainframe/guide/creator/GuideCreateUtils.java index 39c6b55742..a20384379b 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/guide/creator/GuideCreateUtils.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/guide/creator/GuideCreateUtils.java @@ -139,7 +139,7 @@ public class GuideCreateUtils { designer.setDropTarget(new FormCreatorDropTarget(designer)); designer.repaint(); } else { - designer.startDraggingBean(xCreator); + designer.startDraggingNewWidget(xCreator); } FormCreatorDropTarget dropTarget = (FormCreatorDropTarget) designer.getDropTarget();