home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc102 / JTreeVue / Src / JTreeVuePanel.java < prev   
Encoding:
Java Source  |  2000-05-04  |  5.8 KB  |  211 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.Event;
  5. import java.io.InputStream;
  6. import java.io.StreamTokenizer;
  7. import java.io.BufferedInputStream;
  8. import java.net.URL;
  9. import java.net.MalformedURLException;
  10. import java.util.Vector;
  11. import java.util.Enumeration;
  12. import java.util.Hashtable;
  13.  
  14. import com.ms.ui.*;
  15. import com.ms.fx.*;
  16.  
  17. public class JTreeVuePanel extends UIPanel implements JTreeConsts, JTCallbacks
  18. {
  19.     public UITree objroot = null;        // "Object" class subtree of jtree
  20.     private Hashtable htable;
  21.     private UITree jtree = null;    // root to the whole shabang
  22.     private UIStatus status;
  23.     private UITree ifacelst = null;        // interface sublist(tree) of jtree 
  24.     private FxColor ifacecolor, classcolor;
  25.  
  26.     public JTreeVuePanel(InputStream stream)
  27.     {
  28.         UITree curtree = null;        // current position in subtree
  29.         int depth = 0;
  30.  
  31.         setLayout(new UIBorderLayout());
  32.         setBackground(java.awt.Color.lightGray);
  33.         setFont(new FxFont("Helvetica", FxFont.PLAIN, 12));
  34.  
  35.         htable = new Hashtable(10000, (float).1);
  36.  
  37.         ifacecolor = new FxColor(225, 225, 225);
  38.         classcolor = new FxColor(180, 180, 200);
  39.  
  40.         jtree = new UITree(new JTreeNode("Class Hierarchy"), 17);
  41.         add(new UIScrollViewer(jtree), "Center");
  42.         curtree = jtree;
  43.  
  44.         ifacelst = new UITree(new JTreeNode("Interfaces"), 17);
  45.         status = new UIStatus();
  46.         add(status, "South");
  47.  
  48.         try {
  49.             // 1.1 Method
  50.             // Reader rdr = new BufferedReader(new InputStreamReader(stream));
  51.             // StreamTokenizer tokstrm = new StreamTokenizer(rdr);
  52.  
  53.             // 1.0.2 Method
  54.             BufferedInputStream buffer = new BufferedInputStream(stream);
  55.             StreamTokenizer tokstrm = new StreamTokenizer(buffer);
  56.             
  57.             tokstrm.ordinaryChars('0', '9'); tokstrm.wordChars('0', '9');
  58.             tokstrm.ordinaryChar('/'); tokstrm.wordChars('/', '/');
  59.             tokstrm.ordinaryChar('.'); tokstrm.wordChars('.', '.');
  60.             tokstrm.ordinaryChar('-'); tokstrm.wordChars('-', '-');
  61.             tokstrm.ordinaryChar('+'); tokstrm.wordChars('+', '+');
  62.             tokstrm.eolIsSignificant(true);
  63.             boolean more = true;
  64.             boolean startLine = true;
  65.  
  66.             while (more) {
  67.                 switch(tokstrm.nextToken()) {
  68.                     case StreamTokenizer.TT_EOF: 
  69.                         more = false; 
  70.                         break;
  71.  
  72.                     case StreamTokenizer.TT_NUMBER:
  73.                     case StreamTokenizer.TT_WORD:
  74.                     case StreamTokenizer.TT_EOL: 
  75.                         break;
  76.  
  77.                     case '<':
  78.                         if ( (startLine == true) &&
  79.                              (tokstrm.nextToken() == StreamTokenizer.TT_WORD) ) {
  80.                             if ( tokstrm.sval.equalsIgnoreCase("ul") ) {
  81.                                 // first <ul>! Don't do anything, wait for first li
  82.                                 if ( depth != 0 ) {
  83.                                     int i = curtree.getComponentCount() - 1;
  84.                                     IUIComponent comp = curtree.getComponent(i);
  85.                                     curtree.remove(comp);
  86.  
  87.                                     UITree newtree = new UITree(comp, 17);
  88.                                     curtree.add(newtree);
  89.                                     if ( objroot == null ) {
  90.                                         objroot = newtree;
  91.                                         // Add Interfaces as a sibling branch
  92.                                         //  to Object, we need to add it here 
  93.                                         //  so it will be after Object on tree.
  94.                                         curtree.add(ifacelst);
  95.                                     }
  96.                                     curtree = newtree;
  97.                                 }
  98.                                 depth++;
  99.                             }
  100.                             else if ( tokstrm.sval.equalsIgnoreCase("li") ) {
  101.                                 // skip over '>'
  102.                                 tokstrm.nextToken();
  103.                                 // this will create new node
  104.                                 newNode(tokstrm, curtree); 
  105.                             }
  106.                             else if ( tokstrm.sval.equalsIgnoreCase("/ul") ) {
  107.                                 depth--;
  108.                                 if ( depth == 0)
  109.                                     more = false;
  110.                                 else
  111.                                     curtree = (UITree)curtree.getParent();
  112.                             }
  113.                         }
  114.                         break;
  115.  
  116.                     default: 
  117.                         break;
  118.                 } // end switch
  119.                 startLine = (tokstrm.ttype == StreamTokenizer.TT_EOL);
  120.             } // end while
  121.         } catch (Exception e) {}
  122.  
  123.         jtree.setExpanded(true);
  124.         objroot.setExpanded(true);
  125.         dispatchInfo((JTreeNode)objroot.getHeader());
  126.     }
  127.  
  128.     public void newNode(StreamTokenizer tokstrm, UITree curtree)
  129.     {
  130.         JTreeNode newnode = null;
  131.  
  132.         try {
  133.             // Create node...
  134.             newnode = new JTreeNode(tokstrm, this);
  135.             // ... and add it to hash table using the name as key
  136.             htable.put(newnode.info.name, newnode);
  137.  
  138.             if ( newnode.info.type == HTI_CLS ) {
  139.                 newnode.setBackground(BaseColor.getColor(classcolor));
  140.                 curtree.add(newnode);
  141.             }
  142.             else {
  143.                 newnode.setBackground(BaseColor.getColor(ifacecolor));
  144.                 ifacelst.add(newnode);
  145.             }
  146.  
  147.             if ( (newnode.info.type == HTI_CLS) || (newnode.info.type == HTI_IFACE) ) {
  148.                 if ( tokstrm.nextToken() == '(' ) {
  149.                     JTreeNodeInfo extimpl = new JTreeNodeInfo(tokstrm);
  150.                     newnode.addExtImpl(extimpl);
  151.  
  152.                     while ( tokstrm.nextToken() == ',' ) {
  153.                         tokstrm.pushBack();
  154.                         extimpl = new JTreeNodeInfo(tokstrm);
  155.                         newnode.addExtImpl(extimpl);
  156.                     }
  157.                     // Don't need to pushBack closing brace ')'
  158.                 }
  159.                 else // it must have been EOL push it back into stream
  160.                     tokstrm.pushBack();
  161.             }
  162.         } catch (Exception e) {}
  163.     }
  164.  
  165.     public boolean handleEvent(Event e)
  166.     {
  167.         JTreeNode node;
  168.  
  169.         if (e.id == Event.WINDOW_DESTROY)
  170.             // handle the app exit event
  171.             System.exit(0);
  172.         else if (e.id == Event.LIST_SELECT) {
  173.             if (e.arg instanceof JTreeNode) {
  174.                 node = (JTreeNode)e.arg;
  175.                 dispatchInfo(node);
  176.             }
  177.             else if (e.arg instanceof UITree)
  178.             {
  179.                 IUIComponent comp = ((UITree)e.arg).getHeader();
  180.                 if ( comp instanceof JTreeNode ) {
  181.                     node = (JTreeNode)comp;
  182.                     dispatchInfo(node);
  183.                 }
  184.             }
  185.         }
  186.         return(super.handleEvent(e));
  187.     }
  188.  
  189.     public Hashtable getHTable() { return htable; }
  190.  
  191.     public void dispatchInfo(JTreeNode node)
  192.     {
  193.         String clsname = node.getFullName();
  194.  
  195.         status.setName(clsname);
  196.  
  197.         // if node is not visible it must be in ifacelst tree
  198.         if ( !node.isVisible() ) 
  199.             ifacelst.setExpanded(true);
  200.  
  201.         jtree.setSelectedItem(node);
  202.         node.requestFocus();
  203.     }
  204. }
  205.  
  206. interface JTCallbacks
  207. {
  208.     public Hashtable getHTable();
  209.     public void dispatchInfo(JTreeNode node);
  210. }
  211.