/**************************************************************************
*
*  @@@BUILDINFO@@@ 92documentPrefs-2.jsx 3.0.0.14  27-February-2008
*  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 = { title : "$$$/ESToolkit/PreferencesDlg/textTitle=Documents", sortOrder : 20 };

globalBroadcaster.registerClient( documentPrefs, 'initPrefPanes' );

documentPrefs.onNotify = function( reason )
{
    switch( reason )
    {
        case 'initPrefPanes':
        {
            if( arguments[1] instanceof Array )
                arguments[1].push( this );
        }
        break;
    }
}

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.',
				},
			},
		},
		signature		: Panel {
			orientation		: 'column',
			alignment		: ['left', 'top'],
			alignChildren	: 'left',
			text            : '$$$/ESToolkit/PreferenceDlg/sigTitle=UTF-8 signature',
			sigNever		: RadioButton {
			    text            : '$$$/ESToolkit/PreferenceDlg/sigNever=Never write signature',
			    helpTip         : '$$$/ESToolkit/PreferenceDlg/htSigNever=Never write a signature into the file'
			},
			sigAlways		: RadioButton {
			    text            : '$$$/ESToolkit/PreferenceDlg/sigAlways=Always write signature',
			    helpTip         : '$$$/ESToolkit/PreferenceDlg/htSigAlways=The signature is written always for UTF-8 encoded files'
			},
			sigPossible		: RadioButton {
			    text            : '$$$/ESToolkit/PreferenceDlg/sigPossible=Write signature if source contains non-ASCII characters',
			    helpTip         : '$$$/ESToolkit/PreferenceDlg/htSigPossible=Write the signature if the source contains non-ASCII characters.'
			}
		}
	}""");
	
	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.sigNever      = this.pane.signature.sigNever;
	this.pane.sigAlways     = this.pane.signature.sigAlways;
	this.pane.sigPossible   = this.pane.signature.sigPossible;

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

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

	this.toDefault = function()
	{
		this.pane.autoReload.value = prefs.document.autoReload.getDefault( Preference.BOOLEAN );
		this.pane.lineNums.value   = prefs.document.lineNumbers.getDefault( Preference.BOOLEAN );
		this.pane.wrap.value       = prefs.document.wrap.getDefault( Preference.BOOLEAN );
		this.pane.fold.value       = prefs.document.folding.getDefault( Preference.BOOLEAN );
		
		switch( prefs.document.BOM.getDefault( Preference.NUMBER ) )
		{
		    case 1:     this.pane.sigAlways.value   = true;     break;
		    case 2:     this.pane.sigPossible.value = true;     break;
		    default:    this.pane.sigNever.value    = true;
		}

	    //	Load the possible tab stops and select the preferred one
	    var tabs = prefs.document.tabs.getDefault( Preference.NUMBER );
		
	    for( i=0; i<documentPrefs.tabStopSettings.length; i++ ) 
	    {
		    if( this.pane.tabsList.items[i].text == tabs.toString() )
			    this.pane.tabsList.selection = this.pane.tabsList.items[i];
	    }

	    //	Create a list of languages			
	    var langID = prefs.document.language.getDefault( Preference.STRING );

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

	    // create a list of lineendings 
	    var le = prefs.document.lineend.getDefault( Preference.STRING );
	    
		if( le == '' )
			// Use the OS line endings as default
			le = Folder.fs;

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

	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 = prefs.document.autoReload.getValue( Preference.BOOLEAN );
		lineNums.value   = prefs.document.lineNumbers.getValue( Preference.BOOLEAN );
		wrap.value       = prefs.document.wrap.getValue( Preference.BOOLEAN );
		fold.value       = prefs.document.folding.getValue( Preference.BOOLEAN );

		switch( prefs.document.BOM.getValue( Preference.NUMBER ) )
		{
		    case 1:     this.pane.sigAlways.value   = true;     break;
		    case 2:     this.pane.sigPossible.value = true;     break;
		    default:    this.pane.sigNever.value    = true;
		}

	    //	Load the possible tab stops and select the preferred one
	    var tabs         = prefs.document.tabs.getValue( Preference.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			
		if( documentPrefs.langObjs.length <= 0 )
		{
			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 = prefs.document.language.getValue( Preference.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 = prefs.document.lineend.getValue( Preference.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( sigAlways.value )           prefs.document.BOM = 1;
		else if( sigPossible.value )    prefs.document.BOM = 2;
		else if( sigNever.value )       prefs.document.BOM = 0;
		
		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;
}
