Search in sources :

Example 16 with com.codename1.rad.ui

use of com.codename1.rad.ui in project CodenameOne by codenameone.

the class ResourceEditorApp method migrateGuiBuilder.

private static void migrateGuiBuilder(File projectDir, EditableResources res, String destPackageName) throws IOException {
    File propertiesFile = new File(projectDir, "codenameone_settings.properties");
    Properties props = new Properties();
    FileInputStream pIn = new FileInputStream(propertiesFile);
    props.load(pIn);
    pIn.close();
    if (props.getProperty("guiResource") == null) {
        System.out.println("Not a legacy GUI builder project!\nConversion failed!");
        System.exit(1);
        return;
    }
    UserInterfaceEditor.exportToNewGuiBuilderMode = true;
    String mainForm = props.getProperty("mainForm");
    File stateMachineBase = new File(projectDir, "src" + File.separatorChar + "generated" + File.separator + "StateMachineBase.java");
    StringBuilder stateMachineBaseSource = new StringBuilder("/**\n * This class was generated by the migration wizard, ultimately both it and the Statemachine can be removed.\n");
    stateMachineBaseSource.append(" * This class is no longer updated automatically\n");
    stateMachineBaseSource.append("*/\n");
    stateMachineBaseSource.append("package generated;\n");
    stateMachineBaseSource.append("\nimport com.codename1.ui.*;\n");
    stateMachineBaseSource.append("import com.codename1.ui.util.*;\n");
    stateMachineBaseSource.append("import com.codename1.ui.plaf.*;\n");
    stateMachineBaseSource.append("import java.util.Hashtable;\n");
    stateMachineBaseSource.append("import com.codename1.ui.events.*;\n\n");
    stateMachineBaseSource.append("public abstract class StateMachineBase extends UIBuilder {\n");
    stateMachineBaseSource.append("    private static final java.util.HashMap<String, Class> formNameToClassHashMap = new java.util.HashMap<String, Class>();");
    stateMachineBaseSource.append("    public static StateMachineBase instance;");
    stateMachineBaseSource.append("    protected void initVars() {}\n\n");
    stateMachineBaseSource.append("    protected void initVars(Resources res) {}\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(Resources res, String resPath, boolean loadTheme) {\n    instance = this;\n");
    stateMachineBaseSource.append("        startApp(res, resPath, loadTheme);\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    public Container startApp(Resources res, String resPath, boolean loadTheme) {\n");
    stateMachineBaseSource.append("        initVars();\n");
    stateMachineBaseSource.append("        if(loadTheme) {\n");
    stateMachineBaseSource.append("            if(res == null) {\n");
    stateMachineBaseSource.append("                try {\n");
    stateMachineBaseSource.append("                    if(resPath.endsWith(\".res\")) {\n");
    stateMachineBaseSource.append("                        res = Resources.open(resPath);\n");
    stateMachineBaseSource.append("                        System.out.println(\"Warning: you should construct the state machine without the .res extension to allow theme overlays\");\n");
    stateMachineBaseSource.append("                    } else {\n");
    stateMachineBaseSource.append("                        res = Resources.openLayered(resPath);\n");
    stateMachineBaseSource.append("                    }\n");
    stateMachineBaseSource.append("                } catch(java.io.IOException err) { err.printStackTrace(); }\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            initTheme(res);\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        if(res != null) {\n");
    stateMachineBaseSource.append("            setResourceFilePath(resPath);\n");
    stateMachineBaseSource.append("            setResourceFile(res);\n");
    stateMachineBaseSource.append("            Resources.setGlobalResources(res);");
    stateMachineBaseSource.append("            initVars(res);\n");
    stateMachineBaseSource.append("            return showForm(getFirstFormName(), null);\n");
    stateMachineBaseSource.append("        } else {\n");
    stateMachineBaseSource.append("            Form f = (Form)createContainer(resPath, getFirstFormName());\n");
    stateMachineBaseSource.append("            Resources.setGlobalResources(fetchResourceFile());");
    stateMachineBaseSource.append("            initVars(fetchResourceFile());\n");
    stateMachineBaseSource.append("            beforeShow(f);\n");
    stateMachineBaseSource.append("            f.show();\n");
    stateMachineBaseSource.append("            postShow(f);\n");
    stateMachineBaseSource.append("            return f;\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    protected String getFirstFormName() {\n");
    stateMachineBaseSource.append("        return \"");
    stateMachineBaseSource.append(mainForm);
    stateMachineBaseSource.append("\";\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    protected void initTheme(Resources res) {\n");
    stateMachineBaseSource.append("            String[] themes = res.getThemeResourceNames();\n");
    stateMachineBaseSource.append("            Resources.setGlobalResources(res);\n");
    stateMachineBaseSource.append("            if(themes != null && themes.length > 0) {\n");
    stateMachineBaseSource.append("                UIManager.getInstance().setThemeProps(res.getTheme(themes[0]));\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    public StateMachineBase() {\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(String resPath) {\n");
    stateMachineBaseSource.append("        this(null, resPath, true);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(Resources res) {\n");
    stateMachineBaseSource.append("        this(res, null, true);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(String resPath, boolean loadTheme) {\n");
    stateMachineBaseSource.append("        this(null, resPath, loadTheme);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(Resources res, boolean loadTheme) {\n");
    stateMachineBaseSource.append("        this(res, null, loadTheme);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public Form showForm(String resourceName, Command sourceCommand) {\n");
    stateMachineBaseSource.append("        try {\n");
    stateMachineBaseSource.append("            Form f = (Form)formNameToClassHashMap.get(resourceName).newInstance();\n");
    stateMachineBaseSource.append("            Form current = Display.getInstance().getCurrent();\n");
    stateMachineBaseSource.append("            if(current != null && isBackCommandEnabled() && allowBackTo(resourceName)) {\n");
    stateMachineBaseSource.append("                f.putClientProperty(\"previousForm\", current);\n");
    stateMachineBaseSource.append("                setBackCommand(f, new Command(getBackCommandText(current.getTitle())) {\n");
    stateMachineBaseSource.append("                    public void actionPerformed(ActionEvent evt) {\n");
    stateMachineBaseSource.append("                          back(null);\n");
    stateMachineBaseSource.append("                    }\n");
    stateMachineBaseSource.append("                });\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            if(sourceCommand != null && current != null && current.getBackCommand() == sourceCommand) {\n");
    stateMachineBaseSource.append("                f.showBack();\n");
    stateMachineBaseSource.append("            } else {\n");
    stateMachineBaseSource.append("                f.show();\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            return f;\n");
    stateMachineBaseSource.append("        } catch(Exception err) {\n");
    stateMachineBaseSource.append("            err.printStackTrace();\n");
    stateMachineBaseSource.append("            throw new RuntimeException(\"Form not found: \" + resourceName);\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void beforeShow(Form f) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public final void beforeShow__(Form f) {\n        beforeShow(f);\n");
    stateMachineBaseSource.append("        if(Display.getInstance().getCurrent() != null) {\n");
    stateMachineBaseSource.append("            exitForm(Display.getInstance().getCurrent());\n");
    stateMachineBaseSource.append("            invokeFormExit__(Display.getInstance().getCurrent());\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void exitForm(Form f) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void postShow(Form f) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public final void postShow__(Form f) {\n        postShow(f);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    private Container getRootComponent__(Component rootComponent) {\n");
    stateMachineBaseSource.append("        if(rootComponent.getParent() != null) {\n");
    stateMachineBaseSource.append("            return getRoot__(rootComponent.getParent());\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        return (Container)rootComponent;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    private Container getRoot__(Container rootComponent) {\n");
    stateMachineBaseSource.append("        Container p = rootComponent.getParent();\n");
    stateMachineBaseSource.append("        while(p != null) {\n");
    stateMachineBaseSource.append("            rootComponent = p;\n");
    stateMachineBaseSource.append("            p = rootComponent.getParent();\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        return rootComponent;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public Component findByName(String componentName, Container rootComponent) {\n");
    stateMachineBaseSource.append("        Container root = getRoot__(rootComponent);\n");
    stateMachineBaseSource.append("        return findByName__(componentName, root);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public Component findByName__(String componentName, Container root) {\n");
    stateMachineBaseSource.append("        int count = root.getComponentCount();\n");
    stateMachineBaseSource.append("        for(int iter = 0 ; iter < count ; iter++) {\n");
    stateMachineBaseSource.append("            Component c = root.getComponentAt(iter);\n");
    stateMachineBaseSource.append("            String n = c.getName();\n");
    stateMachineBaseSource.append("            if(n != null && n.equals(componentName)) {\n");
    stateMachineBaseSource.append("                return c;\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            if(c instanceof Container && ((Container)c).getLeadComponent() == null) {\n");
    stateMachineBaseSource.append("                c = findByName__(componentName, (Container)c);\n");
    stateMachineBaseSource.append("                if(c != null) {\n");
    stateMachineBaseSource.append("                    return c;\n");
    stateMachineBaseSource.append("                }\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        return null;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void handleComponentAction(Component c, ActionEvent event) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void handleComponentAction__(Component c, ActionEvent event) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void processCommand__(ActionEvent ev, Command cmd) {\n");
    stateMachineBaseSource.append("        processCommand(ev, cmd);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void back() {\n");
    stateMachineBaseSource.append("        back(null);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void back(Component sourceComponent) {\n");
    stateMachineBaseSource.append("        Form current = (Form)Display.getInstance().getCurrent().getClientProperty(\"previousForm\");\n");
    stateMachineBaseSource.append("        current.showBack();\n");
    stateMachineBaseSource.append("    }\n\n");
    StringBuilder formNameMapBuilder = new StringBuilder("static {");
    StringBuilder invokeFormExitBuilder = new StringBuilder("    private void invokeFormExit__(Form f) {\n");
    UserInterfaceEditor.componentNames = new HashMap<String, Class>();
    UserInterfaceEditor.commandList = new ArrayList<ActionCommand>();
    for (String uiName : res.getUIResourceNames()) {
        System.out.println("Processing: " + uiName);
        String fileName = convertToVarName(uiName);
        formNameMapBuilder.append("    formNameToClassHashMap.put(\"");
        formNameMapBuilder.append(uiName);
        formNameMapBuilder.append("\", ");
        formNameMapBuilder.append(destPackageName);
        formNameMapBuilder.append(".");
        formNameMapBuilder.append(fileName);
        formNameMapBuilder.append(".class);\n");
        String normalizedUiName = ResourceEditorView.normalizeFormName(uiName);
        if (RESEVERVED_WORDS.contains(fileName)) {
            fileName += "X";
        } else {
            try {
                if (Class.forName("java.lang." + fileName) != null) {
                    fileName += "X";
                }
            } catch (Throwable t) {
            // passed...
            }
        }
        File guiFile = new File(projectDir, "res" + File.separatorChar + "guibuilder" + File.separatorChar + destPackageName.replace('.', File.separatorChar) + File.separatorChar + fileName + ".gui");
        guiFile.getParentFile().mkdirs();
        File sourcePackageDir = new File(projectDir, "src" + File.separatorChar + destPackageName.replace('.', File.separatorChar));
        sourcePackageDir.mkdirs();
        File sourceFile = new File(sourcePackageDir, fileName + ".java");
        UIBuilderOverride u = new UIBuilderOverride();
        com.codename1.ui.Container cnt = u.createContainer(res, uiName);
        FileOutputStream fos = new FileOutputStream(guiFile);
        Writer w = new OutputStreamWriter(fos, "UTF-8");
        w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
        StringBuilder bld = new StringBuilder();
        UserInterfaceEditor.actionEventNames = new ArrayList<String>();
        UserInterfaceEditor.listNames = new ArrayList<String>();
        UserInterfaceEditor.persistToXML(cnt, cnt, bld, res, "");
        w.write(bld.toString());
        w.flush();
        w.close();
        fos = new FileOutputStream(sourceFile);
        w = new OutputStreamWriter(fos, "UTF-8");
        w.write("package ");
        w.write(destPackageName);
        w.write(";\n");
        w.write("\n");
        w.write("/**\n");
        w.write(" * GUI builder created Form\n");
        w.write(" */\n");
        w.write("public class ");
        w.write(fileName);
        String prePostCode;
        w.write(" extends com.codename1.ui.");
        if (cnt instanceof com.codename1.ui.Form) {
            invokeFormExitBuilder.append("        if(f.getName().equals(\"");
            invokeFormExitBuilder.append(uiName);
            invokeFormExitBuilder.append("\")) {\n");
            invokeFormExitBuilder.append("            exit");
            invokeFormExitBuilder.append(normalizedUiName);
            invokeFormExitBuilder.append("(f);\n        }\n");
            stateMachineBaseSource.append("    protected void before");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(Form f) {\n");
            stateMachineBaseSource.append("    }\n\n");
            stateMachineBaseSource.append("    public final void before");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("__(Form f) {\n        before");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(f);\n    }\n\n");
            stateMachineBaseSource.append("    protected void post");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(Form f) {\n");
            stateMachineBaseSource.append("    }\n\n");
            stateMachineBaseSource.append("    public final void post");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("__(Form f) {\n        post");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(f);\n    }\n\n");
            stateMachineBaseSource.append("    protected void exit");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(Form f) {\n");
            stateMachineBaseSource.append("    }\n\n");
        }
        if (cnt instanceof com.codename1.ui.Dialog) {
            w.write("Dialog");
            prePostCode = "\n    public void initComponent() {\n        generated.StateMachineBase.instance.beforeShow__(this);\n";
            prePostCode += "        generated.StateMachineBase.instance.before";
            prePostCode += normalizedUiName;
            prePostCode += "__(this);\n    }\n";
            prePostCode = "\n    public void onShow() {\n        generated.StateMachineBase.instance.postShow__(this);\n";
            prePostCode += "        generated.StateMachineBase.instance.post";
            prePostCode += normalizedUiName;
            prePostCode += "__(this);\n    }\n";
            prePostCode += "    protected void actionCommand(com.codename1.ui.Command cmd) {\n";
            prePostCode += "        generated.StateMachineBase.instance.processCommand__(new com.codename1.ui.events.ActionEvent(cmd), cmd);\n";
            prePostCode += "    }\n\n";
        } else {
            if (cnt instanceof com.codename1.ui.Form) {
                w.write("Form");
                prePostCode = "\n    public void show() {\n        generated.StateMachineBase.instance.beforeShow__(this);\n";
                prePostCode += "        generated.StateMachineBase.instance.before";
                prePostCode += normalizedUiName;
                prePostCode += "__(this);\n        super.show();\n        generated.StateMachineBase.instance.post";
                prePostCode += normalizedUiName;
                prePostCode += "__(this);\n    }\n";
                prePostCode += "    protected void actionCommand(com.codename1.ui.Command cmd) {\n";
                prePostCode += "        generated.StateMachineBase.instance.processCommand__(new com.codename1.ui.events.ActionEvent(cmd), cmd);\n";
                prePostCode += "    }\n\n";
            } else {
                w.write("Container");
                prePostCode = "";
            }
        }
        w.write(" {\n    public ");
        w.write(fileName);
        w.write("() {\n");
        w.write("        this(com.codename1.ui.util.Resources.getGlobalResources());\n");
        w.write("    }\n    \n    public ");
        w.write(fileName);
        w.write("(com.codename1.ui.util.Resources resourceObjectInstance) {\n");
        w.write("        initGuiBuilderComponents(resourceObjectInstance);\n");
        w.write("    }\n\n");
        w.write("//-- DON'T EDIT BELOW THIS LINE!!!\n\n    private void initGuiBuilderComponents(com.codename1.ui.util.Resources resourceObjectInstance) {}\n\n");
        w.write("//-- DON'T EDIT ABOVE THIS LINE!!!\n");
        for (String actionListenerNames : UserInterfaceEditor.actionEventNames) {
            w.write("\n    public void on");
            w.write(actionListenerNames);
            w.write("ActionEvent(com.codename1.ui.events.ActionEvent ev) {\n        ");
            w.write("generated.StateMachineBase.instance.handleComponentAction__((com.codename1.ui.Component)ev.getSource(), ev);\n        ");
            w.write("generated.StateMachineBase.instance.on");
            w.write(normalizedUiName);
            w.write("_");
            String normalizedActionListenerName = ResourceEditorView.normalizeFormName(actionListenerNames);
            w.write(normalizedActionListenerName);
            w.write("Action__((com.codename1.ui.Component)ev.getSource(), ev);\n    }\n\n");
            stateMachineBaseSource.append("    protected void on");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("_");
            stateMachineBaseSource.append(normalizedActionListenerName);
            stateMachineBaseSource.append("Action(Component cmp, ActionEvent ev) {\n    }\n\n");
            stateMachineBaseSource.append("    public void on");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("_");
            stateMachineBaseSource.append(normalizedActionListenerName);
            stateMachineBaseSource.append("Action__(Component cmp, ActionEvent ev) {\n        on");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("_");
            stateMachineBaseSource.append(normalizedActionListenerName);
            stateMachineBaseSource.append("Action(cmp, ev);\n    }\n\n");
        }
        w.write(prePostCode);
        w.write("}\n");
        w.flush();
        w.close();
    }
    formNameMapBuilder.append("}\n");
    invokeFormExitBuilder.append("}\n");
    stateMachineBaseSource.append(formNameMapBuilder);
    stateMachineBaseSource.append(invokeFormExitBuilder);
    ArrayList<String> uniqueNames = new ArrayList<String>();
    for (String cmpName : UserInterfaceEditor.componentNames.keySet()) {
        String nomName = ResourceEditorView.normalizeFormName(cmpName);
        if (uniqueNames.contains(nomName)) {
            continue;
        }
        uniqueNames.add(nomName);
        stateMachineBaseSource.append("    public ");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(" find");
        stateMachineBaseSource.append(nomName);
        stateMachineBaseSource.append("(Component root) {\n        return (");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(")findByName(\"");
        stateMachineBaseSource.append(cmpName);
        stateMachineBaseSource.append("\", getRootComponent__(root));\n    }\n\n");
        stateMachineBaseSource.append("    public ");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(" find");
        stateMachineBaseSource.append(nomName);
        stateMachineBaseSource.append("() {\n        return (");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(")findByName(\"");
        stateMachineBaseSource.append(cmpName);
        stateMachineBaseSource.append("\", Display.getInstance().getCurrent());\n    }\n\n");
    }
    ArrayList<Integer> commandIdsAdded = new ArrayList<Integer>();
    ArrayList<String> commandNamesAdded = new ArrayList<String>();
    for (ActionCommand cmd : UserInterfaceEditor.commandList) {
        String formName = (String) cmd.getClientProperty("FORMNAME");
        if (formName == null) {
            continue;
        }
        String normalizedCommandName = ResourceEditorView.normalizeFormName(formName) + ResourceEditorView.normalizeFormName(cmd.getCommandName());
        if (commandNamesAdded.contains(normalizedCommandName)) {
            continue;
        }
        if (commandIdsAdded.contains(cmd.getId())) {
            continue;
        }
        commandIdsAdded.add(cmd.getId());
        commandNamesAdded.add(normalizedCommandName);
        stateMachineBaseSource.append("    public static final int COMMAND_");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append(" = ");
        stateMachineBaseSource.append(cmd.getId());
        stateMachineBaseSource.append(";\n\n    protected boolean on");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append("() {\n        return false;\n    }\n\n");
    }
    stateMachineBaseSource.append("    protected void processCommand(ActionEvent ev, Command cmd) {\n");
    stateMachineBaseSource.append("        switch(cmd.getId()) {\n");
    commandIdsAdded.clear();
    commandNamesAdded.clear();
    for (ActionCommand cmd : UserInterfaceEditor.commandList) {
        String formName = (String) cmd.getClientProperty("FORMNAME");
        if (formName == null) {
            continue;
        }
        String normalizedCommandName = ResourceEditorView.normalizeFormName(formName) + ResourceEditorView.normalizeFormName(cmd.getCommandName());
        if (commandNamesAdded.contains(normalizedCommandName)) {
            continue;
        }
        if (commandIdsAdded.contains(cmd.getId())) {
            continue;
        }
        commandIdsAdded.add(cmd.getId());
        commandNamesAdded.add(normalizedCommandName);
        stateMachineBaseSource.append("\n        case COMMAND_");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append(":\n");
        if (cmd.getAction() != null && cmd.getAction().length() > 0) {
            if (!cmd.getAction().startsWith("$")) {
                stateMachineBaseSource.append("            showForm(\"");
                stateMachineBaseSource.append(cmd.getAction());
                stateMachineBaseSource.append("\", null);\n");
            }
        }
        stateMachineBaseSource.append("            if(on");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append("()) {\n");
        stateMachineBaseSource.append("                ev.consume();\n");
        stateMachineBaseSource.append("                return;\n");
        stateMachineBaseSource.append("            }\n");
        stateMachineBaseSource.append("            break;\n\n");
    }
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        if(ev.getComponent() != null) {\n");
    stateMachineBaseSource.append("            handleComponentAction(ev.getComponent(), ev);\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("\n}\n");
    FileOutputStream sbout = new FileOutputStream(stateMachineBase);
    sbout.write(stateMachineBaseSource.toString().getBytes("UTF-8"));
    sbout.close();
    props.remove("mainForm");
    props.remove("package");
    props.remove("guiResource");
    props.remove("baseClass");
    props.remove("userClass");
    FileOutputStream pOut = new FileOutputStream(propertiesFile);
    props.store(pOut, "Updated by GUI builder migration wizard");
    pOut.close();
    System.out.println("Conversion completed successfully!");
    System.exit(0);
}
Also used : UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Container(com.codename1.ui.Container) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 17 with com.codename1.rad.ui

use of com.codename1.rad.ui in project CodenameOne by codenameone.

the class AddAndScaleMultiImage method generate.

public void generate(File[] files, EditableResources res) {
    for (File f : files) {
        try {
            BufferedImage bi = ImageIO.read(f);
            EditableResources.MultiImage newImage = new EditableResources.MultiImage();
            int[] DPIS = new int[] { com.codename1.ui.Display.DENSITY_VERY_LOW, com.codename1.ui.Display.DENSITY_LOW, com.codename1.ui.Display.DENSITY_MEDIUM, com.codename1.ui.Display.DENSITY_HIGH, com.codename1.ui.Display.DENSITY_VERY_HIGH, com.codename1.ui.Display.DENSITY_HD, com.codename1.ui.Display.DENSITY_560, com.codename1.ui.Display.DENSITY_2HD, com.codename1.ui.Display.DENSITY_4K };
            EncodedImage[] images = new EncodedImage[DPIS.length];
            int imageCount = 0;
            JSpinner[] ws = { veryLowWidth, lowWidth, mediumWidth, highWidth, veryHighWidth, hdWidth, hd560Width, hd2Width, hd4kWidth };
            JSpinner[] hs = { veryLowHeight, lowHeight, mediumHeight, highHeight, veryHighHeight, hdHeight, hd560Height, hd2Height, hd4kHeight };
            if (squareImages.isSelected()) {
                hs = ws;
            }
            for (int iter = 0; iter < ws.length; iter++) {
                images[iter] = createScale(bi, ws[iter], hs[iter]);
                if (images[iter] != null) {
                    imageCount++;
                }
            }
            if (imageCount > 0) {
                int offset = 0;
                EncodedImage[] result = new EncodedImage[imageCount];
                int[] resultDPI = new int[imageCount];
                for (int iter = 0; iter < images.length; iter++) {
                    if (images[iter] != null) {
                        result[offset] = images[iter];
                        resultDPI[offset] = DPIS[iter];
                        offset++;
                    }
                }
                newImage.setDpi(resultDPI);
                newImage.setInternalImages(result);
                String destName = f.getName();
                int count = 1;
                while (res.containsResource(destName)) {
                    destName = f.getName() + " " + count;
                }
                res.setMultiImage(destName, newImage);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error reading file: " + f, "IO Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) JSpinner(javax.swing.JSpinner) File(java.io.File) EditableResources(com.codename1.ui.util.EditableResources)

Example 18 with com.codename1.rad.ui

use of com.codename1.rad.ui in project CodenameOne by codenameone.

the class AddThemeEntry method setKeyValue.

private void setKeyValue(String key, Object value) {
    int pos = key.indexOf(".");
    String attr;
    if (pos > -1) {
        componentName.setSelectedItem(key.substring(0, pos));
        attr = key.substring(pos + 1, key.length());
    } else {
        componentName.setSelectedIndex(0);
        attr = key;
    }
    pos = attr.indexOf('#');
    if (pos > -1) {
        attr = attr.substring(pos + 1);
    }
    if (attr.indexOf("fgColor") > -1) {
        deriveForegroundColor.setSelected(false);
        highlightTab(1);
        changeColorButtonFG.setEnabled(true);
        colorValueFG.setEnabled(true);
        if (value instanceof String) {
            colorValueFG.setText((String) value);
        } else {
            colorValueFG.setText(Integer.toHexString(((Number) value).intValue()));
        }
        return;
    }
    if (attr.indexOf("bgColor") > -1) {
        deriveBackgroundColor.setSelected(false);
        highlightTab(1);
        changeColorButtonBG.setEnabled(true);
        colorValueBG.setEnabled(true);
        if (value instanceof String) {
            colorValueBG.setText((String) value);
        } else {
            colorValueBG.setText(Integer.toHexString(((Number) value).intValue()));
        }
        return;
    }
    if (attr.indexOf("derive") > -1) {
        highlightTab(6);
        baseStyle.setEnabled(true);
        baseStyleType.setEnabled(true);
        defineAttribute.setSelected(false);
        String baseItemValue = (String) value;
        int keyPos = baseItemValue.indexOf('.');
        if (keyPos < 0) {
            baseStyle.setSelectedItem(baseItemValue);
        } else {
            String b = baseItemValue.substring(0, keyPos);
            String k = baseItemValue.substring(keyPos + 1);
            baseStyle.setSelectedItem(b);
            if (k.equals("sel")) {
                baseStyleType.setSelectedIndex(1);
                return;
            }
            if (k.equals("press")) {
                baseStyleType.setSelectedIndex(2);
                return;
            }
            if (k.equals("dis")) {
                baseStyleType.setSelectedIndex(3);
                return;
            }
        }
        return;
    }
    if (attr.indexOf("align") > -1) {
        highlightTab(2);
        deriveAlignment.setSelected(false);
        alignmentCombo.setEnabled(true);
        switch(((Number) value).intValue()) {
            case com.codename1.ui.Component.LEFT:
                alignmentCombo.setSelectedIndex(0);
                break;
            case com.codename1.ui.Component.RIGHT:
                alignmentCombo.setSelectedIndex(1);
                break;
            case com.codename1.ui.Component.CENTER:
                alignmentCombo.setSelectedIndex(2);
                break;
        }
        return;
    }
    if (attr.indexOf("textDecoration") > -1) {
        highlightTab(7);
        deriveTextDecoration.setSelected(false);
        textDecorationCombo.setEnabled(true);
        switch(((Number) value).intValue()) {
            case com.codename1.ui.plaf.Style.TEXT_DECORATION_UNDERLINE:
                textDecorationCombo.setSelectedIndex(1);
                break;
            case com.codename1.ui.plaf.Style.TEXT_DECORATION_STRIKETHRU:
                textDecorationCombo.setSelectedIndex(2);
                break;
            case com.codename1.ui.plaf.Style.TEXT_DECORATION_3D:
                textDecorationCombo.setSelectedIndex(3);
                break;
            case com.codename1.ui.plaf.Style.TEXT_DECORATION_3D_LOWERED:
                textDecorationCombo.setSelectedIndex(4);
                break;
            default:
                textDecorationCombo.setSelectedIndex(0);
                break;
        }
        return;
    }
    if (attr.indexOf("border") > -1) {
        highlightTab(5);
        customizeBorder.setEnabled(true);
        deriveBorder.setSelected(false);
        borderLabel.setText(Accessor.toString((Border) value));
        ((CodenameOneComponentWrapper) borderLabel).getCodenameOneComponent().getStyle().setBorder((Border) value);
        borderLabel.repaint();
        if (value != null && value instanceof Border) {
            currentBorder = (Border) value;
        } else {
            currentBorder = Border.getDefaultBorder();
        }
        return;
    }
    if (attr.indexOf("font") > -1) {
        highlightTab(7);
        Font font = (Font) value;
        deriveFont.setSelected(false);
        systemFont.setEnabled(true);
        bitmapFont.setEnabled(true);
        if (resources.getFontResourceNames() != null) {
            for (String fontName : resources.getFontResourceNames()) {
                if (font == resources.getFont(fontName)) {
                    // this is a bitmap font
                    bitmapFont.setSelected(true);
                    bitmapFontValue.setEnabled(true);
                    addNewBitmapFont.setEnabled(true);
                    bitmapFontValue.setSelectedItem(fontName);
                    return;
                }
            }
        }
        // this is a system font
        systemFont.setSelected(true);
        fontFace.setEnabled(true);
        fontSize.setEnabled(true);
        fontStyle.setEnabled(true);
        trueTypeFont.setEnabled(trueTypeFont.getModel().getSize() > 0);
        trueTypeFontSizeOption.setEnabled(trueTypeFont.getModel().getSize() > 0);
        trueTypeFontSizeValue.setEnabled(trueTypeFont.getModel().getSize() > 0);
        fontFace.setSelectedIndex(getSystemFontOffset(font.getFace(), FONT_FACE_VALUES));
        fontSize.setSelectedIndex(getSystemFontOffset(font.getSize(), FONT_SIZE_VALUES));
        fontStyle.setSelectedIndex(getSystemFontOffset(font.getStyle(), FONT_STYLE_VALUES));
        if (font instanceof EditorTTFFont) {
            EditorTTFFont ed = (EditorTTFFont) font;
            if (ed.getFontFile() != null) {
                trueTypeFont.setSelectedItem(ed.getFontFile().getName());
                trueTypeFontSizeOption.setSelectedIndex(ed.getSizeSetting());
                trueTypeFontSizeValue.setValue(new Double(ed.getActualSize()));
            } else {
                if (ed.getNativeFontName() != null) {
                    trueTypeFont.setSelectedItem(ed.getNativeFontName());
                    trueTypeFontSizeOption.setSelectedIndex(ed.getSizeSetting());
                    trueTypeFontSizeValue.setValue(new Double(ed.getActualSize()));
                }
            }
        }
        return;
    }
    if (attr.indexOf("bgImage") > -1) {
        highlightTab(0);
        updateBackgroundAttribute();
        for (int iter = 0; iter < imagesCombo.getModel().getSize(); iter++) {
            String name = (String) imagesCombo.getModel().getElementAt(iter);
            if (value == resources.getImage(name)) {
                imagesCombo.setSelectedItem(name);
                return;
            }
        }
        return;
    }
    if (attr.indexOf("transparency") > -1) {
        highlightTab(1);
        deriveTransparency.setSelected(false);
        transparencyValue.setEnabled(true);
        transparencyValue.setValue(Integer.valueOf((String) value));
        return;
    }
    if (attr.indexOf("padding") > -1) {
        highlightTab(3);
        derivePadding.setSelected(false);
        paddingBottom.setEnabled(true);
        paddingTop.setEnabled(true);
        paddingLeft.setEnabled(true);
        paddingRight.setEnabled(true);
        paddingBottomUnit.setEnabled(true);
        paddingTopUnit.setEnabled(true);
        paddingLeftUnit.setEnabled(true);
        paddingRightUnit.setEnabled(true);
        StringTokenizer tokenizer = new StringTokenizer((String) value, ", ");
        paddingTop.setValue(Float.parseFloat(tokenizer.nextToken()));
        paddingBottom.setValue(Float.parseFloat(tokenizer.nextToken()));
        paddingLeft.setValue(Float.parseFloat(tokenizer.nextToken()));
        paddingRight.setValue(Float.parseFloat(tokenizer.nextToken()));
        return;
    }
    if (attr.indexOf("padUnit") > -1) {
        byte[] padUnit = (byte[]) value;
        paddingBottomUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.BOTTOM]);
        paddingTopUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.TOP]);
        paddingLeftUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.LEFT]);
        paddingRightUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.RIGHT]);
        return;
    }
    if (attr.indexOf("margin") > -1) {
        highlightTab(4);
        deriveMargin.setSelected(false);
        marginBottom.setEnabled(true);
        marginTop.setEnabled(true);
        marginLeft.setEnabled(true);
        marginRight.setEnabled(true);
        marginBottomUnit.setEnabled(true);
        marginTopUnit.setEnabled(true);
        marginLeftUnit.setEnabled(true);
        marginRightUnit.setEnabled(true);
        StringTokenizer tokenizer = new StringTokenizer((String) value, ", ");
        marginTop.setValue(Float.parseFloat(tokenizer.nextToken()));
        marginBottom.setValue(Float.parseFloat(tokenizer.nextToken()));
        marginLeft.setValue(Float.parseFloat(tokenizer.nextToken()));
        marginRight.setValue(Float.parseFloat(tokenizer.nextToken()));
        return;
    }
    if (attr.indexOf("marUnit") > -1) {
        byte[] padUnit = (byte[]) value;
        marginBottomUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.BOTTOM]);
        marginTopUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.TOP]);
        marginLeftUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.LEFT]);
        marginRightUnit.setSelectedIndex(padUnit[com.codename1.ui.Component.RIGHT]);
        return;
    }
    if (attr.indexOf("bgType") > -1) {
        highlightTab(0);
        updateBackgroundAttribute();
        byte bgType = ((Byte) value).byteValue();
        for (int iter = 0; iter < BACKGROUND_VALUES.length; iter++) {
            if (bgType == BACKGROUND_VALUES[iter]) {
                backgroundType.setSelectedIndex(iter);
                break;
            }
        }
        return;
    }
    if (attr.indexOf("bgGradient") > -1) {
        highlightTab(0);
        updateBackgroundAttribute();
        Object[] gradient = (Object[]) value;
        gradientStartColor.setText(Integer.toHexString(((Number) gradient[0]).intValue()));
        gradientEndColor.setText(Integer.toHexString(((Number) gradient[1]).intValue()));
        if (gradient.length > 2) {
            gradientX.setValue(new Double(((Number) gradient[2]).doubleValue()));
            gradientY.setValue(new Double(((Number) gradient[3]).doubleValue()));
            if (gradient.length > 4) {
                gradientSize.setValue(new Double(((Number) gradient[4]).doubleValue()));
            }
        }
        return;
    }
}
Also used : CodenameOneComponentWrapper(com.codename1.ui.resource.util.CodenameOneComponentWrapper) EditorTTFFont(com.codename1.ui.EditorTTFFont) EditorFont(com.codename1.ui.EditorFont) Font(com.codename1.ui.Font) EditorTTFFont(com.codename1.ui.EditorTTFFont) StringTokenizer(java.util.StringTokenizer) Border(com.codename1.ui.plaf.Border)

