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

📄 codeseed.java

📁 SWING的界面UI包 SWING的界面UI包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                tabs += "\t";            }            code = code.replaceAll("\\Q" + Constants.VAR_CONTAINER + "\\E", parentName);            block.addSourceCode(code, tabs);        }    }    private void initContainerChild(Container container, Component child) {        if (declaration == null) {            declaration = new CodeBlock(CodeType.declare, null);            copy.insetCodeAt(init_line_no, declaration);        }        CodeBlock child_block = findGetChild(child);        String cname = Util.getComponentName(child);        String classname = child.getClass().getName();        declaration.addCodeLine("\tprivate " + classname + " " + cname + ";");        if (child_block == null) {            child_block = new CodeBlock(CodeType.get, cname);        }        copy.insetCodeAt(init_line_no, child_block);        ArrayList<CodeBlock> custom_codes = new ArrayList<CodeBlock>();        ArrayList<CodeBlock> body_codes = child_block.getCodes();        if (body_codes != null) {            for (CodeBlock line_block : body_codes) {                if (line_block.getType() == CodeType.custom) {                    custom_codes.add(line_block);                }            }        }        child_block.clearCodes();        child_block.addCodeLine("\tprivate " + classname + " " + Util.getGetName(cname) + "(){");        child_block.addCodeLine("\t\tif(" + cname + " == null){");        child_block.addCodeLine("\t\t\t" + cname + " = new " + classname + "();");        initComponentProperties(child_block, child);        initComponentChildren(child_block, child);        initComponentEvents(child_block, child);        child_block.addAllCode(custom_codes);        child_block.addCodeLine("\t\t}");        child_block.addCodeLine("\t\treturn " + cname + ";");        child_block.addCodeLine("\t}");    }    private CodeBlock findGetChild(Component child) {        String cname = Util.getComponentName(child);        for (CodeBlock block : current_code.getCodes()) {            if (block.getType() == CodeType.get) {                if (cname.equals(block.getParam())) {                    return block;                }            }        }        return null;    }    private void initRootComponentEvents(CodeBlock init_block, Component rootComponent) {        JComponent jcomponent = AdapterBus.getJComponent(rootComponent);        HashMap<EventSetDescriptor, HashMap<String, EventHandler>> eventHandlers = (HashMap<EventSetDescriptor, HashMap<String, EventHandler>>) jcomponent.getClientProperty("event.handler");        if (eventHandlers != null) {            Set<EventSetDescriptor> keys = eventHandlers.keySet();            for (EventSetDescriptor key : keys) {                HashMap<String, EventHandler> handlers = eventHandlers.get(key);                if (!handlers.isEmpty() && hasHandler(handlers)) {                    CodeBlock listener_block = new CodeBlock(CodeType.listener, key.getName());                    init_block.addCode(listener_block);                    initRootListenerBlock(key, listener_block, handlers);                }            }        }    }    private void initRootListenerBlock(EventSetDescriptor esd, CodeBlock listener_block, HashMap<String, EventHandler> handlers) {        listener_block.addCodeLine("\t\t" + esd.getAddListenerMethod().getName() + "(new " + esd.getListenerType().getName() + "(){");        MethodDescriptor[] methods = esd.getListenerMethodDescriptors();        for (MethodDescriptor method : methods) {            String code_line = "\t\t\tpublic void " + method.getMethod().getName() + "(";            Class[] paraTypes = method.getMethod().getParameterTypes();            for (int i = 0; i < paraTypes.length; i++) {                if (i != 0) {                    code_line += ",";                }                code_line += paraTypes[i].getName() + " event";            }            code_line += ") {";            listener_block.addCodeLine(code_line);            String mHandler = method.getName();            EventHandler handler = handlers.get(mHandler);            String code_buffer = handler.getCode_buffer();            if (code_buffer != null) {                CodeBlock body_block = new CodeBlock(CodeType.event, mHandler);                body_block.addSourceCode(code_buffer);                listener_block.addCode(body_block);            }            listener_block.addCodeLine("\t\t\t}");        }        listener_block.addCodeLine("\t\t});");    }    private boolean hasHandler(HashMap<String, EventHandler> hm) {        Set<String> keys = hm.keySet();        for (String key : keys) {            EventHandler h = (EventHandler) hm.get(key);            if (h != null && h.getCode_buffer() != null) {                return true;            }        }        return false;    }    private void initRootComponentProperties(CodeBlock init_block, Component rootComponent) {        try {            JComponent jRootComponent = AdapterBus.getJComponent(rootComponent);            LayoutManager layout = jRootComponent.getLayout();            boolean default_layout = rootComponent instanceof RootPaneContainer && layout != null && layout instanceof BorderLayout ||                    rootComponent instanceof JPanel && layout != null && layout instanceof FlowLayout;            if (!default_layout) {                if (layout == null) {                    init_block.addCodeLine("\t\tthis.setLayout(null);");                } else {                    ContainerAdapter adapter = (ContainerAdapter) AdapterBus.createComponentAdapter(designer, rootComponent);                    String code = adapter.getLayoutCode();                    if (code != null) {                        code = code.replaceAll("\\Q" + Constants.VAR_CONTAINER + "\\E", "this");                        init_block.addSourceCode(code, "\t\t");                    }                }            }            BeanInfo info = Introspector.getBeanInfo(rootComponent.getClass());            PropertyDescriptor[] properties = info.getPropertyDescriptors();            for (PropertyDescriptor property : properties) {                if (Util.isPropertyChanged(designer.getDesignLnf(), rootComponent, property)) {                    Object beanValue = Util.readBeanValue(rootComponent, property);                    String objectString = beanValue == null ? "null" : beanValue.toString();                    SourceCoder coder = getSourceCoder(beanValue, property);                    if (coder != null) {                        objectString = coder.getJavaCode(beanValue);                    }                    String code_line = "\t\t" + property.getWriteMethod().getName() + "(" + objectString + ");";                    init_block.addCodeLine(code_line);                }            }        } catch (Exception ex) {            ex.printStackTrace();        }    }    public void initSourceCode() {        StringWriter sw = new StringWriter();        PrintWriter writer = new PrintWriter(sw);        if (packager != null) {            writer.println("package " + packager + ";");        }        writer.println("public class " + class_name + " extends " + guitype.getCode() + " {");        writer.println("\tpublic " + class_name + "(){");        writer.println("\t\tinitComponents();");        writer.println("\t}");        writer.println("\t//[init(400, 300)");        writer.println("\tprivate void initComponents(){");        writer.println("\t}");        writer.println("\t//]");        writer.println("}");        writer.close();        source_code = sw.toString();    }    private int init_line_no;    private CodeBlock declaration;    private CodeBlock copy;    private void reset() {        init_line_no = -1;        declaration = null;        copy = null;    }    public String generateSourceCode(Component rootComponent) {        reset();        copy = new CodeBlock(CodeType.source, null);        ArrayList<CodeBlock> blocks = current_code.getCodes();        CodeBlock init_block = null;        for (int i = 0; i < blocks.size(); i++) {            CodeBlock block = blocks.get(i);            switch (block.getType()) {                case packager:                case importer:                case init:                    init_line_no = copy.getBlockCount() + 1;                    init_block = block;                case code:                    copy.addCode(block);                    break;            }        }        ArrayList<CodeBlock> custom_codes = new ArrayList<CodeBlock>();        blocks = init_block.getCodes();        for (int i = 0; i < blocks.size(); i++) {            CodeBlock block = blocks.get(i);            CodeType type = block.getType();            if (type == CodeType.custom) {                custom_codes.add(block);            }        }        init_block.clearCodes();        init_block.setParam("" + rootComponent.getWidth() + ", " + rootComponent.getHeight());        initRootComponent(init_block, custom_codes, rootComponent);        current_code = copy;        source_code = current_code.getSourceCode();        return source_code;    }    private Component[] getChildren(Container container) {        ArrayList<Component> components = new ArrayList<Component>();        _collectDesigningComponents(container, components);        return components.toArray(new Component[0]);    }    private void _collectDesigningComponents(Container container, ArrayList<Component> components) {        int count = container.getComponentCount();        for (int i = 0; i < count; i++) {            Component child = container.getComponent(i);            if (Util.isDesigning(child)) {                components.add(child);            } else if (child instanceof Container) {                _collectDesigningComponents((Container) child, components);            }        }    }    public static File getCodeBufferDir() {        File codeBufferDir = new File(new File("."), ".swing_designer_buffer");        if (!codeBufferDir.exists()) {            codeBufferDir.mkdir();        }        return codeBufferDir;    }    private static File getCodeBufferFile() {        File codeBufferDir = getCodeBufferDir();        return new File(codeBufferDir, "temp.java");    }    private static void cleanCacheDir() {        deleteDir(getCodeBufferDir());    }    private static void deleteDir(File dir) {        if (dir.exists()) {            for (File file : dir.listFiles()) {                if (file.isDirectory()) {                    deleteDir(file);                } else {                    file.delete();                }            }            dir.delete();        }    }    public String generateSourceFile() {        try {            File bf = getCodeBufferFile();            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(bf)));            String cName = generatePrototype(writer);            writer.close();            return cName;        } catch (Exception ex) {            ex.printStackTrace();            return null;        }    }    public Component generateComponent(PrintWriter pw) {        try {            current_code = analyseCode();            String className = generateSourceFile();            File srcFile = saveToFile(className);            Class clazz = compileAndLoad(className, srcFile, pw);            if (clazz != null) {                Component comp = (Component) clazz.newInstance();                if (comp != null) {                    initComponentName(comp);                    initListeners(comp);                    comp.setSize(getInitSize());                    return comp;                }            }        } catch (Exception ex) {            ex.printStackTrace(pw);        }        return null;    }    private Dimension getInitSize() {        CodeBlock init_block = prototype.getInit();        String param = init_block.getParam();        if (param != null) {            int coma = param.indexOf(",");            if (coma != -1) {                String xStr = param.substring(0, coma).trim();                String yStr = param.substring(coma + 1).trim();                int x = Integer.parseInt(xStr);                int y = Integer.parseInt(yStr);                return new Dimension(x, y);            }        }        return new Dimension(400, 300);    }    private Component getComponentByName(Component root, String varName) {        try {            Class clazz = root.getClass();            Field field = clazz.getDeclaredField(varName);            field.setAccessible(true);            return (Component) field.get(root);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    private void initComponentName(Component comp) {        Class clazz = comp.getClass();        Field[] fields = clazz.getDeclaredFields();        for (Field field : fields) {            try {                field.setAccessible(true);                Component subcomp = (Component) field.get(comp);                if (!Util.isDesigning(subcomp)) {                    Util.setComponentName(subcomp, field.getName());                }            } catch (Exception ex) {                ex.printStackTrace();            }        }        if (!Util.isDesigning(comp)) {            Util.setComponentName(comp, getClassName());        }    }    public String getClassName() {        return class_name;    }    public String getSourceCode() {        return source_code;    }    public void setSourceCode(String source_code) {        this.source_code = source_code;    }    private File saveToFile(String cName) {        String file_name = cName;        String pack = null;        int dot = cName.lastIndexOf(".");        if (dot != -1) {            file_name = cName.substring(dot + 1);            pack = cName.substring(0, dot);            pack = pack.replace('.', '/');        }        file_name += ".java";        File bf = getCodeBufferFile();        if (pack != null) {            File dir = getCodeBufferDir();            File src_dir = new File(dir, pack);            if (!src_dir.exists()) {                src_dir.mkdirs();            }            File newfile = new File(src_dir, file_name);            bf.renameTo(newfile);            return newfile;        } else {            File newfile = new File(getCodeBufferDir(), file_name);            bf.renameTo(newfile);            return newfile;        }    }    public Item getGuitype() {        return guitype;    }    public void setGuitype(Item guitype) {        this.guitype = guitype;    }}

⌨️ 快捷键说明

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