/******************************************************************************
 BindingsDir.cc

	BASE CLASS = JXWindowDirector

	Copyright © 1997 by Glenn Bach. All rights reserved.

 ******************************************************************************/

#include "BindingsDir.h"
#include "BindingsList.h"
#include "PatternBinding.h"
#include "BindingsTable.h"

#include <JXScrollbarSet.h>
#include <JXApplication.h>
#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXColHeaderWidget.h>
#include <JXHelpManager.h>

#include <JUserNotification.h>
#include <JChooseSaveFile.h>
#include <JString.h>

#include <jXGlobals.h>

#include <jStreamUtil.h>
#include <jFileUtil.h>
#include <jDirUtil.h>
#include <jGlobals.h>

#include <iostream.h>
#include <fstream.h>
#include <strstream.h>
#include <stdlib.h>

#include <jAssert.h>

const JCoordinate kCurrentPrefsVersion	= 1;

const JCharacter* BindingsDir::kEditorClosing = 
	"EditorClosing::BindingsDir";

/******************************************************************************
 Constructor

 ******************************************************************************/

JBoolean
BindingsDir::Create
	(
	JXDirector* 	supervisor,
	BindingsDir**	dir
	)
{
	BindingsList* list;
	if (BindingsList::Create(&list))
		{
		*dir = new BindingsDir(supervisor, list);
		assert(*dir != NULL);
		return kTrue;
		}
	return kFalse;
}
/******************************************************************************
 Constructor

 ******************************************************************************/

BindingsDir::BindingsDir
	(
	JXDirector* supervisor,
	BindingsList* list
	)
	:
	JXWindowDirector(supervisor),
	itsList(list)
{
	BuildWindow();
	itsNeedsSave = kFalse;
}

/******************************************************************************
 Destructor

 ******************************************************************************/

BindingsDir::~BindingsDir()
{
	delete itsList;
}

/******************************************************************************
 BuildWindow

 ******************************************************************************/

void
BindingsDir::BuildWindow()
{

	JSize w = 580;
	JSize h = 420;

// begin JXLayout

    JXWindow* window = new JXWindow(this, 580,420, "");
    assert( window != NULL );
    SetWindow(window);

    itsSaveButton =
        new JXTextButton("Save", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 390,390, 70,20);
    assert( itsSaveButton != NULL );
    itsSaveButton->SetShortcuts("#S");

    itsCloseButton =
        new JXTextButton("Close", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 480,390, 70,20);
    assert( itsCloseButton != NULL );
    itsCloseButton->SetShortcuts("#W");

    JXScrollbarSet* obj1 =
        new JXScrollbarSet(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 0,0, 580,380);
    assert( obj1 != NULL );

    itsNewButton =
        new JXTextButton("New", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedBottom, 30,390, 70,20);
    assert( itsNewButton != NULL );
    itsNewButton->SetShortcuts("#N");

    itsDeleteButton =
        new JXTextButton("Delete", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedBottom, 120,390, 70,20);
    assert( itsDeleteButton != NULL );

    itsRevertButton =
        new JXTextButton("Revert", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 300,390, 70,20);
    assert( itsRevertButton != NULL );
    itsRevertButton->SetShortcuts("#R");

    itsHelpButton =
        new JXTextButton("Help", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 210,390, 70,20);
    assert( itsHelpButton != NULL );

// end JXLayout

	window->SetTitle("File bindings editor");

	itsTable = 
		new BindingsTable(itsList,
			obj1, obj1->GetScrollEnclosure(),
			JXWidget::kHElastic, JXWidget::kVElastic,
			0,25,580,410 - 25);
	assert(itsTable != NULL);
	ListenTo(itsTable);

	JXColHeaderWidget* header = 
		JXColHeaderWidget::Create(itsTable, obj1, obj1->GetScrollEnclosure(),
			JXWidget::kHElastic, JXWidget::kFixedTop,
			0,0,580,25);
	assert(header != NULL);
	header->SetColTitle(1, "File Ending");
	header->SetColTitle(2, "Action");
	header->SetColTitle(3, "Alternate Action");

	ListenTo(itsSaveButton);
	itsSaveButton->Deactivate();
	ListenTo(itsCloseButton);
	ListenTo(itsNewButton);
	ListenTo(itsDeleteButton);
	itsDeleteButton->Deactivate();
	ListenTo(itsRevertButton);

	itsHelpButton->Hide();
	ListenTo(itsHelpButton);
	
	window->SetMinSize(w,h);
}

