Package edu.indiana.extreme.xbaya.graph.dynamic.gui

Source Code of edu.indiana.extreme.xbaya.graph.dynamic.gui.DynamicWorkflowRunnerWindow

/*
* Copyright (c) 2008 Extreme! Lab, Indiana University. All rights reserved.
*
* This software is open source. See the bottom of this file for the license.
*
* $Id: DynamicWorkflowRunnerWindow.java,v 1.2 2009/01/25 02:58:47 cherath Exp $
*/
package edu.indiana.extreme.xbaya.graph.dynamic.gui;



import java.awt.event.ActionEvent;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.xml.namespace.QName;

import org.xmlpull.infoset.XmlElement;
import org.xmlpull.v1.builder.XmlInfosetBuilder;

import xsul.XmlConstants;
import xsul5.MLogger;
import edu.indiana.extreme.xbaya.XBayaConfiguration;
import edu.indiana.extreme.xbaya.XBayaConstants;
import edu.indiana.extreme.xbaya.XBayaEngine;
import edu.indiana.extreme.xbaya.XBayaException;
import edu.indiana.extreme.xbaya.graph.system.InputNode;
import edu.indiana.extreme.xbaya.graph.util.GraphUtil;
import edu.indiana.extreme.xbaya.gui.ErrorMessages;
import edu.indiana.extreme.xbaya.gui.GridPanel;
import edu.indiana.extreme.xbaya.gui.XBayaDialog;
import edu.indiana.extreme.xbaya.gui.XBayaLabel;
import edu.indiana.extreme.xbaya.gui.XBayaTextField;
import edu.indiana.extreme.xbaya.interpretor.WorkflowInterpreter;
import edu.indiana.extreme.xbaya.jython.script.JythonScript;
import edu.indiana.extreme.xbaya.monitor.MonitorConfiguration;
import edu.indiana.extreme.xbaya.util.StringUtil;
import edu.indiana.extreme.xbaya.util.XMLUtil;
import edu.indiana.extreme.xbaya.wf.Workflow;



/**
* @author Chathura Herath
*/
public class DynamicWorkflowRunnerWindow {

  private static final MLogger logger = MLogger.getLogger();

    private XBayaEngine engine;

    private Workflow workflow;

    private XBayaDialog dialog;

    private GridPanel parameterPanel;

    private XBayaTextField topicTextField;


    private List<XBayaTextField> parameterTextFields = new ArrayList<XBayaTextField>();

  private XBayaTextField xRegistryTextField;

  private XBayaTextField gfacTextField;


  protected final static XmlInfosetBuilder builder = XmlConstants.BUILDER;
    /**
     * Constructs a TavernaRunnerWindow.
     *
     * @param engine
     *
     */
    public DynamicWorkflowRunnerWindow(XBayaEngine engine) {
        this.engine = engine;
        initGUI();
    }

    /**
     * Shows the dialog.
     */
    public void show() {
        this.workflow = this.engine.getWorkflow();
       

        MonitorConfiguration notifConfig = this.engine.getMonitor()
                .getConfiguration();
        if (notifConfig.getBrokerURL() == null) {
            this.engine.getErrorWindow().error(
                    ErrorMessages.BROKER_URL_NOT_SET_ERROR);
            return;
        }


        // Create input fields
        Collection<InputNode> inputNodes = GraphUtil
                .getInputNodes(this.workflow.getGraph());
        for (InputNode node : inputNodes) {
            String id = node.getID();
            QName parameterType = node.getParameterType();
            JLabel nameLabel = new JLabel(id);
            JLabel typeField = new JLabel(parameterType.getLocalPart());
            XBayaTextField paramField = new XBayaTextField();
            Object value = node.getDefaultValue();

            String valueString;
            if (value == null) {
                valueString = "";
            } else {
                if (value instanceof XmlElement) {
                    XmlElement valueElement = (XmlElement) value;
                    valueString = XMLUtil.xmlElementToString(valueElement);
                } else {
                    // Only string comes here for now.
                    valueString = value.toString();
                }
            }
            paramField.setText(valueString);
            this.parameterPanel.add(nameLabel);
            this.parameterPanel.add(typeField);
            this.parameterPanel.add(paramField);
            this.parameterTextFields.add(paramField);
        }
        this.parameterPanel.layout(inputNodes.size(), 3, GridPanel.WEIGHT_NONE,
                2);

        this.topicTextField.setText(notifConfig.getTopic());

        XBayaConfiguration config = this.engine.getConfiguration();
        this.gfacTextField.setText(config.getGFacURL().toString());
        URI registryURL = config.getXRegistryURL();
        if(null != registryURL){
          this.xRegistryTextField.setText(registryURL.toString());
        }else{
          this.xRegistryTextField.setText(XBayaConstants.DEFAULT_XREGISTRY_URL);
        }
       
        this.dialog.show();
    }

    /**
     * Hides the dialog.
     */
    public void hide() {
        this.dialog.hide();

        this.parameterPanel.getContentPanel().removeAll();
        this.parameterTextFields.clear();
    }

    private void initGUI() {
        this.parameterPanel = new GridPanel(true);

        this.topicTextField = new XBayaTextField();
        XBayaLabel topicLabel = new XBayaLabel("Notification topic",
                this.topicTextField);
        this.xRegistryTextField = new XBayaTextField();
        XBayaLabel xRegistryLabel = new XBayaLabel("XRegistry URL",
                this.xRegistryTextField);
        this.gfacTextField = new XBayaTextField();
        XBayaLabel gfacLabel = new XBayaLabel("GFac URL", this.gfacTextField);

        GridPanel infoPanel = new GridPanel();
        infoPanel.add(topicLabel);
        infoPanel.add(this.topicTextField);
        infoPanel.add(xRegistryLabel);
        infoPanel.add(this.xRegistryTextField);
        infoPanel.add(gfacLabel);
        infoPanel.add(this.gfacTextField);
        infoPanel.layout(3, 2, GridPanel.WEIGHT_NONE, 1);

        GridPanel mainPanel = new GridPanel();
        mainPanel.add(this.parameterPanel);
        mainPanel.add(infoPanel);
        mainPanel.layout(2, 1, 0, 0);

        JButton okButton = new JButton("OK");
        okButton.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                execute();
            }
        });

        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                hide();
            }
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);

        this.dialog = new XBayaDialog(this.engine, "Invoke  workflow",
                mainPanel, buttonPanel);
        this.dialog.setDefaultButton(okButton);
    }

    private void execute() {
        final List<String> arguments = new ArrayList<String>();

        String topic = this.topicTextField.getText();
        if (topic.length() == 0) {
            this.engine.getErrorWindow().error(ErrorMessages.TOPIC_EMPTY_ERROR);
            return;
        }
       
       
       

        // Use topic as a base of workflow instance ID so that the monitor can
        // find it.
        URI workfowInstanceID = URI.create(StringUtil
                .convertToJavaIdentifier(topic));
        this.workflow.setGPELInstanceID(workfowInstanceID);

        MonitorConfiguration notifConfig = this.engine.getMonitor()
                .getConfiguration();
        notifConfig.setTopic(topic);
        arguments.add("-" + JythonScript.TOPIC_VARIABLE);
        arguments.add(topic);

        // TODO error check for user inputs

        List<InputNode> inputNodes = GraphUtil.getInputNodes(this.workflow
                .getGraph());
        final org.xmlpull.v1.builder.XmlElement inputs = builder.newFragment("inputs");
        for (int i = 0; i < inputNodes.size(); i++) {
            InputNode inputNode = inputNodes.get(i);
            XBayaTextField parameterTextField = this.parameterTextFields.get(i);
            String id = inputNode.getID();
            String value = parameterTextField.getText();
            org.xmlpull.v1.builder.XmlElement input = inputs.addElement(id);
            input.addChild(value);
        }
        
       
        final String xregistryUrl = this.xRegistryTextField.getText();
        if( null != xregistryUrl && !"".equals(xregistryUrl)){
          try {
        this.engine.getConfiguration().setXRegistryURL(new URI(xregistryUrl));
      } catch (URISyntaxException e) {
        this.engine.getErrorWindow().error(e);
      }
        }
       
        final String gFacUrl = this.gfacTextField.getText();
        if( null != gFacUrl && !"".equals(gFacUrl)){
          try {
        this.engine.getConfiguration().setGFacURL(new URI(gFacUrl));
      } catch (URISyntaxException e) {
        this.engine.getErrorWindow().error(e);
      }
        }
       
       
       
       
       
        final String topicString = topic;
        new Thread() {
        /**
         * @see java.lang.Thread#run()
         */
        @Override
        public void run() {
          try {
            MonitorConfiguration notifConfig = DynamicWorkflowRunnerWindow.this.engine.getMonitor()
                    .getConfiguration();
            DynamicWorkflowRunnerWindow.this.engine.getMonitor().start();
            notifConfig.setTopic(topicString);
            DynamicWorkflowRunnerWindow.this.engine.getGUI().addDynamicExecutionToolsToToolbar();
            new WorkflowInterpreter(DynamicWorkflowRunnerWindow.this.engine).scheduleDynamically(topicString);           
          } catch (XBayaException e) {
            DynamicWorkflowRunnerWindow.this.engine.getErrorWindow().error(e);
          }
         
        }
      }.start();
       
        hide();
    }
}


/*
* Indiana University Extreme! Lab Software License, Version 1.2
*
* Copyright (c) 2008 The Trustees of Indiana University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) All redistributions of source code must retain the above copyright notice,
* the list of authors in the original source code, this list of conditions and
* the disclaimer listed in this license;
*
* 2) All redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the disclaimer listed in this license in
* the documentation and/or other materials provided with the distribution;
*
* 3) Any documentation included with all redistributions must include the
* following acknowledgement:
*
* "This product includes software developed by the Indiana University Extreme!
* Lab. For further information please visit http://www.extreme.indiana.edu/"
*
* Alternatively, this acknowledgment may appear in the software itself, and
* wherever such third-party acknowledgments normally appear.
*
* 4) The name "Indiana University" or "Indiana University Extreme! Lab" shall
* not be used to endorse or promote products derived from this software without
* prior written permission from Indiana University. For written permission,
* please contact http://www.extreme.indiana.edu/.
*
* 5) Products derived from this software may not use "Indiana University" name
* nor may "Indiana University" appear in their name, without prior written
* permission of the Indiana University.
*
* Indiana University provides no reassurances that the source code provided
* does not infringe the patent or any other intellectual property rights of any
* other entity. Indiana University disclaims any liability to any recipient for
* claims brought by any other entity based on infringement of intellectual
* property rights or otherwise.
*
* LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO
* WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
* NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF
* INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS.
* INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS",
* "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.
* LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR
* ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION
* GENERATED USING SOFTWARE.
*/ 
TOP

Related Classes of edu.indiana.extreme.xbaya.graph.dynamic.gui.DynamicWorkflowRunnerWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.