361 lines
11 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using AT.STO.UI.Win;
using VEPROMS.CSLA.Library;
namespace Volian.Controls.Library
{
public partial class DisplayLibDocs : UserControl
{
#region Properties
private DocumentInfo _CurLibDoc;
public DocumentInfo CurLibDoc
{
get { return _CurLibDoc; }
set { _CurLibDoc = value; }
}
// selected index into listBoxLibDocs...
private int _SelectedLibDoc = -1;
private DocumentInfoList _LibDocList;
public DocumentInfoList LibDocList
{
get { return _LibDocList; }
set { _LibDocList = value; }
}
private Color saveGroupPanelUsages;
#endregion
#region Constructors
public DisplayLibDocs()
{
InitializeComponent();
btnOpenLibDoc.Enabled = false;
btnDelLibDoc.Enabled = false;
btnPrint.Enabled = listBoxLibDocs.Items.Count > 0;
saveGroupPanelUsages = groupPanelUsages.Style.BackColor;
}
DisplayTabControl _DisplayTabControl;
#endregion
#region LoadControlData
private bool _InitializingLibDocList; // used? see if selectionchanged event (still coding)
public void LibDocListFillIn(DisplayTabControl tc)
{
_InitializingLibDocList = true;
listBoxLibDocs.Items.Clear();
tbComment.Clear();
tbName.Clear();
groupPanelLibDocs.Style.BackColor = Color.Yellow;
btnSave.Enabled = false;
btnCancel.Enabled = false;
LibDocList = DocumentInfoList.GetLibraries(true);
for (int i = 0; i < LibDocList.Count; i++)
listBoxLibDocs.Items.Add(((LibDocList[i].DocumentEntries == null || LibDocList[i].DocumentEntries.Count == 0) ? "0 " : LibDocList[i].DocumentEntries.Count.ToString() + " ") + LibDocList[i].DocumentTitle); // + LibDocList[i].DocumentEntries.Count);
_DisplayTabControl = tc;
listBoxUsages.Items.Clear();
_InitializingLibDocList = false;
btnPrint.Enabled = listBoxLibDocs.Items.Count > 0;
}
public void SetSelectedLibDoc()
{
if (_SelectedLibDoc >= 0)
{
if (listBoxLibDocs.Items.Count==0 || _SelectedLibDoc > listBoxLibDocs.Items.Count) return; // B2018-126: if no items in list return also (count==0)
if (_SelectedLibDoc > listBoxLibDocs.Items.Count) return;
listBoxLibDocs.SelectedIndex = _SelectedLibDoc;
}
}
#endregion
#region Events
public event DisplayLibDocEvent PrintRequest;
private void OnPrintRequest(DisplayLibDocEventArgs args)
{
if (PrintRequest != null)
PrintRequest(this, args);
}
private void listBoxLibDocs_Click(object sender, EventArgs e)
{
UpdateUsageList();
}
public void RefreshLibDocPanel(DisplayTabControl tc)
{
int cursel = listBoxLibDocs.SelectedIndex;
LibDocListFillIn(tc);
listBoxLibDocs.SelectedIndex = cursel;
UpdateUsageList();
}
private void UpdateUsageList()
{
if (listBoxLibDocs.SelectedIndex == -1) return;
groupPanelLibDocs.Style.BackColor = Color.Orange;
_SelectedLibDoc = listBoxLibDocs.SelectedIndex;
listBoxUsages.Items.Clear();
tbComment.Clear();
tbName.Clear();
btnSave.Enabled = false;
btnCancel.Enabled = false;
_InitializingLibDocList = true; // need this so textchanged code doesn't turn save/cancel buttons on.
tbName.Text = LibDocList[_SelectedLibDoc].LibTitle;
DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]);
tbComment.Text = dc.LibDoc_Comment;
_InitializingLibDocList = false;
// If there are no usages, then enable delete & open button (open for docs with usages occurs
// from usage list), otherwise disable buttons & list usages
DocumentInfo dicnt = LibDocList[_SelectedLibDoc];
ItemInfoList iil = dicnt.LibraryDocumentUsageList;
groupPanelUsages.Style.BackColor = dicnt.LibraryDocumentUsageList == null || dicnt.LibraryDocumentUsageList.Count == 0 ? this.saveGroupPanelUsages : Color.LightGreen;
if (iil == null)
{
btnDelLibDoc.Enabled = true;
btnOpenLibDoc.Enabled = true;
return;
}
// Put the usages in the usage list for the selected library document.
btnDelLibDoc.Enabled = false;
btnOpenLibDoc.Enabled = false;
foreach (ItemInfo ii in dicnt.LibraryDocumentUsageList)
{
if (ii.MyDocVersion != null)
listBoxUsages.Items.Add(ii.MyProcedure.ToString() + " - " + ii.DisplayNumber + " " + ii.DisplayText);
}
btnPrint.Enabled = listBoxLibDocs.Items.Count > 0 ? true : false;
}
private void btnDelLibDoc_Click(object sender, EventArgs e)
{
DocumentInfo di = LibDocList[_SelectedLibDoc];
try
{
Document.Delete(di.DocID);
}
catch (Exception ex)
{
Console.WriteLine("error deleting doc = " + ex.Message);
}
LibDocListFillIn(_DisplayTabControl);
}
private void btnSave_Click(object sender, EventArgs e)
{
bool chg = false;
DocumentInfo di = LibDocList[_SelectedLibDoc];
Document doc = Document.Get(di.DocID);
if (tbName.Text != LibDocList[_SelectedLibDoc].LibTitle)
{
chg = true;
doc.LibTitle = tbName.Text;
}
DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]);
if (tbComment.Text != dc.LibDoc_Comment)
{
chg = true;
dc.LibDoc_Comment = tbComment.Text;
doc.Config = dc.ToString();
}
if (chg == true)
{
doc.Save();
int cursel = _SelectedLibDoc;
LibDocListFillIn(_DisplayTabControl);
listBoxLibDocs.SelectedIndex = cursel;
UpdateUsageList();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DocumentInfo di = LibDocList[_SelectedLibDoc];
Document doc = Document.Get(di.DocID);
tbName.Text = doc.LibTitle;
DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]);
tbComment.Text = dc.LibDoc_Comment;
btnSave.Enabled = false;
btnCancel.Enabled = false;
}
private void btnOpenLibDoc_Click(object sender, EventArgs e)
{
DocumentInfo di = LibDocList[_SelectedLibDoc];
_DisplayTabControl.OpenDSOTabPage(di);
}
private void tbName_TextChanged(object sender, EventArgs e)
{
if (_InitializingLibDocList) return;
btnSave.Enabled = true;
btnCancel.Enabled = true;
}
private void tbComment_TextChanged(object sender, EventArgs e)
{
if (_InitializingLibDocList) return;
btnSave.Enabled = true;
btnCancel.Enabled = true;
}
private void btnImpLibDoc_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Word files (*.doc;*.rtf;*.docx)|*.doc;*.rtf;*.docx|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 0;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
int done = 0;
int ntry = 0;
while (done == 0 && ntry < 4)
{
ntry++;
done = SaveTheDoc(openFileDialog1.FileName, openFileDialog1.SafeFileName.Substring(0, openFileDialog1.SafeFileName.LastIndexOf(".")));
}
if (done > 0)
{
LibDocListFillIn(_DisplayTabControl);
// set selectedindex to the imported doc...
int indx = 0;
DocumentInfoList dil = DocumentInfoList.GetLibraries(true);
for (int i = 0; i < listBoxLibDocs.Items.Count; i++)
{
if (dil[i].DocID == done)
{
indx = i;
break;
}
}
listBoxLibDocs.SelectedIndex = indx;
}
}
}
private int SaveTheDoc(string temppath, string title)
{
try
{
FileInfo fi = new FileInfo(temppath);
FileStream fs = File.Open(temppath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);// B2016-053
long len = fs.Length;
byte[] ByteArray = new byte[len];
int nBytesRead = fs.Read(ByteArray, 0, (int)len);
fs.Close();
string t1 = (title == null || title == "") ? null : title;
Document doc = Document.MakeDocument(t1, ByteArray, null, null, fi.Extension); //, DateTime.Now, "Migration");
return doc.DocID;
}
// for an io exception, keep trying
catch (IOException)
{
Wait(2);
return 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not save the document, {0}, {1}", ex.Message, ex.InnerException));
return -1;
}
}
private void Wait(int n)
{
DateTime dtw = DateTime.Now.AddSeconds(n);
while (DateTime.Now < dtw)
{
Application.DoEvents();
}
}
private bool _ProcessingSelectedValueChanged = false;
private void listBoxUsages_SelectedValueChanged(object sender, EventArgs e)
{
if (!_InitializingLibDocList)
{
if (_ProcessingSelectedValueChanged) return;
_ProcessingSelectedValueChanged = true;
if (listBoxUsages.SelectedIndex == -1) return;
DocumentInfo di = LibDocList[listBoxLibDocs.SelectedIndex];
// see if the library document actually has data - data migration may have created a 'null' content within the
// record for missing library documents.
if (di.DocContent == null)
{
MessageBox.Show("No Content in this document");
return;
}
if (listBoxUsages.SelectedIndex < di.LibraryDocumentUsageList.Count && listBoxUsages.SelectedIndex >= 0)
{
ItemInfo ii = di.LibraryDocumentUsageList[listBoxUsages.SelectedIndex];
if (ii.MyDocVersion == null)
MessageBox.Show("This usage is not connected properly", "Disconnected Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
_DisplayTabControl.OpenItem(ii);
}
_ProcessingSelectedValueChanged = false;
}
}
#endregion
private void btnPrint_Click(object sender, EventArgs e)
{
string paperSize = "Letter";
paperSize = LibDocList[listBoxLibDocs.SelectedIndex].LibraryDocumentUsageList[0].ActiveFormat.PlantFormat.FormatData.PDFPageSize.PaperSize; // C2020-002 paper size is now set in the format files
OnPrintRequest(new DisplayLibDocEventArgs("Library Document Usage", LibDocList, paperSize));
}
}
public class DisplayLibDocEventArgs
{
private string _ReportTitle;
public string ReportTitle
{
get { return _ReportTitle; }
set { _ReportTitle = value; }
}
private DocumentInfoList _libDocList;
public DocumentInfoList LibDocList
{
get { return _libDocList; }
set { _libDocList = value; }
}
private string _PaperSize; // C2020-002 paper size is now set in the format files
public string PaperSize
{
get { return _PaperSize; }
set { _PaperSize = value; }
}
//private string _SearchString = null;
//public string SearchString
//{
// get { return _SearchString; }
// set { _SearchString = value; }
//}
//private string _TypesSelected;
//public string TypesSelected
//{
// get { return _TypesSelected; }
// set { _TypesSelected = value; }
//}
//private ICollection<ItemInfo> _MyItemInfoList;
//public ICollection<ItemInfo> MyItemInfoList
//{
// get { return _MyItemInfoList; }
// set { _MyItemInfoList = value; }
//}
public DisplayLibDocEventArgs(string reportTitle, DocumentInfoList libDocList, string paperSize)
{
_ReportTitle = reportTitle;
_libDocList = libDocList;
_PaperSize = paperSize; // C2020-002 paper size is now set in the format files
}
}
public delegate void DisplayLibDocEvent(object sender, DisplayLibDocEventArgs args);
}