Example usage for javax.swing SwingWorker addPropertyChangeListener

List of usage examples for javax.swing SwingWorker addPropertyChangeListener

Introduction

In this page you can find the example usage for javax.swing SwingWorker addPropertyChangeListener.

Prototype

public final void addPropertyChangeListener(PropertyChangeListener listener) 

Source Link

Document

Adds a PropertyChangeListener to the listener list.

Usage

From source file:ui.frame.UILogin.java

public UILogin() {
    super("Login");

    Label l = new Label();

    setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(400, 300));

    Panel p = new Panel();

    loginPanel = p.createPanel(Layouts.grid, 4, 2);
    loginPanel.setBorder(new EmptyBorder(25, 25, 0, 25));
    lblUser = l.createLabel("Username:");
    lblPassword = l.createLabel("Password:");
    lblURL = l.createLabel("Server URL");
    lblBucket = l.createLabel("Bucket");

    tfURL = new JTextField();
    tfURL.setText("kaiup.kaisquare.com");
    tfBucket = new JTextField();
    PromptSupport.setPrompt("BucketName", tfBucket);
    tfUser = new JTextField();
    PromptSupport.setPrompt("Username", tfUser);
    pfPassword = new JPasswordField();
    PromptSupport.setPrompt("Password", pfPassword);

    buttonPanel = p.createPanel(Layouts.flow);
    buttonPanel.setBorder(new EmptyBorder(0, 0, 25, 0));
    Button b = new Button();
    JButton btnLogin = b.createButton("Login");
    JButton btnExit = b.createButton("Exit");
    btnLogin.setPreferredSize(new Dimension(150, 50));
    btnExit.setPreferredSize(new Dimension(150, 50));
    Component[] arrayBtn = { btnExit, btnLogin };
    p.addComponentsToPanel(buttonPanel, arrayBtn);

    Component[] arrayComponents = { lblURL, tfURL, lblBucket, tfBucket, lblUser, tfUser, lblPassword,
            pfPassword };//from   w w  w . j av a  2  s.c o m
    p.addComponentsToPanel(loginPanel, arrayComponents);
    add(loginPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    setVisible(true);
    btnLogin.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    if (tfUser.getText().equals("") || String.valueOf(pfPassword.getPassword()).equals("")
                            || tfBucket.getText().equals("")) {
                        JOptionPane.showMessageDialog(Data.mainFrame, "Please fill up all the fields", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    } else {
                        String username = tfUser.getText();
                        String password = String.valueOf(pfPassword.getPassword());
                        Data.URL = Data.protocol + tfURL.getText();
                        Data.targetURL = Data.protocol + tfURL.getText() + "/api/" + tfBucket.getText() + "/";

                        String response = api.loginBucket(Data.targetURL, username, password);

                        try {

                            if (DesktopAppMain.checkResult(response)) {
                                JSONObject responseJSON = new JSONObject(response);
                                Data.sessionKey = responseJSON.get("session-key").toString();
                                response = api.getUserFeatures(Data.targetURL, Data.sessionKey);
                                if (checkFeatures(response)) {
                                    Data.mainFrame = new KAIQRFrame();
                                    Data.mainFrame.uiInventorySelect = new UIInventorySelect();
                                    Data.mainFrame.addPanel(Data.mainFrame.uiInventorySelect, "inventory");
                                    Data.mainFrame.showPanel("inventory");
                                    Data.mainFrame.pack();
                                    setVisible(false);
                                    Data.mainFrame.setVisible(true);

                                } else {
                                    JOptionPane.showMessageDialog(Data.loginFrame,
                                            "User does not have necessary features", "Error",
                                            JOptionPane.ERROR_MESSAGE);
                                }
                            } else {
                                JOptionPane.showMessageDialog(Data.loginFrame, "Wrong username/password",
                                        "Error", JOptionPane.ERROR_MESSAGE);
                            }

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }

                    return null;
                }
            };

            Window win = SwingUtilities.getWindowAncestor((AbstractButton) e.getSource());
            final JDialog dialog = new JDialog(win, "Login", ModalityType.APPLICATION_MODAL);

            mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("state")) {
                        if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                            dialog.dispose();
                        }
                    }
                }
            });

            mySwingWorker.execute();

            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(progressBar, BorderLayout.CENTER);
            panel.add(new JLabel("Logging in .........."), BorderLayout.PAGE_START);
            dialog.add(panel);
            dialog.pack();
            dialog.setBounds(50, 50, 300, 100);
            dialog.setLocationRelativeTo(Data.mainFrame);
            dialog.setVisible(true);
        }
    });

    btnExit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }

    });

}

