Example #1
0
        private void OpenFileTab(string filePath, bool pushRecentFile = false)
        {
            // Is the file already open?
            EditorState alreadyOpenState = null;

            foreach (TabPage tab in tcFiles.TabPages)
            {
                EditorState state = (EditorState)tab.Tag;
                if (string.Equals(state.FilePath, filePath))
                {
                    alreadyOpenState = state;
                    break;
                }
            }

            if (alreadyOpenState != null)
            {
                // Focus existing state
                tcFiles.SelectedTab = (TabPage)alreadyOpenState.Tag;
                alreadyOpenState.SetFocus();
            }
            else
            {
                EditorState newState = AddFileTab(Path.GetFileName(filePath));
                TabPage     tab      = (TabPage)newState.Tag;
                tcFiles.SelectedIndex = tcFiles.TabPages.IndexOf(tab);
                Tuple <bool, Exception> openRes = IOOpenFile(newState, filePath);
                if (!openRes.Item1)
                {
                    Exception e      = openRes.Item2;
                    var       values = new Dictionary <string, string>()
                    {
                        { "filepath", filePath }
                    };
                    var msg = e.ToErrorMessage("Open file", values);
                    ShowError(msg.Caption, msg.ShortText, msg.Details);
                    RemoveFileTab(newState);
                }
                else
                {
                    _workspace.PushRecentFiles(filePath);

                    // Remove first tab when it was a "New" and is still unchanged
                    if (tcFiles.TabPages.Count == 2)
                    {
                        TabPage     firstTab      = tcFiles.TabPages[0];
                        EditorState existingState = (EditorState)firstTab.Tag;
                        if (existingState.FilePath == null && !existingState.IsChanged)
                        {
                            RemoveFileTab(existingState);
                        }
                    }

                    // Focus new tab/state
                    tcFiles.SelectedTab = tab;
                    newState.SetFocus();
                }
            }
        }