LiteratureEntryView.cs

00001 using System;
00002 using System.Collections.Generic;
00003 using System.ComponentModel;
00004 using System.Data;
00005 using System.Drawing;
00006 using System.Text;
00007 using System.Windows.Forms;
00008 using Shared;
00009 using System.Text.RegularExpressions;
00010 
00011 namespace Client
00012 {
00016     public partial class LiteratureEntryView : Form
00017     {
00019         private LiteratureInfo info;
00021         private IAccessHandler accessHandler;
00023         private bool newWindow = false;
00025         private bool createReview = false;
00027         private bool dirty = false;
00028 
00033         public LiteratureEntryView(IAccessHandler accessHandler)
00034         {
00035             InitializeComponent();
00036 
00037             this.accessHandler = accessHandler;
00038 
00039             btnSave.Text = "Create literature";
00040             newWindow = true;
00041             tagContainer1.Enabled = false;
00042             InitializeInfopage();
00043 
00044             dirty = false;
00045         }
00046         
00052         public LiteratureEntryView(IAccessHandler accessHandler, LiteratureInfo info)
00053         {
00054             this.accessHandler = accessHandler;
00055             this.info = info;
00056 
00057             InitializeComponent();
00058             InitializeInfopage();
00059             newWindow = false;
00060 
00061             btnSave.Text = "Save details";
00062             dirty = false;
00063         }
00064 
00072         public LiteratureEntryView(IAccessHandler accesshandler, int literature, int project, bool createReview)
00073         {
00074             this.accessHandler = accesshandler;
00075             this.info = accessHandler.GetLiteratureInfo(literature);
00076             InitializeComponent();
00077             InitializeInfopage();
00078             newWindow = false;
00079 
00080             btnSave.Text = "Save details";
00081             dirty = false;
00082             tabControl1.SelectedTab = tabReferences;
00083             SelectReference(project);
00084             this.createReview = createReview;
00085         }
00086 
00090         public LiteratureInfo LiteratureInfo
00091         {
00092             get { return info; }
00093             set { info = value; }
00094         }
00095         
00101         private void btnClose_Click(object sender, EventArgs e)
00102         {
00103             this.Close();
00104         }
00105 
00111         private void btnSave_Click(object sender, EventArgs e)
00112         {
00113             Save();
00114         }
00119         private bool Save()
00120         {
00121             string isbn = textBoxISBN.Text;
00122             string title = textBoxTitle.Text;
00123             string author = textBoxAuthors.Text;
00124             string summary = textBoxSummary.Text;
00125             string source = textBoxSource.Text;
00126 
00127             if (!Regex.IsMatch(textYear.Text.Trim(), @"^\d+$") || textYear.Text.Trim().Length > 4)
00128             {
00129                 MessageBox.Show("Year needs to be a number and it can be at most 4 digits long", "Invalid format", MessageBoxButtons.OK, MessageBoxIcon.Information);
00130                 return false;
00131             }
00132             DateTime date;
00133             try
00134             {
00135                 date = textYear.Text.Trim().Length > 0 ? new DateTime(Int32.Parse(textYear.Text), 1, 1) : DateTime.MinValue;
00136             }
00137             catch
00138             {
00139                 date = DateTime.MinValue;
00140             }
00141             LiteratureType type = (LiteratureType)Enum.Parse(typeof(LiteratureType), comboBoxType.SelectedItem.ToString());
00142             
00143             if (title.Length < 1 || author.Length < 1)
00144             {
00145                 MessageBox.Show("You need to fill in the fields \"Title\" and \"Author(s)\"", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Information);
00146                 return false;
00147             }
00148             
00149             info = new LiteratureInfo(info.id, info.creator, isbn, title, author,
00150                 summary, source, date, type);
00151             if (!newWindow)
00152                 accessHandler.UpdateLiterature(info);
00153             else
00154             {
00155                 int id;
00156                 accessHandler.CreateLiterature(out id, info);
00157                 info.id = id;
00158                 btnSave.Text = "Save details";
00159                 newWindow = false;
00160                 tagContainer1.Enabled = true;
00161                 InitializeTags();
00162             }
00163             dirty = false;
00164             btnSave.Enabled = false;
00165             return true;
00166         }
00167 
00168         #region infopage
00169         
00173         private void InitializeInfopage()
00174         {
00175             textBoxTitle.Text = info.title;
00176             textBoxAuthors.Text = info.author;
00177             textBoxISBN.Text = info.isbn;
00178             textBoxSummary.Text = info.summary;
00179             textBoxSource.Text = info.source;
00180             textYear.Text = info.date.Year.ToString();
00181             foreach (String theType in Enum.GetNames(typeof(LiteratureType)))
00182             {
00183                 comboBoxType.Items.Add(theType);
00184                 if (theType == info.type.ToString())
00185                     comboBoxType.SelectedIndex = comboBoxType.Items.Count - 1;
00186             }
00187             if (comboBoxType.SelectedIndex < 0)
00188                 comboBoxType.SelectedIndex = 0;
00189             if(tagContainer1.Enabled)
00190                 InitializeTags();
00191         }
00192 
00193         #endregion
00194                         
00195         #region TagContainer
00196 
00200         private void InitializeTags()
00201         {
00202             int i;
00203             List<TagInfo> tags = new List<TagInfo>();
00204             TagInfo[] TagsFromHandler = accessHandler.GetTags(null, info.id, null);
00205 
00206             for (i = 0; i < TagsFromHandler.Length; i++)
00207             {
00208                 tags.Add(TagsFromHandler[i]);
00209             }
00210             tagContainer1.Username = this.accessHandler.WhoAmI;
00211             tagContainer1.Tags = tags;
00212         }
00213 
00219         private void tagContainer1_TagAdded(object sender, TagControls.TagContainerEventArgs e)
00220         {
00221             accessHandler.AddTag(tagContainer1.AddedTag.Name, info.id);
00222         }
00223 
00229         private void tagContainer1_TagRemoved(object sender, TagControls.TagContainerEventArgs e)
00230         {
00231             this.accessHandler.RemoveTag(tagContainer1.RemovedTag.Name, info.id);
00232         }
00233 
00234         #endregion  
00235 
00236         #region References
00237 
00241         private void InitializeReferences()
00242         {
00243             ListViewGroup grpRef = new ListViewGroup("References", "References to:");
00244             ListViewGroup grpReview = new ListViewGroup("Review", "Reviewed references to:");
00245             ListViewGroup grpSuggestion = new ListViewGroup("Suggestion", "Suggested to:");
00246 
00247             listView1.Groups.Clear();
00248             listView1.Groups.Add(grpRef);
00249             listView1.Groups.Add(grpReview);
00250             listView1.Groups.Add(grpSuggestion);
00251 
00252             listView1.Items.Clear();
00253 
00254             ReferenceInfo[] infRef = accessHandler.ListReferences(info.id);
00255             foreach (ReferenceInfo ri in infRef)
00256             {
00257                 ProjectInfo pi = accessHandler.GetProjectInfo(ri.project);
00258                 ListViewItem lvi = new ListViewItem(new string[] { pi.Title }, "project");
00259                 lvi.Tag = new object[] { pi, ri };
00260                 lvi.Text = pi.title;
00261                 lvi.Group = grpRef;
00262                 lvi.Name = pi.id.ToString() ;
00263                 listView1.Items.Add(lvi);
00264             }
00265 
00266             ReviewInfo[] infReview = accessHandler.ListReviews(info.id);
00267             foreach (ReviewInfo ri in infReview)
00268             {
00269                 ProjectInfo pi = accessHandler.GetProjectInfo(ri.project);
00270                 ListViewItem lvi = new ListViewItem(new string[] { pi.Title }, "project");
00271                 lvi.Tag = new object[] { pi, ri };
00272                 lvi.Text = pi.title;
00273                 lvi.Group = grpReview;
00274                 lvi.Name = pi.id.ToString();
00275                 listView1.Items.Add(lvi);
00276             }
00277             
00278             SuggestionInfo[] infSuggestion = accessHandler.ListSuggestions(info.id);
00279             foreach (SuggestionInfo ri in infSuggestion)
00280             {
00281                 ProjectInfo pi = accessHandler.GetProjectInfo(ri.project);
00282                 ListViewItem lvi = new ListViewItem(new string[] { pi.Title }, "project");
00283                 lvi.Tag = new object[] { pi, ri };
00284                 lvi.Text = pi.title;
00285                 lvi.Group = grpSuggestion;
00286                 lvi.Name = pi.id.ToString();
00287 
00288                 listView1.Items.Add(lvi);
00289             }
00290             InitializeProjectsCombobox();
00291             if (listView1.Items.Count > 0)
00292                 listView1.Items[0].Selected = true;
00293         }
00294         
00298         private void InitializeProjectsCombobox()
00299         {
00300             createReferenceContextMenu.Items.Clear();
00301             createReferenceContextMenu.Items.Add(findProjectToolStripMenuItem);
00302 
00303             ProjectInfo[] projects = accessHandler.GetUsersProjects(accessHandler.WhoAmI);
00304             bool added = false;
00305             foreach (ProjectInfo p in projects)
00306             {
00307                 bool found = false;
00308                 foreach (ListViewItem lvi in listView1.Items)
00309                 {
00310                     ProjectInfo pi = (ProjectInfo)((object[])lvi.Tag)[0];
00311                     if (pi.id == p.id)
00312                     {
00313                         found = true;
00314                         break;
00315                     }
00316                 }
00317 
00318                 if (!found)
00319                 {
00320                     ToolStripMenuItem t = new ToolStripMenuItem(p.title);
00321                     t.Tag = p;
00322                     t.Click += new EventHandler(Project_Click);
00323                     createReferenceContextMenu.Items.Add(t);
00324                     added = true;
00325                 }
00326             }
00327             if (added)
00328             {
00329                 createReferenceContextMenu.Items.Insert(1, toolStripMenuItem1);
00330                 createReferenceButton.Enabled = true;
00331             }
00332             else
00333             {
00334                 createReferenceButton.Enabled = false;
00335             }
00336 
00337 
00338         }
00339 
00345         private void Project_Click(object sender, EventArgs ea)
00346         {
00347             ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
00348             ProjectInfo p  = (ProjectInfo)tsmi.Tag;
00349             RoleInfo[] roles = accessHandler.GetRoles(p.id);
00350             string me = accessHandler.WhoAmI;
00351             foreach (RoleInfo ri in roles)
00352             {
00353                 if (ri.UserName != me)
00354                     continue;
00355 
00356                 if (ri.Type == RoleType.Supervisor)
00357                 {
00358                     Messages m = accessHandler.CreateSuggestion(new SuggestionInfo(p.id, info.id, DateTime.Now, false, "Unspecified", me, ""));
00359                     break;
00360                 }
00361                 else
00362                 {
00363                     Messages m = accessHandler.CreateReference(new ReferenceInfo(p.id, info.id, DateTime.Now, false, "Unspecified"));
00364                     break;
00365                 }
00366             }
00367             InitializeReferences();
00368             InitializeProjectsCombobox();
00369         }
00370 
00376         private void findProjectToolStripMenuItem_Click(object sender, EventArgs e)
00377         {
00378             ISearchable[] allItems = accessHandler.ListItems(ResultType.Project, accessHandler.WhoAmI);
00379             
00380             Reference addRef = new Reference(allItems);
00381 
00382             addRef.ShowDialog();
00383 
00384             if (addRef.ID != -1)
00385             {
00386                 ReferenceInfo addedRef = new ReferenceInfo(addRef.ID, LiteratureInfo.id, DateTime.Now, false, null);
00387                 accessHandler.CreateReference(addedRef);
00388             }
00389         }
00390 
00396         private void createReferenceButton_Click(object sender, EventArgs e)
00397         {
00398             createReferenceContextMenu.Show(createReferenceButton, createReferenceButton.Margin.Left, createReferenceButton.Height);
00399         }
00400 
00401         private void listView1_SelectedIndexChanged(object sender, EventArgs e)
00402         {
00403             tabControl2.TabPages.Clear();
00404             if (listView1.SelectedIndices.Count == 1)
00405             {
00406                 tabControl2.TabPages.Add(tabRefInfo);
00407                 ProjectInfo pi = (ProjectInfo)((object[])listView1.SelectedItems[0].Tag)[0];
00408                 object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00409 
00410                 llblProjectLink.Text = pi.Title;
00411 
00412                 RoleInfo[] roles = accessHandler.GetRoles(pi.id);
00413                 RoleInfo myRole = new RoleInfo();
00414                 string me = accessHandler.WhoAmI;
00415                 foreach (RoleInfo role in roles)
00416                 {
00417                     if (role.UserName == me)
00418                     {
00419                         myRole = role;
00420                         break;
00421                     }
00422                 }
00423 
00424                 CreateReviewButton.Enabled = btnDelRef.Enabled = !string.IsNullOrEmpty(myRole.UserName) && myRole.Type != RoleType.Supervisor;
00425                 if (o2 is ReferenceInfo)
00426                 {
00427                     ReferenceInfo ri = (ReferenceInfo)o2;
00428                     lblDate.Text = ri.date.ToLongDateString();
00429                     txtStatus.Text = ri.status;
00430                     grpSuggestion.Visible = false;
00431                     grpReview.Visible = false;
00432                     CreateReviewButton.Visible = true;
00433                                         
00434                     if (!string.IsNullOrEmpty(myRole.UserName))
00435                     {
00436                         txtStatus.ReadOnly = myRole.Type == RoleType.Supervisor;
00437                         
00438                     }
00439                     else
00440                     {
00441                         txtStatus.ReadOnly = true;
00442                     }
00443                 }
00444                 else if (o2 is ReviewInfo)
00445                 {
00446                     ReviewInfo ri = (ReviewInfo)o2;
00447                     lblDate.Text = ri.date.ToLongDateString();
00448                     txtStatus.Text = ri.status;
00449                     grpSuggestion.Visible = false;
00450                     CreateReviewButton.Visible = false;
00451                     grpReview.Visible = true;
00452                     textBox1.Text = ri.title;
00453                     textBox2.Text = ri.text;
00454                     trackBar1.Value = ri.rating;
00455                     tabControl2.TabPages.Add(tabRefComments);
00456 
00457                     if (!string.IsNullOrEmpty(myRole.UserName) && myRole.Type != RoleType.Supervisor)
00458                     {
00459                         txtStatus.ReadOnly = false;
00460                         textBox2.ReadOnly = false;
00461                         textBox1.ReadOnly = false;
00462                         trackBar1.Enabled = true;
00463                     }
00464                     else
00465                     {
00466                         trackBar1.Enabled = false;
00467                         textBox2.ReadOnly = true;
00468                         textBox1.ReadOnly = true;
00469                         txtStatus.ReadOnly = true;
00470                     }
00471                 }
00472                 else if (o2 is SuggestionInfo)
00473                 {
00474                     SuggestionInfo ri = (SuggestionInfo)o2;
00475                     lblDate.Text = ri.date.ToLongDateString();
00476                     txtStatus.Text = ri.status;
00477                     grpSuggestion.Visible = true;
00478                     grpReview.Visible = false;
00479                     txtReason.Text = ri.reason;
00480                     CreateReviewButton.Visible = false;
00481 
00482                     if (!string.IsNullOrEmpty(myRole.UserName))
00483                     {
00484                         txtStatus.ReadOnly = false;
00485                         txtReason.ReadOnly = myRole.Type != RoleType.Supervisor;
00486                     }
00487                     else
00488                     {
00489                         txtStatus.ReadOnly = true;
00490                         txtReason.ReadOnly = true;
00491                     }
00492                 }
00493 
00494             }
00495             else
00496             {
00497                 tabControl2.TabPages.Clear();
00498             }
00499         }
00500 
00501         private void llblProjectLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
00502         {
00503             ProjectInfo pi = (ProjectInfo)((object[])listView1.SelectedItems[0].Tag)[0];
00504             ProjectView pv = new ProjectView(accessHandler, pi);
00505             pv.ShowDialog();
00506             object[] o = (object[])listView1.SelectedItems[0].Tag;
00507             o[0] = pv.Projectinfo;
00508             listView1.SelectedItems[0].Text = pv.Projectinfo.Title;
00509             llblProjectLink.Text = pv.Projectinfo.title;
00510         }
00511 
00512         private void txtStatus_Leave(object sender, EventArgs e)
00513         {
00514             if (listView1.SelectedIndices.Count == 1)
00515             {
00516                 object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00517 
00518                 if (o2 is ReferenceInfo)
00519                 {
00520                     ReferenceInfo ri = (ReferenceInfo)o2;
00521                     ri.status = txtStatus.Text;
00522                     accessHandler.UpdateReference(ri);
00523                     ((object[])listView1.SelectedItems[0].Tag)[1] = ri;
00524                 }
00525                 else if (o2 is ReviewInfo)
00526                 {
00527                     ReviewInfo ri = (ReviewInfo)o2;
00528                     ri.status = txtStatus.Text;
00529                     accessHandler.UpdateReview(ri);
00530                     ((object[])listView1.SelectedItems[0].Tag)[1] = ri;
00531                 }
00532                 else if (o2 is SuggestionInfo)
00533                 {
00534                     SuggestionInfo ri = (SuggestionInfo)o2;
00535                     ri.status = txtStatus.Text;
00536                     accessHandler.UpdateSuggestion(ri);
00537                     ((object[])listView1.SelectedItems[0].Tag)[1] = ri;
00538                 }
00539             }
00540         }
00541 
00542 
00543         #endregion
00544 
00545         private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
00546         {
00547             if (e.TabPage == tabComments)
00548             {
00549                 workingCommentControl1.AccessHandler = accessHandler;
00550                 workingCommentControl1.Literature = info.id;
00551                 workingCommentControl1.Project = -1;
00552                 workingCommentControl1.UpdateComments();
00553             }
00554             else if (e.TabPage == tabReferences)
00555             {
00556                 tabControl2.TabPages.Clear();
00557                 InitializeReferences();
00558             }
00559         }
00560 
00561         private void textBox1_Leave(object sender, EventArgs e)
00562         {
00563             if (listView1.SelectedIndices.Count == 1)
00564             {
00565                 object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00566 
00567                 if (o2 is SuggestionInfo)
00568                 {
00569                     SuggestionInfo ri = (SuggestionInfo)o2;
00570                     ri.reason = txtReason.Text;
00571                     accessHandler.UpdateSuggestion(ri);
00572                     ((object[])listView1.SelectedItems[0].Tag)[1] = ri;
00573                 }
00574             }
00575         }
00576 
00577         private void btnDelRef_Click(object sender, EventArgs e)
00578         {
00579             DialogResult dr = MessageBox.Show("Do you want to delete this reference?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
00580             if (dr == DialogResult.Yes)
00581             {
00582                 if (listView1.SelectedIndices.Count == 1)
00583                 {
00584                     object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00585 
00586                     if (o2 is SuggestionInfo)
00587                     {
00588                         SuggestionInfo ri = (SuggestionInfo)o2;
00589                         accessHandler.DeleteReference(ri.project, ri.literature);
00590                         InitializeReferences();
00591                     }
00592                     else if (o2 is ReferenceInfo)
00593                     {
00594                         ReferenceInfo ri = (ReferenceInfo)o2;
00595                         accessHandler.DeleteReference(ri.project, ri.literature);
00596                         InitializeReferences();
00597                     }
00598                     else if (o2 is ReviewInfo)
00599                     {
00600                         ReviewInfo ri = (ReviewInfo)o2;
00601                         accessHandler.DeleteReference(ri.project, ri.literature);
00602                         InitializeReferences();
00603                     }
00604                 }
00605             }
00606             listView1_SelectedIndexChanged(null, null);
00607         }
00608 
00609         private void CreateReviewButton_Click(object sender, EventArgs e)
00610         {
00611             if (listView1.SelectedIndices.Count == 1)
00612             {
00613                 object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00614 
00615                 if (o2 is SuggestionInfo)
00616                 {
00617                     SuggestionInfo ri = (SuggestionInfo)o2;
00618                     ReviewInfo r = new ReviewInfo(ri.project, ri.literature, ri.date, ri.used, ri.status, "", "", "", 0);
00619                     accessHandler.CreateReview(r);
00620                     InitializeReferences();
00621                     SelectReference(ri.project);
00622                 }
00623                 else if (o2 is ReferenceInfo)
00624                 {
00625                     ReferenceInfo ri = (ReferenceInfo)o2;
00626                     ReviewInfo r = new ReviewInfo(ri.project, ri.literature, ri.date, ri.used, ri.status, "", "", "", 0);
00627                     accessHandler.CreateReview(r);
00628                     InitializeReferences();
00629                     SelectReference(ri.project);
00630                 }
00631             }
00632         }
00633 
00634         private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e)
00635         {
00636             if (newWindow)
00637             {
00638                 DialogResult result = MessageBox.Show("You have unsaved changes. Do you wish to save before closing the literature window?", "Unsaved changes!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
00639                 if (result == DialogResult.Cancel)
00640                     e.Cancel = true;
00641                 else if (result == DialogResult.Yes)
00642                 {
00643                     if (!Save())
00644                         e.Cancel = true;
00645                 }
00646                 else
00647                 {
00648                     e.Cancel = true;
00649                     dirty = false;
00650                     this.Close();
00651                 }
00652             }
00653         }
00654 
00660         private void setDirty(object sender, EventArgs e)
00661         {
00662             dirty = true;
00663             btnSave.Enabled = true;        }
00664 
00665         private void LiteratureEntryView_FormClosing(object sender, FormClosingEventArgs e)
00666         {
00667             if (dirty)
00668             {
00669                 DialogResult result = MessageBox.Show("You have unsaved changes. Do you wish to save before closing the literature window?", "Unsaved changes!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
00670                 if (result == DialogResult.Cancel)
00671                     e.Cancel = true;
00672                 else if (result == DialogResult.Yes)
00673                     if (!Save())
00674                         e.Cancel = true;
00675             }
00676         }
00677 
00678         private void SelectReference(int project)
00679         {
00680             if (!listView1.Items.ContainsKey(project.ToString()))
00681                 return;
00682 
00683             listView1.Items[project.ToString()].Selected = true;
00684         }
00685 
00686         private void dirtify(object sender, EventArgs e)
00687         {
00688             if (listView1.SelectedIndices.Count == 1)
00689             {
00690                 object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00691 
00692                 if (o2 is ReviewInfo)
00693                 {
00694                     ReviewInfo ri = (ReviewInfo)o2;
00695                     ri.rating = trackBar1.Value;
00696                     ri.text = textBox2.Text;
00697                     ri.title = textBox1.Text;
00698                     accessHandler.UpdateReview(ri);
00699                     ((object[])listView1.SelectedItems[0].Tag)[1] = ri;
00700                 }
00701             }
00702             
00703         }
00704 
00705         private void tabControl2_Selecting(object sender, TabControlCancelEventArgs e)
00706         {
00707             if (listView1.SelectedIndices.Count == 1)
00708             {
00709                 object o2 = ((object[])listView1.SelectedItems[0].Tag)[1];
00710                 
00711                 if (o2 is ReviewInfo && e.TabPage == tabRefComments)
00712                 {
00713                     ReviewInfo ri = (ReviewInfo)o2;
00714                     
00715 
00716                     workingCommentControl2.AccessHandler = accessHandler;
00717                     workingCommentControl2.Literature = info.id;
00718                     workingCommentControl2.Project = ri.project;
00719                     workingCommentControl2.UpdateComments();
00720                 }
00721             }
00722         }
00723 
00724         private void LiteratureEntryView_Shown(object sender, EventArgs e)
00725         {
00726             if (createReview)
00727                 CreateReviewButton_Click(null, null);
00728             createReview = false;
00729             textBoxTitle.Focus();
00730         }
00731     }
00732 }

Generated on Thu Dec 21 06:21:55 2006 for SCRAML by  doxygen 1.5.1-p1