Search in sources :

Example 11 with Button

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

the class BorderEditor method updateBorder.

private void updateBorder(boolean updateEnabled) {
    if (updateEnabled) {
        okButton.setEnabled(true);
        for (JComponent c : comboSelectionEnabled[0]) {
            c.setEnabled(false);
        }
        if (borderType.getSelectedIndex() > 1) {
            List<JComponent> colorElements = colorComponents;
            if (borderType.getSelectedIndex() == 6 && !imageMode.isSelected()) {
                colorElements = new ArrayList<JComponent>();
            } else {
                if (borderType.getSelectedIndex() < 6 && !themeColors.isSelected()) {
                    colorElements = new ArrayList<JComponent>();
                } else {
                    if (borderType.getSelectedIndex() > 6) {
                        colorElements = new ArrayList<JComponent>();
                    }
                }
            }
            for (JComponent c : comboSelectionEnabled[borderType.getSelectedIndex()]) {
                // if colors arrive from the theme then don't enable any color related element
                c.setEnabled(!colorElements.contains(c));
            }
        }
    }
    if (borderType.getSelectedIndex() == borderType.getItemCount() - 2) {
        // we need to use a special case because a theme with no images will have a different offset for the border
        currentBorder = RoundBorder.create().color(getColor(backgroundColor)).opacity(((Number) opacity.getValue()).intValue()).rectangle(isRectangle.isSelected()).shadowBlur(((Number) shadowBlur.getValue()).floatValue()).shadowOpacity(((Number) shadowOpacity.getValue()).intValue()).shadowSpread(((Number) shadowSpread.getValue()).intValue()).shadowX(((Number) shadowX.getValue()).floatValue()).shadowY(((Number) shadowY.getValue()).floatValue()).stroke(((Number) strokeThickness.getValue()).floatValue(), strokeMillimeter.isSelected()).strokeColor(getColor(strokeColor)).strokeOpacity(((Number) strokeOpacity.getValue()).intValue());
    } else {
        if (borderType.getSelectedIndex() == borderType.getItemCount() - 1) {
            // we need to use a special case because a theme with no images will have a different offset for the border
            currentBorder = RoundRectBorder.create().shadowBlur(((Number) rrShadowBlur.getValue()).floatValue()).shadowOpacity(((Number) rrShadowOpacity.getValue()).intValue()).shadowSpread(((Number) rrShadowSpread.getValue()).floatValue()).shadowX(((Number) rrShadowX.getValue()).floatValue()).shadowY(((Number) rrShadowY.getValue()).floatValue()).stroke(((Number) rrStrokeThickness.getValue()).floatValue(), rrStrokeMillimeter.isSelected()).strokeColor(getColor(rrStrokeColor)).strokeOpacity(((Number) rrStrokeOpacity.getValue()).intValue()).bezierCorners(rrBezier.isSelected()).cornerRadius(((Number) rrRadius.getValue()).floatValue()).bottomOnlyMode(rrMode.getSelectedIndex() == 1).topOnlyMode(rrMode.getSelectedIndex() == 2);
        } else {
            switch(borderType.getSelectedIndex()) {
                case 0:
                    // null border
                    currentBorder = null;
                    break;
                case 1:
                    // empty border
                    currentBorder = Border.getEmpty();
                    break;
                case 2:
                    // bevel border
                    if (themeColors.isSelected()) {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createBevelRaised();
                        } else {
                            currentBorder = Border.createBevelLowered();
                        }
                    } else {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createBevelRaised(getColor(highlightColor), getColor(secondaryHighlightColor), getColor(shadowColor), getColor(secondaryShadowColor));
                        } else {
                            currentBorder = Border.createBevelLowered(getColor(highlightColor), getColor(secondaryHighlightColor), getColor(shadowColor), getColor(secondaryShadowColor));
                        }
                    }
                    break;
                case 3:
                    // etched border
                    if (themeColors.isSelected()) {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createEtchedRaised();
                        } else {
                            currentBorder = Border.createEtchedLowered();
                        }
                    } else {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createEtchedRaised(getColor(highlightColor), getColor(shadowColor));
                        } else {
                            currentBorder = Border.createEtchedLowered(getColor(highlightColor), getColor(shadowColor));
                        }
                    }
                    break;
                case 7:
                    {
                        // this is a theme with no images
                        if (borderType.getItemCount() < 8) {
                            break;
                        }
                        // image border
                        Image c = getButtonImageBorderIcon(this.center);
                        if (imageMode.isSelected()) {
                            currentBorder = Border.createImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(topLeft), c);
                        } else {
                            currentBorder = Border.createImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(bottom), getButtonImageBorderIconNotNull(left), getButtonImageBorderIconNotNull(right), getButtonImageBorderIconNotNull(topLeft), getButtonImageBorderIconNotNull(topRight), getButtonImageBorderIconNotNull(bottomLeft), getButtonImageBorderIconNotNull(bottomRight), c);
                        }
                        break;
                    }
                case 8:
                    {
                        Image c = getButtonImageBorderIcon(this.center);
                        currentBorder = Border.createHorizonalImageBorder(getButtonImageBorderIconNotNull(left), getButtonImageBorderIconNotNull(right), c);
                        break;
                    }
                case 9:
                    {
                        Image c = getButtonImageBorderIcon(this.center);
                        currentBorder = Border.createVerticalImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(bottom), c);
                        break;
                    }
                case 4:
                    // line border
                    if (thicknessMillimeters.isSelected()) {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).floatValue());
                        } else {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).floatValue(), getColor(lineColor));
                        }
                    } else {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).intValue());
                        } else {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).intValue(), getColor(lineColor));
                        }
                    }
                    break;
                case 5:
                    // underline border
                    if (thicknessMillimeters.isSelected()) {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createUndelineBorder(((Number) thickness.getValue()).floatValue());
                        } else {
                            currentBorder = Border.createUnderlineBorder(((Number) thickness.getValue()).floatValue(), getColor(lineColor));
                        }
                    } else {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createUndelineBorder(((Number) thickness.getValue()).intValue());
                        } else {
                            currentBorder = Border.createUnderlineBorder(((Number) thickness.getValue()).intValue(), getColor(lineColor));
                        }
                    }
                    break;
                case 6:
                    // rounded border
                    if (themeColors.isSelected()) {
                        currentBorder = Border.createRoundBorder(((Number) arcWidth.getValue()).intValue(), ((Number) arcHeight.getValue()).intValue());
                    } else {
                        currentBorder = Border.createRoundBorder(((Number) arcWidth.getValue()).intValue(), ((Number) arcHeight.getValue()).intValue(), getColor(lineColor));
                    }
                    break;
            }
        }
    }
    final CodenameOneComponentWrapper w = (CodenameOneComponentWrapper) imageBorderPreview;
    final Border finalBorder = currentBorder;
    final Button b = (Button) w.getCodenameOneComponent();
    Display.getInstance().callSerially(new Runnable() {

        @Override
        public void run() {
            b.clearClientProperties();
            b.setPreferredSize(new com.codename1.ui.geom.Dimension(200, 100));
            b.getAllStyles().setPadding(20, 20, 20, 20);
            b.getAllStyles().setBorder(finalBorder);
            b.getParent().revalidate();
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    w.revalidate();
                }
            });
        }
    });
}
Also used : CodenameOneComponentWrapper(com.codename1.ui.resource.util.CodenameOneComponentWrapper) JComponent(javax.swing.JComponent) Image(com.codename1.ui.Image) Button(com.codename1.ui.Button) JButton(javax.swing.JButton) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 12 with Button

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

