JXMLAppKit

http://www.japisoft.com
v1.0

JXMLAppKit is an open-source framework using JXMLPad for editing XML content with multiple editor views. Each editor manages one or several parts of the main XML document.

JXMLAppKit is composed of three parts :
All XML editors are stored inside an XMLEditorsModel. This model is available using the getXMLEditorsModel from the XMLAppContainer. By default, the main container builds a default one but the user can override it calling the setXMLEditorsModel. This model is delegated to a view which is responsable to show a set of editors. This view is available calling the getXMLEditorsView. By default this view is available as a swing JTabbedPane but this is quite possible to replace it by another one like internal frames implementing the XMLEditorsView interface.

An XML editor has three states :
An XML editor  implements  the XMLEditor interface.  An abstract class AbstractXMLEditor is available for convenience. In addition, you will have to specify which XML element can be edited by your XMLEditor using the XMLEditingChoices interface. Another important part is the User interface that your editor must specify with the getView method. This method returns always the same instance, so you must prepare your user interface once and not each time the getView method is called. Your editor can also have a set of swing Action implementing the getXMLActions or
using the overrided actions instance from the AbstractXMLEditor. Each action will be available under a main toolbar.

Here a complete sample of an custom XML editor that edits a group1 tag including a set of persons with a JTable. We call the setView with our interface parameters (a title, a toolTip, an icon and an UI component). We add a new actions calling addAction from the actions model.

package demo;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.japisoft.fastparser.node.SimpleNode;
import com.japisoft.xmlappkit.editor.AbstractXMLEditor;
import com.japisoft.xmlappkit.editor.XMLEditingContext;

/**
 * Here a an editor sample for the group1 tag. This editor manages a list of persons
 * with a first name, a last name and a phone number
 * @author (c) 2004 JAPISoft / http://www.japisoft.com
 * @version 1.0 */
public class Group1Editor extends AbstractXMLEditor {

        private DefaultTableModel model = null;

        public  Group1Editor() {
                // Here the name of our editor
                super( "groupEditor" );

                // We edit only the following tags
               setEditingTags( new String[] { "group1" } );

                // We update the User Interface Part
                // Thus user can update the persons content

                JTable persons = new JTable();
               setView(
                        "Group1",
                        "Editing persons for Group1",
                        new ImageIcon( ClassLoader.getSystemResource( "demo/users2.png" ) ),
                        new JScrollPane( persons ) );
                       
                       
                model = new DefaultTableModel(
                        new String[] { "FirstName", "LastName", "Phone number" },
                        0 );
                persons.setModel( model );

                // Add an action available from the toolBar
               actions.addAction( new ClearAction() );
        }

        private SimpleNode lastGroupNode = null;

        // We start editing
        public void start( XMLEditingContext context ) {
               
                // This is a <group1> node
                SimpleNode groupNode = context.getSelectedNode();

                // We update the model with the selection node content
                while ( model.getRowCount() > 0 )
                        model.removeRow( 0 );
                for ( int i = 0; i < groupNode.childCount(); i++ ) {
                        SimpleNode child = groupNode.childAt( i );
                        if ( "person".equals( child.getNodeContent() ) ) {
                                model.addRow(
                                        new Object[] {
                                                child.getAttribute( "firstname" ),
                                                child.getAttribute( "lastname"),
                                                child.getAttribute( "phone" )
                                        } );
                        }
                }
                for ( int i = 40 - model.getRowCount(); i >= 0; i-- )
                        model.addRow( new Object[] { "", "", "" } );
                       
                lastGroupNode = groupNode;
        }

        // We terminate editing, so we need to update your current element
        public void stop( XMLEditingContext context ) {

                // Remove all children
                lastGroupNode.removeChildrenNodes();
       
                for ( int i = 0; i < model.getRowCount(); i++ ) {
                        String firstname = ( String )model.getValueAt( i, 0 );                 
                        String lastname = ( String )model.getValueAt( i, 1 );
                        String phone = ( String )model.getValueAt( i, 2 );

                        if ( !"".equals( firstname ) ) {
                                SimpleNode node = new SimpleNode( "person" );
                               
                                node.setAttribute( "firstname", firstname );
                                node.setAttribute( "lastname", lastname );
                                node.setAttribute( "phone", phone );

                                lastGroupNode.addChildNode( node );                    
                        }
                }

                // Notify the main application that the node has been changed
                // This method must be called once at the end of the editing
                context.commit();
        }

        // Sample of action for clearing the main table

        class ClearAction extends AbstractAction {
                public ClearAction() {
                        putValue( Action.NAME, "Clear" );
                }

                public void actionPerformed( ActionEvent e ) {
                        for ( int i = 0; i < model.getRowCount(); i++ ) {
                                model.setValueAt( "", i, 0 );
                                model.setValueAt( "", i, 1 );
                                model.setValueAt( "", i, 2 );
                        }
                }
        }
}

Here the last part  for adding this editor into our final application :

package demo;

import javax.swing.JFrame;
import com.japisoft.xmlappkit.XMLAppContainer;
import com.japisoft.xmlpad.action.ActionModel;

/**
 * @author (c) 2004 JAPISoft / http://www.japisoft.com
 * @version 1.0 */
public class Demo {

        public static void main( String[] args ) {

                // Remove the 'new'' action of the source editor
                ActionModel.removeActionByName( ActionModel.NEW_ACTION );

                JFrame fr = new JFrame();
                XMLAppContainer container = XMLAppContainer.getInstance();

                // Add the view of the XMLAppContainer
                fr.getContentPane().add( container.getView() );

                // Here an initial document
                container.getXMLContext().setText(
                        "<?xml version=\"1.0\"?>\n<!-- SELECT group1 for the custom editor -->\n" +
                                        "<persons>\n " +
                                        " <group1>\n" +
                                        " </group1>\n" +
                                        " <group2>\n" +
                                        " </group2>\n" +
                                        "</persons>\n" );

                // Add this new editor
                container.getXMLEditorsModel().addXMLEditor(
                        new Group1Editor()
                );

                fr.setSize( 700, 400 );
                fr.setVisible( true );         
        }

}



(c) 2004 JAPISoft