private void setWhiskTree()

in src/main/java/org/apache/openwhisk/intellij/explorer/toolwindow/ui/WhiskExplorerWindowForm.java [102:355]


    private void setWhiskTree(WhiskService service,
                              WhiskActionService whiskActionService,
                              WhiskPackageService whiskPackageService,
                              WhiskTriggerService whiskTriggerService) {

        if (StringUtils.isNotEmpty(service.getEndpoints())) {
            try {
                endpoints = getEntities(whiskPackageService,
                        whiskActionService,
                        whiskTriggerService,
                        JsonParserUtils.parseWhiskEndpoints(service.getEndpoints()));
            } catch (IOException e) {
                final String msg = "Failed to parsing endpoints: " + service.getEndpoints();
                LOG.error(msg, e);
                NOTIFIER.notify(project, msg, NotificationType.ERROR);
            }
        }

        whiskJTree.setModel(new WhiskTree(endpoints, whiskPackageService));
        whiskJTree.setRootVisible(false);
        whiskJTree.setCellRenderer(new WhiskTreeCellRenderer());
        expandToNamespace(whiskJTree);
        whiskJTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() > 1) {
                    if (whiskJTree == null) {
                        return;
                    }

                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) whiskJTree.getLastSelectedPathComponent();
                    if (node == null) {
                        return;
                    }

                    Object userObject = node.getUserObject();
                    if (userObject instanceof WhiskEndpoint) {
                        WhiskEndpoint whiskEndpoint = (WhiskEndpoint) userObject;
                        LOG.info(whiskEndpoint.getAlias());
                    } else if (userObject instanceof WhiskNamespace) {
                        WhiskNamespace whiskNamespace = (WhiskNamespace) userObject;
                        LOG.info(whiskNamespace.getPath());
                    } else if (userObject instanceof WhiskPackage) {
                        WhiskPackage whiskPackage = (WhiskPackage) userObject;
                        LOG.info(whiskPackage.getName());
                    } else if (userObject instanceof WhiskActionMetaData) {
                        WhiskActionMetaData whiskAction = (WhiskActionMetaData) userObject;
                        LOG.info(whiskAction.getName());

                        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();
                        if (parentNode.getUserObject() instanceof WhiskPackage) {
                            // TODO refactor getAuthFromTreeNode method
                            WhiskPackage pkg = (WhiskPackage) parentNode.getUserObject();

                            DefaultMutableTreeNode namespaceNode = (DefaultMutableTreeNode) parentNode.getParent();
                            WhiskNamespace namespace = (WhiskNamespace) namespaceNode.getUserObject();

                            DefaultMutableTreeNode endpointNode = (DefaultMutableTreeNode) namespaceNode.getParent();
                            WhiskEndpoint endpoint = (WhiskEndpoint) endpointNode.getUserObject();

                            WhiskAuth auth = new WhiskAuth(namespace.getAuth(), endpoint.getApihost());

                            try {
                                whiskActionService.getWhiskAction(auth,
                                        Optional.ofNullable(namespace.getPath()),
                                        Optional.ofNullable(pkg.getName()), whiskAction.getName())
                                        .ifPresent(executableWhiskAction -> openEditorAndWhiskRunWindow(auth, executableWhiskAction));
                            } catch (IOException e1) {
                                final String msg1 = "The action cannot be opened.";
                                LOG.error(msg1, e1);
                                NOTIFIER.notify(project, msg1, NotificationType.ERROR);
                            }
                        } else if (parentNode.getUserObject() instanceof WhiskNamespace) {
                            WhiskNamespace namespace = (WhiskNamespace) parentNode.getUserObject();

                            DefaultMutableTreeNode endpointNode = (DefaultMutableTreeNode) parentNode.getParent();
                            WhiskEndpoint endpoint = (WhiskEndpoint) endpointNode.getUserObject();

                            WhiskAuth auth = new WhiskAuth(namespace.getAuth(), endpoint.getApihost());

                            try {
                                whiskActionService.getWhiskAction(auth, Optional.ofNullable(namespace.getPath()), Optional.empty(), whiskAction.getName())
                                        .ifPresent(executableWhiskAction -> openEditorAndWhiskRunWindow(auth, executableWhiskAction));
                            } catch (IOException e1) {
                                final String msg1 = "The action cannot be opened.";
                                LOG.error(msg1, e1);
                                NOTIFIER.notify(project, msg1, NotificationType.ERROR);
                            }

                        } else {
                            LOG.error("[WhiskActionMetaData] The action cannot be loaded: " + whiskAction.getFullyQualifiedName());
                        }
                    } else if (userObject instanceof CompactWhiskAction) {
                        CompactWhiskAction action = (CompactWhiskAction) userObject;
                        LOG.info(action.getName());

                        DefaultMutableTreeNode boundPkgNode = (DefaultMutableTreeNode) node.getParent();
                        if (boundPkgNode.getUserObject() instanceof WhiskPackage) {
                            WhiskPackage boundPkg = (WhiskPackage) boundPkgNode.getUserObject();

                            DefaultMutableTreeNode namespaceNode = (DefaultMutableTreeNode) boundPkgNode.getParent();
                            WhiskNamespace namespace = (WhiskNamespace) namespaceNode.getUserObject();

                            DefaultMutableTreeNode endpointNode = (DefaultMutableTreeNode) namespaceNode.getParent();
                            WhiskEndpoint endpoint = (WhiskEndpoint) endpointNode.getUserObject();

                            WhiskAuth auth = new WhiskAuth(namespace.getAuth(), endpoint.getApihost());

                            boundPkg.getBinding().ifPresent(p -> {
                                try {
                                    //When clicked, the code is fetched from the remote server.
                                    whiskActionService.getWhiskAction(auth,
                                            Optional.ofNullable(p.getNamespace()),
                                            Optional.ofNullable(p.getName()), action.getName())
                                            .ifPresent(executableWhiskAction -> openEditorAndWhiskRunWindow(auth, executableWhiskAction));
                                } catch (IOException e1) {
                                    final String msg1 = "The action cannot be opened.";
                                    LOG.error(msg1, e1);
                                    NOTIFIER.notify(project, msg1, NotificationType.ERROR);
                                }

                            });
                        } else {
                            LOG.error("[CompactWhiskAction] The action cannot be loaded: " + action.getName());
                        }
                    } else if (userObject instanceof WhiskTriggerRoot) {
                        // Nothing to do
                        LOG.debug("[WhiskTriggerRoot] is selected");
                    } else if (userObject instanceof WhiskTriggerMetaData) {
                        DefaultMutableTreeNode namespaceNode = (DefaultMutableTreeNode) node.getParent().getParent();

                        WhiskNamespace namespace = (WhiskNamespace) namespaceNode.getUserObject();

                        DefaultMutableTreeNode endpointNode = (DefaultMutableTreeNode) namespaceNode.getParent();
                        WhiskEndpoint endpoint = (WhiskEndpoint) endpointNode.getUserObject();

                        WhiskAuth auth = new WhiskAuth(namespace.getAuth(), endpoint.getApihost());

                        WhiskTriggerMetaData whiskTriggerMetaData = (WhiskTriggerMetaData) userObject;
                        LOG.info(whiskTriggerMetaData.getName());
                        try {
                            whiskTriggerService.getWhiskTrigger(auth, whiskTriggerMetaData.getName()).ifPresent(executableWhiskTrigger ->
                                    EventUtils.publish(project,
                                            OpenTriggerControlActionListener.TOPIC,
                                            (l) -> l.openTriggerControlWindow(auth, executableWhiskTrigger)));
                        } catch (IOException ex) {
                            final String msg = "The trigger cannot be loaded: " + whiskTriggerMetaData.getName();
                            LOG.error(msg, ex);
                            NOTIFIER.notify(project, msg, NotificationType.ERROR);
                        }
                    } else {
                        LOG.error("The unknown object of tree: " + userObject.toString());
                    }
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    if (whiskJTree == null) {
                        return;
                    }

                    TreePath path = whiskJTree.getPathForLocation(e.getX(), e.getY());
                    if (path == null) {
                        return;
                    }

                    whiskJTree.setSelectionPath(path);
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                    if (node.getUserObject() instanceof WhiskEndpoint) {
                        WhiskEndpoint whiskEndpoint = (WhiskEndpoint) node.getUserObject();
                        ActionGroup actionGroup = new WhiskEndpointGroup(whiskEndpoint);
                        ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                        actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                    } else if (node.getUserObject() instanceof WhiskNamespace) {
                        getAuthFromTreeNode(node).ifPresent(auth -> {
                            WhiskNamespace whiskNamespace = (WhiskNamespace) node.getUserObject();
                            ActionGroup actionGroup = new WhiskNamespaceGroup(auth, whiskNamespace, endpoints);
                            ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                            actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                        });
                    } else if (node.getUserObject() instanceof WhiskActionMetaData) {
                        WhiskActionMetaData whiskActionMetaData = (WhiskActionMetaData) node.getUserObject();
                        getAuthFromTreeNode(node).ifPresent(auth -> {
                            ActionGroup actionGroup = new WhiskActionGroup(auth, whiskActionMetaData, endpoints);
                            ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                            actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                        });
                    } else if (node.getUserObject() instanceof CompactWhiskAction) {
                        CompactWhiskAction compactWhiskAction = (CompactWhiskAction) node.getUserObject();
                        WhiskPackage pkg = (WhiskPackage) ((DefaultMutableTreeNode) node.getParent()).getUserObject();
                        WhiskNamespace ns = (WhiskNamespace) ((DefaultMutableTreeNode) node.getParent().getParent()).getUserObject();

                        getAuthFromTreeNode(node).ifPresent(auth -> {
                            try {
                                whiskActionService.getWhiskAction(auth, Optional.of(ns.getPath()), Optional.of(pkg.getName()), compactWhiskAction.getName())
                                        .ifPresent(action -> {
                                            ActionGroup actionGroup = new WhiskActionGroup(auth, toBindingWhiskActionMetaData(pkg, action), endpoints);
                                            ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                                            actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                                        });
                            } catch (IOException ex) {
                                final String msg = "The action cannot be loaded.";
                                LOG.error(msg, ex);
                                NOTIFIER.notify(project, msg, NotificationType.ERROR);
                            }
                        });
                    } else if (node.getUserObject() instanceof WhiskTriggerMetaData) {
                        WhiskTriggerMetaData whiskTriggerMetaData = (WhiskTriggerMetaData) node.getUserObject();
                        getAuthFromTreeNode(node).ifPresent(auth -> {
                            ActionGroup actionGroup = new WhiskTriggerGroup(auth, whiskTriggerMetaData, endpoints);
                            ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                            actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                        });
                    } else if (node.getUserObject() instanceof WhiskTriggerRoot) {
                        getAuthFromTreeNode(node).ifPresent(auth -> {
                            ActionGroup actionGroup = new WhiskTriggerRootGroup(auth);
                            ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                            actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                        });
                    } else if (node.getUserObject() instanceof WhiskPackage) {
                        getAuthFromTreeNode(node).ifPresent(auth -> {
                            WhiskPackage whiskPackage = (WhiskPackage) node.getUserObject();
                            ActionGroup actionGroup = new WhiskPackageGroup(auth, whiskPackage);
                            ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu("", actionGroup);
                            actionPopupMenu.getComponent().show(whiskJTree, e.getX(), e.getY());
                        });
                    }
                }
            }
        });

        EventUtils.subscribe(project, project, RefreshWhiskTreeListener.TOPIC, () -> {
            if (whiskJTree != null) {
                try {
                    List<WhiskEndpoint> whiskEndpoints = JsonParserUtils.parseWhiskEndpoints(service.getEndpoints());
                    endpoints = getEntities(whiskPackageService, whiskActionService, whiskTriggerService, whiskEndpoints);
                    whiskJTree.setModel(new WhiskTree(endpoints, whiskPackageService));
                    expandToNamespace(whiskJTree);

                    if (whiskEndpoints.isEmpty()) {
                        final String msg = "There are no endpoints saved.";
                        LOG.info(msg);
                        NOTIFIER.notify(project, msg, NotificationType.INFORMATION);
                    }
                } catch (IOException e) {
                    final String msg = "Failed to parsing endpoints: " + service.getEndpoints();
                    LOG.error(msg, e);
                    NOTIFIER.notify(project, msg, NotificationType.ERROR);
                }
            }
        });
    }