Search in sources :

Example 11 with com.codename1.rad.ui

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

the class ResourceEditorView method imageSizesActionPerformed.

// GEN-LAST:event_pulsateEffectActionPerformed
private void imageSizesActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_imageSizesActionPerformed
    class ImageSize {

        String name;

        int size;
    }
    int total = 0;
    Vector images = new Vector();
    for (String imageName : loadedResources.getImageResourceNames()) {
        com.codename1.ui.Image img = loadedResources.getImage(imageName);
        ImageSize size = new ImageSize();
        size.name = imageName;
        Object o = loadedResources.getResourceObject(imageName);
        // special case for multi image which can be all of the internal images...
        if (o instanceof EditableResources.MultiImage) {
            for (Object c : ((EditableResources.MultiImage) o).getInternalImages()) {
                size.size += ((com.codename1.ui.EncodedImage) c).getImageData().length;
            }
            images.add(size);
        } else {
            if (img instanceof com.codename1.ui.EncodedImage) {
                size.size = ((com.codename1.ui.EncodedImage) img).getImageData().length;
                images.add(size);
            } else {
                if (img.isSVG()) {
                    SVG s = (SVG) img.getSVGDocument();
                    size.size = s.getSvgData().length;
                    images.add(size);
                }
            }
        }
        total += size.size;
    }
    Collections.sort(images, new Comparator() {

        public int compare(Object o1, Object o2) {
            ImageSize i1 = (ImageSize) o1;
            ImageSize i2 = (ImageSize) o2;
            return i2.size - i1.size;
        }
    });
    JPanel p = new JPanel(new java.awt.BorderLayout());
    JList list = new JList(images);
    p.add(java.awt.BorderLayout.NORTH, new JLabel("Total " + (total / 1024) + "kb in " + loadedResources.getImageResourceNames().length + " images"));
    p.add(java.awt.BorderLayout.CENTER, new JScrollPane(list));
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            ImageSize s = (ImageSize) value;
            value = s.name + " " + (s.size / 1024) + "kb (" + s.size + "b)";
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    });
    JOptionPane.showMessageDialog(mainPanel, p, "Sizes", JOptionPane.PLAIN_MESSAGE);
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) SVG(com.codename1.impl.javase.SVG) JLabel(javax.swing.JLabel) EncodedImage(com.codename1.ui.EncodedImage) Comparator(java.util.Comparator) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) AnimationObject(com.codename1.ui.animations.AnimationObject) BorderLayout(java.awt.BorderLayout) Component(java.awt.Component) Vector(java.util.Vector) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) JList(javax.swing.JList)

Example 12 with com.codename1.rad.ui

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

the class ResourceEditorView method initCommandMapAndNameToClassLookup.

private static void initCommandMapAndNameToClassLookup(final Map<String, String> nameToClassLookup, final Map<String, Integer> commandMap, final List<Integer> unhandledCommands, final List<String[]> actionComponentNames, final Map<String, String> allComponents) {
    // register the proper handlers for the component types used
    UIBuilderOverride.registerCustom();
    PickMIDlet.getCustomComponents();
    for (String currentResourceName : loadedResources.getUIResourceNames()) {
        final String currentName = currentResourceName;
        UIBuilder b = new UIBuilder() {

            protected com.codename1.ui.Command createCommand(String commandName, com.codename1.ui.Image icon, int commandId, String action) {
                if (unhandledCommands != null) {
                    if (action == null) {
                        unhandledCommands.add(commandId);
                    }
                }
                // we already have that command id...
                if (commandMap.values().contains(commandId)) {
                    removeCommandDups(commandMap, commandId);
                }
                if (commandName == null || commandName.length() == 0) {
                    commandName = "Command" + commandId;
                }
                commandName = normalizeFormName(currentName) + normalizeFormName(commandName);
                commandMap.put(commandName, commandId);
                return super.createCommand(commandName, icon, commandId, action);
            }

            public boolean caseInsensitiveContainsKey(String s) {
                return caseInsensitiveKey(s) != null;
            }

            public String caseInsensitiveKey(String s) {
                for (String k : allComponents.keySet()) {
                    if (k.equalsIgnoreCase(s)) {
                        return k;
                    }
                }
                return null;
            }

            public void postCreateComponent(com.codename1.ui.Component cmp) {
                if (allComponents != null) {
                    String name = cmp.getName();
                    String componentClass = cmp.getClass().getName();
                    if (allComponents.containsKey(name)) {
                        if (!componentClass.equals(allComponents.get(name))) {
                            allComponents.put(name, "com.codename1.ui.Component");
                        } else {
                            allComponents.put(name, componentClass);
                        }
                    } else {
                        if (!caseInsensitiveContainsKey(name)) {
                            allComponents.put(name, componentClass);
                        }
                    }
                }
                com.codename1.ui.Component actual = cmp;
                if (cmp instanceof com.codename1.ui.Container) {
                    actual = ((com.codename1.ui.Container) cmp).getLeadComponent();
                    if (actual == null) {
                        actual = cmp;
                    }
                }
                if (actionComponentNames != null && (actual instanceof com.codename1.ui.Button || actual instanceof com.codename1.ui.List || actual instanceof com.codename1.ui.list.ContainerList || actual instanceof com.codename1.ui.TextArea || actual instanceof com.codename1.ui.Calendar)) {
                    if (actual instanceof com.codename1.ui.Button) {
                        if (((com.codename1.ui.Button) actual).getCommand() != null) {
                            return;
                        }
                    }
                    String componentName = cmp.getName();
                    for (String[] arr : actionComponentNames) {
                        if (arr[0].equals(componentName) && arr[1].equals(currentName)) {
                            return;
                        }
                    }
                    actionComponentNames.add(new String[] { componentName, currentName });
                }
            }

            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(loadedResources, currentResourceName);
    }
}
Also used : JTextArea(javax.swing.JTextArea) UIBuilder(com.codename1.ui.util.UIBuilder) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) JButton(javax.swing.JButton) ArrayList(java.util.ArrayList) List(java.util.List) JList(javax.swing.JList) Component(java.awt.Component)

