示例#1
0
        public IIDE NewIDE(IVirtualMachine machine, ICodeEditor codeEditor
                           , IInputPort inputPort   = null, int withInputPortIndex  = -1
                           , IOutputPort outputPort = null, int withOutputPortIndex = -1
                           , bool withConsole       = false)
        {
            if (!IsInitialized)
            {
                throw new InvalidOperationException();
            }

            (codeEditor as Form).MdiParent = MainForm as Form;
            IIDE newIde = new IDE(machine, codeEditor);

            newIde.ParentWorkplace    = this;
            newIde.ShowingMessage    += MainSymbolService.ShowMessage;
            newIde.AskingUser        += MainSymbolService.AskUser;
            newIde.OpeningFileByUser += MainSymbolService.OpenFileByUser;
            newIde.SavingFileByUser  += MainSymbolService.SaveFileByUser;

            if (inputPort != null && withInputPortIndex >= -1)
            {
                newIde.MachineInsertInputPort(withInputPortIndex, inputPort);
            }
            if (outputPort != null && withOutputPortIndex >= -1)
            {
                newIde.MachineInsertOutputPort(withOutputPortIndex, outputPort);
            }

            if (withConsole && (inputPort != null || outputPort != null))
            {
                IInputDevice  newConsoleAsInputDevice;
                IOutputDevice newConsoleAsOutputDevice = null;
                if (inputPort != null)
                {
                    InputDeviceTypeInfo consoleDeviceTypeInfo =
                        InputDeviceBuilder.RegisteredTypes.Find(i => i.TypeFullName == typeof(ConsoleDevice).FullName);
                    if (consoleDeviceTypeInfo != null)
                    {
                        NewInputDevice(consoleDeviceTypeInfo, out newConsoleAsInputDevice);
                        newConsoleAsOutputDevice = newConsoleAsInputDevice as IOutputDevice;
                        newIde.MachineSetInputDevice(inputPort, newConsoleAsInputDevice);
                    }
                }
                if (outputPort != null)
                {
                    if (newConsoleAsOutputDevice != null)
                    {
                        newIde.MachineSetOutputDevice(outputPort, newConsoleAsOutputDevice);
                    }
                    else
                    {
                        OutputDeviceTypeInfo consoleDeviceTypeInfo =
                            OutputDeviceBuilder.RegisteredTypes.Find(i => i.TypeFullName == typeof(ConsoleDevice).FullName);
                        if (consoleDeviceTypeInfo != null)
                        {
                            NewOutputDevice(consoleDeviceTypeInfo, out newConsoleAsOutputDevice);
                            newIde.MachineSetOutputDevice(outputPort, newConsoleAsOutputDevice);
                        }
                    }
                }
            }

            newIde.Machine.Id         = MachinesIdCounter++;
            newIde.Machine.CustomName = newIde.Machine.ToString();

            newIde.Id = IdeIdCounter++;
            newIde.ProgramFile.ShortFileName = GetNewProgramFileShortName(newIde);
            newIde.Title = GetNewIDEtitle(newIde);
            newIde.ConfigFile.ShortFileName = newIde.Title;

            IDEs.Add(newIde);
            OnGotUpdated();

            return(newIde);
        }
示例#2
0
        public bool NewOutputDevice(OutputDeviceTypeInfo outputDeviceTypeInfo, out IOutputDevice newOutputDevice)
        {
            if (outputDeviceTypeInfo == null)
            {
                throw new NullReferenceException();
            }

            newOutputDevice = outputDeviceTypeInfo != null?
                              OutputDeviceBuilder.GetNew(outputDeviceTypeInfo) : null;

            if (newOutputDevice as IFileOutputable != 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      = 1,
                    RestoreDirectory = true
                };

                OpeningFileByUser?.Invoke(this, openingFileDialogArgs);

                if (openingFileDialogArgs.Answer == DialogResult.OK)
                {
                    try
                    {
                        if (!(System.IO.File.Exists(openingFileDialogArgs.FileName) &&
                              (System.IO.File.GetAttributes(openingFileDialogArgs.FileName) & FileAttributes.ReadOnly) != 0))
                        {
                            (newOutputDevice as IFileOutputable).FileName = openingFileDialogArgs.FileName;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

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