Search in sources :

Example 11 with DefaultListCellRenderer

use of javax.swing.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 12 with DefaultListCellRenderer

use of javax.swing.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 13 with DefaultListCellRenderer

use of javax.swing.DefaultListCellRenderer in project CodenameOne by codenameone.

the class AddThemeEntry method initUIIDComboBox.

/**
 * Initializes a combo box for editing UIID's
 */
public static void initUIIDComboBox(JComboBox jc) {
    jc.setEditable(true);
    Vector uiids = new Vector();
    uiids.add("");
    for (Object k : Accessor.getThemeProps().keySet()) {
        String key = (String) k;
        int dot = key.indexOf('.');
        if (dot > -1 && key.indexOf('@') < 0) {
            key = key.substring(0, dot);
            if (!uiids.contains(key)) {
                uiids.add(key);
            }
        }
    }
    Collections.sort(uiids, String.CASE_INSENSITIVE_ORDER);
    jc.setModel(new DefaultComboBoxModel(uiids));
    com.codename1.ui.Form currentForm = com.codename1.ui.Display.getInstance().getCurrent();
    if (currentForm != null) {
        final List<String> currentFormUIIDs = new ArrayList<String>();
        findAllUIIDs(currentFormUIIDs, currentForm);
        Collections.sort(currentFormUIIDs, String.CASE_INSENSITIVE_ORDER);
        Collections.reverse(currentFormUIIDs);
        for (String cmp : currentFormUIIDs) {
            jc.insertItemAt(cmp, 1);
        }
        jc.setRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                String uiid = (String) value;
                if (index > 0 && index < currentFormUIIDs.size() + 1) {
                    value = "<html><body><b>" + value + "</b></body></html>";
                } else {
                    if (value == null || ((String) value).length() == 0) {
                        value = "[null]";
                    }
                }
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                setIcon(ThemeEditor.getUIIDPreviewImage(uiid, false, false, false));
                return this;
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) JComponent(javax.swing.JComponent) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) Vector(java.util.Vector) JList(javax.swing.JList)

Example 14 with DefaultListCellRenderer

use of javax.swing.DefaultListCellRenderer in project CodenameOne by codenameone.

the class Import9Patch method pickDirectoryActionPerformed.

// </editor-fold>//GEN-END:initComponents
private void pickDirectoryActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_pickDirectoryActionPerformed
    File[] result = ResourceEditorView.showOpenFileChooserWithTitle("Select The Directory", true, "Directory");
    if (result != null && result.length == 1) {
        imageDirectory.setText(result[0].getAbsolutePath());
    }
    File drawableHdpi = new File(imageDirectory.getText() + File.separator + "drawable-hdpi");
    File[] potentials = drawableHdpi.listFiles(new FileFilter() {

        @Override
        public boolean accept(File file) {
            return file.getName().endsWith(".9.png");
        }
    });
    if (potentials == null || potentials.length == 0) {
        JOptionPane.showMessageDialog(this, "Can't find 9-patch files in the hdpi directory", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    imagesCombo.setRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList jlist, Object o, int i, boolean bln, boolean bln1) {
            if (o instanceof File) {
                o = ((File) o).getName();
            }
            return super.getListCellRendererComponent(jlist, o, i, bln, bln1);
        }
    });
    imagesCombo.setModel(new DefaultComboBoxModel(potentials));
}
Also used : DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) FileFilter(java.io.FileFilter) Component(java.awt.Component) File(java.io.File) JList(javax.swing.JList)

Example 15 with DefaultListCellRenderer

use of javax.swing.DefaultListCellRenderer in project cayenne by apache.

the class PreferenceDialogView method init.

private void init() {
    split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.setBorder(TopBorder.create());
    list = new JList<>();
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 0));
            return this;
        }
    });
    list.setFont(new JLabel().getFont().deriveFont(Font.BOLD, 12));
    detailLayout = new CardLayout();
    detailPanel = new JPanel(detailLayout);
    saveButton = new JButton("Save");
    cancelButton = new JButton("Cancel");
    // assemble
    Container leftContainer = new JPanel(new BorderLayout());
    JScrollPane scrollPane = new JScrollPane(list);
    scrollPane.setBorder(BorderFactory.createEmptyBorder());
    leftContainer.add(scrollPane);
    leftContainer.setPreferredSize(new Dimension(180, 400));
    split.setLeftComponent(leftContainer);
    split.setRightComponent(detailPanel);
    split.setDividerSize(3);
    getRootPane().setDefaultButton(saveButton);
    JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    buttons.add(cancelButton);
    buttons.add(saveButton);
    buttons.setBorder(TopBorder.create());
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(split, BorderLayout.CENTER);
    getContentPane().add(buttons, BorderLayout.SOUTH);
    setTitle("Edit Preferences");
}
Also used : JScrollPane(javax.swing.JScrollPane) CardLayout(java.awt.CardLayout) JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Container(java.awt.Container) BorderLayout(java.awt.BorderLayout) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) JSplitPane(javax.swing.JSplitPane) Component(java.awt.Component) JList(javax.swing.JList)

Aggregations

DefaultListCellRenderer (javax.swing.DefaultListCellRenderer)55 Component (java.awt.Component)45 JList (javax.swing.JList)30 JLabel (javax.swing.JLabel)19 JComboBox (javax.swing.JComboBox)18 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)14 JComponent (javax.swing.JComponent)13 JPanel (javax.swing.JPanel)11 JScrollPane (javax.swing.JScrollPane)10 Dimension (java.awt.Dimension)9 GridBagConstraints (java.awt.GridBagConstraints)9 BorderLayout (java.awt.BorderLayout)8 GridBagLayout (java.awt.GridBagLayout)8 Point (java.awt.Point)6 ArrayList (java.util.ArrayList)6 Insets (java.awt.Insets)5 ActionEvent (java.awt.event.ActionEvent)5 Feature (cbit.vcell.model.Feature)4 Membrane (cbit.vcell.model.Membrane)4 SpeciesContext (cbit.vcell.model.SpeciesContext)3