/******************************************************************************
 Receive

 ******************************************************************************/

void
BindingsDir::Receive
	(
	JBroadcaster* sender,
	const JBroadcaster::Message& message
	)
{
	if (sender == itsSaveButton && message.Is(JXButton::kPushed))
		{
		itsTable->EndEditing();
		Save();
		}
	else if (sender == itsCloseButton && message.Is(JXButton::kPushed))
		{
		TryToClose();
		}
	else if (sender == itsNewButton && message.Is(JXButton::kPushed))
		{
		PatternBinding* pb = new PatternBinding("","","",kFalse);
		assert(pb != NULL);
		itsList->InsertSorted(pb, kTrue);
		itsDeleteButton->Activate();
		}
	else if (sender == itsDeleteButton && message.Is(JXButton::kPushed))
		{
		itsTable->DeletePattern();
		}
	else if (sender == itsRevertButton && message.Is(JXButton::kPushed))
		{
		itsList->Revert();
		itsTable->SyncWithList();
		Reverted();
		}
	else if (sender == itsHelpButton && message.Is(JXButton::kPushed))
		{
		if (!itsHelpSection.IsEmpty())
			{
			JXGetHelpManager()->ShowSection(itsHelpSection);
			}
		}
	else if (sender == itsTable)
		{
		if (message.Is(BindingsTable::kEditStarted))
			{
			itsDeleteButton->Activate();
			}
		else if (message.Is(BindingsTable::kEditStopped))
			{
			itsDeleteButton->Deactivate();
			}
		else if (message.Is(BindingsTable::kChanged))
			{
			NeedsSave();
			}
		}

	else
		{
		JXWindowDirector::Receive(sender, message);
		}

}


/******************************************************************************
 Save

 ******************************************************************************/

void
BindingsDir::Save()
{
	itsList->Save();
	Reverted();
}

/******************************************************************************
 NeedsSave

 ******************************************************************************/

void
BindingsDir::NeedsSave()
{
	itsNeedsSave = kTrue;
	itsSaveButton->Activate();
}

/******************************************************************************
 Reverted

 ******************************************************************************/

void
BindingsDir::Reverted()
{
	itsNeedsSave = kFalse;
	itsSaveButton->Deactivate();
}

/******************************************************************************
 ReadPrefs 

 ******************************************************************************/

void
BindingsDir::ReadSetup
	(
	istream& is
	)
{
	JCoordinate version;
	is >> version;
	GetWindow()->ReadGeometry(is);
}

/******************************************************************************
 WritePrefs 

 ******************************************************************************/

void
BindingsDir::WriteSetup
	(
	ostream& os
	)
{
	os << kCurrentPrefsVersion << ' ';
	GetWindow()->WriteGeometry(os);
}

/******************************************************************************
 TryToClose

 ******************************************************************************/

void
BindingsDir::TryToClose()
{
	itsTable->EndEditing();
	if (itsNeedsSave)
		{
		JUserNotification::CloseAction ok = JGetUserNotification()->OKToClose("Save bindings before closing?");
		if (ok == JUserNotification::kDontClose)
			{
			return;
			}
		if (ok == JUserNotification::kSaveData)
			{
			Save();
			}
		}
	Broadcast(EditorClosing());
	Close();
}

/******************************************************************************
 SetHelpSection (public)

 ******************************************************************************/

void
BindingsDir::SetHelpSection
	(
	const JCharacter* section
	)
{
	itsHelpSection = section;
	itsHelpButton->Show();
}