the class CodenameOneImplementation method openGallery.

/**
 * Opens the device gallery
 * The method returns immediately and the response will be sent asynchronously
 * to the given ActionListener Object
 *
 * use this in the actionPerformed to retrieve the file path
 * String path = (String) evt.getSource();
 *
 * @param response a callback Object to retrieve the file path
 * @param type one of the following GALLERY_IMAGE, GALLERY_VIDEO, GALLERY_ALL
 * @throws RuntimeException if this feature failed or unsupported on the platform
 */
public void openGallery(final ActionListener response, int type) {
    final Dialog d = new Dialog("Select a picture");
    d.setLayout(new BorderLayout());
    FileTreeModel model = new FileTreeModel(true);
    if (type == Display.GALLERY_IMAGE) {
        model.addExtensionFilter("jpg");
        model.addExtensionFilter("png");
    } else if (type == Display.GALLERY_VIDEO) {
        model.addExtensionFilter("mp4");
        model.addExtensionFilter("3pg");
        model.addExtensionFilter("avi");
        model.addExtensionFilter("mov");
    } else if (type == Display.GALLERY_ALL) {
        model.addExtensionFilter("jpg");
        model.addExtensionFilter("png");
        model.addExtensionFilter("mp4");
        model.addExtensionFilter("3pg");
        model.addExtensionFilter("avi");
        model.addExtensionFilter("mov");
    }
    FileTree t = new FileTree(model) {

        protected Button createNodeComponent(final Object node, int depth) {
            if (node == null || !getModel().isLeaf(node)) {
                return super.createNodeComponent(node, depth);
            }
            Hashtable t = (Hashtable) Storage.getInstance().readObject("thumbnails");
            if (t == null) {
                t = new Hashtable();
            }
            final Hashtable thumbs = t;
            final Button b = super.createNodeComponent(node, depth);
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    response.actionPerformed(new ActionEvent(node, ActionEvent.Type.Other));
                    d.dispose();
                }
            });
            final ImageIO imageio = ImageIO.getImageIO();
            if (imageio != null) {
                Display.getInstance().scheduleBackgroundTask(new Runnable() {

                    public void run() {
                        byte[] data = (byte[]) thumbs.get(node);
                        if (data == null) {
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            try {
                                imageio.save(FileSystemStorage.getInstance().openInputStream((String) node), out, ImageIO.FORMAT_JPEG, b.getIcon().getWidth(), b.getIcon().getHeight(), 1);
                                data = out.toByteArray();
                                thumbs.put(node, data);
                                Storage.getInstance().writeObject("thumbnails", thumbs);
                            } catch (IOException ex) {
                                Log.e(ex);
                            }
                        }
                        Image im = Image.createImage(data, 0, data.length);
                        b.setIcon(im);
                    }
                });
            }
            return b;
        }
    };
    d.addComponent(BorderLayout.CENTER, t);
    d.placeButtonCommands(new Command[] { new Command("Cancel") });
    Command c = d.showAtPosition(2, 2, 2, 2, true);
    if (c != null) {
        response.actionPerformed(null);
    }
}
Also used : Hashtable(java.util.Hashtable) ActionEvent(com.codename1.ui.events.ActionEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageIO(com.codename1.ui.util.ImageIO) FileTreeModel(com.codename1.components.FileTreeModel) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) FileTree(com.codename1.components.FileTree)

