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

📄 util.java

📁 SWING的界面UI包 SWING的界面UI包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                UIManager.setLookAndFeel(idelnf);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取焦点组件所在的顶层容器
     */
    public static Container getHotspotContainer(SwingDesigner designer, Component comp) {
        while (comp != null) {
            ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, comp);

            if (adapter instanceof ContainerAdapter && Util.isDesigning(comp)) {
                return (Container) comp;
            }

            comp = comp.getParent();
        }

        return null;
    }

    public static HoverPainter getContainerPainter(SwingDesigner designer, Component container) {

        //容器组件的适配器
        ComponentAdapter componentAdapter = AdapterBus.getComponentAdapter(designer, container);

        if (componentAdapter instanceof ContainerAdapter) {
            ContainerAdapter containerAdapter = (ContainerAdapter) componentAdapter;
            HoverPainter painter = containerAdapter.getPainter();
            if (painter != null) {
                return painter;
            }
        }
        //否则,看当前的容器是否有布局管理器
        Container parent = (Container) container;
        LayoutManager layout = parent.getLayout();

        if (layout != null) {
            //如果有,则获取其布局管理器的适配器
            LayoutAdapter layoutAdapter = AdapterBus.getLayoutAdapter(designer, parent);
            HoverPainter painter = layoutAdapter.getPainter();
            if (painter != null) {
                return painter;
            } else {
                return new NullLayoutPainter(designer, parent);
            }
        } else {
            //设置一个缺省的提示渲染器
            return new NullLayoutPainter(designer, parent);
        }
    }

    /**
     * 恢复双缓冲状态,dbcomponents保存着初始状态为启动双缓冲的组件
     */
    public static void resetBuffer(ArrayList<JComponent> dbcomponents) {
        for (JComponent jcomponent : dbcomponents) {
            jcomponent.setDoubleBuffered(true);
        }
    }

    /**
     * 禁止双缓冲状态,并将初始状态为启动双缓冲的组件保存到dbcomponents中
     */
    public static void disableBuffer(Component comp, ArrayList<JComponent> dbcomponents) {
        if ((comp instanceof JComponent) && ((JComponent) comp).isDoubleBuffered()) {
            JComponent jcomponent = (JComponent) comp;

            dbcomponents.add(jcomponent);
            jcomponent.setDoubleBuffered(false);
        }

        if (comp instanceof Container) {
            Container container = (Container) comp;
            int count = container.getComponentCount();

            if (count > 0) {
                for (int i = 0; i < count; i++) {
                    Component component = container.getComponent(i);

                    disableBuffer(component, dbcomponents);
                }
            }
        }
    }

    public static Component getFirstInvisibleParent(Component comp) {
        Component parent = comp;

        while ((parent != null) && parent.isVisible()) {
            parent = parent.getParent();
        }

        return parent;
    }

    public static int indexOfComponent(Container container, Component target) {
        int count = container.getComponentCount();

        for (int i = 0; i < count; i++) {
            Component child = container.getComponent(i);

            if (child == target) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 计算组件root相对于其顶层容器的可见区域
     */
    public static Rectangle computeVisibleRectRel2Root(Component root) {
        Container container = findAncestorScrollPane(root);

        if (container == null) {
            return getRelativeBounds(root);
        } else {
            //如果是JScrollPane的子组件,需要计算其viewport与改组件的交叉的可见区域
            return getBoundsRel2Parent(root, container);
        }
    }

    /**
     * 计算组件root相对于其顶层容器的可见区域
     */
    public static Rectangle computeVisibleRect(Component root) {
        Rectangle root_bounds = Util.getRelativeBounds(root);
        Rectangle rect = computeVisibleRectRel2Root(root);
        rect.x -= root_bounds.x;
        rect.y -= root_bounds.y;

        return rect;
    }

    private static Rectangle getBoundsRel2Parent(Component child, Container parent) {
        Rectangle cRect = getRelativeBounds(child);
        Rectangle pRect = getRelativeBounds(parent);
        Rectangle bounds = new Rectangle();
        Rectangle.intersect(cRect, pRect, bounds);

        return bounds;
    }

    public static Container findAncestorScrollPane(Component p) {
        if ((p == null) || !(p instanceof Container)) {
            return null;
        }

        Container container = (Container) p;

        if (container instanceof JScrollPane) {
            return (JScrollPane) container;
        }

        Container c = p.getParent();

        return findAncestorScrollPane(c);
    }

    public static Class<? extends PropertyEditor> getPropertyEditorClass(Class propType) {
        return propertyEditorClasses.get(propType);
    }

    public static void showMessage(String message, Component editorComponent) {
        JOptionPane.showMessageDialog(editorComponent, message, "Validation Error", JOptionPane.ERROR_MESSAGE);
    }

    public static boolean isStringNull(String string) {
        return (string == null) || (string.trim().length() == 0);
    }

    public static Class<? extends TableCellRenderer> getTableCellRendererClass(Class propType) {
        return cellRendererClasses.get(propType);
    }

    public static Class<? extends SourceCoder> getSourceCoder(Class propType) {
        Class<? extends SourceCoder> clazz = sourceCoders.get(propType);
        if (clazz != null) {
            return clazz;
        }
        Class[] interfaces = propType.getInterfaces();
        if (interfaces != null) {
            for (Class ins : interfaces) {
                clazz = getSourceCoder(ins);
                if (clazz != null) {
                    return clazz;
                }
            }
        }
        if (!propType.isInterface()) {
            Class superclass = propType.getSuperclass();
            if (superclass == null) {
                return null;
            }
            clazz = getSourceCoder(superclass);
            if (clazz != null) {
                return clazz;
            }
        }
        return null;
    }

    public static void set12FontFor(Component container) {
        container.setFont(new Font("Dialog", 0, 12));
        if (container instanceof Container) {
            int count = ((Container) container).getComponentCount();
            for (int i = 0; i < count; i++) {
                Component component = ((Container) container).getComponent(i);
                set12FontFor(component);
            }
        }
    }

    public static boolean isPropertyChanged(LookAndFeel lnf, Object bean, PropertyDescriptor property) {
        if (property.getName().equals("preferredSize")) {
            Component component = (Component) bean;
            return component.isPreferredSizeSet();
        } else if (property.getName().equals("minimumSize")) {
            Component component = (Component) bean;
            return component.isMinimumSizeSet();
        } else if (property.getName().equals("minimumSize")) {
            Component component = (Component) bean;
            return component.isMaximumSizeSet();
        } else if (property.getName().equals("name")) {
            return false;
        } else if (property.getName().equals("text")) {
            return true;
        } else if (needGenCode(property)) {
            Object default_value = getDefaultValue(lnf, property);
            Object value = readBeanValue(bean, property);
            return isChanged(value, default_value);
        } else {
            return false;
        }
    }

    public static Object getDefaultValue(LookAndFeel lnf, PropertyDescriptor property) {
        HashMap<String, DefaultValue> dValues = (HashMap<String, DefaultValue>) property.getValue("default-value");
        DefaultValue dValue = dValues.get(lnf.getName());
        Class beanClass = (Class) property.getValue("bean-class");
        String beanName = beanClass.getName();
        if (!dValue.isSet()) {
            HashMap<String, Object> objects = lnf_beans.get(lnf.getName());
            if (objects == null) {
                objects = new HashMap<String, Object>();
                lnf_beans.put(lnf.getName(), objects);
            }
            Object bean = objects.get(beanName);
            if (bean == null) {
                bean = createBean(lnf, beanName);
                objects.put(beanName, bean);
            }
            Object value = Util.readBeanValue(bean, property);
            dValue.setValue(value);
        }
        return dValue.getValue();
    }

    private static Object createBean(LookAndFeel lnf, String beanName) {
        LookAndFeel ideLnf = UIManager.getLookAndFeel();
        try {
            UIManager.setLookAndFeel(lnf);
            return Beans.instantiate(Util.class.getClassLoader(), beanName);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            try {
                UIManager.setLookAndFeel(ideLnf);
            } catch (Exception ex) {
            }
        }
    }
    private static HashMap<String, HashMap<String, Object>> lnf_beans = new HashMap<String, HashMap<String, Object>>();

    private static boolean needGenCode(PropertyDescriptor property) {
        String gencode = (String) property.getValue("gencode");
        if (Util.isStringNull(gencode)) {
            return true;
        } else {
            return gencode.toLowerCase().trim().equals("true");
        }
    }

    @SuppressWarnings(value = "unchecked")
    public static boolean isChanged(Object old_value, Object value) {
        if (old_value != null && value != null && old_value != null) {
            Class old_class = old_value.getClass();
            Class new_class = value.getClass();
            if (old_class != new_class) {
                return false;
            }
            Comparator comparator = getComparator(old_class);
            if (comparator != null) {
                return comparator.compare(old_value, value) != 0;
            } else if (old_value == null) {
                return !value.equals(old_value);
            } else {
                return !old_value.equals(value);
            }
        } else {
            return false;
        }
    }

    public static Comparator getComparator(Class propType) {
        Comparator comparator = comparators.get(propType);
        if (comparator != null) {
            return comparator;
        }
        Class[] interfaces = propType.getInterfaces();
        if (interfaces != null) {
            for (Class ins : interfaces) {
                comparator = getComparator(ins);
                if (comparator != null) {
                    return comparator;
                }
            }
        }
        if (!propType.isInterface()) {
            Class superclass = propType.getSuperclass();
            if (superclass == null) {
                return null;
            }
            comparator = getComparator(superclass);

⌨️ 快捷键说明

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