Rewrite the Javascript function makeCollapsible of fig515 t
. Rewrite the Javascript function makeCollapsible() of fig.5.15 (the function adds a button) as an equivalent Java method. Additionally, your method has to add new button with a hyperlink to a Web site.
You have to write a Java method makeCollapsible() for a class that contains an instance variable document of type org.w3c.dom.Document. The variable has been already in advance initialized with a DOM document. Your method should perform the same operations on document as are performed by the JavaScript function on window.document.
Here is the original JavaScript method:
// BlockCollapse.js
 // Add a button before the specified element (assumed
 // to be block style) that will make the element
 // disappear when clicked once and re-appear when
 // clicked a second time.
 // The button is placed within a div to ensure that
 // the markup we generate is valid XHTML.
 function makeCollapsible(elementId) {
 var element = window.document.getElementById(elementId);
 if (element) {
 var div = window.document.createElement(\"div\");
 element.parentNode.insertBefore(div, element);
 var button = window.document.createElement(\"button\");
 div.appendChild(button);
 button.setAttribute(\"type\", \"button\");
 var buttonText = window.document.createTextNode(\"Click to collapse\");
 button.appendChild(buttonText);
 button.setAttribute(\"onclick\",
 \"toggleVisibility(this,\'\" + elementId + \"\');\");
 }
 return;
 }
 // Function called when the button is clicked.
 function toggleVisibility(button, elementId) {
 var element = window.document.getElementById(elementId);
 if (element) {
 if (element.style.display == \"none\") {
 element.style.display = \"block\";
 button.childNodes[0].data = \"Click to collapse\";
 } else {
 element.style.display = \"none\";
 button.childNodes[0].data = \"Click to expand\";
 }
 }
 return;
 }
Solution
Here is the java functionality for above given JavaScript functions.
import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
public class makeCollapsible {
 private JFrame guiFrame;
 private JButton my_button;
 private AddButtonListener listener;
public makeCollapsible(){
 guiFrame = new JFrame(\"Make collapsible button\");
 button = new JButton(\"Click to collapse\");
 guiFrame.setLayout(new FlowLayout());//setting layout
 listener = new AddButtonListener();//setting up event listener
 guiFrame.add(button);
 button.addActionListener(listener);
guiFrame.setSize(200, 200);
 guiFrame.setLocation(400, 400);
 guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
public void executeFrame() {
 guiFrame.setVisible(true);
 }
 class AddButtonListener implements ActionListener {
 //private JFrame frame;
 private JButton button;
 private AddButtonListener listener;
 int current_button_number = 1;
public void actionPerformed(ActionEvent e) {
 if(((JButton)e.getSource()).getText().equals(\"Click to collapse\")) {
                (JButton)e.getSource()).setText(\"Click to expand\");
            }
            else{
                (JButton)e.getSource()).setText(\"Click to collapse\");
            }
 }
}
public static void main(String[] args) {
 makeCollapsible button = new makeCollapsible();
 button.executeFrame();
}
 }


