/**************************************************************************
*
*  @@@BUILDINFO@@@ 92documentPrefs-2.jsx 2.0.0.54  08-Feb-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

// Each Prefs object needs five methods:
// prefsObject.create (parentPane)  - create the pane and return the Pane object.
// prefsObject.preProcess()         - preprocess the pane just before being shown.
// prefsObject.postProcess()        - do any work just before the pane is being hidden.
// prefsObject.store()              - store the preferences. Return true if the Next Time dialog is needed.
// prefsObject.toDefault()          - set back to default values

var documentPrefs = {};

documentPrefs.tabStopSettings   = [2, 4, 6, 8, 12, 16];
documentPrefs.langObjs          = [];
documentPrefs.lineendings       = [ 'Macintosh', 'Unix', 'Windows' ];

documentPrefs.create = function (parentPane)
{
	this.pane = parentPane.add (
	"group {											\
		orientation		: 'column',						\
		alignChildren	: 'left',						\
		visible			: false,						\
		alignment		: ['left','top'],				\
		autoReload		: Checkbox {					\
			text			: '$$$/ESToolkit/PreferencesDlg/AutoReload=&Automatic Reload of Changed Files', \
			helpTip			: '$$$/ESToolkit/PreferencesDlg/htAutoReload=Reload files automatically if they changed outside of the editor.' \
		},												\
		lineNums		: Checkbox {					\
			text			: '$$$/ESToolkit/PreferencesDlg/linenumbers=&Display Line Numbers',	\
			helpTip			: '$$$/ESToolkit/PreferencesDlg/htLinenumbers=Display line numbers to the left of text.' \
		},												\
		wrap			: Checkbox {					\
			text			: '$$$/ESToolkit/PreferencesDlg/wraplines=&Word Wrap',	\
			helpTip			: '$$$/ESToolkit/PreferencesDlg/htLinewrap=Wrap lines if the line is too long for the window.' \
		},												\
		fold			: Checkbox {					\
			text			: '$$$/ESToolkit/PreferencesDlg/folding=&Code Collapse', \
			helpTip			: '$$$/ESToolkit/PreferencesDlg/htFolding=Enables collapsing or expanding of groups of code lines,' \
		},												\
		fields			: Group {						\
			orientation		: 'column',					\
			alignment		: ['fill', 'top'],			\
			hilite			: Group {					\
				orientation		: 'row',				\
				alignment		: ['right', 'top'],		\
				lbl				: StaticText {			\
					text			: '$$$/ESToolkit/PreferencesDlg/hilite=Enable &Syntax Highlighting:' \
				},										\
				list			: DropDownList {		\
					helpTip			: '$$$/ESToolkit/PreferencesDlg/htHilite=Highlight reserved words, comments, etc, with different colors.',\
				},										\
			},											\
			tabs			: Group {					\
				orientation		: 'row',				\
				alignment		: ['right', 'top'],		\
				lbl				: StaticText {			\
					text			: '$$$/ESToolkit/PreferencesDlg/tabs=&Tab Stops:' \
				},										\
				list			: DropDownList {		\
					helpTip			: '$$$/ESToolkit/PreferencesDlg/htTabs=The number of spaces between tab stops.', \
				},										\
			},											\
			lfs				: Group {					\
				orientation		: 'row',				\
				alignment		: ['right', 'top'],		\
				lbl				: StaticText {			\
					text			: '$$$/ESToolkit/PreferencesDlg/lf=&Line Endings:' \
				},										\
				list			: DropDownList {		\
					helpTip			: '$$$/ESToolkit/PreferencesDlg/htLF=The line feed character sequence.', \
				},										\
			},											\
		}												\
	}");
	this.pane.hiliteList = this.pane.fields.hilite.list;
	this.pane.tabsList	 = this.pane.fields.tabs.list;
	this.pane.lfList	 = this.pane.fields.lfs.list;

	this.pane.hiliteList.minimumSize.width =
	this.pane.tabsList.minimumSize.width   =
	this.pane.lfList.minimumSize.width	   = 120;

	this.loaded = false;
	this.pane.prefsObj = this;
	this.pane.preProcess = function()
	{
		if (!this.prefsObj.loaded)
			this.prefsObj.load();
	}
	this.pane.postProcess = function()
	{}

	this.pane.toDefault = function()
	{
		this.autoReload.value = PrefUtils.getDefaultValue( 'prefs.document.autoReload', 'Boolean' );
		this.lineNums.value   = PrefUtils.getDefaultValue( 'prefs.document.lineNumbers', 'Boolean' );
		this.wrap.value       = PrefUtils.getDefaultValue( 'prefs.document.wrap', 'Boolean' );
		this.fold.value       = PrefUtils.getDefaultValue( 'prefs.document.folding', 'Boolean' );

	    //	Load the possible tab stops and select the preferred one
	    var tabs = PrefUtils.getDefaultValue( 'prefs.document.tabs', 'Number' );
		
	    for( i=0; i<documentPrefs.tabStopSettings.length; i++ ) 
	    {
		    if( this.tabsList.items[i].text == tabs.toString() )
			    this.tabsList.selection = item;
	    }

	    //	Create a list of languages			
	    var langID = PrefUtils.getDefaultValue( 'prefs.document.language', 'String' );

		for (i = 0; i < documentPrefs.langObjs.length; i++)
	    {
		    if( this.hiliteList.items[i].id == langID )
		        this.hiliteList.selection = item;
	    }

	    // create a list of lineendings 
	    var le = PrefUtils.getDefaultValue( 'prefs.document.lineend', 'String' );
	    
		if( le == '' )
			// Use the OS line endings as default
			le = Folder.fs;

	    for( i=0; i<documentPrefs.lineendings.length; i++ ) 
	    {
		    if( this.lfList.items[i].text == le.toString() )
			    this.lfList.selection = item;
	    }
	}

	return this.pane;
}

/////////////////////////////////////////////////////////////////////////
// load Prefs into the text formatting options pane

documentPrefs.load = function()
{
 	if (this.loaded)
		return;
	this.loaded = true;

	var i;
	with (this.pane) 
	{
		autoReload.value = PrefUtils.getValue( 'prefs.document.autoReload', 'Boolean' );
		lineNums.value   = PrefUtils.getValue( 'prefs.document.lineNumbers', 'Boolean' );
		wrap.value       = PrefUtils.getValue( 'prefs.document.wrap', 'Boolean' );
		fold.value       = PrefUtils.getValue( 'prefs.document.folding', 'Boolean' );

	    //	Load the possible tab stops and select the preferred one
	    var tabs = PrefUtils.getValue( 'prefs.document.tabs', 'Number' );
		tabsList.removeAll();
	    for( i=0; i<documentPrefs.tabStopSettings.length; i++ ) 
	    {
		    item = tabsList.add( 'item', documentPrefs.tabStopSettings[i] );
		
		    if( item.text == tabs.toString() )
			    tabsList.selection = item;
	    }

	    //	Create a list of languages			
		for (i = 0; i < lang.langIDs.length; i++)
		{
			var langID  = lang.langIDs [i];
			var langObj = languages [langID];
			if (langID != "text")
				// use the text without any Windows '&' escapes
				documentPrefs.langObjs.push ( { id	: langID,
								  text	: langObj.menu.toString().replace (/&([^&])/,"$1") } );
		}

	    var langID = PrefUtils.getValue( 'prefs.document.language', 'String' );
        hiliteList.removeAll();
		for (i = 0; i < documentPrefs.langObjs.length; i++)
	    {
		    item = hiliteList.add ( "item", documentPrefs.langObjs [i].text );
		    item.id = documentPrefs.langObjs [i].id;				
			
		    if( item.id == langID )
		        hiliteList.selection = item;
	    }
		
	    // create a list of lineendings 
	    var le = PrefUtils.getValue( 'prefs.document.lineend', 'String' );
		if (le == "")
			// Use the OS line endings as default
			le = Folder.fs;
        
        lfList.removeAll();
	    for( i=0; i<documentPrefs.lineendings.length; i++ ) 
	    {
		    item = lfList.add( 'item', documentPrefs.lineendings[i] );
		
		    if( item.text == le.toString() )
			    lfList.selection = item;
	    }
		
		//	Set preferred width of all lists to widest width, with minimum width 100
		this.adjustWidthsToWidest ([hiliteList, tabsList, lfList], 100);
	}
}

/////////////////////////////////////////////////////////////////////////
// set preferences from the selected font options
documentPrefs.store = function()
{
	if (!this.loaded)
		return false;
	with (this.pane) 
	{
		prefs.document.autoReload	= autoReload.value;
		prefs.document.lineNumbers  = lineNums.value;
		prefs.document.wrap         = wrap.value;
		prefs.document.folding      = fold.value;
		if (tabsList.selection)
		    prefs.document.tabs     = tabsList.selection.text; 
		if (hiliteList.selection)
			prefs.document.language = hiliteList.selection.id;
		if (lfList.selection)
		    prefs.document.lineend  = lfList.selection.text;
	}
	return false;
}

/////////////////////////////////////////////////////////////////////////
// layout utility: find widest control in 'elements' array, use it as value of 
// preferredSize.width for all 'elements'. Use 'minWidth' as the minimum width.
documentPrefs.adjustWidthsToWidest = function (elements, minWidth)
{
	var i, widest = minWidth;
	for (i = 0; i < elements.length; i++)
		if (elements[i].preferredSize.width > widest)
			widest = elements[i].preferredSize.width;
	for (i = 0; i < elements.length; i++)
		elements[i].preferredSize.width = widest;
}
