Search in sources :

Example 1 with ActionListener

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

the class UserInterfaceEditor method createCustomComponentButton.

private void createCustomComponentButton(final CustomComponent c) {
    try {
        final JButton b = new JButton(c.getType());
        b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/jdesktop/swingx/resources/placeholder32.png")));
        b.setHorizontalAlignment(SwingConstants.LEFT);
        b.setBorder(null);
        userComponents.add(b);
        b.putClientProperty("CustomComponent", c);
        final Class codenameOneBaseClass = c.getCls();
        makeDraggable(b, codenameOneBaseClass, c.getType(), c);
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (lockForDragging) {
                    lockForDragging = false;
                    return;
                }
                try {
                    if (c.isUiResource()) {
                        UIBuilderOverride u = new UIBuilderOverride();
                        com.codename1.ui.Component cmp = u.createContainer(res, c.getType());
                        String t = (String) cmp.getClientProperty(TYPE_KEY);
                        if (t == null) {
                            cmp.putClientProperty(TYPE_KEY, c.getType());
                            t = c.getType();
                        }
                        addComponentToContainer(cmp, t);
                        return;
                    }
                    com.codename1.ui.Component cmp = (com.codename1.ui.Component) codenameOneBaseClass.newInstance();
                    cmp.putClientProperty("CustomComponent", c);
                    cmp.putClientProperty(TYPE_KEY, c.getType());
                    initializeComponentText(cmp);
                    addComponentToContainer(cmp, c.getType());
                } catch (Exception err) {
                    err.printStackTrace();
                    JOptionPane.showMessageDialog(UserInterfaceEditor.this, err.getClass().getName() + ": " + err, "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    /*b.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if(BaseForm.isRightClick(e)) {
                        JPopupMenu p = new JPopupMenu();
                        AbstractAction deleteAction = new AbstractAction("Delete") {
                            public void actionPerformed(ActionEvent e) {
                                componentPalette.remove(b);
                                componentPalette.revalidate();
                                customComponents.remove(c);
                                res.setUi(name, persistContainer(containerInstance));
                            }
                        };
                        p.add(deleteAction);
                        p.show(b, e.getPoint().x, e.getPoint().y);
                    }
                }
            });*/
    } catch (Exception err) {
        err.printStackTrace();
        JOptionPane.showMessageDialog(UserInterfaceEditor.this, err.getClass().getName() + ": " + err, "Error", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) ActionListener(java.awt.event.ActionListener) ImageIcon(javax.swing.ImageIcon) Component(java.awt.Component) JComponent(javax.swing.JComponent)

Example 2 with ActionListener

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

the class ResourceEditorApp method main.

/**
 * Main method launching the application.
 */
public static void main(String[] args) throws Exception {
    JavaSEPortWithSVGSupport.blockMonitors();
    JavaSEPortWithSVGSupport.setDesignMode(true);
    JavaSEPortWithSVGSupport.setShowEDTWarnings(false);
    JavaSEPortWithSVGSupport.setShowEDTViolationStacks(false);
    // creates a deadlock between FX, Swing and CN1. Horrible horrible deadlock...
    JavaSEPortWithSVGSupport.blockNativeBrowser = true;
    if (args.length > 0) {
        if (args[0].equalsIgnoreCase("-buildVersion")) {
            Properties p = new Properties();
            try {
                p.load(ResourceEditorApp.class.getResourceAsStream("/version.properties"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println(p.getProperty("build", "1"));
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-style")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            final String uiid = args[2];
            String themeName = args[3];
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            Hashtable themeHash = res.getTheme(themeName);
            final AddThemeEntry entry = new AddThemeEntry(false, res, null, new Hashtable(themeHash), "", themeName);
            entry.setKeyValues(uiid, "");
            entry.setPreferredSize(new Dimension(1000, 600));
            JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.add(entry, BorderLayout.CENTER);
            JPanel bottom = new JPanel();
            ButtonGroup gr = new ButtonGroup();
            JRadioButton unsel = new JRadioButton("Unselected", true);
            gr.add(unsel);
            unsel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("");
                    entry.setKeyValues(uiid, "");
                    entry.revalidate();
                }
            });
            bottom.add(unsel);
            JRadioButton sel = new JRadioButton("Selected");
            gr.add(sel);
            sel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("sel#");
                    entry.setKeyValues(uiid, "sel#");
                    entry.revalidate();
                }
            });
            bottom.add(sel);
            JRadioButton press = new JRadioButton("Pressed");
            gr.add(press);
            press.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("press#");
                    entry.setKeyValues(uiid, "press#");
                    entry.revalidate();
                }
            });
            bottom.add(press);
            JRadioButton dis = new JRadioButton("Disabled");
            gr.add(dis);
            dis.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("dis#");
                    entry.setKeyValues(uiid, "dis#");
                    entry.revalidate();
                }
            });
            bottom.add(dis);
            wrapper.add(bottom, BorderLayout.SOUTH);
            if (ModifiableJOptionPane.showConfirmDialog(null, wrapper, "Edit") == JOptionPane.OK_OPTION) {
                Hashtable tmp = new Hashtable(themeHash);
                entry.updateThemeHashtable(tmp);
                res.setTheme(themeName, tmp);
            }
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-img")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            String imageName;
            String fileName;
            if (args.length == 3) {
                imageName = args[2];
                fileName = args[2];
            } else {
                if (args.length == 4) {
                    imageName = args[3];
                    fileName = args[2];
                } else {
                    System.out.println("The img command works as: -img path_to_resourceFile.res pathToImageFile [image name]");
                    System.exit(1);
                    return;
                }
            }
            File imageFile = new File(fileName);
            if (!imageFile.exists()) {
                System.out.println("File not found: " + imageFile.getAbsolutePath());
                System.exit(1);
                return;
            }
            com.codename1.ui.Image img = ImageRGBEditor.createImageStatic(imageFile);
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            res.setImage(imageName, img);
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-mimg")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            String fileName;
            if (args.length == 4) {
                fileName = args[3];
            } else {
                System.out.println("The mimg command works as: -img path_to_resourceFile.res dpi pathToImageFile");
                System.out.println("dpi can be one of:  high, veryhigh, hd, 560, 2hd, 4k");
                System.exit(1);
                return;
            }
            String dpi = args[2];
            int dpiInt = -1;
            switch(dpi.toLowerCase()) {
                case "high":
                    dpiInt = 3;
                    break;
                case "veryhigh":
                    dpiInt = 4;
                    break;
                case "hd":
                    dpiInt = 5;
                    break;
                case "560":
                    dpiInt = 6;
                    break;
                case "2hd":
                    dpiInt = 7;
                    break;
                case "4k":
                    dpiInt = 8;
                    break;
                default:
                    System.out.println("dpi can be one of:  high, veryhigh, hd, 560, 2hd, 4k");
                    System.exit(1);
                    return;
            }
            File imageFile = new File(fileName);
            if (!imageFile.exists()) {
                System.out.println("File not found: " + imageFile.getAbsolutePath());
                System.exit(1);
                return;
            }
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            AddAndScaleMultiImage.generateImpl(new File[] { imageFile }, res, dpiInt);
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("gen")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File output = new File(args[1]);
            generateResourceFile(output, args[2], args[3]);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("mig")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File projectDir = new File(args[1]);
            EditableResources.setXMLEnabled(true);
            EditableResources res = new EditableResources();
            res.openFileWithXMLSupport(new File(args[2]));
            migrateGuiBuilder(projectDir, res, args[3]);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-regen")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File output = new File(args[1]);
            EditableResources.setXMLEnabled(true);
            EditableResources res = new EditableResources();
            res.openFileWithXMLSupport(output);
            FileOutputStream fos = new FileOutputStream(output);
            res.save(fos);
            fos.close();
            generate(res, output);
            System.exit(0);
            return;
        }
    }
    JavaSEPortWithSVGSupport.setDefaultInitTarget(new JPanel());
    Display.init(null);
    launch(ResourceEditorApp.class, args);
}
Also used : JPanel(javax.swing.JPanel) JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) Properties(java.util.Properties) Container(com.codename1.ui.Container) BorderLayout(java.awt.BorderLayout) EditableResources(com.codename1.ui.util.EditableResources) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Dimension(java.awt.Dimension) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with ActionListener

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

