2010-07-20 19:32:05 +00:00

273 lines
9.7 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;
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;
}
#endregion
#region Events
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)
{
listBoxUsages.Items.Add(ii.MyProcedure.ToString() + " - " + ii.MyContent.Number + " " + ii.MyContent.Text);
}
}
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)|*.doc;*.rtf|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
{
FileStream fs = File.Open(temppath, FileMode.Open, FileAccess.Read, FileShare.None);
long len = fs.Length + 1;
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, null); //, 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 void listBoxUsages_SelectedValueChanged(object sender, EventArgs e)
{
if (!_InitializingLibDocList)
{
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;
}
_DisplayTabControl.OpenItem(di.LibraryDocumentUsageList[listBoxUsages.SelectedIndex]);
}
}
#endregion
}
}