Example 19 with com.codename1.rad.ui

use of com.codename1.rad.ui in project CodenameOne by codenameone.

the class AddThemeEntry method updateThemeHashtable.

/**
 * Updates the theme hash with the values from this editor
 */
public void updateThemeHashtable(Hashtable themeRes) {
    if (disableRefresh) {
        return;
    }
    String uiid = prefix;
    String item = (String) componentName.getSelectedItem();
    if (item != null && item.length() > 0) {
        uiid = item + "." + prefix;
    }
    removeKeys(themeRes, uiid);
    if (!defineAttribute.isSelected()) {
        String val = (String) baseStyle.getSelectedItem();
        if (val != null && val.length() > 0) {
            switch(baseStyleType.getSelectedIndex()) {
                case 0:
                    themeRes.put(uiid + "derive", val);
                    break;
                case 1:
                    themeRes.put(uiid + "derive", val + ".sel");
                    break;
                case 2:
                    themeRes.put(uiid + "derive", val + ".press");
                    break;
                case 3:
                    themeRes.put(uiid + "derive", val + ".dis");
                    break;
            }
        }
    }
    if (!deriveAlignment.isSelected()) {
        switch(alignmentCombo.getSelectedIndex()) {
            case 0:
                themeRes.put(uiid + "align", new Integer(com.codename1.ui.Component.LEFT));
                break;
            case 1:
                themeRes.put(uiid + "align", new Integer(com.codename1.ui.Component.RIGHT));
                break;
            default:
                themeRes.put(uiid + "align", new Integer(com.codename1.ui.Component.CENTER));
                break;
        }
    }
    if (!deriveBackground.isSelected()) {
        int index = backgroundType.getSelectedIndex();
        themeRes.put(uiid + "bgType", new Byte(BACKGROUND_VALUES[index]));
        if (backgroundType.getSelectedIndex() >= BACKGROUND_VALUES_GRADIENT_ARRAY_OFFSET) {
            // this is a gradient related type
            themeRes.put(uiid + "bgGradient", new Object[] { Integer.valueOf(gradientStartColor.getText(), 16), Integer.valueOf(gradientEndColor.getText(), 16), new Float(((Number) gradientX.getValue()).floatValue()), new Float(((Number) gradientY.getValue()).floatValue()), new Float(((Number) gradientSize.getValue()).floatValue()) });
        } else {
            // this is an image related type
            if (imagesCombo.getSelectedItem() != null && imagesCombo.getSelectedItem().toString().length() > 0) {
                themeRes.put(uiid + "bgImage", resources.getImage((String) imagesCombo.getSelectedItem()));
            } else {
                brokenImage = true;
                themeRes.put(uiid + "bgImage", com.codename1.ui.Image.createImage(5, 5));
            }
        }
    }
    if (!deriveBackgroundColor.isSelected()) {
        themeRes.put(uiid + "bgColor", colorValueBG.getText());
    }
    if (!deriveBorder.isSelected()) {
        if (currentBorder == null) {
            themeRes.remove(uiid + "border");
        } else {
            themeRes.put(uiid + "border", currentBorder);
        }
    }
    if (!deriveFont.isSelected()) {
        Object v;
        if (bitmapFont.isSelected()) {
            String val = (String) bitmapFontValue.getSelectedItem();
            if (val != null) {
                v = resources.getFont(val);
            } else {
                v = Font.getDefaultFont();
            }
        } else {
            if (trueTypeFont.getSelectedIndex() > 0) {
                Font sys = Font.createSystemFont(FONT_FACE_VALUES[fontFace.getSelectedIndex()], FONT_STYLE_VALUES[fontStyle.getSelectedIndex()], FONT_SIZE_VALUES[fontSize.getSelectedIndex()]);
                String selectedItem = (String) trueTypeFont.getSelectedItem();
                if (selectedItem.startsWith("native:")) {
                    v = new EditorTTFFont(selectedItem, trueTypeFontSizeOption.getSelectedIndex(), ((Number) trueTypeFontSizeValue.getValue()).floatValue(), sys);
                } else {
                    v = new EditorTTFFont(new File(ResourceEditorView.getLoadedFile().getParentFile(), selectedItem), trueTypeFontSizeOption.getSelectedIndex(), ((Number) trueTypeFontSizeValue.getValue()).floatValue(), sys);
                }
            } else {
                v = Font.createSystemFont(FONT_FACE_VALUES[fontFace.getSelectedIndex()], FONT_STYLE_VALUES[fontStyle.getSelectedIndex()], FONT_SIZE_VALUES[fontSize.getSelectedIndex()]);
            }
        }
        themeRes.put(uiid + "font", v);
    }
    if (!deriveForegroundColor.isSelected()) {
        themeRes.put(uiid + "fgColor", colorValueFG.getText());
    }
    if (!deriveMargin.isSelected()) {
        themeRes.put(uiid + "margin", marginTop.getValue() + "," + marginBottom.getValue() + "," + marginLeft.getValue() + "," + marginRight.getValue());
        byte[] padUnit = new byte[4];
        padUnit[com.codename1.ui.Component.BOTTOM] = (byte) marginBottomUnit.getSelectedIndex();
        padUnit[com.codename1.ui.Component.TOP] = (byte) marginTopUnit.getSelectedIndex();
        padUnit[com.codename1.ui.Component.LEFT] = (byte) marginLeftUnit.getSelectedIndex();
        padUnit[com.codename1.ui.Component.RIGHT] = (byte) marginRightUnit.getSelectedIndex();
        updateThemeRes(padUnit, themeRes, uiid + "marUnit");
    }
    if (!derivePadding.isSelected()) {
        themeRes.put(uiid + "padding", paddingTop.getValue() + "," + paddingBottom.getValue() + "," + paddingLeft.getValue() + "," + paddingRight.getValue());
        byte[] padUnit = new byte[4];
        padUnit[com.codename1.ui.Component.BOTTOM] = (byte) paddingBottomUnit.getSelectedIndex();
        padUnit[com.codename1.ui.Component.TOP] = (byte) paddingTopUnit.getSelectedIndex();
        padUnit[com.codename1.ui.Component.LEFT] = (byte) paddingLeftUnit.getSelectedIndex();
        padUnit[com.codename1.ui.Component.RIGHT] = (byte) paddingRightUnit.getSelectedIndex();
        updateThemeRes(padUnit, themeRes, uiid + "padUnit");
    }
    if (!deriveTextDecoration.isSelected()) {
        Object v;
        switch(textDecorationCombo.getSelectedIndex()) {
            case 1:
                v = new Integer(com.codename1.ui.plaf.Style.TEXT_DECORATION_UNDERLINE);
                break;
            case 2:
                v = new Integer(com.codename1.ui.plaf.Style.TEXT_DECORATION_STRIKETHRU);
                break;
            case 3:
                v = new Integer(com.codename1.ui.plaf.Style.TEXT_DECORATION_3D);
                break;
            case 4:
                v = new Integer(com.codename1.ui.plaf.Style.TEXT_DECORATION_3D_LOWERED);
                break;
            case 5:
                v = new Integer(com.codename1.ui.plaf.Style.TEXT_DECORATION_3D_SHADOW_NORTH);
                break;
            default:
                v = new Integer(0);
                break;
        }
        themeRes.put(uiid + "textDecoration", v);
    }
    if (!deriveTransparency.isSelected()) {
        themeRes.put(uiid + "transparency", "" + transparencyValue.getValue());
    }
}
Also used : EditorTTFFont(com.codename1.ui.EditorTTFFont) File(java.io.File) EditorTTFFont(com.codename1.ui.EditorTTFFont) EditorFont(com.codename1.ui.EditorFont) Font(com.codename1.ui.Font)

