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