//==================================================================================================
// G r a p h i c                                                                          Interface
// C o n s o l e
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

/*DESCRIPTION*/
/* This module provides a console display for the B++ Library through an
   <CODE>OutputStream</CODE> Java component. */

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "graphic/console.hpp"

// Guardian //--------------------------------------------------------------------------------------
#ifndef guGraphicConsole
#define guGraphicConsole

// Headers //---------------------------------------------------------------------------------------
#include <bpp/display/console.hpp> /*INCLUDE*/
#include <jirk/java/io/outputstream.hpp> /*INCLUDE*/

namespace bpp {

// Importation/Exportation //-----------------------------------------------------------------------
#ifdef GRAPHIC_DLL
 #define dll_export DLL_EXPORT
#else
 #define dll_export DLL_IMPORT
#endif

// Namespaces //------------------------------------------------------------------------------------
#define public_area  graphicConsole
#define private_area graphicConsole_private

namespace public_area  { /*NAMESPACE*/ using namespace java; }
namespace private_area { using namespace public_area; }

// Macrocommands //---------------------------------------------------------------------------------

// Types & Classes //-------------------------------------------------------------------------------
namespace public_area {
 //------------------------------------------------------------------------------------------Classes
 class clDisplay;
 //-----------------------------------------------------------------------------------Constant Types
 typedef const clDisplay ctDisplay;
}

namespace private_area {}

// Functions Interface //---------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Errors //----------------------------------------------------------------------------------------
namespace public_area {}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// D i s p l a y  Interface //----------------------------------------------------------------------
namespace public_area {
 /*CLASS clDisplay */
 /* Represents a console display, text will be transferred through an <CODE>OutputStream</CODE>
    Java component. */
 class clDisplay : public displayConsole::clDisplay {
  //-----------------------------------------------------------------------------------------Private
  private_property jyContext                        atContext;
  private_property jyObject                         atReference;
  private_property jirk::java::io::jaOutputStream * atStream;

  private_property constructor clDisplay(ctDisplay &);
  private_property clDisplay & operator = (ctDisplay &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clDisplay(jyContext=nil,jyObject=nil);
  public_property destructor clDisplay(void);

  public_property void activate(void);
  public_property void nextLine(void);
  public_property void refresh(void);
  public_property void setStream(jyContext,jyObject);
 };
}

// Functions Inline //------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// D i s p l a y  Inline //-------------------------------------------------------------------------
namespace public_area {
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD clDisplay */ /* Builds a display from a given stream. */
 inline clDisplay::clDisplay(jyContext agContext,jyObject agStream)
 : atContext(agContext),atReference(agStream),atStream(nil) {}
 //---------------------------------------------------------------------------------------Destructor
 /*METHOD clDisplay */ /* Destructs the display. */
 inline clDisplay::~clDisplay(void) {}
 //----------------------------------------------------------------------------------------SetStream
 /*METHOD clDisplay */ /* Sets the stream where text will be transferred. */
 inline void clDisplay::setStream(jyContext agContext,jyObject agStream) {
  atContext=agContext;
  atReference=agStream;

  if ((atContext==nil or atReference==nil) and atStream!=nil) {
   delete atStream;
   atStream=nil;
  }
 }
}

// End //-------------------------------------------------------------------------------------------
}
#undef dll_export
#undef public_area
#undef private_area
#endif
 
//==================================================================================================
// G r a p h i c                                                                     Implementation
// C o n s o l e
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "graphic/console.cpp"

// DLL Belonging //---------------------------------------------------------------------------------
#define GRAPHIC_DLL

// Headers //---------------------------------------------------------------------------------------
#include <bpp/graphic/console.hpp> /*INTERFACE*/
#include <jirk/java/lang/string.hpp> /*NEED*/

namespace bpp {

// Namespaces //------------------------------------------------------------------------------------
#define public_area  graphicConsole
#define private_area graphicConsole_private
#define dll_export   DLL_EXPORT

namespace public_area  {}
namespace private_area {}

// Errors //----------------------------------------------------------------------------------------
namespace public_area {}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Static Members //--------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Implementation //----------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// D i s p l a y  Implementation //-----------------------------------------------------------------
namespace public_area {
 //-----------------------------------------------------------------------------------------Activate
 /*METHOD clDisplay */ /* Activates the display, if not already done. */
 property void clDisplay::activate(void) {
  if (environment_private::goJavaActivated and atContext!=nil and atReference!=nil) {
   if (atStream==nil) {
    getVirtualMachine(atContext);
    atStream=new jirk::java::io::jaOutputStream(atReference);
   }
  }
 }
 //-----------------------------------------------------------------------------------------NextLine
 /*METHOD clDisplay */ /* Jumps to the next line. */
 property void clDisplay::nextLine(void) {
  tyString lcLine = atCurrentLine;

  refresh();

  if (environment_private::goJavaActivated and atContext!=nil and atReference!=nil) {
   if (atStream==nil) {
    getVirtualMachine(atContext);
    atStream=new jirk::java::io::jaOutputStream(atReference);
   }

   while (*lcLine!='\0') {
    atStream->j_write(jyInteger(*lcLine));
    ++lcLine;
   }

   atStream->j_write(jyInteger('\n'));
  }

  atOutputFile << atCurrentLine << end_line;
  atCurrentLine[0]=end_string;
 }
 //------------------------------------------------------------------------------------------Refresh
 /*METHOD clDisplay */ /* Refreshes the display. */
 property void clDisplay::refresh(void) {
  if (not environment_private::goJavaActivated) {
   if (atStream!=nil) {
    delete atStream;
    atStream=nil;
   }
  }
 }
}

// Java Zone //-------------------------------------------------------------------------------------
java_zone {
 //---------------------------------------------------------------------------------------Namespaces
 using namespace java;
 using namespace jirk::java::lang;
 using namespace jirk::java::util;
 //------------------------------------------------------------------------------------------Display
 static displayConsole::clDisplay * goDisplay = nil;
 //--------------------------------------------------------------------------EnvironmentLoader::Open
 java_method(void,bpp_java_EnvironmentLoader,open)
 (jyContext agContext,jyClass,jyObject agStream,jyBoolean agGraphic) {
  if (goDisplay==nil) {
   if (agGraphic==java_true) {
    goDisplay=new graphicConsole::clDisplay();
    ((graphicConsole::clDisplay *)goDisplay)->setStream(agContext,agStream);
   }
   else goDisplay=new displayConsole::clDisplay();

   standard::openLibrary(*goDisplay);
  }
 }
 //-------------------------------------------------------------------------EnvironmentLoader::Close
 java_method(void,bpp_java_EnvironmentLoader,close)(jyContext,jyClass) {
  if (goDisplay!=nil) {
   if (environment::informationDisplayed()) environment::nextLine();
   standard::closeLibrary();
   delete goDisplay;
   goDisplay=nil;
  }
 }
}

// End //-------------------------------------------------------------------------------------------
}