Java example code for XUI dialogs

Keith Berard generously shares these extremely useful pieces of code Last Updated: 2006-07-29

A while back I mentioned that we were implementing a complete set of XUI controls in java. I got tied up for a while, so wasn't able to get any examples out. Since then, my code got so large and specific that I wanted to wait till I could get something more basic to post. This is just an example, but should show the basics of implementing XUI in Java. This example has 3 files:
 * loadXuiTools.js goes in custom\init
 * xuiexample/XuiMain.java
 * xuiexample/XuiListDetailInsert.java

All three get compiled into a jar and put in custom\classes The result of this code creates a XUI panel that is docked left, and contains 3 tabs. Each tab has 10 items in a list. The lists are defaulted to having item 2, 4 and 6 (respective per tab) selected. Clicking on an item in the list will show its full text in the box below. Clicking the insert button will put a tag into the document in the form: New Item Our actual implementation uses a tool like this one to manage files in our xml CMS repository. Because of this, the tool needs to load for each edit window, and at startup. The specifics to this have been removed in this example. PS: Pardon if the code is at all sloppy... I decided to cut things from here and there to get an example up quickly, which required a bit of hacking to remove items specific only to our implementation. Also, several helper methods such as createListItem used to be static methods in a XuiHelper class, which helped us alot in code reuse.

