//Called whenever elements are added to the selection
 virtual void selectionAdded(const TreeSelectionEventUnrecPtr e)
 {
     //Get the selected Node
     try
     {
         SelectedNode = boost::any_cast<NodeRefPtr>(TheTree->getLastSelectedPathComponent());
     }
     catch (boost::bad_any_cast &)
     {
         SelectedNode = NULL;
     }
     selectedNodeChanged();
 }
DialogWindowTransitPtr createFCTreeEditorDialog       (FieldContainer* fc, 
                                                                                       CommandManagerPtr CmdManager,
                                                                                       const std::string& editorName)
{
    DialogWindowRefPtr TheDialog = DialogWindow::create();



    //Create the FieldEditorComponent
    FieldContainerEditorComponentRefPtr TheEditor = FieldContainerEditorFactory::the()->createDefaultEditor(fc, CmdManager);
    ScrollPanelRefPtr EditorScrollPanel = ScrollPanel::create();
    EditorScrollPanel->setViewComponent(TheEditor);

    //Field Container Tree Model
    FieldContainerTreeModelRefPtr TheTreeModel = FieldContainerTreeModel::create();
    TheTreeModel->setRoot(fc);
    TheTreeModel->setShowInternalFields(true);
    TheTreeModel->setShowPtrFields(true);
    TheTreeModel->setShowDataFields(false);
    TheTreeModel->setShowParentPtrFields(false);
    TheTreeModel->setShowChildPtrFields(true);
    TheTreeModel->setShowAttachments(true);
    TheTreeModel->setShowCallbackFunctors(false);

    //Field Container Tree Component Generator
    FieldContainerFieldPathComponentGeneratorRefPtr TheTreeComponentGenerator = FieldContainerFieldPathComponentGenerator::create();

    //Create the PopupMenu for the tree
    MenuItemRecPtr ExportMenuItem = MenuItem::create();
    ExportMenuItem->setText("Export ...");

    MenuItemRecPtr ImportMenuItem = MenuItem::create();
    ImportMenuItem->setText("Import ...");

    PopupMenuRecPtr TreePopupMenu = PopupMenu::create();
    TreePopupMenu->addItem(ExportMenuItem);
    TreePopupMenu->addItem(ImportMenuItem);


    //Create the Field Container Tree
    TreeRefPtr TheTree = Tree::create();

    TheTree->setPreferredSize(Vec2f(100, 500));
    TheTree->setRootVisible(true);
    TheTree->setModel(TheTreeModel);
    TheTree->setCellGenerator(TheTreeComponentGenerator);
    TheTree->setPopupMenu(TreePopupMenu);

    TheTree->getSelectionModel()->connectSelectionAdded(boost::bind(&handleFCSelectionAdded, _1,
                                                                    TheTree.get(),
                                                                    EditorScrollPanel.get()));

    ExportMenuItem->connectActionPerformed(boost::bind(&handleTreeNodeExport,
                                                       _1,
                                                       TheTree.get()));

    ImportMenuItem->connectActionPerformed(boost::bind(&handleTreeNodeImport,
                                                       _1,
                                                       TheTree.get()));
    //TheDialog->addTransientObject(boost::any(TheTreeEditorSelectionListener));

    ScrollPanelRefPtr TreeScrollPanel = ScrollPanel::create();
    TreeScrollPanel->setViewComponent(TheTree);

    //Ok button
    ButtonRefPtr ConfirmButton = Button::create();
    ConfirmButton->setText("Ok");
    ConfirmButton->connectActionPerformed(boost::bind(&DialogWindow::handleConfirmButtonAction, TheDialog.get(), _1));

    SpringLayoutRefPtr DialogLayout = OSG::SpringLayout::create();

    //SplitPanel
    SplitPanelRefPtr TheSplitPanel = SplitPanel::create();
    TheSplitPanel->setOrientation(SplitPanel::HORIZONTAL_ORIENTATION);
    TheSplitPanel->setDividerPosition(0.4f);
    TheSplitPanel->setDividerSize(5.0f);
    TheSplitPanel->setMaxDividerPosition(0.8f);
    TheSplitPanel->setMinDividerPosition(0.2f);
    TheSplitPanel->setMinComponent(TreeScrollPanel);
    TheSplitPanel->setMaxComponent(EditorScrollPanel);
    
    //TreeScrollPanel
    DialogLayout->putConstraint(SpringLayoutConstraints::NORTH_EDGE, TheSplitPanel, 2, SpringLayoutConstraints::NORTH_EDGE, TheDialog);
    DialogLayout->putConstraint(SpringLayoutConstraints::SOUTH_EDGE, TheSplitPanel, -15, SpringLayoutConstraints::NORTH_EDGE, ConfirmButton);
    DialogLayout->putConstraint(SpringLayoutConstraints::EAST_EDGE, TheSplitPanel, -2, SpringLayoutConstraints::EAST_EDGE, TheDialog);
    DialogLayout->putConstraint(SpringLayoutConstraints::WEST_EDGE, TheSplitPanel, 2, SpringLayoutConstraints::WEST_EDGE, TheDialog);
    
    //ConfirmButton
    DialogLayout->putConstraint(SpringLayoutConstraints::SOUTH_EDGE, ConfirmButton, -15, SpringLayoutConstraints::SOUTH_EDGE, TheDialog);
    DialogLayout->putConstraint(SpringLayoutConstraints::HORIZONTAL_CENTER_EDGE, ConfirmButton, 0, SpringLayoutConstraints::HORIZONTAL_CENTER_EDGE, TheDialog);

    
    TheDialog->setLayout(DialogLayout);
    TheDialog->setPreferredSize(Vec2f(750.0f, 600.0f));
    TheDialog->pushToChildren(TheSplitPanel);
    TheDialog->pushToChildren(ConfirmButton);

    return DialogWindowTransitPtr(TheDialog);
}
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // Set up Window
    TutorialWindow = createNativeWindow();
    TutorialWindow->initWindow();

    TutorialWindow->setDisplayCallback(display);
    TutorialWindow->setReshapeCallback(reshape);

    TutorialKeyListener TheKeyListener;
    TutorialWindow->addKeyListener(&TheKeyListener);

    NodeRefPtr Root(NULL);
    if(argc == 2)
    {
        Root = SceneFileHandler::the()->read(argv[1]);
    }

    if(Root == NULL)
    {
        // Make Torus Node (creates Torus in background of Root)
        NodeRefPtr TorusGeometryNode = makeTorus(.5, 2, 16, 16);
        setName(TorusGeometryNode, std::string("Torus"));

        NodeRefPtr TorusNode = Node::create();
        TorusNode->setCore(OSG::Transform::create());
        TorusNode->addChild(TorusGeometryNode);
        setName(TorusNode, std::string("Torus Transform"));

        NodeRefPtr SphereGeometryNode = makeSphere(2,1.0f);
        setName(SphereGeometryNode, std::string("Sphere"));
        NodeRefPtr BoxGeometryNode = makeBox(1.0,1.0,1.0,1,1,1);
        setName(BoxGeometryNode, std::string("Box"));

        // Make Main Scene Node and add the Torus
        Root = OSG::Node::create();
        Root->setCore(OSG::Group::create());
        Root->addChild(TorusNode);
        Root->addChild(SphereGeometryNode);
        Root->addChild(BoxGeometryNode);
        setName(Root, std::string("Root"));
    }

    // Create the Graphics
    GraphicsRefPtr TutorialGraphics = OSG::Graphics2D::create();

    // Initialize the LookAndFeelManager to enable default settings
    LookAndFeelManager::the()->getLookAndFeel()->init();

    //Tree Model
    TheTreeModel = SceneGraphTreeModel::create();
    TheTreeModel->setRoot(Root);

    //TheFileSystemTreeModel = FileSystemTreeModel::create();
    //TheFileSystemTreeModel->setRoot(BoostPath("C:\\"));
    //TheFileSystemTreeModel->setRoot(BoostPath("/"));

    //Create the Tree
    TheTree = Tree::create();

    TheTree->setPreferredSize(Vec2f(100, 500));
    TheTree->setModel(TheTreeModel);
    //TheTree->setModel(TheFileSystemTreeModel);
    TutorialTreeSelectionListener  TheTutorialTreeSelectionListener;
    TheTree->getSelectionModel()->addTreeSelectionListener(&TheTutorialTreeSelectionListener);


    // Create a ScrollPanel for easier viewing of the List (see 27ScrollPanel)
    BorderLayoutConstraintsRefPtr SceneTreeConstraints = OSG::BorderLayoutConstraints::create();
    SceneTreeConstraints->setRegion(BorderLayoutConstraints::BORDER_WEST);

    ScrollPanelRefPtr ExampleScrollPanel = ScrollPanel::create();
    ExampleScrollPanel->setPreferredSize(Vec2f(350,300));
    ExampleScrollPanel->setConstraints(SceneTreeConstraints);
    //ExampleScrollPanel->setHorizontalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    //ExampleScrollPanel->setVerticalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    ExampleScrollPanel->setViewComponent(TheTree);

    //Details Panel Labels
    LabelRefPtr NodeNameLabel = Label::create();
    NodeNameLabel->setText("Name");
    NodeNameLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeNameValueLabel = Label::create();
    NodeNameValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeCoreTypeLabel = Label::create();
    NodeCoreTypeLabel->setText("Core Type");
    NodeCoreTypeLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeCoreTypeValueLabel = Label::create();
    NodeCoreTypeValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeMinLabel = Label::create();
    NodeMinLabel->setText("Min");
    NodeMinLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeMinValueLabel = Label::create();
    NodeMinValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeMaxLabel = Label::create();
    NodeMaxLabel->setText("Max");
    NodeMaxLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeMaxValueLabel = Label::create();
    NodeMaxValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeCenterLabel = Label::create();
    NodeCenterLabel->setText("Center");
    NodeCenterLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeCenterValueLabel = Label::create();
    NodeCenterValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeTriCountLabel = Label::create();
    NodeTriCountLabel->setText("TriCount");
    NodeTriCountLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeTriCountValueLabel = Label::create();
    NodeTriCountValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeTravMaskLabel = Label::create();
    NodeTravMaskLabel->setText("Traversal Mask");
    NodeTravMaskLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeTravMaskValueLabel = Label::create();
    NodeTravMaskValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeOcclusionMaskLabel = Label::create();
    NodeOcclusionMaskLabel->setText("Occlusion Mask");
    NodeOcclusionMaskLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeOcclusionMaskValueLabel = Label::create();
    NodeOcclusionMaskValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));

    LabelRefPtr NodeActiveLabel = Label::create();
    NodeActiveLabel->setText("Active");
    NodeActiveLabel->setPreferredSize(Vec2f(100.0f, 20.0f));

    NodeActiveValueLabel = Label::create();
    NodeActiveValueLabel->setPreferredSize(Vec2f(300.0f, 20.0f));
    //Details Panel
    BorderLayoutConstraintsRefPtr NodeDetailPanelConstraints = OSG::BorderLayoutConstraints::create();
    NodeDetailPanelConstraints->setRegion(BorderLayoutConstraints::BORDER_SOUTH);

    GridLayoutRefPtr NodeDetailPanelLayout = OSG::GridLayout::create();

    NodeDetailPanelLayout->setRows(9);
    NodeDetailPanelLayout->setColumns(2);
    NodeDetailPanelLayout->setHorizontalGap(2);
    NodeDetailPanelLayout->setVerticalGap(2);

    PanelRefPtr NodeDetailPanel = Panel::create();
    NodeDetailPanel->setConstraints(NodeDetailPanelConstraints);
    NodeDetailPanel->setPreferredSize(Vec2f(100.0f, 200.0f));
    NodeDetailPanel->setLayout(NodeDetailPanelLayout);
    NodeDetailPanel->pushToChildren(NodeNameLabel);
    NodeDetailPanel->pushToChildren(NodeNameValueLabel);
    NodeDetailPanel->pushToChildren(NodeCoreTypeLabel);
    NodeDetailPanel->pushToChildren(NodeCoreTypeValueLabel);
    NodeDetailPanel->pushToChildren(NodeMinLabel);
    NodeDetailPanel->pushToChildren(NodeMinValueLabel);
    NodeDetailPanel->pushToChildren(NodeMaxLabel);
    NodeDetailPanel->pushToChildren(NodeMaxValueLabel);
    NodeDetailPanel->pushToChildren(NodeCenterLabel);
    NodeDetailPanel->pushToChildren(NodeCenterValueLabel);
    NodeDetailPanel->pushToChildren(NodeTriCountLabel);
    NodeDetailPanel->pushToChildren(NodeTriCountValueLabel);
    NodeDetailPanel->pushToChildren(NodeTravMaskLabel);
    NodeDetailPanel->pushToChildren(NodeTravMaskValueLabel);
    NodeDetailPanel->pushToChildren(NodeOcclusionMaskLabel);
    NodeDetailPanel->pushToChildren(NodeOcclusionMaskValueLabel);
    NodeDetailPanel->pushToChildren(NodeActiveLabel);
    NodeDetailPanel->pushToChildren(NodeActiveValueLabel);

    // Create The Main InternalWindow
    // Create Background to be used with the Main InternalWindow
    ColorLayerRefPtr MainInternalWindowBackground = OSG::ColorLayer::create();
    MainInternalWindowBackground->setColor(Color4f(1.0,1.0,1.0,0.5));

    LayoutRefPtr MainInternalWindowLayout = OSG::BorderLayout::create();

    InternalWindowRefPtr MainInternalWindow = OSG::InternalWindow::create();
    MainInternalWindow->pushToChildren(ExampleScrollPanel);
    MainInternalWindow->pushToChildren(NodeDetailPanel);
    MainInternalWindow->setLayout(MainInternalWindowLayout);
    MainInternalWindow->setBackgrounds(NULL);
    MainInternalWindow->setBorders(NULL);
    MainInternalWindow->setAlignmentInDrawingSurface(Vec2f(0.0f,0.5f));
    MainInternalWindow->setScalingInDrawingSurface(Vec2f(1.0,1.0));
    MainInternalWindow->setDrawTitlebar(false);
    MainInternalWindow->setResizable(false);

    // Create the Drawing Surface
    UIDrawingSurfaceRefPtr TutorialDrawingSurface = UIDrawingSurface::create();
    TutorialDrawingSurface->setGraphics(TutorialGraphics);
    TutorialDrawingSurface->setEventProducer(TutorialWindow);

    TutorialDrawingSurface->openWindow(MainInternalWindow);

    // Create the UI Foreground Object
    UIForegroundRefPtr TutorialUIForeground = OSG::UIForeground::create();

    TutorialUIForeground->setDrawingSurface(TutorialDrawingSurface);

    // Create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // Tell the Manager what to manage
    mgr->setWindow(TutorialWindow);
    mgr->setRoot(Root);

    // Add the UI Foreground Object to the Scene
    ViewportRefPtr TutorialViewport = mgr->getWindow()->getPort(0);
    TutorialViewport->addForeground(TutorialUIForeground);

    // Show the whole Scene
    mgr->showAll();


    //Open Window
    Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
    Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
    TutorialWindow->openWindow(WinPos,
                               WinSize,
                               "52SceneGraphTree");

    //Enter main Loop
    TutorialWindow->mainLoop();

    osgExit();

    return 0;
}