295 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			295 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.ComponentModel;
 | |
| using System.Drawing;
 | |
| using System.Data;
 | |
| using System.Text;
 | |
| using System.Windows.Forms;
 | |
| using VEPROMS.CSLA.Library;
 | |
| 
 | |
| namespace Volian.Controls.Library
 | |
| {
 | |
| 	public partial class AnnotationDetails : UserControl
 | |
| 	{
 | |
| 		#region Properties
 | |
| 		private ItemInfo _CurrentItem = null;
 | |
| 		private AnnotationSearch _AnnotationSearch;
 | |
| 
 | |
| 		private AnnotationInfoList _Annotations;
 | |
| 		public AnnotationInfoList Annotations
 | |
| 		{
 | |
| 			get { return _Annotations; }
 | |
| 			set 
 | |
| 			{ 
 | |
| 				_Annotations = value;
 | |
| 				itemAnnotationsBindingSource.DataSource = _Annotations;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		private AnnotationInfo _CurrentAnnotation = null;
 | |
| 		public AnnotationInfo CurrentAnnotation
 | |
| 		{
 | |
| 			get { return _CurrentAnnotation; }
 | |
| 			set
 | |
| 			{
 | |
| 				if (_CurrentAnnotation == null && value == null) return; // No Change
 | |
| 				if (_CurrentAnnotation != null && value != null)
 | |
| 					if (_CurrentAnnotation.AnnotationID == value.AnnotationID) return; // No Change
 | |
| 				//vlnStackTrace.ShowStack("CurrentAnnotation = '{0}' Old = '{1}'", value, _CurrentAnnotation);
 | |
| 				if (_CurrentAnnotation != null || _AddingAnnotation)
 | |
| 				{
 | |
| 					if (AnnotationDirty)
 | |
| 						SaveAnnotation();
 | |
| 				}
 | |
| 				_CurrentAnnotation = value;
 | |
| 				InitializeAnnotation();
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		private bool _AnnotationDirty = false;
 | |
| 		private bool AnnotationDirty
 | |
| 		{
 | |
| 			get { return _AnnotationDirty; }
 | |
| 			set
 | |
| 			{
 | |
| 				btnRemoveAnnotation.Enabled = btnAddAnnotation.Enabled = !value;
 | |
| 				btnSaveAnnotation.Enabled = btnCancelAnnoation.Enabled = value;
 | |
| 				_AddingAnnotation = value && (CurrentAnnotation == null);
 | |
| 				_AnnotationDirty = value;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		public string AnnotationText
 | |
| 		{
 | |
| 			get { return rtxbComment.Text; }
 | |
| 			set
 | |
| 			{
 | |
| 				rtxbComment.Text = value;
 | |
| 				if (rtxbComment.Text != string.Empty)
 | |
| 					rtxbComment.SelectionStart = rtxbComment.TextLength; // position cursor to end of text
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		public string AnnotationRTFText
 | |
| 		{
 | |
| 			get { return rtxbComment.Rtf; }
 | |
| 			set
 | |
| 			{
 | |
| 				rtxbComment.Rtf = value;
 | |
| 				if (rtxbComment.Rtf != string.Empty)
 | |
| 					rtxbComment.SelectionStart = rtxbComment.TextLength; // position cursor to end of text
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		#endregion
 | |
| 
 | |
| 		#region Constructors
 | |
| 		public AnnotationDetails()
 | |
| 		{
 | |
| 			InitializeComponent();
 | |
| 		}
 | |
| 		#endregion
 | |
| 
 | |
| 		#region Events
 | |
| 		private bool _AddingAnnotation = false;
 | |
| 		private void btnAddAnnotation_Click(object sender, EventArgs e)
 | |
| 		{
 | |
| 			c1AnnotationGrid.Row = -1;
 | |
| 			CurrentAnnotation = null;
 | |
| 			_AddingAnnotation = true;
 | |
| 		}
 | |
| 
 | |
| 		private void btnRemoveAnnotation_Click(object sender, EventArgs e)
 | |
| 		{
 | |
| 			using (Annotation annotation = CurrentAnnotation.Get())
 | |
| 			{
 | |
| 				annotation.Delete();
 | |
| 				_AnnotationSearch.LoadingList = true;
 | |
| 				annotation.Save();
 | |
| 				_AnnotationSearch.LoadingList = false;
 | |
| 				CurrentAnnotation = null;
 | |
| 				UpdateAnnotationGrid();
 | |
| 				_AnnotationSearch.UpdateAnnotationSearchResults(); // update the search results
 | |
| 			}
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		private void btnSaveAnnotation_Click(object sender, EventArgs e)
 | |
| 		{
 | |
| 			if (cbGridAnnoType.SelectedIndex == -1)
 | |
| 			{
 | |
| 				MessageBox.Show("You Must Select an Annotation Type", "Annotation Type Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 | |
| 				cbGridAnnoType.Focus();
 | |
| 				return;
 | |
| 			}
 | |
| 			if (rtxbComment.Text == string.Empty)
 | |
| 			{
 | |
| 				MessageBox.Show("You Must Enter Annotation Text", "Annotation Text Is Blank", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 | |
| 				rtxbComment.Focus();
 | |
| 				return;
 | |
| 			}
 | |
| 			SaveAnnotation();
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		private void btnCancelAnnoation_Click(object sender, EventArgs e)
 | |
| 		{
 | |
| 			InitializeAnnotation();
 | |
| 		}
 | |
| 
 | |
| 		private void cbGridAnnoType_SelectedValueChanged(object sender, EventArgs e)
 | |
| 		{
 | |
| 			if (!_LoadingAnnotation)
 | |
| 				AnnotationDirty = true;
 | |
| 		}
 | |
| 
 | |
| 		private bool _LoadingAnnotation = false;
 | |
| 		private bool _LoadingGrid = false;
 | |
| 		private void c1AnnotationGrid_EnterCell(object sender, EventArgs e)
 | |
| 		{
 | |
| 			if (!_LoadingGrid) // Only set the Current Annotation when not loading the grid
 | |
| 			{
 | |
| 				if ((_Annotations != null) && (c1AnnotationGrid.Row > 0))
 | |
| 					CurrentAnnotation = _Annotations[c1AnnotationGrid.Row - 1];
 | |
| 				else
 | |
| 					CurrentAnnotation = null;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		private void rtxbComment_TextChanged(object sender, EventArgs e)
 | |
| 		{
 | |
| 			if (!_LoadingAnnotation)
 | |
| 				AnnotationDirty = true;
 | |
| 		}
 | |
| 
 | |
| 		#endregion
 | |
| 
 | |
| 		#region LoadControlData
 | |
| 
 | |
| 		public void SetupAnnotations(AnnotationSearch annotation)
 | |
| 		{
 | |
| 			_AnnotationSearch = annotation; // reference the Annotation Search to update its lists
 | |
| 
 | |
| 			cbGridAnnoType.DisplayMember = "Name";
 | |
| 			cbGridAnnoType.ValueMember = "TypeId";
 | |
| 			cbGridAnnoType.DataSource = AnnotationTypeInfoList.Get().Clone();
 | |
| 			cbGridAnnoType.SelectedIndex = -1; //don't pre-select an annotation type
 | |
| 		}
 | |
| 
 | |
| 		private void InitializeAnnotation()
 | |
| 		{
 | |
| 			//vlnCSLAStackTrace.ShowStack("InitializeAnnotation - CurrentAnnotation = {0}", CurrentAnnotation);
 | |
| 			_LoadingAnnotation = true;
 | |
| 			if (CurrentAnnotation == null)
 | |
| 			{
 | |
| 				cbGridAnnoType.SelectedIndex = -1;
 | |
| 				AnnotationText = "";
 | |
| 			}
 | |
| 			else
 | |
| 			{
 | |
| 				cbGridAnnoType.SelectedValue = CurrentAnnotation.TypeID;
 | |
| 				if (CurrentAnnotation.RtfText != "")
 | |
| 					AnnotationRTFText = CurrentAnnotation.RtfText;
 | |
| 				else
 | |
| 					AnnotationText = CurrentAnnotation.SearchText;
 | |
| 			}
 | |
| 			_LoadingAnnotation = false;
 | |
| 			AnnotationDirty = false;
 | |
| 			if (!_LoadingGrid)
 | |
| 				rtxbComment.Focus(); // Set the focus to the comment text
 | |
| 		}
 | |
| 
 | |
| 		#endregion
 | |
| 
 | |
| 		#region VariousSupportMethods
 | |
| 
 | |
| 		/// <summary>
 | |
| 		/// Set up the Annotation Grid for the given item
 | |
| 		/// This is called from frmVEPROMS
 | |
| 		/// </summary>
 | |
| 		/// <param name="currentitem"></param>
 | |
| 		public void UpdateAnnotationGrid(ItemInfo currentitem)
 | |
| 		{
 | |
| 			_CurrentItem = currentitem;
 | |
| 			UpdateAnnotationGrid();
 | |
| 		}
 | |
| 
 | |
| 		private void UpdateAnnotationGrid()
 | |
| 		{
 | |
| 			_LoadingGrid = true;
 | |
| 			_Annotations = (_CurrentItem == null) ? null : _CurrentItem.ItemAnnotations;
 | |
| 			itemAnnotationsBindingSource.DataSource = _Annotations;
 | |
| 			if ((CurrentAnnotation == null || (_CurrentItem.ItemID != CurrentAnnotation.ItemID)))
 | |
| 			{
 | |
| 				if (_Annotations != null && _Annotations.Count > 0)
 | |
| 					CurrentAnnotation = _Annotations[0];
 | |
| 				else
 | |
| 					CurrentAnnotation = null;
 | |
| 			}
 | |
| 			FindCurrentAnnotation(); // position to the grid row of the current annotation
 | |
| 			_LoadingGrid = false;
 | |
| 		}
 | |
| 
 | |
| 		/// <summary>
 | |
| 		/// Find the Current Annotation in the Annotation Grid select the corresponding row
 | |
| 		/// Note: this is also called from AnnotationSearch.cs when a search results is selected
 | |
| 		/// </summary>
 | |
| 		public void FindCurrentAnnotation()
 | |
| 		{
 | |
| 			int row = 0;
 | |
| 			if (CurrentAnnotation != null)
 | |
| 			{
 | |
| 				if (_Annotations != null)
 | |
| 				{
 | |
| 					foreach (AnnotationInfo ai in _Annotations)
 | |
| 					{
 | |
| 						if (ai.AnnotationID == CurrentAnnotation.AnnotationID)
 | |
| 						{
 | |
| 							row = _Annotations.IndexOf(ai) + 1;
 | |
| 							break;
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 			}
 | |
| 			c1AnnotationGrid.Select(row, 0, true); // position to the corresponding grid row
 | |
| 		}
 | |
| 
 | |
| 		private void SaveAnnotation()
 | |
| 		{
 | |
| 			if (cbGridAnnoType.SelectedIndex == -1) return;
 | |
| 			if (rtxbComment.Text == string.Empty) return;
 | |
| 			using (AnnotationType annotationType = AnnotationType.Get((int)cbGridAnnoType.SelectedValue))
 | |
| 			{
 | |
| 				if (_AddingAnnotation)
 | |
| 				{
 | |
| 					_AddingAnnotation = false;
 | |
| 					using (Item myItem = _CurrentItem.Get())
 | |
| 					{
 | |
| 						using (Annotation annotation = Annotation.MakeAnnotation(myItem, annotationType, rtxbComment.Rtf, rtxbComment.Text, ""))
 | |
| 						{
 | |
| 							CurrentAnnotation = AnnotationInfo.Get(annotation.AnnotationID);
 | |
| 							annotation.DTS = DateTime.Now;
 | |
| 							annotation.Save();
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 				else
 | |
| 				{
 | |
| 					using (Annotation annotation = CurrentAnnotation.Get())
 | |
| 					{
 | |
| 						annotation.RtfText = rtxbComment.Rtf;
 | |
| 						annotation.SearchText = rtxbComment.Text;
 | |
| 						annotation.MyAnnotationType = annotationType;
 | |
| 						annotation.DTS = DateTime.Now;
 | |
| 						annotation.Save();
 | |
| 					}
 | |
| 				}
 | |
| 			}
 | |
| 			AnnotationDirty = false;
 | |
| 			UpdateAnnotationGrid();
 | |
| 		}
 | |
| 		#endregion
 | |
| 
 | |
| 	}
 | |
| }
 |