Search in sources :

Example 21 with com.codename1.ui

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

the class EditorFont method getBitmapFont.

/**
 * @return the bitmapFont
 */
public Font getBitmapFont() {
    Font bitmapFont = Font.getBitmapFont(lookupFont);
    if (bitmapFont != null) {
        return bitmapFont;
    }
    BufferedImage image = new BufferedImage(5000, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, bitmapAntialiasing);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.setColor(new Color(0xff0000));
    g2d.setFont(java.awt.Font.decode(lookupFont.split(";")[0]));
    FontMetrics metrics = g2d.getFontMetrics();
    FontRenderContext context = g2d.getFontRenderContext();
    int height = (int) Math.ceil(metrics.getMaxDescent() + metrics.getMaxAscent());
    int baseline = (int) Math.ceil(metrics.getMaxAscent());
    String charsetStr = bitmapCharset;
    int[] offsets = new int[charsetStr.length()];
    int[] widths = new int[offsets.length];
    int currentOffset = 0;
    for (int iter = 0; iter < charsetStr.length(); iter++) {
        offsets[iter] = currentOffset;
        String currentChar = charsetStr.substring(iter, iter + 1);
        g2d.drawString(currentChar, currentOffset, baseline);
        Rectangle2D rect = g2d.getFont().getStringBounds(currentChar, context);
        widths[iter] = (int) Math.ceil(rect.getWidth());
        // occupies more ram
        if (g2d.getFont().isItalic()) {
            currentOffset += metrics.getMaxAdvance();
        } else {
            currentOffset += widths[iter] + 1;
        }
    }
    g2d.dispose();
    BufferedImage shrunk = new BufferedImage(currentOffset, height, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) shrunk.getGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    int[] rgb = new int[shrunk.getWidth() * shrunk.getHeight()];
    shrunk.getRGB(0, 0, shrunk.getWidth(), shrunk.getHeight(), rgb, 0, shrunk.getWidth());
    com.codename1.ui.Image bitmap = com.codename1.ui.Image.createImage(rgb, shrunk.getWidth(), shrunk.getHeight());
    return com.codename1.ui.Font.createBitmapFont(lookupFont, bitmap, offsets, widths, charsetStr);
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) Font(com.codename1.ui.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) com.codename1.ui(com.codename1.ui) FontMetrics(java.awt.FontMetrics) FontRenderContext(java.awt.font.FontRenderContext)

Example 22 with com.codename1.ui

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

the class ResourceEditorView method launchOptiPngActionPerformed.

private void launchOptiPngActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_launchOptiPngActionPerformed
    if (loadedResources != null && configureOptiPNG()) {
        final ProgressMonitor pm = new ProgressMonitor(mainPanel, "Processing Images", "", 0, loadedResources.getImageResourceNames().length);
        new Thread() {

            public void run() {
                String node = Preferences.userNodeForPackage(ResourceEditorView.class).get("optiPng", null);
                int prog = 0;
                for (String imageName : loadedResources.getImageResourceNames()) {
                    if (pm.isCanceled()) {
                        pm.close();
                        return;
                    }
                    pm.setProgress(prog);
                    prog++;
                    pm.setNote(imageName);
                    Object image = loadedResources.getImage(imageName);
                    if (image instanceof com.codename1.ui.EncodedImage) {
                        if (loadedResources.getResourceObject(imageName) != image) {
                            // multi-image...
                            EditableResources.MultiImage multi = (EditableResources.MultiImage) loadedResources.getResourceObject(imageName);
                            EditableResources.MultiImage n = new EditableResources.MultiImage();
                            EncodedImage[] arr = new EncodedImage[multi.getInternalImages().length];
                            for (int iter = 0; iter < multi.getInternalImages().length; iter++) {
                                EncodedImage current = optimize(multi.getInternalImages()[iter], node);
                                if (current != null) {
                                    arr[iter] = current;
                                } else {
                                    arr[iter] = multi.getInternalImages()[iter];
                                }
                            }
                            n.setInternalImages(arr);
                            n.setDpi(multi.getDpi());
                            loadedResources.setMultiImage(imageName, n);
                        } else {
                            EncodedImage current = optimize((EncodedImage) image, node);
                            if (current != null) {
                                loadedResources.setImage(imageName, current);
                            }
                        }
                    }
                }
                pm.close();
            }
        }.start();
    }
}
Also used : ProgressMonitor(javax.swing.ProgressMonitor) AnimationObject(com.codename1.ui.animations.AnimationObject) EncodedImage(com.codename1.ui.EncodedImage) EditableResources(com.codename1.ui.util.EditableResources) EncodedImage(com.codename1.ui.EncodedImage)

