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

📄 swingbuilder.java

📁 Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业务
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    protected void nodeCompleted(Object parent, Object node) {
        // set models after the node has been completed
        if (node instanceof TableModel && parent instanceof JTable) {
            JTable table = (JTable) parent;
            TableModel model = (TableModel) node;
            table.setModel(model);
        }
        if (node instanceof Startable) {
            Startable startable = (Startable) node;
            startable.start();
        }
        if (node instanceof Window) {
            if (!containingWindows.isEmpty() && containingWindows.getLast() == node) {
                containingWindows.removeLast();
            }
        }
    }

    protected Object createNode(Object name) {
        return createNode(name, Collections.EMPTY_MAP);
    }

    protected Object createNode(Object name, Object value) {
        if (passThroughNodes.containsKey(name) && (value != null) && ((Class)passThroughNodes.get(name)).isAssignableFrom(value.getClass())) {
            // value may need to go into containing windows list
            if (value instanceof Window) {
                containingWindows.add(value);
            }
            return value;
        }
        else if (value instanceof String) {
            Object widget = createNode(name);
            if (widget != null) {
                InvokerHelper.invokeMethod(widget, "setText", value);
            }
            return widget;
        }
        else {
        	throw new MissingMethodException((String) name, getClass(), new Object[] {value}, false);
        }
    }

    protected Object createNode(Object name, Map attributes, Object value) {
        if (passThroughNodes.containsKey(name) && (value != null) && ((Class)passThroughNodes.get(name)).isAssignableFrom(value.getClass())) {
            // value may need to go into containing windows list
            if (value instanceof Window) {
                containingWindows.add(value);
            }
            handleWidgetAttributes(value, attributes);
            return value;
        }
        else { 
            Object widget = createNode(name, attributes);
            if (widget != null) {
                InvokerHelper.invokeMethod(widget, "setText", value.toString());
            }
            return widget;
        }
    }
    
    protected Object createNode(Object name, Map attributes) {
        String widgetName = (String) attributes.remove("id");
        constraints = attributes.remove("constraints");
        Object widget = null;
        if (passThroughNodes.containsKey(name)) {
            widget = attributes.get(name);
            if ((widget != null) && ((Class)passThroughNodes.get(name)).isAssignableFrom(widget.getClass())) {
                // value may need to go into containing windows list
                if (widget instanceof Window) {
                    containingWindows.add(widget);
                }
                attributes.remove(name);
            }
            else {
                widget = null;
            }
        }
        if (widget == null) {
            Factory factory = (Factory) factories.get(name);
            if (factory != null) {
                try {
                    widget = factory.newInstance(attributes);
                    if (widgetName != null) {
                        widgets.put(widgetName, widget);
                    }
                    if (widget == null) {
                        log.log(Level.WARNING, "Factory for name: " + name + " returned null");
                    }
                    else {
                        if (log.isLoggable(Level.FINE)) {
                            log.fine("For name: " + name + " created widget: " + widget);
                        }
                    }
                }
                catch (Exception e) {
                    throw new RuntimeException("Failed to create component for" + name + " reason: " + e, e);
                }
            }
            else {
                log.log(Level.WARNING, "Could not find match for name: " + name);
            }
        }
        handleWidgetAttributes(widget, attributes);
        return widget;
    }

    protected void handleWidgetAttributes(Object widget, Map attributes) {
        if (widget != null) {
            if (widget instanceof Action) {
                /** @todo we could move this custom logic into the MetaClass for Action */
                Action action = (Action) widget;

                Closure closure = (Closure) attributes.remove("closure");
                if (closure != null && action instanceof DefaultAction) {
                    DefaultAction defaultAction = (DefaultAction) action;
                    defaultAction.setClosure(closure);
                }

                Object accel = attributes.remove("accelerator");
                KeyStroke stroke = null;
                if (accel instanceof KeyStroke) {
                    stroke = (KeyStroke) accel;
                } else if (accel != null) {
                    stroke = KeyStroke.getKeyStroke(accel.toString());
                }
                action.putValue(Action.ACCELERATOR_KEY, stroke);

                Object mnemonic = attributes.remove("mnemonic");
                if ((mnemonic != null) && !(mnemonic instanceof Number)) {
                    mnemonic = new Integer(mnemonic.toString().charAt(0));
                }
                action.putValue(Action.MNEMONIC_KEY, mnemonic);

                for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String actionName = (String) entry.getKey();    // todo dk: misleading naming. this can be any property name

                    // typically standard Action names start with upper case, so lets upper case it            
                    actionName = capitalize(actionName);            // todo dk: in general, this shouldn't be capitalized
                    Object value = entry.getValue();

                    action.putValue(actionName, value);
                }

            }
            else {
                // some special cases...
                if (attributes.containsKey("buttonGroup")) {
                    Object o = attributes.get("buttonGroup");
                    if ((o instanceof ButtonGroup) && (widget instanceof AbstractButton)) {
                        ((AbstractButton)widget).getModel().setGroup((ButtonGroup)o);
                        attributes.remove("buttonGroup");
                    }
                }

                // this next statement nd if/else is a workaround until GROOVY-305 is fixed
                Object mnemonic = attributes.remove("mnemonic");
                if ((mnemonic != null) && (mnemonic instanceof Number)) {
                    InvokerHelper.setProperty(widget, "mnemonic", new Character((char)((Number)mnemonic).intValue()));
                } 
                else if (mnemonic != null) {
                    InvokerHelper.setProperty(widget, "mnemonic", new Character(mnemonic.toString().charAt(0)));
                } 

                // set the properties
                for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String property = entry.getKey().toString();
                    Object value = entry.getValue();
                    InvokerHelper.setProperty(widget, property, value);
                }
            }
        }
    }

    protected String capitalize(String text) {
        char ch = text.charAt(0);
        if (Character.isUpperCase(ch)) {
            return text;
        }
        StringBuffer buffer = new StringBuffer(text.length());
        buffer.append(Character.toUpperCase(ch));
        buffer.append(text.substring(1));
        return buffer.toString();
    }

    protected void registerWidgets() {
        //
        // non-widget support classes
        //
        registerBeanFactory("action", DefaultAction.class);
        passThroughNodes.put("action", javax.swing.Action.class);
        registerBeanFactory("buttonGroup", ButtonGroup.class);
        registerFactory("map", new Factory() {      // todo dk: is that still needed?
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return properties;
            }
        });
        // ulimate pass through type
        passThroughNodes.put("widget", java.awt.Component.class);

        //
        // standalone window classes
        //
        registerFactory("dialog", new Factory() {
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return createDialog(properties);
            }
        });
        registerFactory("frame", new Factory() {
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return createFrame(properties);
            }
        });
        registerBeanFactory("fileChooser", JFileChooser.class);
        registerFactory("frame", new Factory() {        // todo dk: frame registered twice ???
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return createFrame(properties);
            }
        });
        registerBeanFactory("optionPane", JOptionPane.class);
        registerFactory("window", new Factory() {
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return createWindow(properties);
            }
        });
        
        //
        // widgets
        //
        registerBeanFactory("button", JButton.class);
        registerBeanFactory("checkBox", JCheckBox.class);
        registerBeanFactory("checkBoxMenuItem", JCheckBoxMenuItem.class);
        registerBeanFactory("colorChooser", JColorChooser.class);
        registerFactory("comboBox", new Factory() {
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return createComboBox(properties);
            }
        });
        registerBeanFactory("desktopPane", JDesktopPane.class);
        registerBeanFactory("editorPane", JEditorPane.class);
        registerFactory("formattedTextField", new Factory() {
            public Object newInstance(Map properties)
                throws InstantiationException, InstantiationException, IllegalAccessException {
                return createFormattedTextField(properties);
            }
        });
        registerBeanFactory("internalFrame", JInternalFrame.class);
        registerBeanFactory("label", JLabel.class);
        registerBeanFactory("layeredPane", JLayeredPane.class);
        registerBeanFactory("list", JList.class);
        registerBeanFactory("menu", JMenu.class);
        registerBeanFactory("menuBar", JMenuBar.class);
        registerBeanFactory("menuItem", JMenuItem.class);
        registerBeanFactory("panel", JPanel.class);
        registerBeanFactory("passwordField", JPasswordField.class);
        registerBeanFactory("popupMenu", JPopupMenu.class);
        registerBeanFactory("progressBar", JProgressBar.class);
        registerBeanFactory("radioButton", JRadioButton.class);
        registerBeanFactory("radioButtonMenuItem", JRadioButtonMenuItem.class);
        registerBeanFactory("scrollBar", JScrollBar.class);
        registerBeanFactory("scrollPane", JScrollPane.class);
        registerBeanFactory("separator", JSeparator.class);
        registerBeanFactory("slider", JSlider.class);
        registerBeanFactory("spinner", JSpinner.class);
        registerFactory("splitPane", new Factory() {
            public Object newInstance(Map properties) {
                JSplitPane answer = new JSplitPane();
                answer.setLeftComponent(null);
                answer.setRightComponent(null);
                answer.setTopComponent(null);
                answer.setBottomComponent(null);
                return answer;
            }
        });
        registerBeanFactory("tabbedPane", JTabbedPane.class);
        registerBeanFactory("table", JTable.class);
        registerBeanFactory("textArea", JTextArea.class);
        registerBeanFactory("textPane", JTextPane.class);
        registerBeanFactory("textField", JTextField.class);
        registerBeanFactory("toggleButton", JToggleButton.class);
        registerBeanFactory("toolBar", JToolBar.class);
        //registerBeanFactory("tooltip", JToolTip.class); // doens't work, user toolTipText property
        registerBeanFactory("tree", JTree.class);
        registerBeanFactory("viewport", JViewport.class); // sub class?

        //
        // MVC models   
        //
        registerBeanFactory("boundedRangeModel", DefaultBoundedRangeModel.class);

        // spinner models
	registerBeanFactory("spinnerDateModel", SpinnerDateModel.class);
        registerBeanFactory("spinnerListModel", SpinnerListModel.class);
        registerBeanFactory("spinnerNumberModel", SpinnerNumberModel.class);

	// table models

⌨️ 快捷键说明

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