Example 13 with Button

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

the class InteractionDialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param rect the screen rectangle to which the popup should point
 */
public void showPopupDialog(Rectangle rect) {
    disposed = false;
    if (getUIID().equals("Dialog")) {
        setUIID("PopupDialog");
        if (getTitleComponent().getUIID().equals("DialogTitle")) {
            getTitleComponent().setUIID("PopupDialogTitle");
        }
        getContentPane().setUIID("PopupContentPane");
    }
    Component contentPane = getContentPane();
    Label title = getTitleComponent();
    UIManager manager = getUIManager();
    String dialogTitle = title.getText();
    // preferred size logic of the dialog won't work with large title borders
    if ((dialogTitle != null || dialogTitle.length() == 0) && manager.isThemeConstant("hideEmptyTitleBool", false)) {
        boolean b = getTitle().length() > 0;
        titleArea.setVisible(b);
        getTitleComponent().setVisible(b);
        if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
            getTitleComponent().setPreferredSize(new Dimension(0, 0));
            getTitleComponent().getStyle().setBorder(null);
            titleArea.setPreferredSize(new Dimension(0, 0));
            if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
                getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
                int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
                int titlePT = getTitleComponent().getStyle().getPaddingTop();
                byte[] pu = getContentPane().getStyle().getPaddingUnit();
                if (pu == null) {
                    pu = new byte[4];
                }
                pu[0] = Style.UNIT_TYPE_PIXELS;
                getContentPane().getStyle().setPaddingUnit(pu);
                int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
                getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
            }
        }
    }
    // allows a text area to recalculate its preferred size if embedded within a dialog
    revalidate();
    Style contentPaneStyle = getStyle();
    boolean restoreArrow = false;
    if (manager.isThemeConstant(getUIID() + "ArrowBool", false)) {
        Image t = manager.getThemeImageConstant(getUIID() + "ArrowTopImage");
        Image b = manager.getThemeImageConstant(getUIID() + "ArrowBottomImage");
        Image l = manager.getThemeImageConstant(getUIID() + "ArrowLeftImage");
        Image r = manager.getThemeImageConstant(getUIID() + "ArrowRightImage");
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setImageBorderSpecialTile(t, b, l, r, rect);
            restoreArrow = true;
        }
    }
    calcPreferredSize();
    int prefHeight = getPreferredH();
    int prefWidth = getPreferredW();
    if (contentPaneStyle.getBorder() != null) {
        prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
        prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
    }
    Form f = Display.getInstance().getCurrent();
    int availableHeight = getLayeredPane(f).getParent().getHeight();
    int availableWidth = getLayeredPane(f).getParent().getWidth();
    int width = Math.min(availableWidth, prefWidth);
    int x = 0;
    int y = 0;
    boolean showPortrait = Display.getInstance().isPortrait();
    // if we don't have enough space then disregard device orientation
    if (showPortrait) {
        if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
            showPortrait = false;
        }
    } else {
        if (availableHeight / 2 > availableWidth - rect.getWidth()) {
            showPortrait = true;
        }
    }
    if (showPortrait) {
        if (width < availableWidth) {
            int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealX > 0) {
                // if the idealX is too far to the right just align to the right
                if (idealX + width > availableWidth) {
                    x = availableWidth - width;
                } else {
                    x = idealX;
                }
            }
        }
        if (rect.getY() < availableHeight / 2) {
            // popup downwards
            y = rect.getY();
            int height = Math.min(prefHeight, availableHeight - y);
            show(y, Math.max(0, availableHeight - height - y), x, Math.max(0, availableWidth - width - x));
        } else {
            // popup upwards
            int height = Math.min(prefHeight, rect.getY() - getLayeredPane(f).getAbsoluteY());
            y = rect.getY() - height - getLayeredPane(f).getAbsoluteY();
            show(y, Math.max(0, getLayeredPane(f).getComponentForm().getHeight() - rect.getY()), x, Math.max(0, availableWidth - width - x));
        }
    } else {
        int height = Math.min(prefHeight, availableHeight);
        if (height < availableHeight) {
            int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealY > 0) {
                // if the idealY is too far up just align to the top
                if (idealY + height > availableHeight) {
                    y = availableHeight - height;
                } else {
                    y = idealY;
                }
            }
        }
        if (prefWidth > rect.getX()) {
            // popup right
            x = rect.getX() + rect.getSize().getWidth();
            if (x + prefWidth > availableWidth) {
                x = availableWidth - prefWidth;
            }
            width = Math.min(prefWidth, availableWidth - x);
            show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
        } else {
            // popup left
            width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
            x = rect.getX() - width;
            show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
        }
    }