Example 23 with com.codename1.ui

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

the class ResourceEditorView method generateStateMachineCodeImpl.

private static String generateStateMachineCodeImpl(String uiResourceName, File destFile, boolean promptUserForPackageName, EditableResources loadResources, java.awt.Component errorParent) {
    String packageString = "";
    File currentFile = destFile;
    while (currentFile.getParent() != null) {
        String shortName = currentFile.getParentFile().getName();
        if (shortName.equalsIgnoreCase("src")) {
            break;
        }
        if (shortName.indexOf(':') > -1 || shortName.length() == 0) {
            break;
        }
        if (shortName.equalsIgnoreCase("org") || shortName.equalsIgnoreCase("com") || shortName.equalsIgnoreCase("net") || shortName.equalsIgnoreCase("gov")) {
            if (packageString.length() > 0) {
                packageString = shortName + "." + packageString;
            } else {
                packageString = shortName;
            }
            break;
        }
        if (packageString.length() > 0) {
            packageString = shortName + "." + packageString;
        } else {
            packageString = shortName;
        }
        currentFile = currentFile.getParentFile();
    }
    final Map<String, String> nameToClassLookup = new HashMap<String, String>();
    final Map<String, Integer> commandMap = new HashMap<String, Integer>();
    final List<Integer> unhandledCommands = new ArrayList<Integer>();
    final List<String[]> actionComponents = new ArrayList<String[]>();
    final Map<String, String> allComponents = new HashMap<String, String>();
    initCommandMapAndNameToClassLookup(nameToClassLookup, commandMap, unhandledCommands, actionComponents, allComponents);
    // list all the .ovr files and add them to the nameToClassLookup
    if (loadedFile != null && loadedFile.getParentFile() != null) {
        File overrideDir = new File(loadedFile.getParentFile().getParentFile(), "override");
        if (overrideDir.exists()) {
            File[] ovrFiles = overrideDir.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File file, String string) {
                    return string.endsWith(".ovr");
                }
            });
            for (File ovr : ovrFiles) {
                try {
                    EditableResources er = EditableResources.open(new FileInputStream(ovr));
                    for (String currentResourceName : er.getUIResourceNames()) {
                        UIBuilder b = new UIBuilder() {

                            protected com.codename1.ui.Component createComponentInstance(String componentType, Class cls) {
                                if (cls.getName().startsWith("com.codename1.ui.")) {
                                    // subpackage of CodenameOne should be registered
                                    if (cls.getName().lastIndexOf(".") > 15) {
                                        nameToClassLookup.put(componentType, cls.getName());
                                    }
                                } else {
                                    nameToClassLookup.put(componentType, cls.getName());
                                }
                                return null;
                            }
                        };
                        b.createContainer(er, currentResourceName);
                    }
                } catch (IOException ioErr) {
                    ioErr.printStackTrace();
                }
            }
        }
    }
    if (promptUserForPackageName) {
        JTextField packageName = new JTextField(packageString);
        JOptionPane.showMessageDialog(errorParent, packageName, "Please Pick The Package Name", JOptionPane.PLAIN_MESSAGE);
        packageString = packageName.getText();
    }
    List<String> createdMethodNames = new ArrayList<String>();
    try {
        Writer w = new FileWriter(destFile);
        w.write("/**\n");
        w.write(" * This class contains generated code from the Codename One Designer, DO NOT MODIFY!\n");
        w.write(" * This class is designed for subclassing that way the code generator can overwrite it\n");
        w.write(" * anytime without erasing your changes which should exist in a subclass!\n");
        w.write(" * For details about this file and how it works please read this blog post:\n");
        w.write(" * http://codenameone.blogspot.com/2010/10/ui-builder-class-how-to-actually-use.html\n");
        w.write("*/\n");
        if (packageString.length() > 0) {
            w.write("package " + packageString + ";\n\n");
        }
        String className = destFile.getName().substring(0, destFile.getName().indexOf('.'));
        boolean hasIo = false;
        for (String currentName : nameToClassLookup.keySet()) {
            if (nameToClassLookup.get(currentName).indexOf("com.codename1.ui.io") > -1) {
                hasIo = true;
                break;
            }
        }
        w.write("import com.codename1.ui.*;\n");
        w.write("import com.codename1.ui.util.*;\n");
        w.write("import com.codename1.ui.plaf.*;\n");
        w.write("import java.util.Hashtable;\n");
        if (hasIo) {
            w.write("import com.codename1.ui.io.*;\n");
            w.write("import com.codename1.components*;\n");
        }
        w.write("import com.codename1.ui.events.*;\n\n");
        w.write("public abstract class " + className + " extends UIBuilder {\n");
        w.write("    private Container aboutToShowThisContainer;\n");
        w.write("    /**\n");
        w.write("     * this method should be used to initialize variables instead of\n");
        w.write("     * the constructor/class scope to avoid race conditions\n");
        w.write("     */\n");
        w.write("    /**\n    * @deprecated use the version that accepts a resource as an argument instead\n    \n**/\n");
        w.write("    protected void initVars() {}\n\n");
        w.write("    protected void initVars(Resources res) {}\n\n");
        w.write("    public " + className + "(Resources res, String resPath, boolean loadTheme) {\n");
        w.write("        startApp(res, resPath, loadTheme);\n");
        w.write("    }\n\n");
        w.write("    public Container startApp(Resources res, String resPath, boolean loadTheme) {\n");
        w.write("        initVars();\n");
        if (hasIo) {
            w.write("        NetworkManager.getInstance().start();\n");
        }
        for (String currentName : nameToClassLookup.keySet()) {
            w.write("        UIBuilder.registerCustomComponent(\"" + currentName + "\", " + nameToClassLookup.get(currentName) + ".class);\n");
        }
        w.write("        if(loadTheme) {\n");
        w.write("            if(res == null) {\n");
        w.write("                try {\n");
        w.write("                    if(resPath.endsWith(\".res\")) {\n");
        w.write("                        res = Resources.open(resPath);\n");
        w.write("                        System.out.println(\"Warning: you should construct the state machine without the .res extension to allow theme overlays\");\n");
        w.write("                    } else {\n");
        w.write("                        res = Resources.openLayered(resPath);\n");
        w.write("                    }\n");
        w.write("                } catch(java.io.IOException err) { err.printStackTrace(); }\n");
        w.write("            }\n");
        w.write("            initTheme(res);\n");
        w.write("        }\n");
        w.write("        if(res != null) {\n");
        w.write("            setResourceFilePath(resPath);\n");
        w.write("            setResourceFile(res);\n");
        w.write("            initVars(res);\n");
        w.write("            return showForm(getFirstFormName(), null);\n");
        w.write("        } else {\n");
        w.write("            Form f = (Form)createContainer(resPath, getFirstFormName());\n");
        w.write("            initVars(fetchResourceFile());\n");
        w.write("            beforeShow(f);\n");
        w.write("            f.show();\n");
        w.write("            postShow(f);\n");
        w.write("            return f;\n");
        w.write("        }\n");
        w.write("    }\n\n");
        w.write("    protected String getFirstFormName() {\n");
        w.write("        return \"" + uiResourceName + "\";\n");
        w.write("    }\n\n");
        w.write("    public Container createWidget(Resources res, String resPath, boolean loadTheme) {\n");
        w.write("        initVars();\n");
        if (hasIo) {
            w.write("        NetworkManager.getInstance().start();\n");
        }
        for (String currentName : nameToClassLookup.keySet()) {
            w.write("        UIBuilder.registerCustomComponent(\"" + currentName + "\", " + nameToClassLookup.get(currentName) + ".class);\n");
        }
        w.write("        if(loadTheme) {\n");
        w.write("            if(res == null) {\n");
        w.write("                try {\n");
        w.write("                    res = Resources.openLayered(resPath);\n");
        w.write("                } catch(java.io.IOException err) { err.printStackTrace(); }\n");
        w.write("            }\n");
        w.write("            initTheme(res);\n");
        w.write("        }\n");
        w.write("        return createContainer(resPath, \"" + uiResourceName + "\");\n");
        w.write("    }\n\n");
        w.write("    protected void initTheme(Resources res) {\n");
        w.write("            String[] themes = res.getThemeResourceNames();\n");
        w.write("            if(themes != null && themes.length > 0) {\n");
        w.write("                UIManager.getInstance().setThemeProps(res.getTheme(themes[0]));\n");
        w.write("            }\n");
        w.write("    }\n\n");
        w.write("    public " + className + "() {\n");
        w.write("    }\n\n");
        w.write("    public " + className + "(String resPath) {\n");
        w.write("        this(null, resPath, true);\n");
        w.write("    }\n\n");
        w.write("    public " + className + "(Resources res) {\n");
        w.write("        this(res, null, true);\n");
        w.write("    }\n\n");
        w.write("    public " + className + "(String resPath, boolean loadTheme) {\n");
        w.write("        this(null, resPath, loadTheme);\n");
        w.write("    }\n\n");
        w.write("    public " + className + "(Resources res, boolean loadTheme) {\n");
        w.write("        this(res, null, loadTheme);\n");
        w.write("    }\n\n");
        for (String componentName : allComponents.keySet()) {
            String componentType = allComponents.get(componentName);
            String methodName = " find" + normalizeFormName(componentName);
            // exists without a space might trigger this situation and thus code that won't compile
            if (!createdMethodNames.contains(methodName)) {
                if (componentType.equals("com.codename1.ui.Form") || componentType.equals("com.codename1.ui.Dialog")) {
                    continue;
                }
                createdMethodNames.add(methodName);
                w.write("    public " + componentType + methodName + "(Component root) {\n");
                w.write("        return (" + componentType + ")" + "findByName(\"" + componentName + "\", root);\n");
                w.write("    }\n\n");
                w.write("    public " + componentType + methodName + "() {\n");
                w.write("        " + componentType + " cmp = (" + componentType + ")" + "findByName(\"" + componentName + "\", Display.getInstance().getCurrent());\n");
                w.write("        if(cmp == null && aboutToShowThisContainer != null) {\n");
                w.write("            cmp = (" + componentType + ")" + "findByName(\"" + componentName + "\", aboutToShowThisContainer);\n");
                w.write("        }\n");
                w.write("        return cmp;\n");
                w.write("    }\n\n");
            }
        }
        if (commandMap.size() > 0) {
            for (String key : commandMap.keySet()) {
                w.write("    public static final int COMMAND_" + key + " = " + commandMap.get(key) + ";\n");
            }
            w.write("\n");
            StringBuilder methodSwitch = new StringBuilder("    protected void processCommand(ActionEvent ev, Command cmd) {\n        switch(cmd.getId()) {\n");
            for (String key : commandMap.keySet()) {
                String camelCase = "on" + key;
                boolean isAbstract = unhandledCommands.contains(commandMap.get(key));
                if (isAbstract) {
                    w.write("    protected abstract void ");
                    w.write(camelCase);
                    w.write("();\n\n");
                } else {
                    w.write("    protected boolean ");
                    w.write(camelCase);
                    w.write("() {\n        return false;\n    }\n\n");
                }
                methodSwitch.append("            case COMMAND_");
                methodSwitch.append(key);
                methodSwitch.append(":\n");
                methodSwitch.append("                ");
                if (isAbstract) {
                    methodSwitch.append(camelCase);
                    methodSwitch.append("();\n                break;\n\n");
                } else {
                    methodSwitch.append("if(");
                    methodSwitch.append(camelCase);
                    methodSwitch.append("()) {\n                    ev.consume();\n                    return;\n                }\n                break;\n\n");
                }
            }
            methodSwitch.append("        }\n        if(ev.getComponent() != null) {\n            handleComponentAction(ev.getComponent(), ev);\n        }\n    }\n\n");
            w.write(methodSwitch.toString());
        }
        writeFormCallbackCode(w, "    protected void exitForm(Form f) {\n", "f.getName()", "exit", "f", "Form f");
        writeFormCallbackCode(w, "    protected void beforeShow(Form f) {\n    aboutToShowThisContainer = f;\n", "f.getName()", "before", "f", "Form f");
        writeFormCallbackCode(w, "    protected void beforeShowContainer(Container c) {\n        aboutToShowThisContainer = c;\n", "c.getName()", "beforeContainer", "c", "Container c");
        writeFormCallbackCode(w, "    protected void postShow(Form f) {\n", "f.getName()", "post", "f", "Form f");
        writeFormCallbackCode(w, "    protected void postShowContainer(Container c) {\n", "c.getName()", "postContainer", "c", "Container c");
        writeFormCallbackCode(w, "    protected void onCreateRoot(String rootName) {\n", "rootName", "onCreate", "", "");
        writeFormCallbackCode(w, "    protected Hashtable getFormState(Form f) {\n        Hashtable h = super.getFormState(f);\n", "f.getName()", "getState", "f, h", "Form f, Hashtable h", "return h;");
        writeFormCallbackCode(w, "    protected void setFormState(Form f, Hashtable state) {\n        super.setFormState(f, state);\n", "f.getName()", "setState", "f, state", "Form f, Hashtable state");
        List<String> listComponents = new ArrayList<String>();
        for (String currentName : allComponents.keySet()) {
            String value = allComponents.get(currentName);
            if (value.equals("com.codename1.ui.List") || value.equals("com.codename1.ui.ComboBox") || value.equals("com.codename1.ui.list.MultiList") || value.equals("com.codename1.ui.Calendar")) {
                listComponents.add(currentName);
            }
        }
        List<String> containerListComponents = new ArrayList<String>();
        for (String currentName : allComponents.keySet()) {
            String value = allComponents.get(currentName);
            if (value.equals("com.codename1.ui.list.ContainerList")) {
                containerListComponents.add(currentName);
            }
        }
        if (listComponents.size() > 0) {
            w.write("    protected boolean setListModel(List cmp) {\n");
            w.write("        String listName = cmp.getName();\n");
            for (String listName : listComponents) {
                w.write("        if(\"");
                w.write(listName);
                w.write("\".equals(listName)) {\n");
                w.write("            return initListModel");
                w.write(normalizeFormName(listName));
                w.write("(cmp);\n        }\n");
            }
            w.write("        return super.setListModel(cmp);\n    }\n\n");
            for (String listName : listComponents) {
                w.write("    protected boolean initListModel");
                w.write(normalizeFormName(listName));
                w.write("(List cmp) {\n");
                w.write("        return false;\n    }\n\n");
            }
        }
        if (containerListComponents.size() > 0) {
            w.write("    protected boolean setListModel(com.codename1.ui.list.ContainerList cmp) {\n");
            w.write("        String listName = cmp.getName();\n");
            for (String listName : containerListComponents) {
                w.write("        if(\"");
                w.write(listName);
                w.write("\".equals(listName)) {\n");
                w.write("            return initListModel");
                w.write(normalizeFormName(listName));
                w.write("(cmp);\n        }\n");
            }
            w.write("        return super.setListModel(cmp);\n    }\n\n");
            for (String listName : containerListComponents) {
                w.write("    protected boolean initListModel");
                w.write(normalizeFormName(listName));
                w.write("(com.codename1.ui.list.ContainerList cmp) {\n");
                w.write("        return false;\n    }\n\n");
            }
        }
        if (actionComponents.size() > 0) {
            Object lastFormName = null;
            StringBuilder methods = new StringBuilder();
            w.write("    protected void handleComponentAction(Component c, ActionEvent event) {\n");
            w.write("        Container rootContainerAncestor = getRootAncestor(c);\n");
            w.write("        if(rootContainerAncestor == null) return;\n");
            w.write("        String rootContainerName = rootContainerAncestor.getName();\n");
            w.write("        Container leadParentContainer = c.getParent().getLeadParent();\n");
            w.write("        if(leadParentContainer != null && leadParentContainer.getClass() != Container.class) {\n");
            w.write("            c = c.getParent().getLeadParent();\n");
            w.write("        }\n");
            w.write("        if(rootContainerName == null) return;\n");
            for (String[] currentCmp : actionComponents) {
                if (lastFormName != currentCmp[1]) {
                    if (lastFormName != null) {
                        w.write("        }\n");
                    }
                    w.write("        if(rootContainerName.equals(\"");
                    w.write(currentCmp[1]);
                    w.write("\")) {\n");
                    lastFormName = currentCmp[1];
                }
                w.write("            if(\"");
                w.write(currentCmp[0]);
                w.write("\".equals(c.getName())) {\n");
                String methodName = "on" + normalizeFormName(currentCmp[1]) + "_" + normalizeFormName(currentCmp[0]) + "Action";
                w.write("                ");
                w.write(methodName);
                w.write("(c, event);\n");
                w.write("                return;\n");
                w.write("            }\n");
                methods.append("      protected void ");
                methods.append(methodName);
                methods.append("(Component c, ActionEvent event) {\n      }\n\n");
            }
            w.write("        }\n    }\n\n");
            w.write(methods.toString());
        }
        w.write("}\n");
        w.close();
    } catch (IOException ioErr) {
        ioErr.printStackTrace();
        JOptionPane.showMessageDialog(errorParent, "IO Error: " + ioErr, "IO Error", JOptionPane.ERROR_MESSAGE);
    }
    return packageString;
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) UIBuilder(com.codename1.ui.util.UIBuilder) JTextField(javax.swing.JTextField) FilenameFilter(java.io.FilenameFilter) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) EditableResources(com.codename1.ui.util.EditableResources) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AnimationObject(com.codename1.ui.animations.AnimationObject) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) FileWriter(java.io.FileWriter)