/** * This function loads the initial XUI. * ApplicationLoad event is used because first run will * not have a valid document (editinit) and scripts in * init are called before the window is fully available. */ function handleEvent(e) { if (e.getType.equals("ApplicationLoad")) { var xm = new Packages.xuiexample.XuiMain; xm.show; } } var o = { handleEvent: handleEvent }; var listener = Packages.org.w3c.dom.events.EventListener(o); Application.addEventListener("ApplicationLoad", listener, true); package xuiexample; import com.arbortext.epic.Application; import com.arbortext.epic.ui.Dialog; import com.arbortext.epic.ui.View; import com.arbortext.epic.ui.Window; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Element; import org.w3c.dom.views.DocumentView; /** * This example class shows how a XUI dialog can be created on the fly * in java. */ public class XuiMain { /**     *  Public Identifier as part of the doctype for a XUI document. */    public static final String XUI_PUB_ID = "-//Arbortext//DTD XUI XML 1.0//EN"; /**     *  System Identifier as part of the doctype for a XUI document. */    public static final String XUI_SYS_ID = "xui.dtd"; // The XUI document in Epic private Document xuiDoc = null; // The XUI dialog in Epic private Dialog xuiDialog = null; // The window holding this XUI control private Window myWindow = null; /**     * Base constructor. */    public XuiMain { this.init; }    // Method to setup the base XUI private void init { Document doc = Application.getActiveDocument; View view = (View)((DocumentView)doc).getDefaultView; this.myWindow = view.getWindow; DOMImplementation domImpl = Application.getDomImplementation; DocumentType docType = domImpl.createDocumentType("window",XUI_PUB_ID, XUI_SYS_ID); this.xuiDoc = domImpl.createDocument(XUI_SYS_ID, "window", docType); Element root = this.xuiDoc.getDocumentElement; root.setAttribute("dock","left"); root.setAttribute("enabledocking","any"); root.setAttribute("id","toolId"); root.setAttribute("width","100px"); root.setAttribute("title","Example Java Xui"); Element tabBox = this.xuiDoc.createElement("tabbox"); root.appendChild(tabBox); for (int i=1; i<=3; i++) { Element tabPanel = this.xuiDoc.createElement("tabpanel"); tabPanel.setAttribute("label","Tab " + Integer.toString(i)); tabBox.appendChild(tabPanel); XuiListDetailInsert ldi = new XuiListDetailInsert; tabPanel.appendChild(ldi.render(this.xuiDoc)); for (int j=0; j<10; j++) { String label = "(" + Integer.toString(j) + ") Item for tab " + Integer.toString(i); label += "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum condimentum lectus ut eros. Aenean faucibus, nisi at aliquet feugiat, nulla lorem pretium ipsum, sed pretium nibh sapien vitae purus. Fusce est metus, vestibulum ut, ultricies a, ultricies in, arcu. Vivamus eget turpis lobortis dolor dictum consectetuer. Aliquam risus. Vivamus arcu. Praesent malesuada. Maecenas ut velit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean cursus. Curabitur eget est. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent dui dui, commodo gravida, adipiscing et, scelerisque et, purus. Donec nonummy hendrerit ipsum. Curabitur cursus nonummy libero."; ldi.addListItem(Integer.toString(j),label); }            ldi.selectListItem(Integer.toString(i * 2)); }    }     public Dialog getDialog { if (this.xuiDialog == null) { this.xuiDialog = Application.createDialogFromDocument(this.xuiDoc,null,this.myWindow); }        return this.xuiDialog; }    public void show { Dialog d = this.getDialog; d.show; } } package xuiexample; import com.arbortext.epic.Acl; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.events.Event; import org.w3c.dom.events.EventListener; import org.w3c.dom.events.EventTarget; /** * Example of a reusable XUI tool that interracts with the document. */ public class XuiListDetailInsert implements EventListener { private String name = null; private Element containerBox; private Element listBox; private Element detailBox; private Element insertButton; private Document xuiDoc; private boolean isPdf = false; /**     *      */     public XuiListDetailInsert {    } /**     *      */     public Element render(Document parentXuiDoc) { this.xuiDoc = parentXuiDoc; this.containerBox = this.xuiDoc.createElement("box"); this.containerBox.setAttribute("orient","vertical"); this.containerBox.setAttribute("pack","start"); this.containerBox.setAttribute("resize","vertical"); this.listBox = this.xuiDoc.createElement("listbox"); this.listBox.setAttribute("fontsize","8"); this.listBox.setAttribute("height","150px"); ((EventTarget)this.listBox).addEventListener("DOMActivate",this,false); this.containerBox.appendChild(this.listBox) this.detailBox = this.xuiDoc.createElement("textbox"); this.detailBox.setAttribute("height","100px"); this.detailBox.setAttribute("multiline","true"); this.detailBox.setAttribute("readonly","true"); this.containerBox.appendChild(detailBox); Element buttonBox = this.xuiDoc.createElement("box"); buttonBox.setAttribute("orient","horizontal"); this.containerBox.appendChild(buttonBox); this.insertButton = this.xuiDoc.createElement("button"); this.insertButton.setAttribute("label","Insert ListItem at Cursor"); ((EventTarget)this.insertButton).addEventListener("DOMActivate",this,false); buttonBox.appendChild(this.insertButton); return this.containerBox; }    /**      *      */     public void handleEvent(Event e) { if (e.getTarget instanceof Element) {                    Element targetElement = (Element)e.getTarget; if (targetElement.equals(this.listBox)) { String selectedText = this.listBox.getAttribute("value"); // Ugly way to find current id by looping trough child list nodes // and doing a text compare on the value. // This is necessary because the listitem selected attribute // does not update to true when selected. // TODO: Use arbortext specific getElementsByAttribute call? NodeList childNodes = this.listBox.getChildNodes; for (int j=0; j<childNodes.getLength; j++) {                                    Element listItem = (Element)childNodes.item(j); if (listItem.getAttribute("label").equals(selectedText)) { targetElement.setAttribute("curId",listItem.getAttribute("appdata")); }                                } this.setDisplayText(selectedText); }            else if (targetElement.equals(this.insertButton)) { String curId = this.listBox.getAttribute("curId"); if (!curId.equals("")) {                            System.err.println("Inserting item with id: " + curId); this.insertListitemAtCursor(curId); }                                                                  }         }     }     private void insertListitemAtCursor(String itemId) {                      try {                                                                      Acl.execute("insert('New Item ')"); }                catch (Exception ex) {                    System.err.println("Unable to insert listitem:" + ex.getMessage); }                      }      private void setDisplayText(String text) {                NodeList childNodes = this.detailBox.getChildNodes; if (childNodes != null) { for (int i=childNodes.getLength - 1; i>=0; i--) { detailBox.removeChild(childNodes.item(i)); }                }                 Element valNode = this.xuiDoc.createElement("value"); this.detailBox.appendChild(valNode); valNode.appendChild(this.xuiDoc.createTextNode(text)); }        /**          *          */         public boolean selectListItem(String id) { NodeList children = this.listBox.getChildNodes; String curText = ""; boolean found = false; for (int i=0; i<children.getLength; i++) { Element curChild = (Element)children.item(i); if (curChild.getAttribute("appdata").equals(id)) { curText = curChild.getAttribute("label"); this.listBox.setAttribute("value",curText); found = true; break; }            }         this.setDisplayText(curText); return found; }    /**      *      */     public Element getListBox { return this.listBox; }    public void reset { this.setDisplayText(""); this.clearListBox(this.listBox); }    /**      *      */     public void clearListBox(Element listElement) {                NodeList childNodes = listElement.getChildNodes; for (int i=childNodes.getLength - 1; i>=0; i--) {                        Node childNode = childNodes.item(i); listElement.removeChild(childNode); }        }     public void addListItem(String appdata, String label) {                Element listItem = this.xuiDoc.createElement("listitem"); listItem.setAttribute("appdata",appdata); listItem.setAttribute("label",label); this.listBox.appendChild(listItem); } }
 * 1) loadXuiTools.js ##########
 * 1) XuiMain.java (package xuiexample) ######
 * 1) XuiListDetailInsert.java ##########

Download all three here: