⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectionmodel.java

📁 SWING的界面UI包 SWING的界面UI包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    //设置位置,移动20x20,防止被粘帖的组件重叠,照顾空布局情况下
                    comp.setLocation(comp.getX() + 20, comp.getY() + 20);

                    //使用适配器克隆组件
                    ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, comp);
                    Component clone = adapter.clone();
                    clone.setName(clone.getName() + "_copy");
                    //在容器中添加该组件
                    container.add(clone);
                }
                clip_board.clear();
                //重新布局
                Util.layoutContainer(container);
                //触发事件
                fireComponentPaste();
                designer.repaint();
            }
        } else {
            //不可以粘帖,警告
            Toolkit.getDefaultToolkit().beep();
        }
    }

    private void fireComponentPaste() {
        DesignerEvent evt = new DesignerEvent(this);
        evt.setPastedComponents(clip_board);
        designer.getEditListenerTable().fireComponentPasted(evt);
    }

    /**
     * 当前所选中的组件容器是否可以粘帖剪贴版内的组件
     */
    public boolean isPasteable() {
        if (selection.size() != 1) {
            //必须只有一个容器组件
            return false;
        }

        Component comp = selection.get(0);

        ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, comp);

        //并且该唯一的组件是个容器
        return adapter instanceof ContainerAdapter;
    }

    //向左对齐
    public void adjustLeftAlignment() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int x = comp.getX();

            for (Component current : selection) {
                current.setLocation(x, current.getY());
            }
        }
    }

    //向右对齐
    public void adjustRightAlignment() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int right_x = comp.getX() + comp.getWidth();

            for (Component current : selection) {
                current.setLocation(right_x - current.getWidth(), current.getY());
            }
        }
    }

    //向中对齐
    public void adjustCenterAlignment() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int center_x = comp.getX() + (comp.getWidth() / 2);

            for (Component current : selection) {
                current.setLocation(center_x - (current.getWidth() / 2), current.getY());
            }
        }
    }

    //向上对齐
    public void adjustTopAlignment() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int y = comp.getY();

            for (Component current : selection) {
                current.setLocation(current.getX(), y);
            }
        }
    }

    //向下对齐
    public void adjustBottomAlignment() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int bottom_y = comp.getY() + comp.getHeight();

            for (Component current : selection) {
                current.setLocation(current.getX(), bottom_y - current.getHeight());
            }
        }
    }

    //向中对齐
    public void adjustMiddleAlignment() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int middle_y = comp.getY() + (comp.getHeight() / 2);

            for (Component current : selection) {
                current.setLocation(current.getX(), middle_y - (current.getHeight() / 2));
            }
        }
    }

    //同一宽度
    public void adjustSameWidth() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int width = comp.getWidth();

            for (Component current : selection) {
                current.setSize(width, current.getHeight());
            }
        }
    }

    //同一高度
    public void adjustSameHeight() {
        if (isBoundsAdjustable()) {
            Component comp = selection.get(0);
            int height = comp.getHeight();

            for (Component current : selection) {
                current.setSize(current.getWidth(), height);
            }
        }
    }

    public void clearSelection() {
        selection.clear();
    }

    public void clearClipBoard() {
        clip_board.clear();
    }

    public void addComp2ClipBoard(Component comp) {
        clip_board.add(comp);
    }

    public void removeCompFromClipBoard(Component comp) {
        clip_board.remove(comp);
    }

    public void addComp2ClipBoard(Collection<? extends Component> comps) {
        clip_board.addAll(comps);
    }

    public boolean hasSelection() {
        return !selection.isEmpty();
    }

    public boolean isRootSelected() {
        return hasSelection() && selection.contains(designer.getRootComponent());
    }

    public boolean isClipBoardEmpty() {
        return clip_board.isEmpty();
    }

    public boolean isBoundsAdjustable() {
        return isSelectedResizable() && (selection.size() > 1);
    }

    /**
     * 删除当前所有选择的组件
     */
    public void deleteSelection() {
        //找出所有选中组件的根部
        ArrayList<Component> roots = getSelectedRoots();

        if (!roots.isEmpty()) {
            for (Component component : roots) {
                removeBeanFromContainer(component);
            }
            //触发事件
            fireComponentDeleted();
            //清除被选中的组件
            clearSelection();
            designer.repaint();
        }
    }

    private void fireComponentDeleted() {
        DesignerEvent evt = new DesignerEvent(this);
        evt.setDeletedComponents(selection);
        designer.getEditListenerTable().fireComponentDeleted(evt);
    }

    public void removeComponent(Component component) {
        if (selection.contains(component)) {
            selection.remove(component);
        }

        //从顶层容器中清除这些根组件
        removeBeanFromContainer(component);
        designer.repaint();
    }

    public void setHotspotBounds(Rectangle rect) {
        hotspot_bounds = rect;
    }

    public Rectangle getHotspotBounds() {
        return hotspot_bounds;
    }

    public void setSelectedComponents(Collection<Component> components) {
        selection.clear();
        selection.addAll(components);
        fireComponentSelected();
    }

    public ArrayList<Component> getSelectedComponents() {
        return selection;
    }

    public ArrayList<Component> getClipBoard() {
        return clip_board;
    }

    private void removeBeanFromContainer(Component component) {
        Container parent = component.getParent();
        if(parent instanceof JViewport){
            parent=parent.getParent();
        }
        ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, parent);
        if (adapter instanceof ContainerAdapter) {
            ((ContainerAdapter)adapter).removeComponent(component);
        } else {
            //删除其根组件,同时就删除了同时被选择的叶子组件
            parent.remove(component);
        }
        LayoutManager layout = parent.getLayout();

        if (layout != null) {
            //刷新组件容器的布局
            Util.layoutContainer(parent);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -