Chris Pollett > Students >
Madhuri

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3and4]

    [CS297Report-PDF]

    [CS298Proposal]

    [Final Code]

    [CS298 Presentation-PDF]

    [CS298 Report-PDF]

                          

























A GUI Program that has some sample XForms and that allows you to create new XForms.

Description: This program MenuForm.java can display any XForm that contains textfields, comboboxes and submit buttons. It can also create new XForms and display them.

MenuForm.java:

/* Author MADHURI POTU
This program can display any type of XForm and lets you create new
XForms*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.*;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

import java.io.*;
import java.util.*;
import com.sun.xml.tree.*;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.XmlDocument;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.*;
import org.xml.sax.*;

public class MenuForm extends JPanel implements ActionListener
{
	XmlDocument	doc1;
	Element root;
	int len;
	JFrame frame2;
	JPanel contentPane1;
	JTextField text1;
	JTextField text5;
	String text;
	int nofitems;
	Element child2;
	Element child4;
	protected JButton b1, b2, b3, b4, submitbutton;

/* This is the method where all the buttons are created.
b1 to open form1, b2 to open form2, b3 to open form3 and
New Form to create a new form*/

public MenuForm()
{
	b1 = new JButton("Form1");
    b2 = new JButton("Form2");
    b3 = new JButton("Form3");
    b4 = new JButton("New Form");
	b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
	b4.addActionListener(this);

    add(b1);
    add(b2);
    add(b3);
	add(b4);
}

/* This is the method where all the actions are performed. The actions
Form1, Form2 and Form 3 open up existing XForms.
The action New Form lets you create new Form. The action Submit Form
lets you display the new form created. */


public void actionPerformed(ActionEvent e)
{
	if (e.getActionCommand().equals("Form1"))
	{
    	newform("entryform.xml");
    }
    if (e.getActionCommand().equals("Form2"))
    {
    	newform("form2.xml");
    }
    if (e.getActionCommand().equals("Form3"))
    {
    	newform("form3.xml");
    }
    if (e.getActionCommand().equals("New Form"))
    {
		frame2 = new JFrame("New Form");
	    frame2.setDefaultLookAndFeelDecorated(true);
        frame2.setSize(350, 350);
        contentPane1 = new JPanel();
		contentPane1.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
        contentPane1.setLayout(new FlowLayout());
        frame2.setContentPane(contentPane1);

		doc1 = new XmlDocument ();
		root = (Element) doc1.createElement ("html");
		root.setAttribute("xmlns:xform", "http://www.w3.org/2002/01/xforms");
        doc1.appendChild(root);

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menu.setMnemonic(KeyEvent.VK_A);
        menuBar.add(menu);
        JMenuItem menuItem = new JMenuItem("New TextField",
                                 KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.addActionListener(this);
        menu.add(menuItem);
        menuItem = new JMenuItem("New ComboBox",
                                 KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.addActionListener(this);
        menu.add(menuItem);
        contentPane1.add(menuBar);
        JButton submitbut = new JButton("Submit Form");
        contentPane1.add(submitbut);
        submitbut.addActionListener(this);
        frame2.setVisible(true);
	    frame2.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
	}
    if(e.getActionCommand().equals("New TextField"))
    {
        JLabel label = new JLabel("Enter the name of the textfield: ");
        text1 = new JTextField(10);
        text1.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e) {
            text = text1.getText();
    		Element child = (Element)doc1.createElement("xform:input");
    		root.appendChild(child);
    		child.setAttribute("value", text);
    		Text child1 = (Text)doc1.createTextNode(text);
    		child.appendChild(child1);
        	try{
        		doc1.write(System.out);
        	}catch (IOException io){}
        }});
        contentPane1.add(label);
        contentPane1.add(text1);
        frame2.show();
	}
    if(e.getActionCommand().equals("New ComboBox"))
    {
    	JLabel label = new JLabel("Enter the name of the ComboBox: ");
    	contentPane1.add(label);
        text1 = new JTextField(10);
        contentPane1.add(text1);
        text1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) {
            text = text1.getText();
        	Text child1 = (Text)doc1.createTextNode(text);
        	Element child = (Element)doc1.createElement("xform:selectOne");
        	root.appendChild(child);
        	child.appendChild(child1);
        	child.setAttribute("value", text);

        	child2 = (Element) doc1.createElement("xform:choices");
        	child.appendChild(child2);
        	Text child5 = (Text)doc1.createTextNode("");
        	child2.appendChild(child5);
        }});

        JLabel label1 = new JLabel("Enter the number of items in the ComboBox: ");
        contentPane1.add(label1);
        text5 = new JTextField(10);
        contentPane1.add(text5);
        text5.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e) {
            String text3 = text5.getText();
            nofitems = Integer.parseInt(text3);
            System.out.println("text is" + nofitems);

            System.out.println("no is" + nofitems);
        	for(int i=0; i<nofitems; i++)
        	{
        		JLabel label2 = new JLabel("Enter the name of item" + i);
        		final JTextField textfield =  new JTextField(10);
	        	contentPane1.add(label2);
    	    	contentPane1.add(textfield);
        		frame2.show();
	        	textfield.addActionListener(new ActionListener()
	        	{
        			public void actionPerformed(ActionEvent e) {
        			String text = textfield.getText();
        			child4 = (Element) doc1.createElement("xform:item");
        			child2.appendChild(child4);
        			child4.setAttribute("value", text);
        			Text child3 = (Text)doc1.createTextNode(text);
        			child4.appendChild(child3);
                	try{
        				doc1.write(System.out);
        			}catch (IOException io){}
          		}});
        	}
        }});

        frame2.show();
	}
	if((e.getActionCommand().equals("Submit Form")))
	{
		JLabel filenae = new JLabel("Enter the name of File:");
		contentPane1.add(filenae);
		final JTextField field = new JTextField(10);
		contentPane1.add(field);
		frame2.show();
		field.addActionListener(new ActionListener()
	    {
        	public void actionPerformed(ActionEvent e) {
        	String text1 = field.getText();
        	try
        	{
				PrintWriter fileData = new PrintWriter(new FileWriter(text1));
	    		doc1.write (fileData);
	    	}  catch (Throwable t) {}
	    	newform(text1);
		}});

	}
}

/* This method lets you return the attribute value of the node */

private static String attrivalue (String name)
{
	int start = name.indexOf("\"") + 1;
	int end = name.lastIndexOf("\"");
	String value= name.substring(start, end);
	return value;
}

/* This method opens an existing XForm and displays the form */

private static void newform(String form)
{
	JFrame frame1 = new JFrame("Forms");
    frame1.setDefaultLookAndFeelDecorated(true);
    frame1.setSize(350, 350);
    JPanel contentPane = new JPanel();
	contentPane.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
    contentPane.setLayout(new GridLayout(0,2));
	frame1.setContentPane(contentPane);
    try {
		InputSource	input = Resolver.createInputSource (new File (form));
	    XmlDocument doc = XmlDocument.createXmlDocument (input, false);
	    doc.getDocumentElement ().normalize ();
	    java.io.StringWriter fileData = new java.io.StringWriter();
	    doc.write (fileData);

	    NodeList listOftextf = doc.getElementsByTagName("xform:input");
	    int totallist = listOftextf.getLength();
	    System.out.println("Total no of textfields : " + totallist);

		NodeList listOfcheck = doc.getElementsByTagName("xform:selectOne");
		int totallist1 = listOfcheck.getLength();
		System.out.println("Total no of checkboxes : " + totallist1);

		for(int i=0; i<totallist; i++)
		{
		  	String c = listOftextf.item(i).getAttributes().toString();
		  	String name= attrivalue(c);
		   	JLabel label = new JLabel(name);
		   	JTextField element1 = new JTextField(10);
		    contentPane.add(label);
		  	contentPane.add(element1);
		}
		for(int i=0; i<totallist1; i++)
		{
			Element node = (Element) listOfcheck.item(i).getChildNodes();
			String c1 = node.getAttributes().toString();
			String name1 = attrivalue(c1);
			JLabel label1 = new JLabel(name1);
			JComboBox cb = new JComboBox();
			contentPane.add(label1);
		   	contentPane.add(cb);
		   	NodeList node2 = (NodeList)node.getElementsByTagName("xform:item");
			int len = node2.getLength();
			for(int j=0; j<len; j++)
			{
				Element node3 = (Element) node2.item(j).getChildNodes();
				String c2 = node3.getAttributes().toString();
				String name2 = attrivalue(c2);
		   		cb.addItem(name2);
		   	}
		}

		JButton submitbutton = new JButton("Submit");
		contentPane.add(submitbutton);

	} catch (SAXParseException err) {}catch (SAXException er) {} catch (Throwable t) {}
	frame1.setVisible(true);
    frame1.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
}

/* This is the main method*/

public static void main(String[] args)
{
	JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("FormsMenu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MenuForm formsm = new MenuForm();
    frame.getContentPane().add(new MenuForm(), null);
    frame.setSize(450, 260);
    frame.setVisible(true);
}
}