00001
00004 using System;
00005 using System.Collections.Generic;
00006 using System.Windows.Forms;
00007 using System.Reflection;
00008 using BrightIdeasSoftware;
00009
00010 namespace RiggsHill.Windows.Forms.OLVHelper
00011 {
00064 [AttributeUsage( AttributeTargets.Property, AllowMultiple = false, Inherited = true )]
00065 public class OLVColumnAttribute : Attribute
00066 {
00070 public string Title { get; set; }
00075 public string ToolTipText { get; set; }
00080 public int DisplayIndex { get; set; }
00087 public bool IsVisible { get; set; }
00095 public bool UseInitialLetterForGroup { get; set; }
00100 public bool FillsFreeSpace { get; set; }
00105 public int FreeSpaceProportion { get; set; }
00110 public int MaximumWidth { get; set; }
00115 public int MinimumWidth { get; set; }
00120 public int Width { get; set; }
00125 public string Tag { get; set; }
00126
00131 public HorizontalAlignment TextAlign { get; set; }
00132
00170 public object[] GroupValues { get; set; }
00175 public string[] GroupDescrs { get; set; }
00176
00181 public OLVColumnAttribute( string colTitle )
00182 {
00183 Title = colTitle;
00184 DisplayIndex = 0;
00185 IsVisible = true;
00186 UseInitialLetterForGroup = true;
00187 FillsFreeSpace = false;
00188 MaximumWidth = MinimumWidth = -1;
00189 Width = 0;
00190 }
00191 }
00192
00193
00197 public class ColumnBuilder
00198 {
00202 private ColumnBuilder() { }
00203
00224 static public List<OLVColumn> BuildColumns<T>( ObjectListView list )
00225 {
00226
00227 List<OLVColumn> cols = new List<OLVColumn>();
00228
00229
00230 foreach ( PropertyInfo pinfo in typeof( T ).GetProperties() ) {
00231
00232 object[] propAttrs = pinfo.GetCustomAttributes( typeof( OLVColumnAttribute ), true );
00233 if ( propAttrs.Length <= 0 )
00234 continue;
00235
00236
00237 OLVColumnAttribute colAttr = (OLVColumnAttribute)propAttrs[ 0 ];
00238
00239
00240 OLVColumn col = new OLVColumn( colAttr.Title, pinfo.Name );
00241
00242
00243 col.DisplayIndex = colAttr.DisplayIndex;
00244 col.UseInitialLetterForGroup = colAttr.UseInitialLetterForGroup;
00245 col.ToolTipText = colAttr.ToolTipText;
00246 col.IsVisible = colAttr.IsVisible;
00247 col.FreeSpaceProportion = colAttr.FreeSpaceProportion;
00248 col.FillsFreeSpace = colAttr.FillsFreeSpace;
00249 col.MaximumWidth = colAttr.MaximumWidth;
00250 col.MinimumWidth = colAttr.MinimumWidth;
00251 col.Width = colAttr.Width;
00252 col.TextAlign = colAttr.TextAlign;
00253 col.Tag = colAttr.Tag;
00254
00255
00256
00257
00258 if ( colAttr.GroupValues != null && colAttr.GroupDescrs != null )
00259 MakeGroupies( col, colAttr );
00260 cols.Add( col );
00261 }
00262
00263
00264 if ( cols.Count > 0 ) {
00265 cols.Sort( ( a, b ) => a.DisplayIndex.CompareTo( b.DisplayIndex ) );
00266 list.Columns.Clear();
00267 list.AllColumns = cols;
00268 list.RebuildColumns();
00269 }
00270 return cols;
00271 }
00272
00273
00277 private static MethodInfo c_makeGroupieInfo = null;
00289 static private void MakeGroupies( OLVColumn col, OLVColumnAttribute attr )
00290 {
00291
00292
00293 if ( c_makeGroupieInfo == null )
00294 c_makeGroupieInfo = col.GetType().GetMethod( "MakeGroupies" );
00295 if ( c_makeGroupieInfo == null )
00296 throw new ApplicationException( "MakeGroupies<T> not found in OLVColumn" );
00297
00298
00299 Type valType = attr.GroupValues[ 0 ].GetType();
00300 MethodInfo m = c_makeGroupieInfo.MakeGenericMethod( valType );
00301
00302
00303
00304
00305 Array typedVals = Array.CreateInstance( valType, attr.GroupValues.Length );
00306 Array.Copy( attr.GroupValues, typedVals, typedVals.Length );
00307
00308
00309 m.Invoke( col, new object[] { typedVals, attr.GroupDescrs } );
00310 }
00311 }
00312 }