include/GUI/components/DOMTreeViewer.hpp

Go to the documentation of this file.
00001 #ifndef __JUCER_HEADER_DOMTREEVIEWER_DOMTREEVIEWER_1DED779__
00002 #define __JUCER_HEADER_DOMTREEVIEWER_DOMTREEVIEWER_1DED779__
00003 
00004 //[Headers]     -- You can add your own extra header files here --
00005 #include "../UZIBase.hpp"
00006 // We need DOM tree declaration
00007 #include "../../HTMLParser/DOM.hpp"
00008 // We need HTML element
00009 #include "../../HTMLParser/Elements.hpp"
00010 
00011 //[/Headers]
00012 
00013 
00014 namespace juce
00015 {
00016     //==============================================================================
00024     class DOMTreeViewer  : public Component
00025     {
00026     public:
00027         //==============================================================================
00028         DOMTreeViewer (HTML::DOMTree & root);
00029         ~DOMTreeViewer();
00030 
00031         //==============================================================================
00032         //[UserMethods]     -- You can add your own custom methods in this section.
00033         class DOMTreeItem : public TreeViewItem
00034         {
00035             // Members
00036         public:
00038             const DOM::Node * node;
00039 
00040             // TreeViewItem required Interface
00041         public:
00043             bool mightContainSubItems () { return node ? (node->hasChildNodes()) : false; }
00045             int getItemWidth() const { return 400; }
00047             const String getUniqueName() const
00048             {
00049                 if (node == 0) return String::empty;
00050                 else
00051                 {
00052                     String nodeType;
00053                     String nodeName; 
00054                     nodeName = String::fromUTF8(Strings::convert(node->nodeName()));
00055                     switch(node->nodeType)
00056                     {
00057                     case DOM::Node::NodeType::Element:
00058                         nodeType = JUCE_T("[Element ] ");
00059                         break;
00060                     case DOM::Node::NodeType::Comment:
00061                         nodeType = JUCE_T("[Comment ] ");
00062                         break;
00063                     case DOM::Node::NodeType::Text:
00064                         nodeType = JUCE_T("[  Text  ] ");
00065                         break;
00066                     case DOM::Node::NodeType::Attribute:
00067                         nodeType = JUCE_T("[ Attrib ] ");
00068                         break;
00069                     case DOM::Node::NodeType::Document:
00070                         nodeType = JUCE_T("[Document] ");
00071                         break;
00072                     case DOM::Node::NodeType::DocumentType:
00073                         nodeType = JUCE_T("[DocType ] ");
00074                         break;
00075                     default:
00076                         nodeType = JUCE_T("[NotProc ] ");
00077                         break;
00078                     }
00079                     return nodeType + nodeName;
00080                 }
00081             }
00082             
00084             void paintItem (Graphics& g, int width, int height)
00085             {
00086                 if (node)
00087                 {
00088                     // if this item is selected, fill it with a background colour..
00089                     if (isSelected()) g.fillAll (Colours::blue.withAlpha (0.3f));
00090 
00091                     g.setColour (Colours::black);
00092                     g.setFont (height * 0.7f);
00093 
00094                     g.drawText (getUniqueName(), 4, 0, width - 4, height, Justification::centredLeft, true);
00095                 }
00096             }
00097 
00098             void itemOpennessChanged (bool isNowOpen)
00099             {
00100                 if (isNowOpen)
00101                 {
00102                     // if we've not already done so, we'll now add the tree's sub-items. You could
00103                     // also choose to delete the existing ones and refresh them if that's more suitable
00104                     // in your app.
00105                     if (getNumSubItems() == 0)
00106                     {
00107                         // create and add sub-items to this node of the tree, corresponding to
00108                         if (node)
00109                         {
00110                             const DOM::Node * child = node->firstChild();
00111                             while (child)
00112                             {
00113                                 addSubItem(new DOMTreeItem(child));
00114                                 child = child->nextSibling();
00115                             }
00116                         }
00117                     }
00118                 }
00119                 else
00120                 {
00121                     // in this case, we'll leave any sub-items in the tree when the node gets closed,
00122                     // though you could choose to delete them if that's more appropriate for
00123                     // your application.
00124                 }
00125             }
00126 
00127             void itemClicked (const MouseEvent &e)
00128             {
00129                 String value;
00130                 if (node)
00131                 {
00132                     if (node->nodeType == DOM::Node::NodeType::Element)
00133                     {
00134                         const DOM::Element* element = (const DOM::Element*)node;
00135                         if (element->getElement())
00136                         {
00137                             HTML::Elements::Element * el = element->getElement();
00138                             value << JUCE_T("[<:") << el->getStartPosition() << JUCE_T(",>:") << el->getEndPosition() << JUCE_T(" </:") << el->getEndTagPosition() << JUCE_T(",>:") << el->getFinalPosition() << JUCE_T("]\n");
00139                             const DOM::NamedNodeMap * attributes = element->attributes();
00140                             if (attributes)
00141                             {
00142                                 for (uint32 i = 0; i < attributes->length(); i++)
00143                                 {
00144                                     const DOM::Attr * attr = (const DOM::Attr *)attributes->item(i);
00145                                     value << String::fromUTF8(Strings::convert(attr->name())) << JUCE_T(" = ") << String::fromUTF8(Strings::convert(attr->value())) << JUCE_T("\n");
00146                                 }
00147                             }
00148                             value += String::fromUTF8(Strings::convert(node->nodeValue()));
00149                         }
00150                     }
00151                     else 
00152                         value = String::fromUTF8(Strings::convert(node->nodeValue()));
00153 
00154                     ((DOMTreeViewer *)getOwnerView()->getParentComponent())->selectionChanged(value);
00155                 }
00156             }
00157     
00158 
00159 
00160             // Construction / Destruction
00161         public:
00163             DOMTreeItem(const DOM::Node * _node) : node(_node) {}
00165             virtual ~DOMTreeItem() {}
00166         };
00167 
00168         void selectionChanged(const String & value)
00169         {
00170             if (textEditor) textEditor->setText(value);
00171         }
00172 
00173         //[/UserMethods]
00174 
00175         void paint (Graphics& g);
00176         void resized();
00177 
00178 
00179         //==============================================================================
00180         juce_UseDebuggingNewOperator
00181 
00182     private:
00183         //[UserVariables]   -- You can add your own custom variables in this section.
00184         TreeViewItem * rootItem;
00185         //[/UserVariables]
00186 
00187         //==============================================================================
00188         Label* title;
00189         TreeView* domTree;
00190         TextEditor* textEditor;
00191 
00192         //==============================================================================
00193         // (prevent copy constructor and operator= being generated..)
00194         DOMTreeViewer (const DOMTreeViewer&);
00195         const DOMTreeViewer& operator= (const DOMTreeViewer&);
00196     };
00197 }
00198 
00199 #endif   // __JUCER_HEADER_DOMTREEVIEWER_DOMTREEVIEWER_1DED779__

(C) An X-Ryl669 project 2007

This document describes Unlimited Zooming Interface source code. UZI stands for Unlimited Zooming Interface, and source code license is