Coverage Report - ca.uhn.hl7v2.view.TreePanel
 
Classes in this File Line Coverage Branch Coverage Complexity
TreePanel
0%
0/111
0%
0/28
2.583
TreePanel$1
0%
0/3
N/A
2.583
 
 1  
 /**
 2  
  * The contents of this file are subject to the Mozilla Public License Version 1.1
 3  
  * (the "License"); you may not use this file except in compliance with the License.
 4  
  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 5  
  * Software distributed under the License is distributed on an "AS IS" basis,
 6  
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
 7  
  * specific language governing rights and limitations under the License.
 8  
  *
 9  
  * The Original Code is "TreePanel.java".  Description:
 10  
  * "This is a Swing panel that displays the contents of a Message object in a JTree"
 11  
  *
 12  
  * The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
  * 2001.  All Rights Reserved.
 14  
  *
 15  
  * Contributor(s): ______________________________________.
 16  
  *
 17  
  * Alternatively, the contents of this file may be used under the terms of the
 18  
  * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
 19  
  * applicable instead of those above.  If you wish to allow use of your version of this
 20  
  * file only under the terms of the GPL and not to allow others to use your version
 21  
  * of this file under the MPL, indicate your decision by deleting  the provisions above
 22  
  * and replace  them with the notice and other provisions required by the GPL License.
 23  
  * If you do not delete the provisions above, a recipient may use your version of
 24  
  * this file under either the MPL or the GPL.
 25  
  *
 26  
  */
 27  
 
 28  
 /*
 29  
  * Created on October 17, 2001, 11:44 AM
 30  
  */
 31  
 package ca.uhn.hl7v2.view;
 32  
 
 33  
 import java.awt.BorderLayout;
 34  
 import java.awt.event.WindowAdapter;
 35  
 import java.awt.event.WindowEvent;
 36  
 import java.io.File;
 37  
 import java.io.FileReader;
 38  
 
 39  
 import javax.swing.JFrame;
 40  
 import javax.swing.JScrollPane;
 41  
 import javax.swing.JTree;
 42  
 import javax.swing.tree.DefaultMutableTreeNode;
 43  
 import javax.swing.tree.MutableTreeNode;
 44  
 
 45  
 import org.slf4j.Logger;
 46  
 import org.slf4j.LoggerFactory;
 47  
 
 48  
 import ca.uhn.hl7v2.HL7Exception;
 49  
 import ca.uhn.hl7v2.model.Composite;
 50  
 import ca.uhn.hl7v2.model.Group;
 51  
 import ca.uhn.hl7v2.model.Message;
 52  
 import ca.uhn.hl7v2.model.Primitive;
 53  
 import ca.uhn.hl7v2.model.Segment;
 54  
 import ca.uhn.hl7v2.model.Structure;
 55  
 import ca.uhn.hl7v2.model.Type;
 56  
 import ca.uhn.hl7v2.model.Varies;
 57  
 import ca.uhn.hl7v2.parser.EncodingCharacters;
 58  
 import ca.uhn.hl7v2.parser.PipeParser;
 59  
 
 60  
 /**
 61  
  * This is a Swing panel that displays the contents of a Message object in a JTree.
 62  
  * The tree currently only expands to the field level (components shown as one node).
 63  
  * @author Bryan Tripp (bryan_tripp@sourceforge.net)
 64  
  * @deprecated
 65  
  */
 66  
 @SuppressWarnings("serial")
 67  
 public class TreePanel extends javax.swing.JPanel {
 68  
         
 69  0
         private static final Logger log = LoggerFactory.getLogger(TreePanel.class);
 70  
     
 71  
     private EncodingCharacters encChars;
 72  
     private Message message;
 73  
     
 74  
     /** Creates new TreePanel */
 75  0
     public TreePanel() {
 76  0
         this.encChars = new EncodingCharacters('|', null);
 77  0
     }
 78  
     
 79  
     /**
 80  
      * Updates the panel with a new Message.
 81  
      */
 82  
     public void setMessage(Message message) {
 83  0
         this.message = message;
 84  0
             if (message == null) {
 85  0
                     this.removeAll();
 86  0
                     this.revalidate();
 87  0
                     return;
 88  
             }
 89  
 
 90  0
         DefaultMutableTreeNode top = new DefaultMutableTreeNode(message.getClass().getName());
 91  0
         addChildren(message, top);
 92  
         
 93  0
         JTree tree = new JTree(top);
 94  
         //JScrollPane treeView = new JScrollPane(tree);
 95  0
         this.removeAll();
 96  0
         this.add(tree);
 97  0
         this.revalidate();
 98  0
     }
 99  
     
 100  
     /**
 101  
      * Returns the message that is currently displayed in the tree panel.
 102  
      */
 103  
     public Message getMessage() {
 104  0
         return this.message;
 105  
     }
 106  
     
 107  
     /**
 108  
      * Adds the children of the given group under the given tree node.
 109  
      */
 110  
     private void addChildren(Group messParent, MutableTreeNode treeParent) {
 111  0
         String[] childNames = messParent.getNames();
 112  0
         int currChild = 0;
 113  0
         for (int i = 0; i < childNames.length; i++) {
 114  
             try {
 115  0
                 Structure[] childReps = messParent.getAll(childNames[i]);
 116  0
                 for (int j = 0; j < childReps.length; j++) {
 117  0
                     DefaultMutableTreeNode newNode = null;
 118  0
                     if (childReps[j] instanceof Group) {
 119  0
                         String groupName = childReps[j].getClass().getName();
 120  0
                         groupName = groupName.substring(groupName.lastIndexOf('.') + 1, groupName.length());
 121  0
                         newNode = new DefaultMutableTreeNode(groupName + " (rep " + j + ")");
 122  0
                         addChildren((Group)childReps[j], newNode);
 123  0
                     } else if (childReps[j] instanceof Segment) {
 124  0
                         newNode = new DefaultMutableTreeNode(PipeParser.encode((Segment)childReps[j], encChars));
 125  0
                         addChildren((Segment)childReps[j], newNode);
 126  
                     }
 127  0
                     treeParent.insert(newNode, currChild++);
 128  
                 }
 129  0
             } catch (HL7Exception e) {
 130  0
                 e.printStackTrace();
 131  0
             }
 132  
         }
 133  0
     }
 134  
     
 135  
     /**
 136  
      * Add fields of a segment to the tree ...
 137  
      */
 138  
     private void addChildren(Segment messParent, MutableTreeNode treeParent) {
 139  0
         int n = messParent.numFields();
 140  0
         int currChild = 0;
 141  0
         for (int i = 1; i <= n; i++) {
 142  
             try {
 143  0
                 Type[] reps = messParent.getField(i);
 144  0
                 for (int j = 0; j < reps.length; j++) {
 145  0
                     String field = PipeParser.encode(reps[j], encChars);
 146  0
                     DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Field " + i + " rep " + j + " (" + getLabel(reps[j]) + "): " + field);
 147  0
                     addChildren(reps[j], newNode);
 148  0
                     treeParent.insert(newNode, currChild++);
 149  
                 }
 150  0
             } catch (HL7Exception e) {
 151  0
                 e.printStackTrace();
 152  0
             }
 153  
         }
 154  0
     }
 155  
     
 156  
     /**
 157  
      * Adds children to the tree.  If the Type is a Varies, the Varies data are 
 158  
      * added under a new node called "Varies".  If there are extra components, 
 159  
      * these are added under a new node called "ExtraComponents"
 160  
      */
 161  
     private void addChildren(Type messParent, MutableTreeNode treeParent) {
 162  0
         if (Varies.class.isAssignableFrom(messParent.getClass())) {
 163  
             //DefaultMutableTreeNode variesNode = new DefaultMutableTreeNode("Varies");
 164  
             //treeParent.insert(variesNode, treeParent.getChildCount());
 165  0
             Type data = ((Varies) messParent).getData();
 166  0
             DefaultMutableTreeNode dataNode = new DefaultMutableTreeNode(getLabel(data));
 167  0
             treeParent.insert(dataNode, 0);
 168  0
             addChildren(data, dataNode);
 169  0
         } else {
 170  0
             if (Composite.class.isAssignableFrom(messParent.getClass())) {
 171  0
                 addChildren((Composite)messParent, treeParent);
 172  0
             } else if (Primitive.class.isAssignableFrom(messParent.getClass())) {
 173  0
                 addChildren((Primitive)messParent, treeParent);
 174  
             }
 175  
             
 176  0
             if (messParent.getExtraComponents().numComponents() > 0) {
 177  0
                 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("ExtraComponents");
 178  0
                 treeParent.insert(newNode, treeParent.getChildCount());
 179  0
                 for (int i = 0; i < messParent.getExtraComponents().numComponents(); i++) {
 180  0
                     DefaultMutableTreeNode variesNode = new DefaultMutableTreeNode("Varies");
 181  0
                     newNode.insert(variesNode, i);                    
 182  0
                     addChildren(messParent.getExtraComponents().getComponent(i), variesNode);
 183  
                 }
 184  
             }
 185  
         }
 186  0
     }
 187  
     
 188  
     /**
 189  
      * Adds components of a composite to the tree ...
 190  
      */
 191  
     private void addChildren(Composite messParent, MutableTreeNode treeParent) {
 192  0
         Type[] components = messParent.getComponents();
 193  0
         for (int i = 0; i < components.length; i++) {
 194  
             DefaultMutableTreeNode newNode;
 195  0
             newNode = new DefaultMutableTreeNode(getLabel(components[i]));
 196  0
             addChildren(components[i], newNode);
 197  0
             treeParent.insert(newNode, i);
 198  
         }
 199  0
     }
 200  
     
 201  
     /**
 202  
      * Adds single text value to tree as a leaf
 203  
      */
 204  
     private void addChildren(Primitive messParent, MutableTreeNode treeParent) {
 205  0
         DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(messParent.getValue());
 206  0
         treeParent.insert(newNode, 0);
 207  0
     }
 208  
     
 209  
     /**
 210  
      * Returns the unqualified class name as a label for tree nodes. 
 211  
      */
 212  
     private static String getLabel(Object o) {
 213  0
         String name = o.getClass().getName();
 214  0
         return name.substring(name.lastIndexOf('.')+1, name.length());
 215  
     }    
 216  
     
 217  
     /**
 218  
      * A convenience method for displaying a message by creating a new
 219  
      * TreePanel and displaying the given message in a new window.
 220  
      * Currently only works with v2.4 messages.
 221  
      */
 222  
     public static void showInNewWindow(Message message) {
 223  
         //Create the top-level container and add contents to it.
 224  0
         JFrame frame = new JFrame(message.getClass().getName());
 225  
         
 226  
         try {
 227  0
             TreePanel panel = new TreePanel();
 228  0
             panel.setMessage(message);
 229  0
             JScrollPane scroll = new JScrollPane(panel);
 230  0
             frame.getContentPane().add(scroll, BorderLayout.CENTER);
 231  
             
 232  
             //Finish setting up the frame
 233  0
             frame.addWindowListener(new WindowAdapter() {
 234  
                 public void windowClosing(WindowEvent e) {
 235  0
                     System.exit(0);
 236  0
                 }
 237  
             });
 238  
             
 239  0
             frame.pack();
 240  0
             frame.setVisible(true);
 241  0
         } catch (Exception e) {
 242  0
             System.err.println("Can't display message in new window: ");
 243  0
             e.printStackTrace();
 244  0
         }
 245  0
     }
 246  
     
 247  
     /**
 248  
      * Opens window and displays a message in a file (file named in command line arg).
 249  
      */
 250  
     public static void main(String args[]) {
 251  0
         if (args.length != 1) {
 252  0
             System.out.println("Usage: TreePanel msg_file_name");
 253  0
             System.exit(1);
 254  
         }
 255  
         
 256  
         try {            
 257  0
             PipeParser parser = new PipeParser();
 258  0
             File messageFile = new File(args[0]);
 259  0
             long fileLength = messageFile.length();
 260  0
             FileReader r = new FileReader(messageFile);
 261  0
             char[] cbuf = new char[(int)fileLength];
 262  0
             System.out.println("Reading message file ... " + r.read(cbuf) + " of " + fileLength + " chars");
 263  0
             r.close();
 264  0
             String messString = String.valueOf(cbuf);
 265  0
             Message mess = parser.parse(messString);
 266  0
             System.out.println("Got message of type " + mess.getClass().getName());
 267  0
             showInNewWindow(mess);
 268  
             
 269  
             //write message to console ...
 270  0
             System.out.println(parser.encode(mess, "VB"));
 271  0
         } catch (Exception e) {
 272  0
             e.printStackTrace();
 273  0
             log.error( e.getMessage(), e );
 274  0
         }
 275  0
     }
 276  
     
 277  
 }