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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QuickRoute.BusinessEntities;
namespace QuickRoute.Controls
{
public partial class FileSelectorControl : UserControl
{
public string FileDialogTitle { get; set; }
public string FileDialogFilter { get; set; }
public int FileDialogFilterIndex { get; set; }
public event EventHandler<EventArgs> FilesChanged;
public FileSelectorControl()
{
InitializeComponent();
}
public FileSelectorControl(IEnumerable<string> fileNames, bool allowUrlAdding)
: this()
{
AddFiles(fileNames);
AllowUrlAdding = allowUrlAdding;
}
public List<string> FileNames
{
get
{
var fileNames = new List<string>();
foreach (var fileName in files.Items)
{
fileNames.Add(fileName.ToString());
}
return fileNames;
}
}
public bool AllowUrlAdding
{
get
{
return addUrlButton.Visible;
}
set
{
addUrlButton.Visible = value;
}
}
public void AddFiles(IEnumerable<string> fileNames)
{
foreach (var fileName in fileNames)
{
files.Items.Add(fileName);
}
InvokeFilesChanged();
}
private void RemoveFiles(IEnumerable<string> fileNames)
{
foreach (var fileName in fileNames)
{
files.Items.Remove(fileName);
}
InvokeFilesChanged();
}
public void AddFile(string fileName)
{
AddFiles(new List<string>() { fileName });
}
public void RemoveFile(string fileName)
{
RemoveFiles(new List<string>() { fileName });
}
private void InvokeFilesChanged()
{
var eventHandler = FilesChanged;
if (eventHandler != null) eventHandler(this, EventArgs.Empty);
}
private void addFileButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog
{
AddExtension = true,
CheckFileExists = true,
Multiselect = true,
Title = FileDialogTitle,
Filter = FileDialogFilter,
FilterIndex = FileDialogFilterIndex
})
{
if (ofd.ShowDialog() == DialogResult.OK)
{
AddFiles(GetDuplicateFreeFiles(ofd.FileNames));
}
}
}
private void AddUrlButton_Click(object sender, EventArgs e)
{
using (var urlDialog = new Forms.UrlDialog())
{
if (urlDialog.ShowDialog() == DialogResult.OK)
{
AddFiles(GetDuplicateFreeFiles(urlDialog.Urls));
}
}
}
private void removeButton_Click(object sender, EventArgs e)
{
RemoveSelectedFiles();
}
private void RemoveSelectedFiles()
{
var filesToRemove = new List<string>();
foreach (var item in files.SelectedItems)
{
filesToRemove.Add(item.ToString());
}
RemoveFiles(filesToRemove);
}
private List<string> GetDuplicateFreeFiles(IEnumerable<string> fileNamesToAdd)
{
var fileNames = FileNames;
var duplicateFreeFiles = new List<string>();
foreach (var fileToAdd in fileNamesToAdd)
{
if (!fileNames.Contains(fileToAdd))
{
duplicateFreeFiles.Add(fileToAdd);
}
}
return duplicateFreeFiles;
}
private void files_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
RemoveSelectedFiles();
}
}
private void files_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void files_DragDrop(object sender, DragEventArgs e)
{
var manager = new DragDropManager();
var allowedFileExtensions = FileFormatManager.GetQuickRouteFileExtensions();
var fileNames = manager.GetDroppedFileNames(e, allowedFileExtensions);
if(fileNames.Count > 0)
{
AddFiles(fileNames);
}
}
}
}
|