1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using RoM.Cfg;
using RoM.Tools;
namespace RoM.Views
{
public partial class LstViewRoms : UserControl
{
ListViewRomsOptions _options = new ListViewRomsOptions();
public FileInfo[] Files { get; private set; } = {};
public ListViewRomsOptions Options { get{ return _options; } set{ _options = value; Refresh(); } }
public LstViewRoms()
{
InitializeComponent();
}
#region Menucontextuel
void TsmGroupNameClick(object sender, EventArgs e)
{
_options.ShowGroupBy = GroupBy.Name;
ChangeCtxGroupBy();
Refresh( true );
}
void TsmGroupTypeClick(object sender, EventArgs e)
{
_options.ShowGroupBy = GroupBy.Extension;
ChangeCtxGroupBy();
Refresh( true );
}
void ChangeCtxGroupBy()
{
TsmGroupName.Checked = (_options.ShowGroupBy == GroupBy.Name);
TsmGroupType.Checked = (_options.ShowGroupBy == GroupBy.Extension);
}
#endregion
public void Load( DirectoryInfo dir, ListViewRomsOptions option = null ) //FIXME : afficher les fichier des sous-dossiers?
{
if(option != null)
_options = option;
lstView.BeginUpdate();
try {
Files = dir.GetFiles( "*", ( ( _options.ShowRecursif )? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly) );
CleanFiles();
if( Files.Length == 0 )
lblNoFile.Visible = true;
else
lblNoFile.Visible = false;
CreateGameGroup();
lstView.Items.AddRange( CreateItems() );
} catch (Exception ex) {
OnError();
throw;
}
lstView.EndUpdate();
}
void Refresh()
{
if( Files.Length == 0 )
lblNoFile.Visible = true;
else
lblNoFile.Visible = false;
lstView.Items.AddRange( CreateItems() );
}
void Refresh( bool GroupChanged)
{
if( GroupChanged )
CreateGameGroup();
Refresh();
}
void CleanFiles()
{
if( !_options.FiltreExt )
return;
foreach (string ext in _options.DontShowExt) {
Files = Files.Where( x => x.Extension != ext).ToArray();
}
}
void CreateGameGroup()
{
if( !_options.ShowGroup )
return;
if(lstView.Groups.Count != 0)
lstView.Groups.Clear(); //FIXME : System.NullReferenceException*: 'La référence d'objet n'est pas définie à une instance d'un objet.'
IEnumerable<IGrouping<string, FileInfo>> temp =
( (_options.ShowGroupBy == GroupBy.Name) ?
Files.GroupBy( x => FileCtrlr.RomNameCleaner( x.Name ) ) :
Files.GroupBy( x => x.Extension ) ) ;
List<ListViewGroup> lst = new List<ListViewGroup>();
ListViewGroup gr;
foreach(var va in temp)
{
gr = new ListViewGroup( FileCtrlr.RomNameCleaner( va.Key ), HorizontalAlignment.Left );
gr.Name = va.Key;
gr.Header = va.Key;
lst.Add(gr);
}
lstView.Groups.AddRange( lst.OrderBy( x => x.Header ).ToArray() );
}
public ListViewItem[] CreateItems()
{
object parallelLock = new object();
List<ListViewItem> myNewList = new List<ListViewItem>();
lstView.Items.Clear();
if(Files.Length == 0)
return new ListViewItem[0];
Parallel.ForEach( Files, theFile =>
{
ListViewItem lvItem = new ListViewItem( theFile.Name );
lvItem.Tag = theFile;
lvItem.ForeColor = ColorItem( theFile );
CreateSubItems( lvItem, theFile );
if( _options.ShowGroup ) {
string group = (_options.ShowGroupBy == GroupBy.Name) ? FileCtrlr.RomNameCleaner( theFile.Name ) : theFile.Extension;
lvItem.Group = lstView.Groups[group];
}
lock(parallelLock)
myNewList.Add(lvItem );
});
return myNewList.OrderBy(x => x.Text).ToArray();
}
Color ColorItem(FileInfo fi)
{
return Color.Black;
}
void CreateSubItems( ListViewItem lvItem, FileInfo fi)
{
lvItem.SubItems.Add( (Regex.IsMatch( fi.Name, "(\\[b\\d*\\])" ))? "X" : "" ); //regex [b1] -> (\[b\d*\])
lvItem.SubItems.Add( (Regex.IsMatch( fi.Name, "(\\[T[a-zA-z0-9+ ._]*\\])" ))? "X" : "" );//regex traduction -> (\[T[a-zA-z0-9+ ._]*\])
}
void ListView1SelectedIndexChanged(object sender, EventArgs e) //FIXME : est également lancé lors de la déselection...
{
//QUID : avec selection multiple
FileInfo fi = null;
if(lstView.SelectedItems.Count == 1)
fi = (FileInfo)lstView.SelectedItems[0].Tag; //TODO : recupérer toujours le dernier sauf en cas de deselction????
OnSelectionChanged(fi);
}
} |
Partager