From source file:ui.panel.UIAccessKeySelect.java

public void runaccessKeySelect() {
    getAccessKeyData();/* ww w.  j  ava 2 s  .  co  m*/
    Panel p = new Panel();
    Button b = new Button();
    Label l = new Label();

    setLayout(new BorderLayout());

    JPanel pnlInstruction = p.createPanel(Layouts.flow);
    JLabel lblInstruction = l.createLabel("Access Keys");
    pnlInstruction.setBackground(CustomColor.LightBlue.returnColor());
    lblInstruction.setForeground(Color.white);
    lblInstruction.setFont(new Font("San Serif", Font.PLAIN, 18));
    pnlInstruction.add(lblInstruction);

    JPanel pnlBucketList = p.createPanel(Layouts.border);

    listBucket.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollBucket = new JScrollPane(listBucket);
    Component[] BucketListComponents = { scrollBucket };
    pnlBucketList.add(scrollBucket, BorderLayout.CENTER);

    JPanel pnlButtons = p.createPanel(Layouts.flow);

    JButton btnBack = b.createButton("Back");
    JButton btnSelectElements = b.createButton("Next");
    JButton btnAdd = b.createButton("Generate Access Key");
    JButton btnRefresh = b.createButton("Refresh");

    pnlButtons.add(btnBack);
    pnlButtons.add(btnAdd);
    pnlButtons.add(btnRefresh);
    pnlButtons.add(btnSelectElements);

    add(pnlInstruction, BorderLayout.NORTH);
    add(pnlBucketList, BorderLayout.CENTER);
    add(pnlButtons, BorderLayout.SOUTH);

    btnBack.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Data.mainFrame.showPanel("license");
        }
    });

    btnRefresh.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            getAccessKeyData();
        }
    });
    btnAdd.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Data.mainFrame.uiGenerateKey = new UIGenerateKey();
            Data.mainFrame.addPanel(Data.mainFrame.uiGenerateKey, "generateKey");
            Data.mainFrame.pack();
            Data.mainFrame.showPanel("generateKey");
        }
    });

    btnSelectElements.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    int selected = listBucket.getSelectedRow();

                    if (selected != -1) {
                        Data.accessKey = (String) listBucket.getModel().getValueAt(selected, 0);
                        Data.mainFrame.qrGenerator = new JavaQR();
                        Data.mainFrame.pack();
                        Data.mainFrame.addPanel(Data.mainFrame.qrGenerator, "generateQR");
                        Data.mainFrame.showPanel("generateQR");
                    } else {
                        JOptionPane.showMessageDialog(Data.mainFrame, "Please Select Access Key", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    }

                    return null;
                }
            };

            Window win = SwingUtilities.getWindowAncestor((AbstractButton) e.getSource());
            final JDialog dialog = new JDialog(win, "Loading", ModalityType.APPLICATION_MODAL);

            mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("state")) {
                        if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                            dialog.dispose();
                        }
                    }
                }
            });
            mySwingWorker.execute();

            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(progressBar, BorderLayout.CENTER);
            panel.add(new JLabel("Generating QR Code......."), BorderLayout.PAGE_START);
            dialog.add(panel);
            dialog.pack();
            dialog.setBounds(50, 50, 300, 100);
            dialog.setLocationRelativeTo(Data.mainFrame);
            dialog.setVisible(true);
        }
    });
}

From source file:ui.panel.UIBucketSelect.java

