Abstract Action
I've been looking at some code but I can't figure something out. The idea is to generate a JMenu and JToolbar from an XML menu.
The first class builds either the Menu or the toolbar with an XML file that is provided to the methods.
package XML;
import java.awt.Insets;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import stresslabo.Main;
import swingGUI.ImageDelegate;
publicclass XMLBouwer
{
publicstaticvoid buildToolbarFromXML(JToolBar bar, String file){
Class clazz = stresslabo.HoofdForm.class;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(clazz.getResourceAsStream(file));
treeWalker(doc.getFirstChild(), bar);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
publicstaticvoid buildJMenuFromXML(JMenuBar bar, String file)
{
Class clazz = stresslabo.HoofdForm.class;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(clazz.getResourceAsStream(file));
treeWalk(doc.getFirstChild(), bar);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
privatestaticvoid treeWalk(Node node, JComponent component)
{
String name = node.getNodeName();
if(node.hasChildNodes())
{
if(name.equals("jmenu"))
{
Element e = (Element)node;
JMenu mnu =new JMenu(e.getAttribute("caption"));
NodeList list = node.getChildNodes();
component.add(mnu);
int len = list.getLength();
for(int i = 0; i < len; i++)
{
treeWalk(list.item(i), ((JComponent) (mnu)));
}
}
}else
if(name.equals("jmenuitem"))
{
Element e = (Element)node;
JMenuItem item =new JMenuItem();
try
{
Action action = (Action)Thread.currentThread().getContextClassLoader().loadClass(e.getAttribute("action-class")).newInstance();
item.setAction(action);
item.setText(e.getAttribute("caption"));
item.setIcon(ImageDelegate.setIcon(e.getAttribute("image")));
}
catch(InstantiationException e1)
{
e1.printStackTrace();
}
catch(IllegalAccessException e1)
{
e1.printStackTrace();
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
component.add(item);
}else
if(name.equals("separator"))
{
component.add(new JSeparator());
}
}
privatestaticvoid treeWalker(Node node, JComponent component)
{
String name = node.getNodeName();
if(node.hasChildNodes())
{
if(name.equals("jmenu"))
{
Element e = (Element)node;
NodeList list = node.getChildNodes();
int len = list.getLength();
for(int i = 0; i < len; i++)
{
treeWalker(list.item(i), ((JComponent) (component)));
}
}
}else
if(name.equals("jmenuitem"))
{
Element e = (Element)node;
JButton item =new JButton();
try
{
Action action = (Action)Thread.currentThread().getContextClassLoader().loadClass(e.getAttribute("action-class")).newInstance();
item.setAction(action);
item.setToolTipText(e.getAttribute("caption"));
Insets margins =new Insets(0, 2, 2, 0);
item.setMargin(margins);
item.setIcon(ImageDelegate.setIcon(e.getAttribute("image")));
}
catch(InstantiationException e1)
{
e1.printStackTrace();
}
catch(IllegalAccessException e1)
{
e1.printStackTrace();
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
component.add(item);
}else
if(name.equals("separator"))
{
component.add(new JSeparator());
}
}
}
Example XML file:
<?xml version="1.0"?>
<jmenu caption="Wijzigen">
<!--<jmenu caption="Exit">-->
<jmenuitem caption="Arts Wijzigen" action-class="actions.Arts.ArtsWijzigenAction" image="images/icon/artswijzigen.gif"/>
</jmenu>
You see that the author has given an action class to this XML file.
package actions;
import ejb.Arts;
import ejb.SuperFacadeBean;
import ejb.SuperFacadeRemote;
import forms.Toevoegen;
import forms.Wijzigen;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.ejb.EJB;
import javax.swing.*;
import javax.swing.AbstractAction;
import java.awt.*;
import login.Login;
import login.LoginGegevens;
import stresslabo.HoofdForm;
publicclass ArtsWijzigenActionextends AbstractAction{
//@EJB
privatestatic SuperFacadeRemote superFacadeBean;
publicvoid actionPerformed(ActionEvent e){
superFacadeBean = Login.getSuperFacade();
System.out.println("jos");
System.out.println(""+superFacadeBean);
new Wijzigen(LoginGegevens.getLoginGegeven(),"arts").setVisible(true);
}
}
Now what I don't understand is the use of Abstract Action. What does that do here and why has it been used? I can't seem to figure it out and can't find a good explanation of what it is. Can anyone help me and explain the use to me?

