TagContainer.cs

00001 using System;
00002 using System.Collections.Generic;
00003 using System.ComponentModel;
00004 using System.Drawing;
00005 using System.Data;
00006 using System.Text;
00007 using System.Windows.Forms;
00008 using Shared;
00009 
00010 namespace TagControls
00011 {
00016     public partial class TagContainer : UserControl
00017     {
00019         private List<TagInfo> tags = new List<TagInfo>();
00021         private List<TagsInfo> updatedTagList = new List<TagsInfo>();
00023         private TagInfo addedTag = new TagInfo();
00025         private TagInfo removedTag = new TagInfo();
00027         private int id;
00029         private string username;
00031         private int NumberOfTagsToPaint;
00033         private int tagSpace = 20;
00035         private TagControle MyTags;
00036 
00038         public event TagContainerEventHandler TagAdded;
00040         public event TagContainerEventHandler TagRemoved;
00041 
00043         public TagContainer()
00044         {
00045             InitializeComponent();
00046             tagsToPaint();
00047             MyTags = new TagControle(panContainer, this);
00048         }
00049 
00054         protected override void OnPaint(PaintEventArgs e)
00055         {
00056             base.OnPaint(e);
00057             AddTagsToControl(NumberOfTagsToPaint);
00058         }
00059 
00061         [
00062             TypeConverter(typeof(TagContainer)),
00063             Category("Appearance"),
00064             Description("The library tags to work from")
00065         ]
00066         public List<TagInfo> Tags
00067         {
00068             get { return tags; }
00069             set { tags = value; sortTags(); }
00070         }
00071 
00073         public int ID
00074         {
00075             get { return id; }
00076             set { id = value; }
00077         }
00078 
00080         public string Username
00081         {
00082             get { return username; }
00083             set { username = value; }
00084         }
00085 
00087         [
00088             TypeConverter(typeof(TagContainer)),
00089             Category("Appearance"),
00090             Description("The tag which was added")
00091         ]
00092         public TagInfo AddedTag
00093         {
00094             get { return addedTag; }
00095             set { addedTag = value; }
00096         }
00097 
00099         [
00100             TypeConverter(typeof(TagContainer)),
00101             Category("Appearance"),
00102             Description("The tag which was removed")
00103         ]
00104         public TagInfo RemovedTag
00105         {
00106             get { return removedTag; }
00107             set { removedTag = value; }
00108         }
00109 
00111         private void sortTags()
00112         {
00113             //Sorts the tags the Alphabetical
00114             Sorter.AlphabeticalSortTags(tags);
00115 
00116             //add weight and user info to the tag list
00117             Sorter.WeightAndGroupTags(tags, updatedTagList, username); 
00118             
00119             Sorter.WeightSortTags(updatedTagList);
00120         }
00121 
00124         private void AddTagsToControl(int numberOfTagsToAdd)
00125         {
00126             int NumberAdded = 0, numberOfTagsMyTags = 0, numberOfTagsOhterTags = 0;
00127             int X = 8, Y = 18;
00128             int OtherTagStart = 0;
00129             bool owerflow = false;
00130 
00131             this.SuspendLayout();
00132 
00133             MyTags.TagCleaner(X,Y);
00134 
00135             if (updatedTagList.Count > 0)
00136             {
00137                 txtFirstHelp.Hide();
00138 
00139                 foreach (TagsInfo tmpTag in updatedTagList)
00140                     if (tmpTag.myTag)
00141                         numberOfTagsMyTags++;
00142                     else
00143                         numberOfTagsOhterTags++;
00144 
00145                 if (numberOfTagsMyTags > numberOfTagsToAdd)
00146                 {
00147                     numberOfTagsMyTags = numberOfTagsToAdd;
00148                     owerflow = true;
00149                 }
00150 
00151                 if (numberOfTagsOhterTags > numberOfTagsToAdd)
00152                 {
00153                     numberOfTagsOhterTags = numberOfTagsToAdd;
00154                     owerflow = true;
00155                 }
00156                 //rette placeringen at de andre controls så de kommer til at stå under my tag
00157                 panNewTag.Location = new Point(X - 5, Y + (numberOfTagsMyTags * tagSpace) + 3);
00158 
00159                 OtherTagStart = panNewTag.Location.Y + panNewTag.Height;
00160 
00161                 //flytter det panel tags skal tegner i før vi tagner noget
00162                 panContainer.Height = OtherTagStart + (numberOfTagsOhterTags * 20);
00163 
00164                 //flytter lidt rundt med show all tags linklabelet
00165                 if (owerflow)
00166                 {
00167                     linShowAllTags.Top = panContainer.Height + 1;
00168                     linShowAllTags.Left = 5;
00169                 }
00170                 //tagner other tags
00171                 foreach (TagsInfo tmpTag in updatedTagList)
00172                 {
00173                     if (tmpTag.myTag)
00174                     {
00175                         MyTags.draw(X, Y, tmpTag.Weight, tmpTag.Name, true);
00176                         NumberAdded++;
00177                         
00178                         if (NumberAdded >= numberOfTagsToAdd)
00179                         {
00180                             break;
00181                         }
00182                         else
00183                         {
00184                             Y += tagSpace;
00185                         }
00186                     }
00187                 }
00188 
00189                 NumberAdded = 1;
00190                 foreach (TagsInfo tmpTag in updatedTagList)
00191                 {
00192                     if (!tmpTag.myTag)
00193                     {
00194                         MyTags.draw(X, OtherTagStart, tmpTag.Weight, tmpTag.Name, false);
00195                         NumberAdded++;
00196 
00197                         if (NumberAdded > numberOfTagsToAdd)
00198                         {
00199                             break;
00200                         }
00201                         else
00202                         {
00203                             OtherTagStart += tagSpace;
00204                         }
00205                     }
00206                 }
00207             }
00208 
00209             this.ResumeLayout(false);
00210             this.PerformLayout();
00211         }
00212 
00216         private void scrollbarupdater()
00217         {
00218             //scrollbar
00219             vScrollBar.Maximum = 1;
00220             vScrollBar.Enabled = false;
00221             if (this.Height < panContainer.Height)
00222             {
00223                 vScrollBar.Maximum = panContainer.Height + 45 - this.Height;
00224                 vScrollBar.LargeChange = 40;
00225                 vScrollBar.SmallChange = 20;
00226                 vScrollBar.Enabled = true;
00227             }
00228             vScrollBar.Minimum = 0;
00229         }
00230 
00234         public void FireRemoveTag()
00235         {
00236             // Fire the TagRemoved event
00237             TagContainerEventArgs args = new TagContainerEventArgs(TagContainerEventArgs.TagContainerEventType.TagRemoved);
00238             if (TagRemoved != null)
00239                 TagRemoved(this, args);
00240 
00241             sortTags();
00242             AddTagsToControl(NumberOfTagsToPaint);
00243             scrollbarupdater();
00244         }
00245 
00249         public void FireAddTag()
00250         {
00251             // Fire the TagAdded event
00252             TagContainerEventArgs args = new TagContainerEventArgs(TagContainerEventArgs.TagContainerEventType.TagAdded);
00253             if (TagAdded != null)
00254                 TagAdded(this, args);
00255 
00256             sortTags();
00257             AddTagsToControl(NumberOfTagsToPaint);
00258             scrollbarupdater();
00259         }
00260 
00266         private void txtNewTag_Enter(object sender, EventArgs e)
00267         {
00268             if (txtNewTag.Text == "(click here to create a new tag)")
00269             {
00270                 txtNewTag.Text = "";
00271                 txtNewTag.ForeColor = SystemColors.WindowText;
00272             }
00273         }
00274 
00281         private void txtNewTag_Leave(object sender, EventArgs e)
00282         {
00283             AddTagViaTextbox();
00284         }
00285 
00291         private void txtNewTag_KeyDown(object sender, KeyEventArgs e)
00292         {
00293             if (e.KeyCode == Keys.Enter)
00294             {
00295                 AddTagViaTextbox();
00296                 button1.Select();
00297             }
00298         }
00299 
00303         private void AddTagViaTextbox()
00304         {
00305             bool TagFound = false;
00306 
00307             if (txtNewTag.Text.Trim().Length != 0 && txtNewTag.Text != "(click here to create a new tag)")
00308             {
00309                 foreach (TagsInfo tag in updatedTagList)
00310                 {
00311                     if (tag.Name == txtNewTag.Text && tag.myTag)
00312                         TagFound = true;
00313                 }
00314 
00315                 if (!TagFound)
00316                 {
00317                     // Then we're adding a new tag!
00318                     this.addedTag = new TagInfo(txtNewTag.Text, this.id, this.username);
00319                     this.tags.Add(this.addedTag);
00320 
00321                     // Fire the TagAdded event
00322                     TagContainerEventArgs args = new TagContainerEventArgs(TagContainerEventArgs.TagContainerEventType.TagAdded);
00323                     if (TagAdded != null)
00324                         TagAdded(this, args);
00325                 }
00326             }
00327             // Reset the text box again...
00328             txtNewTag.Text = "(click here to create a new tag)";
00329             txtNewTag.ForeColor = SystemColors.GrayText;
00330 
00331             sortTags();
00332             AddTagsToControl(NumberOfTagsToPaint);
00333             scrollbarupdater();
00334         }
00335 
00339         private void tagsToPaint() // aka the magic number function
00340         {
00341             NumberOfTagsToPaint = this.Height;
00342             NumberOfTagsToPaint -= panTitleOne.Height;
00343             NumberOfTagsToPaint -= panTitleTwo.Height;
00344             NumberOfTagsToPaint = NumberOfTagsToPaint / tagSpace;
00345             NumberOfTagsToPaint = NumberOfTagsToPaint / 2;
00346         }
00347 
00348 
00354         private void linShowAllTags_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
00355         {
00356             //set the number of tags to be painted to 1000
00357             NumberOfTagsToPaint = 1000;
00358             //hide the show all buttom
00359             linShowAllTags.Hide();
00360             
00361             //add all the tags the the controle
00362             AddTagsToControl(NumberOfTagsToPaint);
00363             //Update the scrollbar
00364             scrollbarupdater();
00365         }
00366 
00372         private void vScrollBar_Scroll(object sender, ScrollEventArgs e)
00373         {
00374             panContainer.Top = e.NewValue * -1;
00375             
00376         }
00377 
00383         private void vScrollBar_MouseCaptureChanged(object sender, EventArgs e)
00384         {
00385             AddTagsToControl(NumberOfTagsToPaint);
00386         }
00387     }
00388 
00392     public class TagContainerEventArgs : EventArgs
00393     {
00397         public enum TagContainerEventType
00398         {
00399             TagAdded,
00400             TagRemoved
00401         }
00402 
00403         private TagContainerEventType eventType;
00404 
00405         public TagContainerEventType EventType
00406         {
00407             get { return eventType; }
00408             set { eventType = value; }
00409         }
00410 
00415         public TagContainerEventArgs(TagContainerEventType et)
00416         {
00417             eventType = et;
00418         }
00419     }
00420 
00426     public delegate void TagContainerEventHandler(
00427       object sender, TagContainerEventArgs e);
00428 
00429 }

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