View Javadoc

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  	private static final Logger log = LoggerFactory.getLogger(TreePanel.class);
70      
71      private EncodingCharacters encChars;
72      private Message message;
73      
74      /** Creates new TreePanel */
75      public TreePanel() {
76          this.encChars = new EncodingCharacters('|', null);
77      }
78      
79      /**
80       * Updates the panel with a new Message.
81       */
82      public void setMessage(Message message) {
83          this.message = message;
84      	if (message == null) {
85      		this.removeAll();
86      		this.revalidate();
87      		return;
88      	}
89  
90          DefaultMutableTreeNode top = new DefaultMutableTreeNode(message.getClass().getName());
91          addChildren(message, top);
92          
93          JTree tree = new JTree(top);
94          //JScrollPane treeView = new JScrollPane(tree);
95          this.removeAll();
96          this.add(tree);
97          this.revalidate();
98      }
99      
100     /**
101      * Returns the message that is currently displayed in the tree panel.
102      */
103     public Message getMessage() {
104         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         String[] childNames = messParent.getNames();
112         int currChild = 0;
113         for (int i = 0; i < childNames.length; i++) {
114             try {
115                 Structure[] childReps = messParent.getAll(childNames[i]);
116                 for (int j = 0; j < childReps.length; j++) {
117                     DefaultMutableTreeNode newNode = null;
118                     if (childReps[j] instanceof Group) {
119                         String groupName = childReps[j].getClass().getName();
120                         groupName = groupName.substring(groupName.lastIndexOf('.') + 1, groupName.length());
121                         newNode = new DefaultMutableTreeNode(groupName + " (rep " + j + ")");
122                         addChildren((Group)childReps[j], newNode);
123                     } else if (childReps[j] instanceof Segment) {
124                         newNode = new DefaultMutableTreeNode(PipeParser.encode((Segment)childReps[j], encChars));
125                         addChildren((Segment)childReps[j], newNode);
126                     }
127                     treeParent.insert(newNode, currChild++);
128                 }
129             } catch (HL7Exception e) {
130                 e.printStackTrace();
131             }
132         }
133     }
134     
135     /**
136      * Add fields of a segment to the tree ...
137      */
138     private void addChildren(Segment messParent, MutableTreeNode treeParent) {
139         int n = messParent.numFields();
140         int currChild = 0;
141         for (int i = 1; i <= n; i++) {
142             try {
143                 Type[] reps = messParent.getField(i);
144                 for (int j = 0; j < reps.length; j++) {
145                     String field = PipeParser.encode(reps[j], encChars);
146                     DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Field " + i + " rep " + j + " (" + getLabel(reps[j]) + "): " + field);
147                     addChildren(reps[j], newNode);
148                     treeParent.insert(newNode, currChild++);
149                 }
150             } catch (HL7Exception e) {
151                 e.printStackTrace();
152             }
153         }
154     }
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         if (Varies.class.isAssignableFrom(messParent.getClass())) {
163             //DefaultMutableTreeNode variesNode = new DefaultMutableTreeNode("Varies");
164             //treeParent.insert(variesNode, treeParent.getChildCount());
165             Type data = ((Varies) messParent).getData();
166             DefaultMutableTreeNode dataNode = new DefaultMutableTreeNode(getLabel(data));
167             treeParent.insert(dataNode, 0);
168             addChildren(data, dataNode);
169         } else {
170             if (Composite.class.isAssignableFrom(messParent.getClass())) {
171                 addChildren((Composite)messParent, treeParent);
172             } else if (Primitive.class.isAssignableFrom(messParent.getClass())) {
173                 addChildren((Primitive)messParent, treeParent);
174             }
175             
176             if (messParent.getExtraComponents().numComponents() > 0) {
177                 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("ExtraComponents");
178                 treeParent.insert(newNode, treeParent.getChildCount());
179                 for (int i = 0; i < messParent.getExtraComponents().numComponents(); i++) {
180                     DefaultMutableTreeNode variesNode = new DefaultMutableTreeNode("Varies");
181                     newNode.insert(variesNode, i);                    
182                     addChildren(messParent.getExtraComponents().getComponent(i), variesNode);
183                 }
184             }
185         }
186     }
187     
188     /**
189      * Adds components of a composite to the tree ...
190      */
191     private void addChildren(Composite messParent, MutableTreeNode treeParent) {
192         Type[] components = messParent.getComponents();
193         for (int i = 0; i < components.length; i++) {
194             DefaultMutableTreeNode newNode;
195             newNode = new DefaultMutableTreeNode(getLabel(components[i]));
196             addChildren(components[i], newNode);
197             treeParent.insert(newNode, i);
198         }
199     }
200     
201     /**
202      * Adds single text value to tree as a leaf
203      */
204     private void addChildren(Primitive messParent, MutableTreeNode treeParent) {
205         DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(messParent.getValue());
206         treeParent.insert(newNode, 0);
207     }
208     
209     /**
210      * Returns the unqualified class name as a label for tree nodes. 
211      */
212     private static String getLabel(Object o) {
213         String name = o.getClass().getName();
214         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         JFrame frame = new JFrame(message.getClass().getName());
225         
226         try {
227             TreePanel panel = new TreePanel();
228             panel.setMessage(message);
229             JScrollPane scroll = new JScrollPane(panel);
230             frame.getContentPane().add(scroll, BorderLayout.CENTER);
231             
232             //Finish setting up the frame
233             frame.addWindowListener(new WindowAdapter() {
234                 public void windowClosing(WindowEvent e) {
235                     System.exit(0);
236                 }
237             });
238             
239             frame.pack();
240             frame.setVisible(true);
241         } catch (Exception e) {
242             System.err.println("Can't display message in new window: ");
243             e.printStackTrace();
244         }
245     }
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         if (args.length != 1) {
252             System.out.println("Usage: TreePanel msg_file_name");
253             System.exit(1);
254         }
255         
256         try {            
257             PipeParser parser = new PipeParser();
258             File messageFile = new File(args[0]);
259             long fileLength = messageFile.length();
260             FileReader r = new FileReader(messageFile);
261             char[] cbuf = new char[(int)fileLength];
262             System.out.println("Reading message file ... " + r.read(cbuf) + " of " + fileLength + " chars");
263             r.close();
264             String messString = String.valueOf(cbuf);
265             Message mess = parser.parse(messString);
266             System.out.println("Got message of type " + mess.getClass().getName());
267             showInNewWindow(mess);
268             
269             //write message to console ...
270             System.out.println(parser.encode(mess, "VB"));
271         } catch (Exception e) {
272             e.printStackTrace();
273             log.error( e.getMessage(), e );
274         }
275     }
276     
277 }