void LuaDebuggerInterface::handlLuaError(LuaErrorEventDetails* const details)
{
    std::string ErrorType("");
    switch(details->getStatus())
    {
        case LUA_ERRSYNTAX:
            //Syntax Error
            ErrorType = "Lua Syntax Error";
            break;
        case LUA_ERRMEM:
            //Memory Allocation Error
            ErrorType = "Lua Memory Allocation Error";
            break;
        case LUA_ERRRUN:
            //Memory Allocation Error
            ErrorType = "Lua Runtime Error";
            break;
        case LUA_ERRERR:
            //Memory Allocation Error
            ErrorType = "Lua Error in Error Handler";
            break;
        default:
            //Unknown
            ErrorType = "Lua Unknown Error";
            break;
    }
    _ErrorTextArea->moveCaretToEnd();
    if(_ErrorTextArea->getText().size() != 0)
    {
        _ErrorTextArea->write("\n");
    }
    _ErrorTextArea->write(ErrorType + ":\n    " + details->getErrorString());

    //Select the Error Tab
    _InfoTabPanel->setSelectedIndex(1);

    //Fill Stack Trace
    if(details->getStatus() == LUA_ERRMEM ||
       details->getStatus() == LUA_ERRERR ||
       details->getStatus() == LUA_ERRRUN)
    {
        std::stringstream ss;
        ss << "Lua Stack Trace: " << std::endl << TheLuaManager->getCallStack() << std::endl;
        _StackTraceTextArea->write(ss.str());
    }
}
void LuaDebuggerInterface::createUtilTabs(void)
{
    //Create the Error Text Area
    _ErrorTextArea = TextArea::create();

    _ErrorTextArea->setPreferredSize(Vec2f(600, 150));
    _ErrorTextArea->setText("");
    _ErrorTextArea->setMinSize(Vec2f(300, 150));
    _ErrorTextArea->setFont(_CodeFont);
    _ErrorTextArea->setTextColors(Color4f(0.2,0.0,0.0,1.0));
    _ErrorTextArea->setEditable(false);
    setName(_ErrorTextArea,"Error TextArea");
    TheLuaManager->connectLuaError(boost::bind(&LuaDebuggerInterface::handlLuaError,
                                                this,
                                                _1));

    // Create a ScrollPanel
    ScrollPanelRefPtr ErrorAreaScrollPanel = ScrollPanel::create();
    ErrorAreaScrollPanel->setPreferredSize(Vec2f(200,150));
    ErrorAreaScrollPanel->setHorizontalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    // Add the TextArea to the ScrollPanel so it is displayed
    ErrorAreaScrollPanel->setViewComponent(_ErrorTextArea);

    //Create the StackTrace Text Area
    _StackTraceTextArea = TextArea::create();

    _StackTraceTextArea->setPreferredSize(Vec2f(600, 150));
    _StackTraceTextArea->setText("");
    _StackTraceTextArea->setMinSize(Vec2f(300, 150));
    _StackTraceTextArea->setFont(_CodeFont);
    _StackTraceTextArea->setTextColors(Color4f(0.2,0.0,0.0,1.0));
    _StackTraceTextArea->setEditable(false);
    setName(_StackTraceTextArea,"Stack Trace TextArea");

    // Create a ScrollPanel
    ScrollPanelRefPtr StackTraceAreaScrollPanel = ScrollPanel::create();
    StackTraceAreaScrollPanel->setPreferredSize(Vec2f(200,150));
    StackTraceAreaScrollPanel->setHorizontalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    // Add the TextArea to the ScrollPanel so it is displayed
    StackTraceAreaScrollPanel->setViewComponent(_StackTraceTextArea);

    //Create the Message Text Area
    _MessageTextArea = TextArea::create();

    _MessageTextArea->setPreferredSize(Vec2f(600, 150));
    _MessageTextArea->setText("");
    _MessageTextArea->setMinSize(Vec2f(300, 150));
    _MessageTextArea->setFont(_CodeFont);
    _MessageTextArea->setTextColors(Color4f(0.2,0.0,0.0,1.0));
    _MessageTextArea->setEditable(false);
    setName(_MessageTextArea,"Message TextArea");

    // Create a ScrollPanel
    ScrollPanelRefPtr MessageAreaScrollPanel = ScrollPanel::create();
    MessageAreaScrollPanel->setPreferredSize(Vec2f(200,150));
    MessageAreaScrollPanel->setHorizontalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    // Add the TextArea to the ScrollPanel so it is displayed
    MessageAreaScrollPanel->setViewComponent(_MessageTextArea);

    //Create the Message Text Area
    _HelpTextArea = TextArea::create();

    _HelpTextArea->setPreferredSize(Vec2f(600, 150));
    _HelpTextArea->setText("");
    _HelpTextArea->setMinSize(Vec2f(300, 150));
    _HelpTextArea->setFont(_CodeFont);
    _HelpTextArea->setTextColors(Color4f(0.2,0.0,0.0,1.0));
    _HelpTextArea->setEditable(false);
    setName(_HelpTextArea,"Help TextArea");

    // Create a ScrollPanel
    ScrollPanelRefPtr HelpAreaScrollPanel = ScrollPanel::create();
    HelpAreaScrollPanel->setPreferredSize(Vec2f(200,150));
    HelpAreaScrollPanel->setHorizontalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    // Add the TextArea to the ScrollPanel so it is displayed
    HelpAreaScrollPanel->setViewComponent(_HelpTextArea);

    //Tab Panel
    LabelRefPtr MessageTabLabel = Label::create();
    MessageTabLabel->setText("Output");

    LabelRefPtr ErrorTabLabel = Label::create();
    ErrorTabLabel->setText("Error");

    LabelRefPtr StackTraceTabLabel = Label::create();
    StackTraceTabLabel->setText("Stack");

    LabelRefPtr HelpTabLabel = Label::create();
    HelpTabLabel->setText("Help");

    _InfoTabPanel = TabPanel::create();
    _InfoTabPanel->addTab(MessageTabLabel, MessageAreaScrollPanel);
    _InfoTabPanel->addTab(ErrorTabLabel, ErrorAreaScrollPanel);
    _InfoTabPanel->addTab(StackTraceTabLabel, StackTraceAreaScrollPanel);
    _InfoTabPanel->addTab(HelpTabLabel, HelpAreaScrollPanel);
#ifdef OSG_WITH_LUA_DEBUGGER
    addIntrospectionTreeTab(_InfoTabPanel);
#endif
    _InfoTabPanel->setTabAlignment(0.5f);
    _InfoTabPanel->setTabPlacement(TabPanel::PLACEMENT_NORTH);
    _InfoTabPanel->setSelectedIndex(0);
    setName(_InfoTabPanel,"Info Tab Panel");
}
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);

    // Make Torus Node (creates Torus in background of scene)
    NodeRefPtr TorusGeometryNode = makeTorus(.5, 2, 16, 16);

    // Make Main Scene Node and add the Torus
    NodeRefPtr scene = OSG::Node::create();
        scene->setCore(OSG::Group::create());
        scene->addChild(TorusGeometryNode);

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

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

    //Background
    TutorialBackground = GradientBackground::create();
    TutorialBackground->addLine(Color3f(1.0,0.0,0.0), 0.0);
    TutorialBackground->addLine(Color3f(0.0,1.0,0.0), 0.2);
    TutorialBackground->addLine(Color3f(0.0,0.0,1.0), 0.4);
    TutorialBackground->addLine(Color3f(0.0,1.0,1.0), 0.6);
    TutorialBackground->addLine(Color3f(1.0,1.0,0.0), 0.8);
    TutorialBackground->addLine(Color3f(1.0,1.0,1.0), 1.0);

	TheUndoManager = UndoManager::create();
	UndoManagerChangeListener TheUndoManagerChangeListener;
	TheUndoManager->addChangeListener(&TheUndoManagerChangeListener);
    
    LabelRefPtr SingleFieldLabel = OSG::Label::create();
    SingleFieldLabel->setText("Single Field");
    SingleFieldLabel->setBorders(NULL);
    SingleFieldLabel->setBackgrounds(NULL);

    LabelRefPtr MultiFieldLabel = OSG::Label::create();
    MultiFieldLabel->setText("Multi Field");
    MultiFieldLabel->setBorders(NULL);
    MultiFieldLabel->setBackgrounds(NULL);

    LabelRefPtr SinglePtrFieldLabel = OSG::Label::create();
    SinglePtrFieldLabel->setText("Single Ptr Field");
    SinglePtrFieldLabel->setBorders(NULL);
    SinglePtrFieldLabel->setBackgrounds(NULL);

    LabelRefPtr MultiPtrFieldLabel = OSG::Label::create();
    MultiPtrFieldLabel->setText("Multi Ptr Field");
    MultiPtrFieldLabel->setBorders(NULL);
    MultiPtrFieldLabel->setBackgrounds(NULL);

    TabPanelRefPtr ExampleTabPanel = OSG::TabPanel::create();
    ExampleTabPanel->setPreferredSize(Vec2f(600,600));
    ExampleTabPanel->addTab(SingleFieldLabel, createSingleFieldPanel());
    ExampleTabPanel->addTab(MultiFieldLabel, createMultiFieldPanel());
    ExampleTabPanel->addTab(SinglePtrFieldLabel, createSinglePtrFieldPanel());
    ExampleTabPanel->addTab(MultiPtrFieldLabel, createMultiPtrFieldPanel());
    ExampleTabPanel->setTabAlignment(0.5f);
    ExampleTabPanel->setTabPlacement(TabPanel::PLACEMENT_NORTH);
    ExampleTabPanel->setSelectedIndex(0);

	//UndoList
	UndoRedoListModel = DefaultListModel::create();
    UndoRedoListModel->pushBack(boost::any(std::string("Top")));
	ListSelectionModelPtr UndoRedoListSelectionModel(new DefaultListSelectionModel());

	UndoRedoList = List::create();
        UndoRedoList->setPreferredSize(Vec2f(200, 300));
        UndoRedoList->setOrientation(List::VERTICAL_ORIENTATION);
		UndoRedoList->setModel(UndoRedoListModel);

    UndoRedoList->setSelectionModel(UndoRedoListSelectionModel);

    UndoRedoListListener TheUndoRedoListListener;
    UndoRedoList->getSelectionModel()->addListSelectionListener(&TheUndoRedoListListener);

    UndoButton = OSG::Button::create();
            UndoButton->setText("Undo");
			UndoButton->setEnabled(TheUndoManager->numberOfUndos() != 0);
    UndoButtonActionListener TheUndoButtonActionListener;
    UndoButton->addActionListener(&TheUndoButtonActionListener);
	

    RedoButton = OSG::Button::create();
            RedoButton->setText("Redo");
			RedoButton->setEnabled(TheUndoManager->numberOfRedos() != 0);
    RedoButtonActionListener TheRedoButtonActionListener;
    RedoButton->addActionListener(&TheRedoButtonActionListener);

    // Create a ScrollPanel for easier viewing of the List (see 27ScrollPanel)
    ScrollPanelRefPtr UndoRedoScrollPanel = ScrollPanel::create();
        UndoRedoScrollPanel->setPreferredSize(Vec2f(200,200));
        UndoRedoScrollPanel->setHorizontalResizePolicy(ScrollPanel::RESIZE_TO_VIEW);
    UndoRedoScrollPanel->setViewComponent(UndoRedoList);

    // 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));
    InternalWindowRefPtr MainInternalWindow = OSG::InternalWindow::create();
    LayoutRefPtr MainInternalWindowLayout = OSG::FlowLayout::create();
       MainInternalWindow->pushToChildren(ExampleTabPanel);
       MainInternalWindow->pushToChildren(UndoRedoScrollPanel);
       MainInternalWindow->pushToChildren(UndoButton);
       MainInternalWindow->pushToChildren(RedoButton);
       MainInternalWindow->setLayout(MainInternalWindowLayout);
       MainInternalWindow->setBackgrounds(MainInternalWindowBackground);
	   MainInternalWindow->setAlignmentInDrawingSurface(Vec2f(0.5f,0.5f));
	   MainInternalWindow->setScalingInDrawingSurface(Vec2f(0.95f,0.95f));
	   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(scene);

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

    // 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,
            "01ChangeFieldCommands");

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

    osgExit();

    return 0;
}