Example 24 with com.codename1.ui

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

the class ResourceEditorView method isInUse.

/**
 * Returns true if the given image is used by a theme or timeline animation,
 * false otherwise.
 */
private boolean isInUse(String imageName) {
    Object multi = loadedResources.getResourceObject(imageName);
    if (multi instanceof EditableResources.MultiImage) {
        EditableResources.MultiImage m = (EditableResources.MultiImage) multi;
        for (com.codename1.ui.Image i : m.getInternalImages()) {
            if (isInUse(i)) {
                return true;
            }
        }
        return false;
    }
    com.codename1.ui.Image resourceValue = loadedResources.getImage(imageName);
    return isInUse(resourceValue);
}
Also used : AnimationObject(com.codename1.ui.animations.AnimationObject) EditableResources(com.codename1.ui.util.EditableResources)

Example 25 with com.codename1.ui

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

the class ResourceEditorView method initImagesComboBox.

/**
 * Creates a sorted image combo box that includes image previews. The combo box
 * can be searched by typing a letter even when images are used for the values...
 */
public static void initImagesComboBox(JComboBox cb, final EditableResources res, boolean asString, final boolean includeNull, boolean blockTimelines) {
    String[] imgs = res.getImageResourceNames();
    if (blockTimelines) {
        List<String> nonT = new ArrayList<String>();
        for (String c : imgs) {
            if (!(res.getImage(c) instanceof Timeline)) {
                nonT.add(c);
            }
        }
        imgs = new String[nonT.size()];
        nonT.toArray(imgs);
    }
    final String[] images = imgs;
    Arrays.sort(images, String.CASE_INSENSITIVE_ORDER);
    if (asString) {
        if (includeNull) {
            String[] n = new String[images.length + 1];
            System.arraycopy(images, 0, n, 1, images.length);
            cb.setModel(new DefaultComboBoxModel(n));
        } else {
            cb.setModel(new DefaultComboBoxModel(images));
        }
        cb.setRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                boolean n = false;
                if (value == null) {
                    value = "[null]";
                    n = true;
                }
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (!n) {
                    setIcon(new CodenameOneImageIcon(res.getImage((String) value), 24, 24));
                } else {
                    setIcon(null);
                }
                return this;
            }
        });
    } else {
        int offset = 0;
        com.codename1.ui.Image[] arr;
        if (includeNull) {
            arr = new com.codename1.ui.Image[images.length + 1];
            offset++;
        } else {
            arr = new com.codename1.ui.Image[images.length];
        }
        for (String c : images) {
            arr[offset] = res.getImage(c);
            offset++;
        }
        cb.setModel(new DefaultComboBoxModel(arr));
        cb.setKeySelectionManager(new JComboBox.KeySelectionManager() {

            private String current;

            private long lastPress;

            public int selectionForKey(char aKey, ComboBoxModel aModel) {
                long t = System.currentTimeMillis();
                aKey = Character.toLowerCase(aKey);
                if (t - lastPress < 800) {
                    current += aKey;
                } else {
                    current = "" + aKey;
                }
                lastPress = t;
                for (int iter = 0; iter < images.length; iter++) {
                    if (images[iter].toLowerCase().startsWith(current)) {
                        if (includeNull) {
                            return iter + 1;
                        }
                        return iter;
                    }
                }
                return -1;
            }
        });
        cb.setRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                com.codename1.ui.Image i = (com.codename1.ui.Image) value;
                if (value == null) {
                    value = "[null]";
                } else {
                    value = res.findId(value);
                }
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (i != null) {
                    setIcon(new CodenameOneImageIcon(i, 24, 24));
                } else {
                    setIcon(null);
                }
                return this;
            }
        });
    }
}
Also used : JComboBox(javax.swing.JComboBox) ArrayList(java.util.ArrayList) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) Timeline(com.codename1.ui.animations.Timeline) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) AnimationObject(com.codename1.ui.animations.AnimationObject) Component(java.awt.Component) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) JList(javax.swing.JList) ComboBoxModel(javax.swing.ComboBoxModel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel)

Aggregations

EncodedImage (com.codename1.ui.EncodedImage)26 Component (com.codename1.ui.Component)23 Point (java.awt.Point)23 IOException (java.io.IOException)23 AnimationObject (com.codename1.ui.animations.AnimationObject)22 ArrayList (java.util.ArrayList)22 BufferedImage (java.awt.image.BufferedImage)19 Hashtable (java.util.Hashtable)18 Form (com.codename1.ui.Form)15 Timeline (com.codename1.ui.animations.Timeline)15 Image (com.codename1.ui.Image)13 EditableResources (com.codename1.ui.util.EditableResources)13 File (java.io.File)13 Vector (java.util.Vector)13 TextArea (com.codename1.ui.TextArea)12 Border (com.codename1.ui.plaf.Border)12 Label (com.codename1.ui.Label)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)10 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)10 Container (com.codename1.ui.Container)9