Exemple #1
0
        public MainForm()
        {
            // Load Configuration
            String configPath = Path.Combine(Application.StartupPath, ConfigFile);
            if (!File.Exists(configPath))
            {
                var cfg = Configuration.Instance; // Force creation of a new instance
                using (var xw = XmlWriter.Create(configPath, new XmlWriterSettings() {Indent = true}))
                {
                    ConfigFileLoader.SaveConfiguration(cfg, xw);
                }
            } else
                Configuration.LoadConfiguration(configPath);

            // Initialize session stuff
            Session = new RenamingSession();

            InitializeComponent();
            UIEventHandlers();
            RenamingTabAttachEventHandlers();

            // Main Menu Events
            tsmiAbout.Click += (@s, e) => (new AboutDialog()).ShowDialog();
            tsmiAddFiles.Click += (@s, e) =>
                {
                    OpenFileDialog dlg = new OpenFileDialog() { Multiselect = true };
                    if (dlg.ShowDialog() == DialogResult.OK)
                        Session.AddFile(dlg.FileNames);
                    UpdatePreview();
                };
            tsmiShowExtInPrev.Click += (@s, a) =>
                {
                    tsmiShowExtInPrev.Checked = !tsmiShowExtInPrev.Checked;
                    UpdatePreview();
                };
            tsmiFillPathInPrev.Click += (@s, a) =>
                {
                    tsmiFillPathInPrev.Checked = !tsmiFillPathInPrev.Checked;
                    UpdatePreview();
                };
            tsmiClearAll.Click += (@s, e) =>
                {
                    Session.Clear();
                    UpdateUI();
                };
            tsmiOptions.Click += (@s, e) =>
                {
                    OptionsDialog dlg = new OptionsDialog();
                    dlg.ShowDialog();
                    UpdateUI();
                };
            tsmiProjectHomepage.Click += (@s, e) =>
                                         System.Diagnostics.Process.Start("http://github.com/jluchiji/RenEx");
            tsmiBugReport.Click += (@s, e) =>
                                         System.Diagnostics.Process.Start("http://github.com/jluchiji/RenEx/issues");
            tsmiOnlineHelp.Click += (@s, e) =>
                                         System.Diagnostics.Process.Start("http://github.com/jluchiji/RenEx/wiki");

            // UI Maintenance

            UpdateUI();
        }
Exemple #2
0
        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();
        }