public void runBucketSelect() {
    Panel p = new Panel();
    Button b = new Button();
    Label l = new Label();

    setLayout(new BorderLayout());

    JPanel pnlInstruction = p.createPanel(Layouts.flow);
    JLabel lblInstruction = l.createLabel("Bucket List");
    pnlInstruction.setBackground(CustomColor.LightBlue.returnColor());
    lblInstruction.setForeground(Color.white);
    lblInstruction.setFont(new Font("San Serif", Font.PLAIN, 18));
    pnlInstruction.add(lblInstruction);/*from ww  w  .j a v  a 2 s.  co  m*/

    JPanel pnlBucketList = p.createPanel(Layouts.border);

    listBucket.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollBucket = new JScrollPane(listBucket);
    scrollBucket.setPreferredSize(new Dimension(300, 150));
    pnlBucketList.add(scrollBucket, BorderLayout.CENTER);

    JPanel pnlButtons = p.createPanel(Layouts.flow);
    JButton btnBack = b.createButton("Back");
    JButton btnSelectElements = b.createButton("Next");
    JButton btnRefresh = b.createButton("Refresh Bucket List");

    pnlButtons.add(btnBack);
    pnlButtons.add(btnRefresh);
    pnlButtons.add(btnSelectElements);

    add(pnlInstruction, BorderLayout.NORTH);
    add(pnlBucketList, BorderLayout.CENTER);
    add(pnlButtons, BorderLayout.SOUTH);
    setVisible(true);

    btnRefresh.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            getBucketData();
        }
    });
    btnSelectElements.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {

                    int selected = listBucket.getSelectedRow();
                    if (selected == -1) {
                        JOptionPane.showMessageDialog(Data.mainFrame, "Please Select a Bucket", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    } else {
                        Data.bucketID = (int) listBucket.getModel().getValueAt(selected, 0);
                        Data.mainFrame.addPanel(Data.mainFrame.uiLicenseDetail = new UILicenseDetail(),
                                "license");
                        Data.mainFrame.pack();
                        Data.mainFrame.showPanel("license");
                    }
                    return null;
                }
            };

            Window win = SwingUtilities.getWindowAncestor((AbstractButton) e.getSource());
            final JDialog dialog = new JDialog(win, "Loading", ModalityType.APPLICATION_MODAL);

            mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("state")) {
                        if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                            dialog.dispose();
                        }
                    }
                }
            });
            mySwingWorker.execute();

            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(progressBar, BorderLayout.CENTER);
            panel.add(new JLabel("Retrieving Licenses......."), BorderLayout.PAGE_START);
            dialog.add(panel);
            dialog.pack();
            dialog.setBounds(50, 50, 300, 100);
            dialog.setLocationRelativeTo(Data.mainFrame);
            dialog.setVisible(true);
        }
    });
    btnBack.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            Data.mainFrame.showPanel("inventory");
        }
    });
}

From source file:ui.panel.UILicenseAdd.java

public JPanel createButtonPanel() {
    JPanel panel = p.createPanel(Layouts.flow);
    panel.setLayout(new FlowLayout(FlowLayout.CENTER));

    btnSubmit = b.createButton("Submit");
    btnCancel = b.createButton("Cancel");

    btnSubmit.addActionListener(new ActionListener() {

        @Override// ww  w . j  av a2  s.  co  m
        public void actionPerformed(ActionEvent e) {

            SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    TreePath[] path = checkTreeManager.getSelectionModel().getSelectionPaths();
                    ArrayList<String> featureL = new ArrayList<String>();
                    String[] features = new String[] {};
                    for (TreePath tp : path) {

                        if (tp.getLastPathComponent().toString().equals("Features")) {
                            Object rootNode = tree.getModel().getRoot();
                            int parentCount = tree.getModel().getChildCount(rootNode);
                            for (int i = 0; i < parentCount; i++) {
                                Object parentNode = tree.getModel().getChild(rootNode, i);
                                int childrenCount = tree.getModel().getChildCount(parentNode);

                                for (int x = 0; x < childrenCount; x++) {
                                    MyDataNode node = (MyDataNode) tree.getModel().getChild(parentNode, x);
                                    featureL.add(node.getValue());
                                }
                            }
                        } else if (tp.getPathCount() == 2) {
                            Object rootNode = tree.getModel().getRoot();
                            int parentCount = tree.getModel().getChildCount(rootNode);
                            for (int i = 0; i < parentCount; i++) {
                                Object parentNode = tree.getModel().getChild(rootNode, i);
                                if (parentNode.toString().equals(tp.getLastPathComponent().toString())) {

                                    int childrenCount = tree.getModel().getChildCount(parentNode);

                                    for (int x = 0; x < childrenCount; x++) {
                                        MyDataNode node = (MyDataNode) tree.getModel().getChild(parentNode, x);
                                        featureL.add(node.getValue());
                                    }
                                }
                            }
                        } else if (tp.getPathCount() == 3) {
                            MyDataNode node = (MyDataNode) tp.getLastPathComponent();
                            featureL.add(node.getValue());
                        }

                    }
                    features = featureL.toArray(features);
                    String duration = spnValidity.getValue().toString();
                    if (cbPerpetual.isSelected()) {
                        duration = "-1";
                    }
                    String storage = spnCloud.getValue().toString();
                    String maxVCA = spnConcurrentVCA.getValue().toString();
                    String response = apiCall.addNodeLicense(Data.targetURL, Data.sessionKey, Data.bucketID,
                            features, duration, storage, maxVCA);

                    try {
                        JSONObject responseObject = new JSONObject(response);
                        if (responseObject.get("result").equals("ok")) {
                            Data.mainFrame.uiLicenseDetail.getLicenseData();
                            Data.mainFrame.showPanel("license");
                        }
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    return null;
                }
            };
            Window win = SwingUtilities.getWindowAncestor((AbstractButton) e.getSource());
            final JDialog dialog = new JDialog(win, "Loading", ModalityType.APPLICATION_MODAL);

            mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("state")) {
                        if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                            dialog.dispose();
                        }
                    }
                }
            });
            mySwingWorker.execute();

            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(progressBar, BorderLayout.CENTER);
            panel.add(new JLabel("Retrieving License..."), BorderLayout.PAGE_START);
            dialog.add(panel);
            dialog.pack();
            dialog.setBounds(50, 50, 300, 100);
            dialog.setLocationRelativeTo(Data.mainFrame);
            dialog.setVisible(true);
        }
    });

    btnCancel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            Data.mainFrame.showPanel("license");
        }
    });

    panel.add(btnSubmit);
    panel.add(btnCancel);

    return panel;
}