Example 20 with com.codename1.rad.ui

use of com.codename1.rad.ui in project CodenameOne by codenameone.

the class ImageBorderCuttingWizard method generate.

// GEN-LAST:event_multiImageComboActionPerformed
public void generate() {
    if (applies.getAppliesTo().getModel().getSize() == 0) {
        JOptionPane.showMessageDialog(this, "You haven't selected components to apply this border to!\nPlease go to the apply tab and ADD component types/styles", "No Components Selected", JOptionPane.ERROR_MESSAGE);
        return;
    }
    BufferedImage img = wiz.getImage();
    BufferedImage buff = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D bg2d = buff.createGraphics();
    bg2d.drawImage(img.getSubimage(get(cropLeft), get(cropTop), img.getWidth() - get(cropLeft) - get(cropRight), img.getHeight() - get(cropTop) - get(cropBottom)), get(cropLeft), get(cropTop), null);
    bg2d.dispose();
    img = buff;
    BufferedImage topLeft = img.getSubimage(0, 0, get(left), get(top));
    BufferedImage topRight = img.getSubimage(img.getWidth() - get(right), 0, get(right), get(top));
    BufferedImage bottomLeft = img.getSubimage(0, img.getHeight() - get(bottom), get(left), get(bottom));
    BufferedImage bottomRight = img.getSubimage(img.getWidth() - get(right), img.getHeight() - get(bottom), get(right), get(bottom));
    BufferedImage center = img.getSubimage(get(left), get(top), img.getWidth() - get(right) - get(left), img.getHeight() - get(bottom) - get(top));
    BufferedImage topImage = img.getSubimage(get(left), 0, img.getWidth() - get(left) - get(right), get(top));
    BufferedImage bottomImage = img.getSubimage(get(left), img.getHeight() - get(bottom), img.getWidth() - get(left) - get(right), get(bottom));
    BufferedImage leftImage = img.getSubimage(0, get(top), get(left), img.getHeight() - get(top) - get(bottom));
    BufferedImage rightImage = img.getSubimage(img.getWidth() - get(right), get(top), get(right), img.getHeight() - get(top) - get(bottom));
    // optimize the size of the center/top/left/bottom/right images which is a HUGE performance deterant
    if (center.getWidth() < 10 || center.getHeight() < 10) {
        center = ImageTools.getScaledInstance(center, Math.max(20, center.getWidth()), Math.max(20, center.getHeight()));
        topImage = ImageTools.getScaledInstance(topImage, Math.max(20, topImage.getWidth()), topImage.getHeight());
        leftImage = ImageTools.getScaledInstance(leftImage, leftImage.getWidth(), Math.max(20, leftImage.getHeight()));
        rightImage = ImageTools.getScaledInstance(rightImage, rightImage.getWidth(), Math.max(20, rightImage.getHeight()));
        bottomImage = ImageTools.getScaledInstance(bottomImage, Math.max(20, bottomImage.getWidth()), bottomImage.getHeight());
    }
    com.codename1.ui.EncodedImage topLeftCodenameOne = com.codename1.ui.EncodedImage.create(toPng(topLeft));
    com.codename1.ui.EncodedImage topRightCodenameOne = com.codename1.ui.EncodedImage.create(toPng(topRight));
    com.codename1.ui.EncodedImage bottomLeftCodenameOne = com.codename1.ui.EncodedImage.create(toPng(bottomLeft));
    com.codename1.ui.EncodedImage bottomRightCodenameOne = com.codename1.ui.EncodedImage.create(toPng(bottomRight));
    com.codename1.ui.EncodedImage centerCodenameOne = com.codename1.ui.EncodedImage.create(toPng(center));
    com.codename1.ui.EncodedImage topImageCodenameOne = com.codename1.ui.EncodedImage.create(toPng(topImage));
    com.codename1.ui.EncodedImage bottomImageCodenameOne = com.codename1.ui.EncodedImage.create(toPng(bottomImage));
    com.codename1.ui.EncodedImage leftImageCodenameOne = com.codename1.ui.EncodedImage.create(toPng(leftImage));
    com.codename1.ui.EncodedImage rightImageCodenameOne = com.codename1.ui.EncodedImage.create(toPng(rightImage));
    String prefix = (String) applies.getAppliesTo().getModel().getElementAt(0);
    topLeftCodenameOne = storeImage(topLeftCodenameOne, prefix + "TopL");
    topRightCodenameOne = storeImage(topRightCodenameOne, prefix + "TopR");
    bottomLeftCodenameOne = storeImage(bottomLeftCodenameOne, prefix + "BottomL");
    bottomRightCodenameOne = storeImage(bottomRightCodenameOne, prefix + "BottomR");
    centerCodenameOne = storeImage(centerCodenameOne, prefix + "Center");
    topImageCodenameOne = storeImage(topImageCodenameOne, prefix + "Top");
    bottomImageCodenameOne = storeImage(bottomImageCodenameOne, prefix + "Bottom");
    leftImageCodenameOne = storeImage(leftImageCodenameOne, prefix + "Left");
    rightImageCodenameOne = storeImage(rightImageCodenameOne, prefix + "Right");
    com.codename1.ui.plaf.Border b = com.codename1.ui.plaf.Border.createImageBorder(topImageCodenameOne, bottomImageCodenameOne, leftImageCodenameOne, rightImageCodenameOne, topLeftCodenameOne, topRightCodenameOne, bottomLeftCodenameOne, bottomRightCodenameOne, centerCodenameOne);
    Hashtable newTheme = new Hashtable(res.getTheme(theme));
    for (int i = 0; i < applies.getAppliesTo().getModel().getSize(); i++) {
        newTheme.put(applies.getAppliesTo().getModel().getElementAt(i), b);
    }
    ((DefaultListModel) applies.getAppliesTo().getModel()).removeAllElements();
    res.setTheme(theme, newTheme);
}
Also used : Hashtable(java.util.Hashtable) DefaultListModel(javax.swing.DefaultListModel) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

IOException (java.io.IOException)34 EncodedImage (com.codename1.ui.EncodedImage)28 ArrayList (java.util.ArrayList)27 Point (java.awt.Point)25 File (java.io.File)24 AnimationObject (com.codename1.ui.animations.AnimationObject)22 BufferedImage (java.awt.image.BufferedImage)22 Hashtable (java.util.Hashtable)19 Component (com.codename1.ui.Component)18 Form (com.codename1.ui.Form)18 Image (com.codename1.ui.Image)16 EditableResources (com.codename1.ui.util.EditableResources)16 FileInputStream (java.io.FileInputStream)16 Timeline (com.codename1.ui.animations.Timeline)15 BorderLayout (com.codename1.ui.layouts.BorderLayout)14 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)11 FileOutputStream (java.io.FileOutputStream)10 AttributedString (java.text.AttributedString)10 EditorFont (com.codename1.ui.EditorFont)9