the class ConnectionRequest method addResponseListener.

/**
 * Adds a listener that would be notified on the CodenameOne thread of a response from the server.
 * This event is specific to the connection request type and its firing will change based on
 * how the connection request is read/processed
 *
 * @param a listener
 */
public void addResponseListener(ActionListener<NetworkEvent> a) {
    if (actionListeners == null) {
        actionListeners = new EventDispatcher();
        actionListeners.setBlocking(false);
    }
    actionListeners.addListener(a);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 4 with ActionListener

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

the class ConnectionRequest method addExceptionListener.

/**
 * Adds a listener that would be notified on the CodenameOne thread of an exception
 * in this connection request
 *
 * @param a listener
 */
public void addExceptionListener(ActionListener<NetworkEvent> a) {
    if (exceptionListeners == null) {
        exceptionListeners = new EventDispatcher();
        exceptionListeners.setBlocking(false);
    }
    exceptionListeners.addListener(a);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 5 with ActionListener

use of com.codename1.ui.events.ActionListener 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)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)61 ActionListener (com.codename1.ui.events.ActionListener)61 IOException (java.io.IOException)25 BorderLayout (com.codename1.ui.layouts.BorderLayout)20 EventDispatcher (com.codename1.ui.util.EventDispatcher)16 Hashtable (java.util.Hashtable)14 NetworkEvent (com.codename1.io.NetworkEvent)13 Form (com.codename1.ui.Form)13 Vector (java.util.Vector)11 Container (com.codename1.ui.Container)10 Button (com.codename1.ui.Button)8 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 Dialog (com.codename1.ui.Dialog)7 EncodedImage (com.codename1.ui.EncodedImage)6 Image (com.codename1.ui.Image)6 Label (com.codename1.ui.Label)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6