Example 13 with com.codename1.rad.ui

use of com.codename1.rad.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 14 with com.codename1.rad.ui

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

the class PreviewInSimulator method main.

/**
 * Called back from simulateDeviceActionPerformed to show the simulator skin
 */
public static void main(String[] argv) {
    com.codename1.ui.Display.init(new Runnable() {

        public void run() {
            try {
                Preferences pref = Preferences.userNodeForPackage(PreviewInSimulator.class);
                String theme = pref.get("previewTheme", null);
                File resFile = new File(pref.get("previewResource", null));
                String baseResDir = pref.get("baseResourceDir", null);
                if (baseResDir != null) {
                    JavaSEPort.setBaseResourceDir(new File(baseResDir));
                }
                String selection = pref.get("previewSelection", null);
                Resources res = Resources.open(new FileInputStream(resFile));
                if (theme == null || theme.length() == 0) {
                    if (com.codename1.ui.Display.getInstance().hasNativeTheme()) {
                        com.codename1.ui.Display.getInstance().installNativeTheme();
                    }
                } else {
                    com.codename1.ui.plaf.UIManager.getInstance().setThemeProps(res.getTheme(theme));
                }
                com.codename1.ui.util.UIBuilder.registerCustomComponent("Table", com.codename1.ui.table.Table.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("MediaPlayer", com.codename1.components.MediaPlayer.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("ContainerList", com.codename1.ui.list.ContainerList.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("ComponentGroup", com.codename1.ui.ComponentGroup.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("Tree", com.codename1.ui.tree.Tree.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("HTMLComponent", com.codename1.ui.html.HTMLComponent.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("RSSReader", com.codename1.components.RSSReader.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("FileTree", com.codename1.components.FileTree.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("WebBrowser", com.codename1.components.WebBrowser.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("NumericSpinner", com.codename1.ui.spinner.NumericSpinner.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("DateSpinner", com.codename1.ui.spinner.DateSpinner.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("TimeSpinner", com.codename1.ui.spinner.TimeSpinner.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("DateTimeSpinner", com.codename1.ui.spinner.DateTimeSpinner.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("GenericSpinner", com.codename1.ui.spinner.GenericSpinner.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("LikeButton", com.codename1.facebook.ui.LikeButton.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("InfiniteProgress", com.codename1.components.InfiniteProgress.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("MultiButton", com.codename1.components.MultiButton.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("SpanButton", com.codename1.components.SpanButton.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("SpanLabel", com.codename1.components.SpanLabel.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("Ads", com.codename1.components.Ads.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("MapComponent", com.codename1.maps.MapComponent.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("MultiList", com.codename1.ui.list.MultiList.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("ShareButton", com.codename1.components.ShareButton.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("OnOffSwitch", com.codename1.components.OnOffSwitch.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("ImageViewer", com.codename1.components.ImageViewer.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("AutoCompleteTextField", com.codename1.ui.AutoCompleteTextField.class);
                com.codename1.ui.util.UIBuilder.registerCustomComponent("Picker", com.codename1.ui.spinner.Picker.class);
                com.codename1.ui.util.UIBuilder builder = new com.codename1.ui.util.UIBuilder();
                com.codename1.ui.Container c = builder.createContainer(res, selection);
                if (c instanceof com.codename1.ui.Form) {
                    ((com.codename1.ui.Form) c).refreshTheme();
                    if (c instanceof com.codename1.ui.Dialog) {
                        ((com.codename1.ui.Dialog) c).showModeless();
                    } else {
                        ((com.codename1.ui.Form) c).show();
                    }
                } else {
                    com.codename1.ui.Form f = new com.codename1.ui.Form();
                    f.setLayout(new com.codename1.ui.layouts.BorderLayout());
                    f.addComponent(com.codename1.ui.layouts.BorderLayout.CENTER, c);
                    f.refreshTheme();
                    f.show();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Error While Running In Simulator: " + ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
}
Also used : Preferences(java.util.prefs.Preferences) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) BackingStoreException(java.util.prefs.BackingStoreException) Resources(com.codename1.ui.util.Resources) File(java.io.File)

Example 15 with com.codename1.rad.ui

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

the class PulsateEditor method updateTimeline.

private void updateTimeline() {
    AnimationObject[] anim = new AnimationObject[get(frames) * 2 - 1];
    internalImages = new EncodedImage[get(frames)];
    int small = Math.min(get(smallSize), get(largeSize));
    int large = Math.max(get(smallSize), get(largeSize));
    int dur = get(duration);
    int segment = dur / anim.length;
    Motion calculator = Motion.createSplineMotion(small, large, dur / 2);
    float ratioOfLargest = ((float) large) / 100.0f;
    int timelineWidth = (int) (ratioOfLargest * ((float) sourceImage.getWidth()));
    int timelineHeight = (int) (ratioOfLargest * ((float) sourceImage.getHeight()));
    for (int iter = 0; iter < internalImages.length; iter++) {
        calculator.setCurrentMotionTime(segment * iter);
        int percentAtTime = calculator.getValue();
        float ratioAtTime = ((float) percentAtTime) / 100.0f;
        int currentWidth = (int) (sourceImage.getWidth() * ratioAtTime);
        int currentHeight = (int) (sourceImage.getHeight() * ratioAtTime);
        internalImages[iter] = EncodedImage.create(scale(sourceImage, currentWidth, currentHeight));
        anim[iter] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
        anim[iter].setStartTime(segment * iter);
        anim[iter].setEndTime(segment * iter + segment);
        // peek at the next frame to calculate the diff
        calculator.setCurrentMotionTime(segment * iter + segment);
        int percentAtNextFrame = calculator.getValue();
        float ratioAtNextFrame = ((float) percentAtNextFrame) / 100.0f;
        int nextWidth = (int) (sourceImage.getWidth() * ratioAtNextFrame);
        int nextHeight = (int) (sourceImage.getHeight() * ratioAtNextFrame);
        anim[iter].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentHeight, nextHeight);
        anim[iter].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentWidth, nextWidth);
        anim[iter].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - nextWidth) / 2);
        anim[iter].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - nextHeight) / 2);
        // create the "inverse" shrinking animation object
        if (iter > 0) {
            int nextOffset = anim.length - iter;
            anim[nextOffset] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
            anim[nextOffset].setStartTime(segment * nextOffset);
            if (iter == 1) {
                // this resolves any rounding errors that might have occured in the creation of the frames
                anim[nextOffset].setEndTime(dur);
            } else {
                anim[nextOffset].setEndTime(segment * nextOffset + segment);
            }
            // peek at the previous frame to calculate the diff
            calculator.setCurrentMotionTime(segment * iter - segment);
            int percentAtPreviousFrame = calculator.getValue();
            float ratioAtPreviousFrame = ((float) percentAtPreviousFrame) / 100.0f;
            int previousWidth = (int) (sourceImage.getWidth() * ratioAtPreviousFrame);
            int previousHeight = (int) (sourceImage.getHeight() * ratioAtPreviousFrame);
            anim[nextOffset].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentHeight, previousHeight);
            anim[nextOffset].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentWidth, previousWidth);
            anim[nextOffset].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - previousWidth) / 2);
            anim[nextOffset].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - previousHeight) / 2);
        }
    }
    currentImage = Timeline.createTimeline(dur, anim, new com.codename1.ui.geom.Dimension(timelineWidth, timelineHeight));
    previewLabel.setIcon(currentImage);
    previewLabel.repaint();
    preview.repaint();
}
Also used : Motion(com.codename1.ui.animations.Motion) AnimationObject(com.codename1.ui.animations.AnimationObject)

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