TagCloud.cs

00001 using System;
00002 using System.Collections.Generic;
00003 using System.Collections;
00004 using System.ComponentModel;
00005 using System.Drawing;
00006 using System.Data;
00007 using System.Text;
00008 using System.Windows.Forms;
00009 using Shared;
00010 
00014 namespace TagControls
00015 {
00019     [System.ComponentModel.DefaultEvent("TagSelected")]
00020     public partial class TagCloud : UserControl
00021     {
00023         private List<TagInfo> tags = new List<TagInfo>();
00025         private string selectedTag;
00027         private string username;
00029         private bool showMyTagsOnly;
00031         private SortOrder sortBy;
00033         private Single mintagsize = 5.0F;
00035         private Single maxsize = 20.0F;
00037         private List<TagsInfo> tagslist = new List<TagsInfo>();
00038 
00040         public event TagCloudEventHandler TagSelected;
00041 
00045         public TagCloud()
00046         {
00047             InitializeComponent();
00048         }
00049 
00053         [
00054             TypeConverter(typeof(TagCloud)),
00055             Category("Appearance"),
00056             Description("The current user's username")
00057         ]
00058         public string Username
00059         {
00060             get { return username; }
00061             set { username = value; }
00062         }
00066         public Single minTagSize
00067         {
00068             get { return mintagsize; }
00069             set { mintagsize = value;}
00070         }
00071 
00075         public Single maxTagSize
00076         {
00077             get { return maxsize; }
00078             set { maxsize = value; }
00079         } 
00083         [
00084             TypeConverter(typeof(TagCloud)),
00085             Category("Appearance"),
00086             Description("The library tags to work from")
00087         ]
00088         public List<TagInfo> Tags
00089         {
00090             get { return tags; }
00091             set { tags = value; Updater(); }
00092         }
00093 
00097         [
00098             TypeConverter(typeof(TagCloud)),
00099             Category("Appearance"),
00100             Description("The currently selected tag")
00101         ]
00102         public string SelectedTag
00103         {
00104             get { return selectedTag; }
00105             set { selectedTag = value; }
00106         }
00107 
00111         [
00112             TypeConverter(typeof(TagCloud)),
00113             Category("Appearance"),
00114             Description("Wether to show only the current user's tags")
00115         ]
00116         public bool ShowMyTagsOnly
00117         {
00118             get { return showMyTagsOnly; }
00119             set
00120             {
00121                 showMyTagsOnly = value;
00122                 chkOnlyMine.Checked = value;
00123             }
00124         }
00125 
00129         public enum SortOrder
00130         {
00131             Alphabetical,
00132             Size
00133         }
00134 
00138         public SortOrder SortBy
00139         {
00140             get { return sortBy; }
00141             set
00142             {
00143                 sortBy = value;
00144                 if (value == SortOrder.Alphabetical)
00145                     chkAlphabetical.Checked = true;
00146                 else
00147                     chkBySize.Checked = true;
00148             }
00149         }
00150 
00155         private void SortTags()
00156         {
00157             //sort input tags efter navn
00158             Sorter.AlphabeticalSortTags(tags);
00159 
00160             //Add user and weight info the the tags
00161             Sorter.WeightAndGroupTags(tags, tagslist, username);
00162    
00163             if (sortBy == SortOrder.Size)
00164             {
00165                 Sorter.WeightSortTags(tagslist);
00166             }
00167         }
00168 
00172         private void RenderCloud()
00173         {
00174             this.SuspendLayout();
00175             flowLayoutPanel.Controls.Clear();
00176                        
00177             if (tagslist.Count == 0)
00178             {
00179                   // Show a greyed out text in the middle of the canvas,
00180                  //  telling the user there are no tags... New users
00181                 //   who turn on "Show only my tags" will need this information
00182 
00183                 Label NoTags = new System.Windows.Forms.Label();
00184                 NoTags.Location = new System.Drawing.Point(0, 0);
00185                 NoTags.Name = "NoTags";
00186                 NoTags.Text = "No tags.";
00187                 NoTags.AutoSize = true;
00188                 NoTags.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
00189                 NoTags.ForeColor = System.Drawing.SystemColors.GrayText;
00190                 NoTags.Font = new System.Drawing.Font("Microsoft Sans Serif", maxsize);
00191 
00192                 flowLayoutPanel.Controls.Add(NoTags);
00193             }
00194             else
00195             {
00196                 Single TagSize;
00197                 // Code which renders the actual tag cloud...
00198 
00199                 foreach (TagsInfo tag in tagslist)
00200                 {
00201                     if (showMyTagsOnly == false)
00202                     {
00203                         LinkLabel lilTagCloud = new LinkLabel();
00204                         lilTagCloud.AutoSize = true;
00205 
00206                         TagSize = Convert.ToSingle(tag.Weight) / 100.0F * maxsize + mintagsize;
00207 
00208                         lilTagCloud.Font = new System.Drawing.Font("Microsoft Sans Serif", TagSize);
00209                         lilTagCloud.Location = new System.Drawing.Point(0, 0);
00210                         lilTagCloud.Name = "linkLabel1";
00211                         lilTagCloud.TabStop = true;
00212                         lilTagCloud.Text = Convert.ToString(tag.Name);
00213                         lilTagCloud.Click += new EventHandler(lilTagCloud_Click);
00214 
00215                         flowLayoutPanel.Controls.Add(lilTagCloud);
00216                     }
00217                     else if (tag.myTag == true)
00218                     {
00219                         LinkLabel lilTagCloud = new LinkLabel();
00220                         lilTagCloud.AutoSize = true;
00221 
00222                         TagSize = Convert.ToSingle(tag.Weight) / 100.0F * maxsize + mintagsize;
00223 
00224                         lilTagCloud.Font = new System.Drawing.Font("Microsoft Sans Serif", TagSize);
00225                         lilTagCloud.Location = new System.Drawing.Point(0, 0);
00226                         lilTagCloud.Name = "linkLabel1";
00227                         lilTagCloud.TabStop = true;
00228                         lilTagCloud.Text = Convert.ToString(tag.Name);
00229                         lilTagCloud.Click += new EventHandler(lilTagCloud_Click);
00230 
00231                         flowLayoutPanel.Controls.Add(lilTagCloud);
00232                     }
00233                 }
00234             }
00235             this.ResumeLayout(false);
00236         }
00237 
00241         public void Updater()
00242         {
00243             SortTags();
00244             RenderCloud();
00245         }
00246 
00252         void lilTagCloud_Click(object sender, EventArgs e)
00253         {
00254             LinkLabel l = (LinkLabel)sender;
00255 
00256             selectedTag = l.Text;
00257 
00258             // Fire the TagSelected event
00259             TagCloudEventArgs args = new TagCloudEventArgs(TagCloudEventArgs.TagCloudEventType.TagSelected);
00260             if (TagSelected != null)
00261                 TagSelected(this, args);
00262             
00263         }
00264 
00270         private void lnkWhatsThis_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
00271         {
00272             MessageBox.Show("This is a tag cloud\n A list of keywords where size reflects popularity.", "What is a Tag Cloud?", MessageBoxButtons.OK, MessageBoxIcon.Information);
00273         }
00274 
00281         private void chkAlphabetical_CheckedChanged(object sender, EventArgs e)
00282         {
00283             // Update the sort order option
00284             if (chkAlphabetical.Checked)
00285                 sortBy = SortOrder.Alphabetical;
00286             else
00287                 sortBy = SortOrder.Size;
00288 
00289             // We do not need to do this in chkBysize as well, because CheckChanged is called
00290             // both when check is set and when it is removed
00291             SortTags();
00292             RenderCloud();
00293         }
00294 
00300         private void chkOnlyMine_CheckedChanged(object sender, EventArgs e)
00301         {
00302             showMyTagsOnly = chkOnlyMine.Checked;
00303             SortTags();
00304             RenderCloud();
00305         }
00306     }
00307 
00311     public class TagCloudEventArgs : EventArgs
00312     {
00316         public enum TagCloudEventType
00317         {
00318             TagSelected
00319         }
00320 
00321         private TagCloudEventType eventType;
00322 
00323         public TagCloudEventType EventType
00324         {
00325             get { return eventType; }
00326             set { eventType = value; }
00327         }
00328 
00333         public TagCloudEventArgs(TagCloudEventType et)
00334         {
00335             eventType = et;
00336         }
00337     }
00338 
00344     public delegate void TagCloudEventHandler(
00345       object sender, TagCloudEventArgs e);
00346 }

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