From source file:ui.panel.UILicenseDetail.java

public JPanel createButtonPanel() {
    JPanel panel = p.createPanel(Layouts.flow);
    panel.setLayout(new FlowLayout(FlowLayout.CENTER));

    btnSubmit = b.createButton("Next");
    btnBack = b.createButton("Back");
    btnAdd = b.createButton("Add License");

    panel.add(btnBack);//from   w w w  .  ja  v a  2  s  .  com
    panel.add(btnAdd);
    panel.add(btnSubmit);
    btnBack.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Data.mainFrame.showPanel("bucket");
        }
    });

    btnAdd.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Data.mainFrame.addPanel(Data.mainFrame.uiLicenseAdd = new UILicenseAdd(), "licenseAdd");
            Data.mainFrame.showPanel("licenseAdd");
        }
    });
    btnSubmit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    int row = tblInfo.getSelectedRow();
                    if (row == -1) {
                        JOptionPane.showMessageDialog(Data.mainFrame, "Please Select a License", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    } else {
                        Data.licenseNumber = (String) tblInfo.getModel().getValueAt(row, 0);
                        Data.mainFrame.uiAccessKeySelect = new UIAccessKeySelect();
                        Data.mainFrame.addPanel(Data.mainFrame.uiAccessKeySelect, "access");
                        Data.mainFrame.showPanel("access");
                    }

                    return null;
                }
            };

            Window win = SwingUtilities.getWindowAncestor((AbstractButton) e.getSource());
            final JDialog dialog = new JDialog(win, "Loading", ModalityType.APPLICATION_MODAL);

            mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("state")) {
                        if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                            dialog.dispose();
                        }
                    }
                }
            });
            mySwingWorker.execute();

            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(progressBar, BorderLayout.CENTER);
            panel.add(new JLabel("Retrieving Access Keys"), BorderLayout.PAGE_START);
            dialog.add(panel);
            dialog.pack();
            dialog.setBounds(50, 50, 300, 100);
            dialog.setLocationRelativeTo(Data.mainFrame);
            dialog.setVisible(true);

        }

    });

    btnBack.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            Data.mainFrame.showPanel("bucket");
        }
    });

    btnAdd.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            Data.mainFrame.addPanel(Data.mainFrame.uiLicenseAdd = new UILicenseAdd(), "licenseAdd");
            Data.mainFrame.pack();
            Data.mainFrame.showPanel("licenseAdd");
        }
    });

    return panel;
}