/*
// -----------------------------------------------------------------------
// 
// Copyright (c) 2001-2009, MeVis Medical Solutions AG, Bremen, Germany
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of MeVis Medical Solutions AG nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY MEVIS MEDICAL SOLUTIONS AG ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL MEVIS MEDICAL SOLUTIONS AG BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//----------------------------------------------------------------------------------
//! Macro module AngioOISettings
/*!
// \file    AngioOISettings.js
// \author  lina
// \date    2009-12-10
//
// 
*/
//----------------------------------------------------------------------------------
/*
// -----------------------------------------------------------------------
// 
// (c) Copyright 2001-2008, MeVis Research GmbH, Bremen, Germany
// ALL RIGHTS RESERVED
// 
// THIS FILE CONTAINS CONFIDENTIAL AND PROPRIETARY INFORMATION OF MEVIS 
// RESEARCH GMBH. ANY DUPLICATION, MODIFICATION, DISTRIBUTION, OR 
// DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY PROHIBITED 
// WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF MEVIS RESEARCH GMBH.
// 
*/
//----------------------------------------------------------------------------------
//! Customization dialog for OpenImage macro
/*!
// \file    OISettings.js
// \author  Tobias Boskamp
// \date    6/2002
*/
//----------------------------------------------------------------------------------


// --- Global object initialization

var gStat = { guiInitialized:false
            };

var gFieldList = ["useInclude", "include", "useExclude", "exclude",
                  "suffix", "recursive", "sortBy"];


// --- Init/exit dialog

/* Initialize dialog */
function InitDialog ()
{
  gStat.guiInitialized = false;
  if (!UseSettingsFile()) {
    ctx.control("save").setVisible(false);
  }
  ImportFields();
  gStat.guiInitialized = true;
}


/* Close dialog */
function CloseDialog (ok)
{
  if (ok) {
    ExportFields();
    ctx.window().accept();
  }
  else {
    ctx.window().reject();
  }
}


/* Perform clean-up before dialog is closed */
function ExitDialog ()
{
  gStat.guiInitialized = false;
}


/* Import or export single field */
function ImportExportField (name, externalModule, doExport)
{
  if (doExport) {
    ctx.parent().field(externalModule+"."+name).value = ctx.field(name).value;
  } else {
    ctx.field(name).value = ctx.parent().field(externalModule+"."+name).value;
  }
}


/* Import or export list of fields */
function ImportExportFields (fieldNames, doExport)
{
  var browserModule = ctx.field("browserModule").stringValue();
  if (browserModule) {
    for (var i = 0; i < fieldNames.length; i++) {
      ImportExportField(fieldNames[i], browserModule, doExport);
    }
  }
}


/* Import settings field values */
function ImportFields ()
{
  ImportExportFields(gFieldList, false);
}


/* Export settings field values, apply changes in external module */
function ExportFields ()
{
  ImportExportFields(gFieldList, true);

  var browserModule = ctx.field("browserModule").stringValue();

/* // No reloading necessary, the last field (sortBy) forces reload
  if (browserModule) {
    var autoApply = false;
    if (ctx.parent().hasField(browserModule+".autoApply")) {
      autoApply = ctx.parent().field(browserModule+".autoApply").boolValue();
    }
    if (!autoApply && ctx.parent().hasField(browserModule+".apply")) {
      ctx.parent().field(browserModule+".apply").touch();
    }
  }
*/
}


// --- Restore/save settings

/* Return true if settings file should be used */
function UseSettingsFile ()
{
  var settingsFile = ctx.field("settingsFile").stringValue();
  return settingsFile.toUpperCase() != "NONE";
}


/* Set settings manager filename */
function SetFilename ()
{
  var settingsFile = ctx.field("settingsFile").stringValue();

  if (!settingsFile) {
    var settingsPath = MLAB.variable("SettingsPath");
    if (!settingsPath) {
      settingsPath = MLABFileManager.getHomeDir();
    }
    settingsFile = settingsPath+"\\mlabOpenImage.prefs";
  }
  ctx.field("settingsManager.filename").setStringValue(settingsFile);

  return settingsFile;
}


/* Set initial standard values */
function SetStandard ()
{
  ctx.field("useInclude").setBoolValue(false);
  ctx.field("include").setStringValue("datasets,results");
  ctx.field("useExclude").setBoolValue(true);
  ctx.field("exclude").setStringValue("slices,html,autoimg");
  ctx.field("suffix").setStringValue("dcm");
  ctx.field("recursive").setBoolValue(true);
  ctx.field("sortBy").setStringValue("Patient");
}


/* Restore settings from settings file or use standard defaults */
function RestoreDefaults ()
{
  if (UseSettingsFile()) {
    var settingsFile = SetFilename();
    if (MLABFileManager.exists(settingsFile)) {
      ctx.field("settingsManager.load").touch();
    } else {
      SetStandard();
    }
  }
  else {
    SetStandard();
  }
  // Running without UI? Export restored settings
  if (!gStat.guiInitialized) {
    ExportFields();
  }
}


/* Save settings to settings file */
function SaveDefaults ()
{
  var settingsFile = SetFilename();
  var isDir = MLABFileManager.isDir(settingsFile);
  var exists = MLABFileManager.exists(settingsFile);
  var writable = MLABFileManager.isWritable(settingsFile);

  if (!exists) {
    // Create settings file directory if necessary
    var settingsDir = MLABFileManager.splitPath(settingsFile).dir;
    if (!MLABFileManager.exists(settingsDir)) {
      MLABFileManager.mkdir(settingsDir);
    }
  }
  if (!exists || (writable && !isDir)) {
    // Save settings
    ctx.field("settingsManager.save").touch();
  } else if (isDir) {
    // Target is a directory
    MLAB.showCritical("Cannot write, "+settingsFile+" is a directory");
  } else {
    // Target file exists and is not writeable
    MLAB.showCritical("Cannot write to file "+settingsFile);
  }
}
//# MeVis signature v1
//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
//# owner: MeVis
//# date: 2009-06-10T04:12:25
//# hash: GR07ofKvUJxcWY2mZPAc+u8Uos0wOFSrGj71GYKLjshEsst7A2837GAdUyJwfkxMwiqDeUVcCaL8r4+m7JhPeA==
//# MeVis end

