Search in sources :

Example 1 with FormActionParser

use of com.centurylink.mdw.common.utilities.form.FormActionParser in project mdw-designer by CenturyLinkCloud.

the class FormPanel method performJavaScriptAction.

private void performJavaScriptAction(ActionEvent event, Component src, String cmd, MbengNode node) {
    String action = node.getAttribute(FormConstants.FORMATTR_ACTION);
    if (cmd.equals(SwingFormGenerator.ACTION_DROPDOWN)) {
        Object selected = ((JComboBox) src).getSelectedItem();
        if (selected != null && selected instanceof SelectOption) {
            this.setDataValue(node, ((SelectOption) selected).getValue());
        }
    } else if (cmd.equals(SwingFormGenerator.ACTION_RADIOBUTTON)) {
        this.setDataValue(node, ((JRadioButton) src).getName());
    } else if (cmd.equals(SwingFormGenerator.ACTION_CHECKBOX)) {
        String av = ((JCheckBox) src).getName();
        String[] choices = av.split(",");
        this.setDataValue(node, ((JCheckBox) src).isSelected() ? choices[1] : choices[0]);
    }
    try {
        FormActionParser command = new FormActionParser(action);
        String functionName = command.getFunctionName();
        if (functionName.equals("dialog_open")) {
            try {
                String formname = command.getArgument(0);
                show_dialog(formname, true);
            } catch (Exception e) {
                e.printStackTrace();
                show_error("Failed to load script " + e.getMessage());
            }
        } else if (functionName.equals("dialog_ok")) {
            frame.setVisible(false);
        } else if (functionName.equals("dialog_cancel")) {
            exit_copy = false;
            frame.setVisible(false);
        } else if (functionName.equals("hyperlink")) {
            this.launchBrowser(command.getArgument(0));
        } else if (functionName.equals("validate")) {
        } else if (functionName.equals("repaint")) {
        } else if (functionName.equals("task_action")) {
        } else if (functionName.equals("perform_action")) {
            performGenericAction(command.getArgument(0));
        } else if (functionName.equals("ajax_action")) {
            performGenericAction(command.getArgument(0));
        } else if (functionName.equals("ajax_action_async")) {
            performGenericAction(command.getArgument(0));
        } else if (functionName.equals("show_page")) {
            boolean inNewWindow = "true".equalsIgnoreCase(command.getArgument(1));
            String formname = command.getArgument(0);
            if (inNewWindow) {
                try {
                    show_dialog(formname, false);
                } catch (Exception e) {
                    e.printStackTrace();
                    show_error("Failed to load script " + e.getMessage());
                }
            } else {
                try {
                    this.resetData(formname, null);
                } catch (Exception e) {
                    e.printStackTrace();
                    show_error("Failed to load script " + e.getMessage());
                }
            }
        } else if (functionName.equals("start_process")) {
            action = FormConstants.ACTION_START_PROCESS + "?" + FormDataDocument.META_PROCESS_NAME + "=" + command.getArgument(0);
            if (command.getArgumentCount() >= 2 && command.getArgument(1).length() > 0)
                action += "&" + FormDataDocument.META_MASTER_REQUEST_ID + "=" + command.getArgument(1);
            performGenericAction(action);
        } else if (functionName.equals("table_row_view")) {
            String tableId = command.getArgument(0);
            // String dialogId = command.getArgument(1); TODO implement custom dialog
            MbengNode tableNode = canvas.getGenerator().getNodeById(tableId);
            if (tableNode == null)
                throw new Exception("Table node does not exist - id=" + tableId);
            JTablePlus table = (JTablePlus) canvas.getGenerator().getWidget(tableNode);
            int row = table.getSelectedRow();
            if (row < 0)
                throw new Exception("You need to select a row");
            TableRowDialog dialog = new TableRowDialog(tableNode, table, row);
            dialog.setVisible(true);
        } else if (functionName.equals("table_row_new")) {
            String tableId = command.getArgument(0);
            // String dialogId = command.getArgument(1); TODO implement custom dialog
            MbengNode tableNode = canvas.getGenerator().getNodeById(tableId);
            if (tableNode == null)
                throw new Exception("Table node does not exist - id=" + tableId);
            JTablePlus table = (JTablePlus) canvas.getGenerator().getWidget(tableNode);
            TableRowDialog dialog = new TableRowDialog(tableNode, table, -1);
            dialog.setVisible(true);
        } else if (functionName.equals("table_row_delete")) {
            String tableId = command.getArgument(0);
            // String dialogId = command.getArgument(1); TODO implement custom dialog
            MbengNode tableNode = canvas.getGenerator().getNodeById(tableId);
            if (tableNode == null)
                throw new Exception("Table node does not exist - id=" + tableId);
            JTablePlus table = (JTablePlus) canvas.getGenerator().getWidget(tableNode);
            String datapath = tableNode.getAttribute(FormConstants.FORMATTR_DATA);
            String paginator = tableNode.getAttribute(FormConstants.FORMATTR_ACTION);
            String av = tableNode.getAttribute(FormConstants.FORMATTR_TABLE_STYLE);
            boolean isPaginated = FormConstants.FORMATTRVALUE_TABLESTYLE_PAGINATED.equals(av);
            boolean useMetaData = true;
            if (useMetaData) {
                action = paginator + "?action=deleterow&table=" + datapath + "&meta=" + datapath + "_META&topage=" + (isPaginated ? "S" : "R");
            } else {
                int row = table.getSelectedRow();
                if (row < 0)
                    throw new Exception("You need to select a row");
                action = paginator + "?action=deleterow&table=" + datapath + "&meta=" + datapath + "_META&row=" + row + "&topage=" + (isPaginated ? "S" : "R");
            }
            performGenericAction(action);
        } else if (functionName.equals("switch_server")) {
            String server_name = datadoc.getValue(command.getArgument(0));
            server_name = server_name.replace(" port ", ":");
            String serverUrl = dao.getCurrentServer().getServerUrl();
            int k1 = serverUrl.indexOf("://");
            int k2 = serverUrl.indexOf("/", k1 + 4);
            String newServerUrl = k2 > 0 ? (serverUrl.substring(0, k1 + 3) + server_name + serverUrl.substring(k2)) : (serverUrl.substring(0, k1 + 3) + server_name);
            dao.getCurrentServer().setServerUrl(newServerUrl);
            performGenericAction(command.getArgument(1));
        } else
            throw new Exception("Unknown action function " + functionName);
    } catch (Exception e) {
        if (!(action == null || action.length() == 0) || !(cmd.equals(SwingFormGenerator.ACTION_DROPDOWN) || cmd.equals(SwingFormGenerator.ACTION_RADIOBUTTON)))
            show_error(e.getMessage());
    }
}
Also used : FormActionParser(com.centurylink.mdw.common.utilities.form.FormActionParser) JRadioButton(javax.swing.JRadioButton) JComboBox(javax.swing.JComboBox) MbengNode(com.qwest.mbeng.MbengNode) MbengException(com.qwest.mbeng.MbengException) JCheckBox(javax.swing.JCheckBox) SelectOption(com.centurylink.mdw.designer.utils.SwingFormGenerator.SelectOption) JTablePlus(com.centurylink.mdw.designer.utils.JTablePlus)

Aggregations

FormActionParser (com.centurylink.mdw.common.utilities.form.FormActionParser)1 JTablePlus (com.centurylink.mdw.designer.utils.JTablePlus)1 SelectOption (com.centurylink.mdw.designer.utils.SwingFormGenerator.SelectOption)1 MbengException (com.qwest.mbeng.MbengException)1 MbengNode (com.qwest.mbeng.MbengNode)1 JCheckBox (javax.swing.JCheckBox)1 JComboBox (javax.swing.JComboBox)1 JRadioButton (javax.swing.JRadioButton)1