Exemplo n.º 1
0
        public bool NewInputDevice(InputDeviceTypeInfo inputDeviceTypeInfo, out IInputDevice newInputDevice)
        {
            if (inputDeviceTypeInfo == null)
            {
                throw new NullReferenceException();
            }

            newInputDevice = inputDeviceTypeInfo != null?
                             InputDeviceBuilder.GetNew(inputDeviceTypeInfo) : null;

            if (newInputDevice as IFileInputable != null)
            {
                var openingFileDialogArgs = new OpenFileByUserEventArgs()
                {
                    InitialDirectory = (MainForm.SelectedIDE.ProgramFile.FileName.IsNotNullOrEmpty())
                        ? Path.GetDirectoryName(MainForm.SelectedIDE.ProgramFile.FileName)
                        : (MainForm.SelectedIDE.ParentWorkplace.MainForm.RecentFiles.Count > 2)
                            ? Path.GetDirectoryName(MainForm.SelectedIDE.ParentWorkplace.MainForm.RecentFiles[0])
                            : Path.GetDirectoryName(Application.ExecutablePath),
                    Filter           = "Text Files (*.txt)|*.txt|Binary Files (*.bin)|*.txt|All Files (*.*)|*.*",
                    FilterIndex      = 3,
                    RestoreDirectory = true
                };

                OpeningFileByUser?.Invoke(this, openingFileDialogArgs);

                if (openingFileDialogArgs.Answer == DialogResult.OK)
                {
                    try
                    {
                        FileInfo fInfo = new FileInfo(openingFileDialogArgs.FileName);
                        if (fInfo.Exists)
                        {
                            (newInputDevice as IFileInputable).FileName = openingFileDialogArgs.FileName;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            if (newInputDevice != null)
            {
                this.InputDevices.Add(newInputDevice);
                newInputDevice.ParentWorkplace = this;
                newInputDevice.Id = this.InputDevices.Count - 1;
            }
            if (newInputDevice as IOutputDevice != null)
            {
                this.OutputDevices.Add(newInputDevice as IOutputDevice);
            }
            return(true);
        }