Search in sources :

Example 1 with DefaultListCellRenderer

use of com.codename1.ui.list.DefaultListCellRenderer 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)

Example 2 with DefaultListCellRenderer

use of com.codename1.ui.list.DefaultListCellRenderer 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 3 with DefaultListCellRenderer

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

the class ComboBox method createPopupList.

/**
 * Creates the list object used within the popup dialog. This method allows subclasses
 * to customize the list creation for the popup dialog shown when the combo box is pressed.
 *
 * @return a newly created list object used when the user presses the combo box.
 */
protected List<T> createPopupList() {
    List<T> l = new List<T>(getModel());
    l.setCommandList(isCommandList());
    l.setSmoothScrolling(isSmoothScrolling());
    l.setFixedSelection(getFixedSelection());
    l.setListCellRenderer(getRenderer());
    l.setItemGap(getItemGap());
    l.setUIID("ComboBoxList");
    if (getUIManager().isThemeConstant("otherPopupRendererBool", false)) {
        DefaultListCellRenderer renderer = new DefaultListCellRenderer();
        renderer.setUIID("PopupItem");
        renderer.getListFocusComponent(l).setUIID("PopupFocus");
        l.setListCellRenderer(renderer);
    }
    return l;
}
Also used : DefaultListCellRenderer(com.codename1.ui.list.DefaultListCellRenderer)

Example 4 with DefaultListCellRenderer

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

the class Toolbar method createOverflowCommandList.

/**
 * Creates the list component containing the commands within the given
 * vector used for showing the menu dialog
 *
 * @param commands list of command objects
 * @return List object
 */
protected List createOverflowCommandList(Vector commands) {
    List l = new List(commands);
    l.setUIID("CommandList");
    Component c = (Component) l.getRenderer();
    c.setUIID("Command");
    c = l.getRenderer().getListFocusComponent(l);
    c.setUIID("CommandFocus");
    l.setFixedSelection(List.FIXED_NONE_CYCLIC);
    l.setScrollVisible(false);
    ((DefaultListCellRenderer) l.getRenderer()).setShowNumbers(false);
    return l;
}
Also used : DefaultListCellRenderer(com.codename1.ui.list.DefaultListCellRenderer) ArrayList(java.util.ArrayList)

Example 5 with DefaultListCellRenderer

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

the class Spinner method updateToDefaultRTL.

void updateToDefaultRTL() {
    boolean r = getUIManager().getLookAndFeel().isRTL();
    DefaultListCellRenderer render = ((DefaultListCellRenderer) super.getRenderer());
    render.setRTL(r);
    setRTL(r);
}
Also used : DefaultListCellRenderer(com.codename1.ui.list.DefaultListCellRenderer)

Aggregations

DefaultListCellRenderer (com.codename1.ui.list.DefaultListCellRenderer)4 ArrayList (java.util.ArrayList)3 EncodedImage (com.codename1.ui.EncodedImage)2 AnimationObject (com.codename1.ui.animations.AnimationObject)2 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)2 Component (java.awt.Component)2 DefaultListCellRenderer (javax.swing.DefaultListCellRenderer)2 JList (javax.swing.JList)2 SVG (com.codename1.impl.javase.SVG)1 Component (com.codename1.ui.Component)1 Timeline (com.codename1.ui.animations.Timeline)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)1 BorderLayout (java.awt.BorderLayout)1 BufferedImage (java.awt.image.BufferedImage)1 Comparator (java.util.Comparator)1 Vector (java.util.Vector)1 ComboBoxModel (javax.swing.ComboBoxModel)1 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)1