/*if(restoreArrow) {
            contentPaneStyle.getBorder().clearImageBorderSpecialTile();
        }*/
}
Also used : Form(com.codename1.ui.Form) Label(com.codename1.ui.Label) UIManager(com.codename1.ui.plaf.UIManager) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component) Image(com.codename1.ui.Image) Border(com.codename1.ui.plaf.Border)

Example 14 with Button

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

the class InteractionDialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param c the context component which is used to position the dialog and can also be pointed at
 */
public void showPopupDialog(Component c) {
    disposed = false;
    Rectangle componentPos = c.getSelectedRect();
    componentPos.setX(componentPos.getX() - c.getScrollX());
    componentPos.setY(componentPos.getY() - c.getScrollY());
    showPopupDialog(componentPos);
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 15 with Button

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

the class RSSReader method updateComponentValues.

void updateComponentValues(Container root, Hashtable h) {
    int c = root.getComponentCount();
    for (int iter = 0; iter < c; iter++) {
        Component current = root.getComponentAt(iter);
        // subclasses
        if (current.getClass() == com.codename1.ui.Container.class || current.getClass() == com.codename1.ui.Tabs.class) {
            updateComponentValues((Container) current, h);
            continue;
        }
        String n = current.getName();
        if (n != null) {
            String val = (String) h.get(n);
            if (val != null) {
                if (current instanceof Button) {
                    final String url = (String) val;
                    ((Button) current).addActionListener(new Listener(url));
                    continue;
                }
                if (current instanceof Label) {
                    ((Label) current).setText(val);
                    continue;
                }
                if (current instanceof TextArea) {
                    ((TextArea) current).setText(val);
                    continue;
                }
                if (current instanceof WebBrowser) {
                    ((WebBrowser) current).setPage(val, null);
                    continue;
                }
            }
        }
    }
}
Also used : Container(com.codename1.ui.Container) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label) Component(com.codename1.ui.Component)

Aggregations

Button (com.codename1.ui.Button)31 BorderLayout (com.codename1.ui.layouts.BorderLayout)25 ActionEvent (com.codename1.ui.events.ActionEvent)21 Container (com.codename1.ui.Container)18 ActionListener (com.codename1.ui.events.ActionListener)17 Component (com.codename1.ui.Component)14 RadioButton (com.codename1.ui.RadioButton)14 Form (com.codename1.ui.Form)12 Label (com.codename1.ui.Label)11 BoxLayout (com.codename1.ui.layouts.BoxLayout)11 TextArea (com.codename1.ui.TextArea)10 Hashtable (java.util.Hashtable)9 Command (com.codename1.ui.Command)8 Image (com.codename1.ui.Image)8 TextField (com.codename1.ui.TextField)8 FlowLayout (com.codename1.ui.layouts.FlowLayout)8 GridLayout (com.codename1.ui.layouts.GridLayout)8 UIManager (com.codename1.ui.plaf.UIManager)8 CheckBox (com.codename1.ui.CheckBox)7 Style (com.codename1.ui.plaf.Style)7