private void UIEventHandlers() { // Drag Drop Files lvPreview.DragEnter += (@s, a) => { if (a.Data.GetDataPresent(DataFormats.FileDrop)) a.Effect = DragDropEffects.Link; }; lvPreview.DragDrop += (@s, e) => { String[] files = (String[]) e.Data.GetData(DataFormats.FileDrop); Session.AddFile(files); UpdatePreview(); }; // Resize lvPreview.Resize += (@s, a) => { Int32 width = (lvPreview.Width - 48) / 2; lvPreview.Columns[0].Width = width; lvPreview.Columns[1].Width = width; }; lvPreview.KeyDown += (@s, e) => { if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control) { foreach (ListViewItem i in lvPreview.Items) i.Selected = true; } else if (e.KeyCode == Keys.Delete) { ListViewItem[] items = new ListViewItem[lvPreview.SelectedItems.Count]; lvPreview.SelectedItems.CopyTo(items, 0); Session.RemoveFiles(from i in items select (String)i.Tag); UpdatePreview(); } }; // Context Menus lvPreview.ItemSelectionChanged += (@s, e) => { Int32 count = lvPreview.SelectedItems.Count; tsmiPvRemSelection.Enabled = count > 0; tsmiPvExtractRule.Enabled = count == 1; }; lvRules.ItemSelectionChanged += (@s, e) => { Int32 count = lvRules.SelectedItems.Count; tsmiRemoveRules.Enabled = count > 0; tsmiAddToTemplates.Enabled = count == 1; }; tsmiAddRule.Click += (@s, e) => { RuleEditorDialog dlg = new RuleEditorDialog(this); if (dlg.ShowDialog() == DialogResult.OK) { RenamingRule rule = dlg.Rule; Session.Rules.AddRule(rule); } }; tsmiRemoveRules.Click += (@s, e) => { ListViewItem[] selection = new ListViewItem[lvRules.SelectedItems.Count]; lvRules.SelectedItems.CopyTo(selection, 0); foreach (RenamingRule rule in selection.Select(v => v.Tag).OfType<RenamingRule>()) Session.Rules.RemoveRule(rule); UpdateUI(); }; tsmiAddToTemplates.Click += (@s, e) => { RenamingRule rule = lvRules.SelectedItems[0].Tag as RenamingRule; if (rule == null) throw new Exception(); OptionsDialog dlg = new OptionsDialog(1, rule); dlg.ShowDialog(); UpdateUI(); }; tsmiPvAddFiles.Click += (@s, e) => { OpenFileDialog dlg = new OpenFileDialog() { Multiselect = true }; if (dlg.ShowDialog() == DialogResult.OK) Session.AddFile(dlg.FileNames); }; tsmiPvRemSelection.Click += (@s, e) => { ListViewItem[] items = new ListViewItem[lvPreview.SelectedItems.Count]; lvPreview.SelectedItems.CopyTo(items, 0); Session.RemoveFiles(from i in items select (String)i.Tag); UpdatePreview(); }; tsmiPvExtractRule.Click += (@s, e) => { String fname = lvPreview.SelectedItems[0].Tag as String; if (fname == null) return; var extConfig = Configuration.Instance.GetCurrentExtensionSettings(); var fds = new FileNameDescriptor(fname, extConfig.MaximumExtensions, extConfig.Validator); RenamingRule rule = new RenamingRule { Type = RenamingRule.RuleType.Name, RegularExpression = Regex.Escape(fds.FileName).Replace("\\ ", " "), ReplacementExpression = String.Format("{0}{1}", Path.GetFileNameWithoutExtension(fds.Directory), "[${i}]") }; RuleEditorDialog dlg = new RuleEditorDialog(this) {Rule = rule}; if (dlg.ShowDialog() == DialogResult.OK) { Session.Rules.AddRule(dlg.Rule); UpdateUI(); } }; tsmiPvRemApplied.Click += (@s, e) => { Session.RemoveApplied(); UpdatePreview(); }; // RUN RENAMING renBtnRunRename.Click += (@s, a) => ApplyRenaming(); }
private void RenamingTabAttachEventHandlers() { renBtnAddRule.Click += (@s, e) => { RuleEditorDialog dlg = new RuleEditorDialog(this); if (dlg.ShowDialog() == DialogResult.OK) { RenamingRule rule = dlg.Rule; rule.Active = true; switch (rule.Type) { case RenamingRule.RuleType.Name: { Session.Rules.NameRules.Add(rule); } break; case RenamingRule.RuleType.Extension: { Session.Rules.ExtensionRules.Add(rule); } break; case RenamingRule.RuleType.Directory: { Session.Rules.DirectoryRules.Add(rule); } break; } UpdateUI(); } }; renBtnRemoveRule.Click += (@s, e) => { ListViewItem[] selection = new ListViewItem[lvRules.SelectedItems.Count]; lvRules.SelectedItems.CopyTo(selection, 0); foreach (RenamingRule rule in selection.Select(v => v.Tag).OfType<RenamingRule>()) { if (rule.Type == RenamingRule.RuleType.Name) Session.Rules.NameRules.Remove(rule); else if (rule.Type == RenamingRule.RuleType.Extension) Session.Rules.ExtensionRules.Remove(rule); else Session.Rules.DirectoryRules.Remove(rule); } UpdateUI(); }; lvRules.ItemChecked += (@s, a) => { RenamingRule rule = a.Item.Tag as RenamingRule; if (rule == null) return; if (rule.Active != a.Item.Checked) { rule.Active = a.Item.Checked; UpdatePreview(); } }; }